程序要求:
采用exec函数族实现shell的功能;
程序如下:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
int main(int argc, const char *argv[])
{
pid_t pid;
char buf[100];
char *arg[100];
int i = 0;
while (1)
{
i = 0;
printf(">");
fgets(buf, sizeof(buf), stdin);
buf[strlen(buf) - 1] = 0;
if (strncmp(buf, "quit", 4) == 0)
break;
arg[i] = strtok(buf, " "); //按照空格来分解字符串
while (arg[i] != NULL)
arg[++i] = strtok(NULL, " ");
if ((pid = fork()) < 0)
{
perror("failed to fork pid");
exit(-1);
}
if