1.进程的工作目录
#include <unistd.h>
int chdir(char * pathname);
//chdir.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
int main(void)
{
int fd;
char *p = "hello world/n"; /* 测试用的字符串 */
if(chdir("/home/admin/tmp") == -1){ /* 改变进程的工作目录 */
perror("fail to change dir");
exit(1);
}
printf("change dir successfully/n"); /* 输出提示信息 */
if((fd = open("test.txt", O_CREAT | O_RDWR)) == -1){ /* 创建一个文件,该文件创建在修改后的目录 */
perror("fail to open");
exit(1);
}
if(write(fd, p, strlen(p)) == -1){ /* 向该文件中输出内容 */
perror("fail to write");
exit(1);
}
close(fd); /* 关闭文件 */
return 0;
}
2.得到进程的当前工作目录
#include<unistd.h>
定义函数 char * getcwd(char * buf,size_t size);
函数说明 getcwd()会将当前的工作目录绝对路径复制到参数buf所指的内存空间,参数size为buf的空间大小。在调用此函数时,buf所指的内存空间要 足够大,若工作目录绝对路径的字符串长度超过参数size大小,则回值NULL,errno的值则为ERANGE。倘若参数buf NULL,getcwd()会依参数size的大小自动配置内存(使用malloc()),如果参数size也为0,则getcwd()会依工作目录绝对 路径的字符串程度来决定所配置的内存大小,进程可以在使用完此字符串后利用free()来释放此空间 。
返回值 执行成功则将结果复制到参数buf所指的内存空间,或是返回自动配置的字符串指针。失败返回NULL,错误代码存于errno。
//get_pwd.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#define MAX 1024
int main(void)
{
pid_t pid;
char buf[MAX];
if(chdir("/home/admin/tmp") == -1){ /* 改变进程的工作目录 */
perror("fail to change dir");
exit(1);
}
if(getcwd(buf, MAX) == NULL){ /* 得到修改后的工作目录 */
perror("fail to get pwd");
exit(1);
}
printf("the parent : %s/n", buf); /* 打印修改后的工作目录 */
pid = fork(); /* 创建一个子进程 */
if(pid == -1){
perror("fail to fork");
exit(1);
}else if(pid == 0){
if(getcwd(buf, MAX) == NULL){ /* 得到子进程的工作目录 */
perror("fail to get pwd");
exit(1);
}
printf("the child : %s/n", buf);
}else{
if(wait(NULL) == -1){ /* 避免僵尸进程 */
perror("fail to wait");
exit(1);
}
}
return 0;
}
3.子进程工作目录对父进程的影响
子进程修改工作目录不会影响到父进程。此时在考虑shell程序的实现,shell调用fork函数和exec函数执行一个命令,。如果cd命令以一个程序的方式实现,然后在shell中调用。这相当于生成一个子进程,然后子进程改变了目录,但是对父进程即原shell的目录没影响。因此,cd命令是在shell直接实现的。
范例:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#define MAX 1024
int main(void)
{
pid_t pid;
char buf[MAX];
if(getcwd(buf, MAX) == NULL){ /* 得到修改后的工作目录 */
perror("fail to get pwd");
exit(1);
}
printf("the parent’s pwd is : %s/n", buf);
pid = fork(); /* 创建一个子进程 */
if(pid == -1){
perror("fail to fork");
exit(1);
}else if(pid == 0){
printf("the child/n");
if(chdir("/home/usr/") == -1){ /* 改变进程的工作目录 */
perror("fail to change dir");
exit(1);
}
if(getcwd(buf, MAX) == NULL){ /* 得到子进程的工作目录 */
perror("fail to get pwd");
exit(1);
}
printf("the child’s pwd is : %s/n", buf); /* 打印子进程修改后的工作目录 */
}else{
if(wait(NULL) == -1){ /* 避免僵尸进程 */
perror("fail to wait");
exit(1);
}
if(getcwd(buf, MAX) == NULL){ /* 再次得到父进程的工作目录 */
perror("fail to get pwd");
exit(1);
}
printf("the parent’s pwd is : %s/n", buf); /* 输出提示信息 */
}
return 0;
}
由上例可以看出子进程改变工作目录对父进程没影响。
下面是错误的改变工作目录的方法:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#define MAX 1024
int main()
{
char buf[MAX];
if(getcwd(buf,MAX)==NULL){
perror("fial to get pwd");
exit(1);
}
printf("%s/n",buf);
if(system("cd /usr/bin")==-1){
perror("fail to exec");
exit(1);
}
if(getcwd(buf,MAX)==NULL){
perror("fial to get pwd");
exit(1);
}
printf("%s/n",buf);
return 0;
}
在本例中工作目录并没有实现。