]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man3/getcwd.3
473c3255c57b6da2c518080a5a57dde45f2ef82d
[thirdparty/man-pages.git] / man3 / getcwd.3
1 .\" (c) 1993 by Thomas Koenig (ig25@rz.uni-karlsruhe.de)
2 .\"
3 .\" Permission is granted to make and distribute verbatim copies of this
4 .\" manual provided the copyright notice and this permission notice are
5 .\" preserved on all copies.
6 .\"
7 .\" Permission is granted to copy and distribute modified versions of this
8 .\" manual under the conditions for verbatim copying, provided that the
9 .\" entire resulting derived work is distributed under the terms of a
10 .\" permission notice identical to this one.
11 .\"
12 .\" Since the Linux kernel and libraries are constantly changing, this
13 .\" manual page may be incorrect or out-of-date. The author(s) assume no
14 .\" responsibility for errors or omissions, or for damages resulting from
15 .\" the use of the information contained herein. The author(s) may not
16 .\" have taken the same level of care in the production of this manual,
17 .\" which is licensed free of charge, as they might when working
18 .\" professionally.
19 .\"
20 .\" Formatted or processed versions of this manual, if unaccompanied by
21 .\" the source, must acknowledge the copyright and authors of this work.
22 .\" License.
23 .\" Modified Wed Jul 21 22:35:42 1993 by Rik Faith (faith@cs.unc.edu)
24 .\" Modified 18 Mar 1996 by Martin Schulze (joey@infodrom.north.de):
25 .\" Corrected description of getwd().
26 .\" Modified Sat Aug 21 12:32:12 MET 1999 by aeb - applied fix by aj
27 .\" Modified Mon Dec 11 13:32:51 MET 2000 by aeb
28 .\" Modified Thu Apr 22 03:49:15 CEST 2002 by Roger Luethi <rl@hellgate.ch>
29 .\"
30 .TH GETCWD 3 2002-04-22 "GNU" "Linux Programmer's Manual"
31 .SH NAME
32 getcwd, get_current_dir_name, getwd \- Get current working directory
33 .SH SYNOPSIS
34 .nf
35 .B #include <unistd.h>
36 .sp
37 .BI "char *getcwd(char *" buf ", size_t " size );
38 .B "char *get_current_dir_name(void);"
39 .BI "char *getwd(char *" buf );
40 .fi
41 .SH DESCRIPTION
42 The
43 .BR getcwd ()
44 function copies an absolute pathname of the current working directory
45 to the array pointed to by
46 .IR buf ,
47 which is of length
48 .IR size .
49 .PP
50 If the current absolute path name would require a buffer longer than
51 .I size
52 elements,
53 .B NULL
54 is returned, and
55 .I errno
56 is set to
57 .BR ERANGE ;
58 an application should check for this error, and allocate a larger
59 buffer if necessary.
60 .PP
61 If
62 .I buf
63 is NULL, the behaviour of
64 .BR getcwd ()
65 is undefined.
66 .PP
67 As an extension to the POSIX.1 standard, Linux (libc4, libc5, glibc)
68 .B getcwd()
69 allocates the buffer dynamically using
70 .B malloc()
71 if
72 .I buf
73 is
74 .B NULL
75 on call. In this case, the allocated buffer has the length
76 .I size
77 unless
78 .I size
79 is zero, when
80 .I buf
81 is allocated as big as necessary. It is possible (and, indeed,
82 advisable) to
83 .B free()
84 the buffers if they have been obtained this way.
85
86 .BR get_current_dir_name ,
87 which is only prototyped if
88 .B _GNU_SOURCE
89 is defined, will
90 .BR malloc (3)
91 an array big enough to hold the current directory name. If the environment
92 variable
93 .B PWD
94 is set, and its value is correct, then that value will be returned.
95
96 .BR getwd ,
97 which is only prototyped if
98 .B _BSD_SOURCE
99 or
100 .B _XOPEN_SOURCE_EXTENDED
101 is defined, will not
102 .BR malloc (3)
103 any memory. The
104 .I buf
105 argument should be a pointer to an array at least
106 .B PATH_MAX
107 bytes long.
108 .BR getwd
109 does only return the first
110 .B PATH_MAX
111 bytes of the actual pathname.
112 Note that
113 .B PATH_MAX
114 need not be a compile-time constant; it may depend on the filesystem
115 and may even be unlimited. For portability and security reasons, use of
116 .B getwd
117 is deprecated.
118 .SH "RETURN VALUE"
119 .B NULL
120 on failure with
121 .I errno
122 set accordingly, and
123 .I buf
124 on success. The contents of the array pointed to by
125 .IR buf
126 is undefined on error.
127 .SH ERRORS
128 .TP
129 .B EACCES
130 Permission to read or search a component of the file name was denied.
131 .TP
132 .B EFAULT
133 .IR buf
134 points to a bad address.
135 .TP
136 .B EINVAL
137 The
138 .IR size
139 argument is zero and
140 .IR buf
141 is not a null pointer.
142 .TP
143 .B ENOENT
144 The current working directory has been unlinked.
145 .TP
146 .B ERANGE
147 The
148 .IR size
149 argument is less than the length of the working directory name.
150 You need to allocate a bigger array and try again.
151 .SH NOTES
152 Under Linux, the function
153 .BR getcwd ()
154 is a system call (since 2.1.92).
155 On older systems it would query
156 .IR /proc/self/cwd .
157 If both system call and proc file system are missing, a
158 generic implementation is called. Only in that case can
159 these calls fail under Linux with
160 .BR EACCES .
161 .LP
162 These functions are often used to save the location of the current working
163 directory for the purpose of returning to it later. Opening the current
164 directory (".") and calling
165 .BR fchdir (2)
166 to return is usually a faster and more reliable alternative when sufficiently
167 many file descriptors are available, especially on platforms other than Linux.
168 .SH "CONFORMING TO"
169 POSIX.1
170 .SH "SEE ALSO"
171 .BR chdir (2),
172 .BR fchdir (2),
173 .BR open (2),
174 .BR unlink (2),
175 .BR free (3),
176 .BR malloc (3)