【发布时间】:2012-09-24 03:45:46
【问题描述】:
我目前正在用 C 编写一个 shell,但遇到了一些问题。例如,当我尝试将我的命令与“退出”进行比较时,它只是在它上面运行 write 并且根据 gdb 表现得它们不相等。我以段错误结束。如果有人可以帮助我找出问题所在,我将不胜感激。顺便说一句,这是我的第一个 shell!
#include <stdio.h>
#include <string.h>
#include <strings.h>
#include <limits.h>
#include <unistd.h>
#include <stdlib.h>
#include <pwd.h>
#include <dirent.h>e
#include <sys/types.h>
#include <sys/wait.h>
#include <signal.h>
#include "sh.h"
int sh( int argc, char **argv, char **envp ){
char *prompt = calloc(PROMPTMAX, sizeof(char));
char *commandline = calloc(MAX_CANON, sizeof(char));
char *command, *arg, *commandpath, *p, *pwd, *owd;
char **args = calloc(MAXARGS, sizeof(char*));
int uid, i, status, argsct, go = 1;
struct passwd *password_entry;
char *homedir;
struct pathelement *pathlist;
uid = getuid();
password_entry = getpwuid(uid);
homedir = password_entry->pw_dir;
if ( (pwd = getcwd(NULL, PATH_MAX+1)) == NULL ){
perror("getcwd");
exit(2);
}
owd = calloc(strlen(pwd) + 1, sizeof(char));
memcpy(owd, pwd, strlen(pwd));
prompt[0] = ' '; prompt[1] = '\0';
pathlist = get_path();
prompt = "[cwd]>";
while ( go ){
printf(prompt);
commandline = fgets(commandline, 100, stdin);
command = strtok(commandline, " ");
printf(command);
if (strcmp(command, "exit")==0){
exit(0);
}
else if (strcmp(command, "which")==0){
// which();
}
else if (strcmp(command, "where")==0){
// where();
}
else if (strcmp(command, "cd")==0){
chdir(argv[0]);
}
else if (strcmp(command, "pwd")==0){
getcwd(pwd, PATH_MAX);
}
else if (strcmp(command, "list")==0){
if (argc == 1){
}
else if (argc > 1){
}
}
else if (strcmp(command, "pid")==0){
getpid();
}
else if (strcmp(command, "kill")==0){
}
else if (strcmp(command, "prompt")==0){
prompt = "argv[0] + prompt";
}
else if (strcmp(command, "printenv")==0){
}
else if (strcmp(command, "alias")==0){
}
else if (strcmp(command, "history")==0){
}
else if (strcmp(command, "setenv")==0){
}
else{
fprintf(stderr, "%s: Command not found.\n", args[0]);
}
}
return 0;
}
其中大部分仍然是裸露的骨头,所以请耐心等待。
【问题讨论】:
-
您可能想尝试代码审查堆栈交换。 codereview.stackexchange.com
-
@OmnipotentEntity: 否 -- CodeReview 适用于有效的代码。
-
我首先创建一个“映射”,从命令名称到执行这些功能的函数,而不是巨大的
if/then/else阶梯。 -
去过那里。我可以提个建议吗?使用调度表而不是所有那些
else ifs,坦率地说这是不可扩展的。创建一个 char 数组的结构来保存内置名称,以及一个指向内置函数的函数指针(所有函数都应该具有相同的原型)。创建这些结构的数组,按内置名称手动排序并使用bsearch为每个内置找到正确的函数。