]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man3/getcwd.3
intro.1, _exit.2, access.2, alarm.2, alloc_hugepages.2, arch_prctl.2, bind.2, chdir...
[thirdparty/man-pages.git] / man3 / getcwd.3
CommitLineData
bf5a7247 1.\" Copyright (c) 1993 by Thomas Koenig (ig25@rz.uni-karlsruhe.de)
fea681da 2.\"
4b72fb64 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.\"
50831f9b 32.TH GETCWD 3 2010-09-20 "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
63_BSD_SOURCE ||
98dbe7af 64 (_XOPEN_SOURCE\ >=\ 500 ||
7b072c4d 65 _XOPEN_SOURCE\ &&\ _XOPEN_SOURCE_EXTENDED) &&
98dbe7af 66 !(_POSIX_C_SOURCE\ >=\ 200809L || _XOPEN_SOURCE\ >=\ 700)
a89b9a38
MK
67.TP 4
68.fi
69Before glibc 2.12:
98dbe7af 70_BSD_SOURCE || _XOPEN_SOURCE\ >=\ 500 ||
a89b9a38
MK
71_XOPEN_SOURCE\ &&\ _XOPEN_SOURCE_EXTENDED
72.PD
73.RE
74.ad b
fea681da 75.SH DESCRIPTION
c359e37d
MK
76These functions return a null-terminated string containing an
77absolute pathname that is the current working directory of
78the calling process.
79The pathname is returned as the function result and via the argument
80.IR buf ,
81if present.
82
fea681da
MK
83The
84.BR getcwd ()
85function copies an absolute pathname of the current working directory
86to the array pointed to by
87.IR buf ,
88which is of length
89.IR size .
90.PP
c359e37d
MK
91If the length of the absolute pathname of the current working directory,
92including the terminating null byte, exceeds
fea681da 93.I size
c359e37d 94bytes, NULL is returned, and
fea681da
MK
95.I errno
96is set to
97.BR ERANGE ;
98an application should check for this error, and allocate a larger
99buffer if necessary.
100.PP
a7fadb55 101As an extension to the POSIX.1-2001 standard, Linux (libc4, libc5, glibc)
63aa9df0 102.BR getcwd ()
fea681da 103allocates the buffer dynamically using
fb186734 104.BR malloc (3)
fea681da
MK
105if
106.I buf
c359e37d 107is NULL.
8478ee02 108In this case, the allocated buffer has the length
fea681da
MK
109.I size
110unless
111.I size
112is zero, when
113.I buf
c13182ef 114is allocated as big as necessary.
c359e37d 115The caller should
fb186734 116.BR free (3)
c359e37d 117the returned buffer.
fea681da 118
51d0a7e9 119.BR get_current_dir_name ()
ecad088b 120will
fea681da 121.BR malloc (3)
c359e37d
MK
122an array big enough to hold the absolute pathname of
123the current working directory.
c13182ef 124If the environment
fea681da
MK
125variable
126.B PWD
127is set, and its value is correct, then that value will be returned.
c359e37d
MK
128The caller should
129.BR free (3)
130the returned buffer.
fea681da 131
7847d5cd 132.BR getwd ()
ecad088b 133does not
fea681da 134.BR malloc (3)
c13182ef 135any memory.
ecad088b 136The
fea681da
MK
137.I buf
138argument should be a pointer to an array at least
139.B PATH_MAX
140bytes long.
c359e37d
MK
141If the length of the absolute pathname of the current working directory,
142including the terminating null byte, exceeds
fea681da 143.B PATH_MAX
c359e37d
MK
144bytes, NULL is returned, and
145.I errno
146is set to
147.BR ENAMETOOLONG .
b4338c3f
MK
148(Note that on some systems,
149.B PATH_MAX
e8f4a1c9 150may not be a compile-time constant;
b4338c3f
MK
151furthermore, its value may depend on the file system, see
152.BR pathconf (3).)
ecad088b 153For portability and security reasons, use of
e511ffb6 154.BR getwd ()
fea681da 155is deprecated.
47297adb 156.SH RETURN VALUE
c359e37d
MK
157On success, these functions return a pointer to a string containing
158the pathname of the current working directory.
159In the case
160.BR getcwd ()
161and
162.BR getwd ()
163this is the same value as
fe48639f 164.IR buf .
c359e37d
MK
165
166On failure, these functions return NULL, and
fea681da 167.I errno
c359e37d 168is set to indicate the error.
c13182ef 169The contents of the array pointed to by
0daa9e92 170.I buf
c359e37d 171are undefined on error.
fea681da
MK
172.SH ERRORS
173.TP
174.B EACCES
c13182ef 175Permission to read or search a component of the filename was denied.
fea681da
MK
176.TP
177.B EFAULT
0daa9e92 178.I buf
fea681da
MK
179points to a bad address.
180.TP
181.B EINVAL
c13182ef 182The
0daa9e92 183.I size
fea681da 184argument is zero and
0daa9e92 185.I buf
ad3fd4c0 186is not a NULL pointer.
fea681da 187.TP
c359e37d
MK
188EINVAL
189.BR getwd ():
190.I buf
191is NULL.
192.TP
193ENAMETOOLONG
194.BR getwd ():
195The size of the null-terminated absolute pathname string exceeds
196.B PATH_MAX
197bytes.
198.TP
fea681da
MK
199.B ENOENT
200The current working directory has been unlinked.
201.TP
202.B ERANGE
203The
0daa9e92 204.I size