您的位置:首页 > 运维架构 > Shell

UNIX高级编程之简易版shell

2015-07-17 20:43 323 查看
#include <stdio.h>

#include <unistd.h>

#include <sys/types.h>

#include <wait.h>

#include <stdlib.h>

#include <sys/stat.h>

#include <string.h>

int main()

{

while(1)

{

#define MAX 1024

char buf[MAX];

fgets(buf,MAX,stdin);

int len =strlen(buf);

buf[len - 1] = 0;

if(strcmp("quit",buf) ==0)

{

exit(0);

}

pid_t pid = fork();

if(pid == 0)

{

if(execlp(buf,buf,NULL) <0)

{

perror("exec");

//exit(0);

}

/*setsid();

chdir("/");

umask(0);

int fdtablesize = getdtablesize();

int fd;

for(fd = 0;fd < fdtablesize;fd++)

close(fd);

fprintf(stdout,"rong?->$:");

*/

}

else if(pid > 0)

printf("rightrunning!\n");

else

perror("create child process wrong!");

}

exit(0);

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: