xref: /netbsd-src/lib/libc/sys/chdir.2 (revision 1644fb637597876e97971529fcaa9c32c511915b)
1.\"	$NetBSD: chdir.2,v 1.23 2019/09/01 19:11:41 sevan Exp $
2.\"
3.\" Copyright (c) 1980, 1991, 1993
4.\"	The Regents of the University of California.  All rights reserved.
5.\"
6.\" Redistribution and use in source and binary forms, with or without
7.\" modification, are permitted provided that the following conditions
8.\" are met:
9.\" 1. Redistributions of source code must retain the above copyright
10.\"    notice, this list of conditions and the following disclaimer.
11.\" 2. Redistributions in binary form must reproduce the above copyright
12.\"    notice, this list of conditions and the following disclaimer in the
13.\"    documentation and/or other materials provided with the distribution.
14.\" 3. Neither the name of the University nor the names of its contributors
15.\"    may be used to endorse or promote products derived from this software
16.\"    without specific prior written permission.
17.\"
18.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28.\" SUCH DAMAGE.
29.\"
30.\"     @(#)chdir.2	8.2 (Berkeley) 12/11/93
31.\"
32.Dd September 1, 2019
33.Dt CHDIR 2
34.Os
35.Sh NAME
36.Nm chdir ,
37.Nm fchdir
38.Nd change current working directory
39.Sh LIBRARY
40.Lb libc
41.Sh SYNOPSIS
42.In unistd.h
43.Ft int
44.Fn chdir "const char *path"
45.Ft int
46.Fn fchdir "int fd"
47.Sh DESCRIPTION
48The
49.Fa path
50argument points to the pathname of a directory.
51The
52.Fn chdir
53function
54causes the named directory
55to become the current working directory, that is,
56the starting point for path searches of pathnames not beginning with
57a slash,
58.Ql / .
59.Pp
60The
61.Fn fchdir
62function
63causes the directory referenced by
64.Fa fd
65to become the current working directory,
66the starting point for path searches of pathnames not beginning with
67a slash,
68.Ql / .
69.Pp
70In order for a directory to become the current directory,
71a process must have execute (search) access to the directory.
72.Sh RETURN VALUES
73Upon successful completion, a value of 0 is returned.
74Otherwise, a value of \-1 is returned and
75.Va errno
76is set to indicate
77the error.
78.Sh ERRORS
79.Fn chdir
80will fail and the current working directory will be unchanged if
81one or more of the following are true:
82.Bl -tag -width Er
83.It Bq Er EACCES
84Search permission is denied for any component of
85the path name.
86.It Bq Er EFAULT
87.Fa path
88points outside the process's allocated address space.
89.It Bq Er EIO
90An I/O error occurred while reading from or writing to the file system.
91.It Bq Er ELOOP
92Too many symbolic links were encountered in translating the pathname.
93.It Bq Er ENAMETOOLONG
94A component of a pathname exceeded
95.Brq Dv NAME_MAX
96characters, or an entire path name exceeded
97.Brq Dv PATH_MAX
98characters.
99.It Bq Er ENOENT
100The named directory does not exist.
101.It Bq Er ENOTDIR
102A component of the path prefix is not a directory.
103.El
104.Pp
105.Fn fchdir
106will fail and the current working directory will be unchanged if
107one or more of the following are true:
108.Bl -tag -width Er
109.It Bq Er EACCES
110Search permission is denied for the directory referenced by the
111file descriptor.
112.It Bq Er EBADF
113The argument
114.Fa fd
115is not a valid file descriptor.
116.It Bq Er ENOTDIR
117The file descriptor does not reference a directory.
118.It Bq Er EPERM
119The argument
120.Fa fd
121references a directory which is not at or below the current process's
122root directory.
123.El
124.Sh SEE ALSO
125.Xr chroot 2 ,
126.Xr getcwd 3
127.Sh STANDARDS
128The
129.Fn chdir
130function conforms to
131.St -p1003.1-90 .
132.Sh HISTORY
133A
134.Fn chdir
135function call appeared in
136.At v1 .
137The
138.Fn fchdir
139function call
140appeared in
141.Bx 4.2 .
142