[lingyun@localhost chdir]$ ls
chdir.c
[lingyun@localhost chdir]$ cat chdir.c
/*********************************************************************************
* Copyright: (C) 2013 fulinux<fulinux@sina.com>
* All rights reserved.
*
* Filename: chdir.c
* Description: This file
*
* Version: 1.0.0(08/06/2013~)
* Author: fulinux <fulinux@sina.com>
* ChangeLog: 1, Release initial version on "08/06/2013 03:35:13 PM"
*
********************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
int main(void)
{
if(chdir("/tmp") < 0)
{
perror("chdir");
exit(1);
}
printf("chdir to /tmp successded\n");
exit(0);
}
[lingyun@localhost chdir]$ gcc chdir.c
[lingyun@localhost chdir]$ pwd
/home/lingyun/apue/chdir
[lingyun@localhost chdir]$ ./a.out
chdir to /tmp successded
[lingyun@localhost chdir]$ pwd
/home/lingyun/apue/chdir
[lingyun@localhost chdir]$
执行./a.out程序的shell的当前工作目录没有变,其原因是创建了一个子进程并由该子程执行./a.out程序。记得有一次我把文件系统整个都删了,什么命令都不能执行,但是cd命令可以,apue上说cd命令是直接包含在shell程序中的。