Support $?, add -l for _kill

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

35
sh.c
View file

@ -23,12 +23,12 @@ int _kill(int argc, char * argv[]);
mode_t _umask = 022;
char * prompt_str;
#define PROMPT prompt_str
int stat_loc; /* Used by waitpid; global because used in _kill() */
int main() {
char** argv;
char* user_input;
pid_t child_pid;
int stat_loc; /* ??? (Needed for waitpid) */
prompt_str = getenv("PS1");
prompt_str = prompt_str == NULL ? "$ " : prompt_str;
@ -118,7 +118,13 @@ char** create_argv(char* command) {
for(temp = strtok(command, ARG_SEP); temp != NULL;
temp = strtok(NULL, ARG_SEP)) {
if(strcmp(temp, "$?") == 0) {
argv[i] = calloc(4, sizeof(**argv));
sprintf(argv[i], "%d", WEXITSTATUS(stat_loc));
}
else {
argv[i] = temp;
}
i++;
}
@ -218,8 +224,31 @@ int _kill(int argc, char * argv[]) {
}
if(write_signame) {
if(argc > 2) {
/* TODO: Figure out how to get exit status from $? */
if(argc > 2 && atoi(argv[3]) == WEXITSTATUS(stat_loc) &&
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 {
printf("SIGHUP\n");