Advanced Programming in the UNIX® Environment

(lily) #1
ptg10805159

Section 4.23 chdir,fchdir,andgetcwdFunctions 135


To illustrate theftwandnftwfunctions, we have provided moregenerality in this
program than needed. For example, the functionmyfunc always returns 0, even
though the function that calls it is prepared to handle a nonzeroreturn.

For additional information on descending through a file system and using this
technique in many standardUNIX System commands —find, ls, tar,and so
on — refer to Fowler,Korn, and Vo[ 1989 ].

4.23 chdir,fchdir,and getcwdFunctions


Every process has a current working directory.This directory is wherethe search for all
relative pathnames starts (i.e., with all pathnames that do not begin with a slash). When
auser logs in to a UNIX system, the current working directory normally starts at the
directory specified by the sixth field in the/etc/passwd file — the user ’s home
directory.The current working directory is an attribute of a process; the home directory
is an attribute of a login name.
We can change the current working directory of the calling process by calling the
chdirorfchdirfunction.
#include <unistd.h>
int chdir(const char *pathname);
int fchdir(intfd);
Both return: 0 if OK,−1 on error

We can specify the new current working directory either as apathnameor through an
open file descriptor.

Example


Because it is an attribute of a process, the current working directory cannot affect
processes that invoke the process that executes the chdir.(We describe the
relationship between processes in moredetail in Chapter 8.) As a result, the program in
Figure4.23 doesn’t do what we might expect.
#include "apue.h"
int
main(void)
{
if (chdir("/tmp") < 0)
err_sys("chdir failed");
printf("chdir to /tmp succeeded\n");
exit(0);
}

Figure 4.23Example ofchdirfunction
Free download pdf