Functions ChDir, Fchdir, and Getcwdchdir, fchdir functions each process has a current working directory, and the current directory is a property of the process when a user logs on to a UNIX system, the current working directory is usually a password file/etc/ The 6th field of the user login entry in passwd process calls the ChDir or Fchdir function to change the current working directory
#include <unistd.h>
int chdir(const char *pathname);
int fchdir(int fd);
Both return: 0 if OK, ?1 on error
Code 1. Examples of chdir and Fchdir function prototypes
/**
* 文件内容:因为当前工作目录是进程的一个属性,所以它只影响到调用chdir的进程本身
* 而不影响其他进程
* 文件时间:
* [email protected]
*/
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
if (chdir("/tmp") < 0)
{
err_sys("chdir failed");
}
printf("chdir to /tmp succeeded\n");
exit(0);
}
Code 2. The ChDir function instance is compiled as follows:
gcc main.c-lerror-llib
Run as follows:
$ pwd
/home/fireway/study/temp2
$./a.out
ChDir To/tmp succeeded
As we can see, the current directory of the shell executing the a.out command has not changed, which is a side effect of how the shell executes. Each program runs in a separate process, and the shell's current working directory is not called with the program
chdirand change. Thus, in order to change the shell process's own working directory, the shell should call directly
chdirfunction, the CD command is built into the shell. GETCWD function
#include <unistd.h>
char *getcwd(char *buf, size_t size);
Returns: buf if OK, NULL on error
Code 3. GETCWD function prototype must pass two parameters to this function, one is the address of the buffer buf, the other is the length of the buffer size note that the buffer must be long enough to accommodate the absolute path name plus a terminating null byte, otherwise an error instance is returned
/**
* 文件名:mycwd.c
* 文件内容: 将工作目录更改至一个指定目录,然后调用getcwd,最后打印该工作目录
* 时间:2016年 11月 14日 星期一 07:59:08 CST
* [email protected]
*/
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include "pathalloc.h"
int main(void)
{
char *ptr = NULL;
size_t size = 0;
if (chdir("/usr/spool/uucppublic") < 0)
{
err_sys("chdir failed");
}
ptr = path_alloc(&size);
if (getcwd(ptr, size) == NULL)
{
err_sys("getcwd failed");
}
printf("cwd = %s\n", ptr);
if (ptr != NULL)
{
free(ptr);
ptr = NULL;
}
exit(0);
}
Code 4. GETCWD function Instance compiles this program
gcc main.c-lerror-l. /temp3
Run this program
#
Ln-s/home/fireway/study/temp3/usr/spool
#
./a.out
CWD =/home/fireway/study/temp3/uucppublic
#
Ls-l/usr/spool
lrwxrwxrwx 1 root root 25 November 08:24/usr/spool-/home/fireway/study/temp3
Attention
chdirFollow the symbolic link when it is
GETCWDIt does not know that the directory is pointed to by the symbolic link/usr/spool when it is traced back to the/home/fireway/study/temp3 directory tree. This is a feature of symbolic links.
GETCWDfunction allows us to save the previous working directory before changing the directory, and after completion, we can pass the saved original working directory pathname as a parameter to the
chdir, which returns the starting point of the file system.
Fchdirfunction provides a faster and easier way to change to a different path without calling the
GETCWDfunction instead of calling the
OpenOpen the current working directory, and then save its returned
FD,When you want to return to your original working directory, simply
FDPassed to
Fchdir。 Refer to Advanced Programming for UNIX Environments (third edition) 4.23 functions ChDir, Fchdir, and GETCWD
Functions ChDir, Fchdir, and GETCWD