1GETCWD(3P)                 POSIX Programmer's Manual                GETCWD(3P)
2
3
4

PROLOG

6       This  manual  page is part of the POSIX Programmer's Manual.  The Linux
7       implementation of this interface may differ (consult the  corresponding
8       Linux  manual page for details of Linux behavior), or the interface may
9       not be implemented on Linux.
10
11

NAME

13       getcwd — get the pathname of the current working directory
14

SYNOPSIS

16       #include <unistd.h>
17
18       char *getcwd(char *buf, size_t size);
19

DESCRIPTION

21       The getcwd() function shall place an absolute pathname of  the  current
22       working  directory in the array pointed to by buf, and return buf.  The
23       pathname shall contain no components that are dot or  dot-dot,  or  are
24       symbolic links.
25
26       If  there are multiple pathnames that getcwd() could place in the array
27       pointed to by buf, one beginning with a single  <slash>  character  and
28       one  or more beginning with two <slash> characters, then getcwd() shall
29       place the pathname beginning with a single  <slash>  character  in  the
30       array.  The  pathname shall not contain any unnecessary <slash> charac‐
31       ters after the leading one or two <slash> characters.
32
33       The size argument is the size in bytes of the character  array  pointed
34       to  by  the  buf  argument.  If  buf is a null pointer, the behavior of
35       getcwd() is unspecified.
36

RETURN VALUE

38       Upon successful completion, getcwd() shall  return  the  buf  argument.
39       Otherwise,  getcwd() shall return a null pointer and set errno to indi‐
40       cate the error. The contents of the array pointed to by  buf  are  then
41       undefined.
42

ERRORS

44       The getcwd() function shall fail if:
45
46       EINVAL The size argument is 0.
47
48       ERANGE The  size  argument  is  greater than 0, but is smaller than the
49              length of the string +1.
50
51       The getcwd() function may fail if:
52
53       EACCES Search permission was denied for the current directory, or  read
54              or  search  permission was denied for a directory above the cur‐
55              rent directory in the file hierarchy.
56
57       ENOMEM Insufficient storage space is available.
58
59       The following sections are informative.
60

EXAMPLES

62       The following example  uses  {PATH_MAX}  as  the  initial  buffer  size
63       (unless  it  is  indeterminate  or very large), and calls getcwd() with
64       progressively larger buffers until it does not give an [ERANGE] error.
65
66           #include <stdlib.h>
67           #include <errno.h>
68           #include <unistd.h>
69
70           ...
71
72           long path_max;
73           size_t size;
74           char *buf;
75           char *ptr;
76
77           path_max = pathconf(".", _PC_PATH_MAX);
78           if (path_max == -1)
79               size = 1024;
80           else if (path_max > 10240)
81               size = 10240;
82           else
83               size = path_max;
84
85           for (buf = ptr