标签:error 传递 返回 2016年 path 改变 系统 shel pat
#include <unistd.h>int chdir(const char *pathname);int fchdir(int fd);Both return: 0 if OK, ?1 on error
/*** 文件内容:因为当前工作目录是进程的一个属性,所以它只影响到调用chdir的进程本身* 而不影响其他进程* 文件时间:* 作者:firewaywei@126.com*/#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);}
gcc main.c -lerror -Llib
$ pwd
/home/fireway/study/temp2
$ ./a.out
chdir to /tmp succeeded
#include <unistd.h>char *getcwd(char *buf, size_t size);Returns: buf if OK, NULL on error
/*** 文件名:mycwd.c* 文件内容: 将工作目录更改至一个指定目录,然后调用getcwd,最后打印该工作目录* 时间:2016年 11月 14日 星期一 07:59:08 CST* 作者:firewaywei@126.com*/#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);}
gcc main.c -lerror -L../temp3
# 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 11月 14 08:24 /usr/spool -> /home/fireway/study/temp3
标签:error 传递 返回 2016年 path 改变 系统 shel pat
原文地址:http://www.cnblogs.com/fireway/p/6064262.html