Functions ChDir, Fchdir, and GETCWD

Source: Internet
Author: User

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
 
   
  
  1. #include <unistd.h>
  2. int chdir(const char *pathname);
  3. int fchdir(int fd);
  4. Both return: 0 if OK, ?1 on error
Code 1. Examples of chdir and Fchdir function prototypes
  
 
  1. /**
  2. * 文件内容:因为当前工作目录是进程的一个属性,所以它只影响到调用chdir的进程本身
  3. * 而不影响其他进程
  4. * 文件时间:
  5. * [email protected]
  6. */
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. int main(void)
  10. {
  11. if (chdir("/tmp") < 0)
  12. {
  13. err_sys("chdir failed");
  14. }
  15. printf("chdir to /tmp succeeded\n");
  16. exit(0);
  17. }
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
 
   
  
  1. #include <unistd.h>
  2. char *getcwd(char *buf, size_t size);
  3. 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
  
 
  1. /**
  2. * 文件名:mycwd.c
  3. * 文件内容: 将工作目录更改至一个指定目录,然后调用getcwd,最后打印该工作目录
  4. * 时间:2016年 11月 14日 星期一 07:59:08 CST
  5. * [email protected]
  6. */
  7. #include <stdio.h>
  8. #include <unistd.h>
  9. #include <stdlib.h>
  10. #include "pathalloc.h"
  11. int main(void)
  12. {
  13. char *ptr = NULL;
  14. size_t size = 0;
  15. if (chdir("/usr/spool/uucppublic") < 0)
  16. {
  17. err_sys("chdir failed");
  18. }
  19. ptr = path_alloc(&size);
  20. if (getcwd(ptr, size) == NULL)
  21. {
  22. err_sys("getcwd failed");
  23. }
  24. printf("cwd = %s\n", ptr);
  25. if (ptr != NULL)
  26. {
  27. free(ptr);
  28. ptr = NULL;
  29. }
  30. exit(0);
  31. }
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.