我使用管道()forks()exec()dup()模拟shell。我已经在 stackoverflow 上看到了一些帖子来指导前进。但是我的 prog 似乎与其他人在这里遇到的类似问题。
我使用的 LinkedList 结构包含:char* cmd 和 char** args(例如 cmd='ls' args = '-l -d')
以下是一些输出结果: ls -l ls -l -a(我想要多少个参数) ls -l | 种类
ls -l | wc -w (runs but spits out wrong value)
ls | wc -w (runs, but spits out the wrong value)
ls (alone - no args, spits out A NULL argv[0] was passed through an exec system call.)
ls -l | sort | wc -w (causes system to hang)
它现在至少假装它正在接受多个参数但无效的结果或系统挂起。
void runCommand(Node* head)
{
int old_fd[2], new_fd[2];
int isValid, cpid, pid;
//Loop through all commands
for(int cmd = 0; cmd < cmdCount; cmd++)
{
//if(curr cmd has next cmd)
if(cmd+1 < cmdCount)
{
//Create pipe new_fd
if( pipe(new_fd) == -1) //pipe error
{
perror("Pipe Error.");
exit(-1);
}
}
//Parent
if( (pid=fork()) != 0 ) //parent
{
//Wait for child process
//wait(0); //moved below
//Curr cmd has next command
if(cmd+1 < cmdCount)
{
old_fd[0] = new_fd[0];
old_fd[1] = new_fd[1];
}
//Curr cmd has previous command
if(cmd-1 > -1)
{
close(old_fd[0]);
close(old_fd[1]);
}
}
//Child process
else //if fork() == 0
{
//Curr cmd has previous command
if(cmd-1 > -1)
{
dup2(old_fd[0], 0); // setting up old_pipe to input into the child
close(old_fd[0]);
close(old_fd[1]);
}
//Curr cmd has next command
if(cmd+1 < cmdCount)
{
close(new_fd[0]); // setting up new_pipe to get output from child
dup2(new_fd[1], 1);
close(new_fd[1]);
}
printf("Running command '%s': \n",getCmd(cmd,head));
printf("Arguments: "); printArgs(cmd,head);
//Below works for 1 cmd 1+ args, but not 1 cmd 0 arg or mult pipes
isValid = execvp(getCmd(cmd,head), getArgs(cmd,head));
if(isValid == -1)
printf("%s: Command not found.\n", getCmd(cmd,head));
}
wait();
}
}
请注意这个线程:Another_Stack_Overflow_Example我在那里使用了示例并将我的函数替换为他们的“工作”示例。他们的工作示例似乎适用于大多数情况,除了一个没有像“ls”这样的参数的命令,它根本什么都不做。并要求输入
这是请求的 getCmd() 和 getArgs() 函数,只需调用“getNode()”,它返回一个 Node* 结构(包含 char* 和一个 char** 和两个整数)getCmd 提取 char* cmd,getArgs 提取char** 参数。
char* getCmd(int index, Node* head)
{
Node* curr = getNode(index,head);
return curr->cmd;
}
char** getArgs(int index, Node* head)
{
Node* curr = getNode(index,head);
return curr->args;
}