getcwd(3c) 맨 페이지 - 윈디하나의 솔라나라

개요

섹션
맨 페이지 이름
검색(S)

getcwd(3c)

getcwd(3C)               Standard C Library Functions               getcwd(3C)

NAME
       getcwd - get pathname of current working directory

SYNOPSIS
       #include <unistd.h>

       char *getcwd(char *buf, size_t size);

DESCRIPTION
       The  getcwd() function places an absolute pathname of the current work‐
       ing directory in the array pointed to by  buf,  and  returns  buf.  The
       pathname  copied  to the array contains no components that are symbolic
       links. The size argument is the size in bytes of  the  character  array
       pointed  to  by buf and must be at least one greater than the length of
       the pathname to be returned.


       If buf is not a null pointer, the  pathname  is  stored  in  the  space
       pointed to by buf.


       If  buf  is  a null pointer, getcwd() obtains size bytes of space using
       malloc(3C). The pointer returned by getcwd() can be used as  the  argu‐
       ment in a subsequent call to free().


       If  the  size  value is 0 and buf is a NULL pointer, getcwd() allocates
       sufficient memory to store the current working directory.

RETURN VALUES
       Upon successful completion, getcwd() returns the buf argument.  If  buf
       is an invalid destination buffer address, NULL is returned and errno is
       set  to  EFAULT. Otherwise, a null pointer is returned and errno is set
       to indicate the error.

ERRORS
       The getcwd() function will fail if:

       EFAULT    The buf argument is an invalid destination buffer address.


       EINVAL    The size argument is equal  to  0  and  buf  is  not  a  NULL
                 pointer.


       ERANGE    The  size argument is greater than 0 and less than the length
                 of the pathname plus 1.



       The getcwd() function may fail if:

       EACCES    A parent directory cannot be read to get its name.


       ENOMEM    Insufficient storage space is available.


EXAMPLES
       Example 1 Determine the absolute pathname of the current working direc‐
       tory.



       The following example returns a pointer to an array that holds the  ab‐
       solute  pathname  of  the current working directory. The pointer is re‐
       turned in the ptr variable, which points to the  buf  array  where  the
       pathname is stored.


         #include <stdlib.h>
         #include <unistd.h>
         ...
         long size;
         char *buf;
         char *ptr;
         size = pathconf(".", _PC_PATH_MAX);
         if ((buf = (char *)malloc((size_t)size)) != NULL)
                ptr = getcwd(buf, (size_t)size);
         ...




       Example 2 Print the current working directory.



       The following example prints the current working directory.


         #include <unistd.h>
         #include <stdio.h>

         main()
         {
             char *cwd;
             if ((cwd = getcwd(NULL, 0)) == NULL) {
                 perror("pwd");
                 exit(2);
             }
             (void)printf("%s\n", cwd);
             free(cwd); /* free memory allocated by getcwd() */
             return(0);
         }




USAGE
       Applications  should  exercise  care when using chdir(2) in conjunction
       with getcwd(). The current working directory is global to  all  threads
       within  a  process. If more than one thread calls chdir() to change the
       working directory, a subsequent call to getcwd()  could  produce  unex‐
       pected results.

ATTRIBUTES
       See attributes(7) for descriptions of the following attributes:

       tab()  box; cw(2.75i) |cw(2.75i) lw(2.75i) |lw(2.75i) ATTRIBUTE TYPEAT‐
       TRIBUTE VALUE _ Interface StabilityCommitted _ MT-LevelMT-Safe _  Stan‐
       dardSee standards(7).


SEE ALSO
       chdir(2), malloc(3C), attributes(7), standards(7)

HISTORY
       Support  for allocating memory when size is 0 and buf is NULL was added
       to Solaris in the Oracle Solaris 11.2.3 release. Prior to that, a  size
       of 0 would cause getcwd() to fail with EINVAL.


       The  getcwd() function has been included in all Sun and Oracle releases
       of Solaris.

Oracle Solaris 11.4               6 Jan 2025                        getcwd(3C)
맨 페이지 내용의 저작권은 맨 페이지 작성자에게 있습니다.
RSS ATOM XHTML 5 CSS3