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