Advertisement
Guest User

getcwd+chdir.c

a guest
Apr 4th, 2020
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.64 KB | None | 0 0
  1. #include <unistd.h>
  2. #include <errno.h>
  3. #include <string.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <limits.h>
  7. int main(void){
  8.         char cwd[PATH_MAX];
  9.         if(!getcwd(cwd,sizeof(cwd))) {
  10.                 fprintf(stderr,"Errorgetcwd(): %s",strerror(errno));
  11.                 exit(EXIT_FAILURE);
  12.         }
  13.         printf("cwd=%s\n",cwd);
  14.         char new_dir[PATH_MAX];
  15.         snprintf(new_dir,sizeof(new_dir),"/tmp/");
  16.         if(chdir(new_dir)==-1){
  17.                 fprintf(stderr,"Can'tchdirto'%s':%s\n",
  18.                                 new_dir,strerror(errno));
  19.                 exit(1);
  20.         }
  21.         return 0;
  22. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement