我的代码在这里。本质上,自定义 shell 需要将 echo、cd 和 quit 作为命令,并 fork 一个子命令作为命令。编译时没有错误,但没有运行。当我给出“echo hello”时,它没有回应论点……它进入了正确的功能,但我无法查明错误。我猜我在 execlp 函数上犯了一个错误。有人可以帮我吗?
#include <stdio.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <string.h>
int main()
{
char command[256];
char * parsedCmd;
char * argument;
//char *sep[]=" ";
while (1<2)
{
printf("\nOS Assignment 1@user: ");
fgets(command, sizeof(command), stdin);
parsedCmd=strtok(command, " ");
argument=strtok(NULL, " ");
//printf("\n%s\n", argument);
if (strncmp("quit", command, 4)==0)
break;
if (strncmp("cd", parsedCmd, 2)==0)
{
printf("\nExecuting cd\n");
execCD(argument);
}
if (strncmp("echo", parsedCmd, 4)==0)
{
printf("\nEchoing now...\n");
shellEcho(argument);
}
else
printf("\nOur shell is simple. Try either cd, echo or quit :) ...\n");
}
}
int execCD(char *receive)
{
printf("\nExecuting cd as Child...\n");
printf("\nDirectory to cd is %s\n", receive);
pid_t pid;
pid=fork();
if (pid<0)
{
fprintf(stderr, "\nFork Failed\n");
return 1;
}
else if (pid==0)
{
execlp(receive, "cd", NULL);
}
else
{
wait(NULL);
printf("Child Complete");
}
return 0;
}
int shellEcho(char *receive)
{
printf("\nExecuting echo as Child...\n");
pid_t pid;
pid=fork();
if (pid<0)
{
fprintf(stderr, "\nFork Failed\n");
return 1;
}
else if (pid==0)
{
execlp(receive, "echo", NULL);
}
else
{
wait(NULL);
printf("Child Complete");
}
return 0;
}