]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man3/getcwd.3
request_key.2: Order ERRORS alphabetically
[thirdparty/man-pages.git] / man3 / getcwd.3
CommitLineData
bf5a7247 1.\" Copyright (c) 1993 by Thomas Koenig (ig25@rz.uni-karlsruhe.de)
fea681da 2.\"
93015253 3.\" %%%LICENSE_START(VERBATIM)
fea681da
MK
4.\" Permission is granted to make and distribute verbatim copies of this
5.\" manual provided the copyright notice and this permission notice are
6.\" preserved on all copies.
7.\"
8.\" Permission is granted to copy and distribute modified versions of this
9.\" manual under the conditions for verbatim copying, provided that the
10.\" entire resulting derived work is distributed under the terms of a
11.\" permission notice identical to this one.
c13182ef 12.\"
fea681da
MK
13.\" Since the Linux kernel and libraries are constantly changing, this
14.\" manual page may be incorrect or out-of-date. The author(s) assume no
15.\" responsibility for errors or omissions, or for damages resulting from
16.\" the use of the information contained herein. The author(s) may not
17.\" have taken the same level of care in the production of this manual,
18.\" which is licensed free of charge, as they might when working
19.\" professionally.
c13182ef 20.\"
fea681da
MK
21.\" Formatted or processed versions of this manual, if unaccompanied by
22.\" the source, must acknowledge the copyright and authors of this work.
4b72fb64 23.\" %%%LICENSE_END
c08df37a 24.\"
fea681da
MK
25.\" Modified Wed Jul 21 22:35:42 1993 by Rik Faith (faith@cs.unc.edu)
26.\" Modified 18 Mar 1996 by Martin Schulze (joey@infodrom.north.de):
27.\" Corrected description of getwd().
28.\" Modified Sat Aug 21 12:32:12 MET 1999 by aeb - applied fix by aj
29.\" Modified Mon Dec 11 13:32:51 MET 2000 by aeb
30.\" Modified Thu Apr 22 03:49:15 CEST 2002 by Roger Luethi <rl@hellgate.ch>
31.\"
97986708 32.TH GETCWD 3 2016-03-15 "GNU" "Linux Programmer's Manual"
fea681da 33.SH NAME
f68512e9 34getcwd, getwd, get_current_dir_name \- get current working directory
fea681da
MK
35.SH SYNOPSIS
36.nf
37.B #include <unistd.h>
38.sp
39.BI "char *getcwd(char *" buf ", size_t " size );
ecad088b 40.sp
fea681da 41.BI "char *getwd(char *" buf );
ecad088b 42.sp
ecad088b 43.B "char *get_current_dir_name(void);"
fea681da 44.fi
cc4615cc
MK
45.sp
46.in -4n
47Feature Test Macro Requirements for glibc (see
48.BR feature_test_macros (7)):
a74ec77e 49.sp
cc4615cc 50.in
cc4615cc 51.BR get_current_dir_name ():
a74ec77e 52.RS
cc4615cc 53_GNU_SOURCE
a74ec77e 54.RE
a89b9a38
MK
55.sp
56.BR getwd ():
57.ad l
58.RS 4
59.PD 0
60.TP 4
61Since glibc 2.12:
62.nf
6e0869f7
MK
63(_XOPEN_SOURCE\ >=\ 500) && ! (_POSIX_C_SOURCE\ >=\ 200809L)
64 || /* Glibc since 2.19: */ _DEFAULT_SOURCE
65 || /* Glibc versions <= 2.19: */ _BSD_SOURCE
a89b9a38
MK
66.TP 4
67.fi
68Before glibc 2.12:
cf7fa0a1
MK
69_BSD_SOURCE || _XOPEN_SOURCE\ >=\ 500
70.\" || _XOPEN_SOURCE\ &&\ _XOPEN_SOURCE_EXTENDED
a89b9a38
MK
71.PD
72.RE
73.ad b
fea681da 74.SH DESCRIPTION
c359e37d
MK
75These functions return a null-terminated string containing an
76absolute pathname that is the current working directory of
77the calling process.
78The pathname is returned as the function result and via the argument
79.IR buf ,
80if present.
81
a2ac97c7 82If the current directory is not below the root directory of the current
2b1f1a35 83process (e.g., because the process set a new filesystem root using
a2ac97c7 84.BR chroot (2)
de0dd427 85without changing its current directory into the new root),
2b1f1a35
MK
86then, since Linux 2.6.36,
87.\" commit 8df9d1a4142311c084ffeeacb67cd34d190eff74
de0dd427
MK
88the returned path will be prefixed with the string "(unreachable)".
89Such behavior can also be caused by an unprivileged user by changing
90the current directory into another mount namespace.
a2ac97c7
JH
91When dealing with paths from untrusted sources, callers of these
92functions should consider checking whether the returned path starts
93with '/' or '(' to avoid misinterpreting an unreachable path
94as a relative path.
95
fea681da
MK
96The
97.BR getcwd ()
98function copies an absolute pathname of the current working directory
99to the array pointed to by
100.IR buf ,
101which is of length
102.IR size .
103.PP
c359e37d
MK
104If the length of the absolute pathname of the current working directory,
105including the terminating null byte, exceeds
fea681da 106.I size
c359e37d 107bytes, NULL is returned, and
fea681da
MK
108.I errno
109is set to
110.BR ERANGE ;
111an application should check for this error, and allocate a larger
112buffer if necessary.
113.PP
acd67de4 114As an extension to the POSIX.1-2001 standard, glibc's
63aa9df0 115.BR getcwd ()
fea681da 116allocates the buffer dynamically using
fb186734 117.BR malloc (3)
fea681da
MK
118if
119.I buf
c359e37d 120is NULL.
8478ee02 121In this case, the allocated buffer has the length
fea681da
MK
122.I size
123unless
124.I size
125is zero, when
126.I buf
c13182ef 127is allocated as big as necessary.
c359e37d 128The caller should
fb186734 129.BR free (3)
c359e37d 130the returned buffer.
fea681da 131
51d0a7e9 132.BR get_current_dir_name ()
ecad088b 133will
fea681da 134.BR malloc (3)
c359e37d
MK
135an array big enough to hold the absolute pathname of
136the current working directory.
c13182ef 137If the environment
fea681da
MK
138variable
139.B PWD
140is set, and its value is correct, then that value will be returned.
c359e37d
MK
141The caller should
142.BR free (3)
143the returned buffer.
fea681da 144
7847d5cd 145.BR getwd ()
ecad088b 146does not
fea681da 147.BR malloc (3)
c13182ef 148any memory.
ecad088b 149The
fea681da
MK
150.I buf
151argument should be a pointer to an array at least
152.B PATH_MAX
153bytes long.
c359e37d
MK
154If the length of the absolute pathname of the current working directory,
155including the terminating null byte, exceeds
fea681da 156.B PATH_MAX
c359e37d
MK
157bytes, NULL is returned, and
158.I errno
159is set to
160.BR ENAMETOOLONG .
b4338c3f
MK
161(Note that on some systems,
162.B PATH_MAX
e8f4a1c9 163may not be a compile-time constant;
9ee4a2b6 164furthermore, its value may depend on the filesystem, see
b4338c3f 165.BR pathconf (3).)
ecad088b 166For portability and security reasons, use of
e511ffb6 167.BR getwd ()
fea681da 168is deprecated.
47297adb 169.SH RETURN VALUE
c359e37d
MK
170On success, these functions return a pointer to a string containing
171the pathname of the current working directory.
172In the case
173.BR getcwd ()
174and
175.BR getwd ()
176this is the same value as
fe48639f 177.IR buf .
c359e37d
MK
178
179On failure, these functions return NULL, and
fea681da 180.I errno
c359e37d 181is set to indicate the error.
c13182ef 182The contents of the array pointed to by
0daa9e92 183.I buf
c359e37d 184are undefined on error.
fea681da
MK
185.SH ERRORS
186.TP
187.B EACCES
c13182ef 188Permission to read or search a component of the filename was denied.
fea681da
MK
189.TP
190.B EFAULT
0daa9e92 191.I buf
fea681da
MK
192points to a bad address.
193.TP
194.B EINVAL
c13182ef 195The
0daa9e92 196.I size
fea681da 197argument is zero and
0daa9e92 198.I buf
b437fdd9 199is not a null pointer.
fea681da 200.TP
83685d1e 201.B EINVAL
c359e37d
MK
202.BR getwd ():
203.I buf
204is NULL.
205.TP
83685d1e 206.B ENAMETOOLONG
c359e37d
MK
207.BR getwd ():
208The size of the null-terminated absolute pathname string exceeds
209.B PATH_MAX
210bytes.
211.TP
28e8ce31
MK
212.B ENOMEM
213Out of memory.
214.TP
fea681da
MK
215.B ENOENT
216The current working directory has been unlinked.
217.TP
218.B ERANGE
219The
0daa9e92 220.I size