1
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <time.h>
#include <string.h>
void getCommand(char* cmd, char** arg_list)
{
pid_t child_pid;
child_pid = fork();
if (child_pid == 0)
{
execvp (cmd, arg_list);
fprintf(stderr, "error");
abort();
}
}
int main(void)
{
printf("Type the command\n");
char *arg_list[] = {NULL, NULL, NULL, NULL, NULL, NULL, NULL};
char cmd[20];
char delim[2] = " ";
char *token;
scanf("%[^\n]", cmd);
token = strtok(cmd, delim);
while (token != NULL)
{
arg_list[0] = token;
token = strtok(NULL, cmd);
}
getCommand (arg_list[0], arg_list);
return 0;
}
我想在这里实现的是我想读取用户输入,它应该是一个Linux命令,然后执行命令。看来我不能用strtok来分割我的字符串。我有点失落,谢谢你的帮助。用户输入到C命令的linux命令
稻田得到你的主要问题。你应该养成检查errno的习惯。打印“错误”不会告诉你很多。你也可能想让父母对孩子“等待”。 – Duck 2013-05-13 02:17:23