apue第三版中文版4.23节
先是一个chdir的例子讲了执行该程序后无法更改当前目录:
#include "apue.h"
int
main(void)
{
if (chdir("/tmp") < 0)
err_sys("chdir failed");
printf("chdir to /tmp succeeded\n");
exit(0);
}
然后先显示pwd 接着执行./a.out 然后pwd发现当前目录没有切换到/tmp
接着讲到getcwd函数,例子:
#include "apue.h"
int
main(void)
{
char *ptr;
size_t size;
if (chdir("/usr/lib") < 0)
err_sys("chdir failed");
ptr = path_alloc(&size); /* our own function */
if (getcwd(ptr, size) == NULL)
err_sys("getcwd failed");
printf("cwd = %s\n", ptr);
exit(0);
}
由于我的ubuntu没有/usr/spool这个目录,所以改成了/usr/lib
getcwd只是获取了当前目录,但执行完之后pwd显示还是之前的目录?
chdir() changes the current working directory of the calling process to the directory specified in path.
man手册中说的很清楚了,chdir改变的是调用进程的当前工作目录,而不是你终端的工作目录。