【发布时间】:2011-07-05 22:53:22
【问题描述】:
我正在编写自己的 shell,但 strcat() 意外覆盖字符串似乎存在某种问题。
问题是在尝试执行本地目录中的文件时。它应该搜索的路径中的第二个值是“。”第一个是/bin,但是当将命令附加到/bin 以获得提供给execlp() 的绝对路径时,句点也被命令覆盖。我知道MYPATH 很奇怪,用#s 分隔很奇怪,但这与问题无关。
如果你想运行它来看看我在说什么,我会在其中添加一些有用的 printf() 语句。
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <readline/readline.h>
#include <readline/history.h>
#include <sys/stat.h>
void execute(char**, int, char**, int);
int main (){
char *command, *mypath, *buffer, *arglist[1024], *pathlist[1024], **ap;
buffer = malloc(1024);
int loop = 1;
while (loop == 1){
int argnum = 0, pathnum = 0;
mypath = malloc(1024);
if(getenv("MYPATH") == NULL)
strcpy(mypath, "/bin#.");
else
strcpy(mypath, getenv("MYPATH"));
printf("myshell$ ");
command = readline("");
if(strcmp(command, "exit") == 0 || strcmp(command, "quit") == 0)
return 0;
if(strcmp(command, "") == 0)
continue;
/*Tokenizes Command*/
for(ap = arglist; (*ap = strsep(&command, " \t")) != NULL;){
argnum++;
if(**ap != '\0')
if(++ap >= &arglist[1024])
break;
}
/*Tokenizes Path*/
for(ap = pathlist; (*ap = strsep(&mypath, "#")) != NULL;){
pathnum++;
if(**ap != '\0')
if(++ap >= &pathlist[1024])
break;
}
execute(pathlist, pathnum, arglist, argnum);
}
return 0;
}
void execute(char *pathlist[], int pathnum, char *arglist[], int argnum){
pid_t pid;
int i;
int found = 0;
struct stat buf;
for(i = 0; i < pathnum; i++){
if (found == 1)
break;
printf("pathlist[0]: %s\n", pathlist[0]);
printf("pathlist[1]: %s\n", pathlist[1]);
strcat(pathlist[i], "/");
strcat(pathlist[i], arglist[0]);
printf("Pathlist[0] after strcat: %s\n", pathlist[0]);
printf("Pathlist[1] after strcat: %s\n", pathlist[1]);
if(stat(pathlist[i], &buf) == 0){
found = 1;
pid = fork();
if(pid == -1)
printf("Error: Fork Failed\n");
else if(pid == 0){
if(argnum == 0)
execlp(pathlist[i], arglist[0], (char *) NULL);
else if(argnum == 1)
execlp(pathlist[i], arglist[0], arglist[1], (char *) NULL);
else if(argnum == 2)
execlp(pathlist[i], arglist[0], arglist[1], arglist[2], (char *) NULL);
else if(argnum == 3)
execlp(pathlist[i], arglist[0], arglist[1], arglist[2], (char *) NULL);
}
else if(strcmp(arglist[argnum-1], "&") != 0){
wait(NULL);
}
}
else if(stat(pathlist[i], &buf) == -1 && i == pathnum-1 && found == 0)
printf("Error: Command '%s' not found.\n", arglist[0]);
}
}
【问题讨论】:
-
与您的问题无关,但您确实应该查找
execvp函数。为每个可能的参数数量编写不同的execlp调用虽然可能,但非常丑陋和低效。
标签: c operating-system posix