Support $?, add -l for _kill

This commit is contained in:
Gitea 2020-12-24 17:31:19 -06:00
parent cb57a99767
commit b3e89fbf84

37
sh.c
View file

@ -23,12 +23,12 @@ int _kill(int argc, char * argv[]);
mode_t _umask = 022; mode_t _umask = 022;
char * prompt_str; char * prompt_str;
#define PROMPT prompt_str #define PROMPT prompt_str
int stat_loc; /* Used by waitpid; global because used in _kill() */
int main() { int main() {
char** argv; char** argv;
char* user_input; char* user_input;
pid_t child_pid; pid_t child_pid;
int stat_loc; /* ??? (Needed for waitpid) */
prompt_str = getenv("PS1"); prompt_str = getenv("PS1");
prompt_str = prompt_str == NULL ? "$ " : prompt_str; prompt_str = prompt_str == NULL ? "$ " : prompt_str;
@ -118,7 +118,13 @@ char** create_argv(char* command) {
for(temp = strtok(command, ARG_SEP); temp != NULL; for(temp = strtok(command, ARG_SEP); temp != NULL;
temp = strtok(NULL, ARG_SEP)) { temp = strtok(NULL, ARG_SEP)) {
argv[i] = temp; if(strcmp(temp, "$?") == 0) {
argv[i] = calloc(4, sizeof(**argv));
sprintf(argv[i], "%d", WEXITSTATUS(stat_loc));
}
else {
argv[i] = temp;
}
i++; i++;
} }
@ -218,8 +224,31 @@ int _kill(int argc, char * argv[]) {
} }
if(write_signame) { if(write_signame) {
if(argc > 2) { if(argc > 2 && atoi(argv[3]) == WEXITSTATUS(stat_loc) &&
/* TODO: Figure out how to get exit status from $? */ WIFSIGNALED(stat_loc)) {
switch(WTERMSIG(stat_loc)) {
case 0: printf("0\n"); break;
case 1: printf("SIGHUP\n"); break;
case 2: printf("SIGINT\n"); break;
case 3: printf("SIGQUIT\n"); break;
case 6: printf("SIGABRT\n"); break;
case 9: printf("SIGKILL\n"); break;
case 14: printf("SIGALRM\n"); break;
case 15: printf("SIGTERM\n"); break;
}
return 0;
}
else if(argc > 2) {
switch(atoi(argv[3])) {
case 0: printf("0\n"); break;
case 1: printf("HUP\n"); break;
case 2: printf("INT\n"); break;
case 3: printf("QUIT\n"); break;
case 6: printf("ABRT\n"); break;
case 9: printf("KILL\n"); break;
case 14: printf("ALRM\n"); break;
case 15: printf("TERM\n"); break;
}
} }
else { else {
printf("SIGHUP\n"); printf("SIGHUP\n");