I'm currently programming a shell in C and I'm running into a few issues. When I try to compare my command to an "exit" for example, it just runs write over it and acts like they don't equal according to gdb. I end with a segfault. If anyone could help me figure out what's wrong, I'd greatly appreciate it. This is my first shell ever btw!
#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;
}
Most of it is still bare bones so bear with me.