我目前正在用 C 编写一个 shell,但遇到了一些问题。例如,当我尝试将我的命令与“退出”进行比较时,它只是在它上面运行 write 并且根据 gdb 表现得它们不相等。我以段错误结束。如果有人可以帮助我找出问题所在,我将不胜感激。顺便说一句,这是我的第一个外壳!
#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;
}
其中大部分仍然是裸露的骨头,所以请耐心等待。