Some minor tweaks

This commit is contained in:
Gitea 2020-12-13 14:15:04 -06:00
parent 6731edbf57
commit ae31e82b64
1 changed files with 16 additions and 27 deletions

43
sh.c
View File

@ -1,3 +1,5 @@
#define _POSIX_C_SOURCE 200809L
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@ -6,19 +8,6 @@
#include <signal.h>
#include <libgen.h>
#include <errno.h>
/*
Gods damn it, of course we have to have special shit
just for the fucking GNU Project. Fuckers can't let
anything be done to standard unless they fucking want
to or you beg them to let you, can they?
-Kat
*/
#ifdef __GLIBC__
#define __USE_POSIX
#define __USE_POSIX2
#endif
#include <limits.h>
#define ARG_SEP " " /* Argument separator for commands */
@ -30,26 +19,26 @@ char* prompt();
int cd(char* path);
int main() {
char** argv; /* The command with arguments */
char** argv;
char* user_input;
pid_t child_pid;
int stat_loc; /* ??? (Needed for waitpid) */
signal(SIGINT, SIG_IGN); /* Tell vsh to ignore SIGINT (^C) */
signal(SIGINT, SIG_IGN);
while(1) {
user_input = prompt();
argv = create_argv(user_input);
if(strcmp(argv[0], "cd") == 0) {
if(cd(&user_input[3]) < 0) {
perror(argv[1]);
if(cd(argv[1]) < 0) {
perror(argv[1]);
}
/* causes the fork to get skipped */
continue;
}
else if(strcmp(argv[0], "exit") == 0 ||
strcmp(argv[0], "quit") == 0) {
strcmp(argv[0], "quit") == 0) {
exit(0);
}
@ -57,25 +46,25 @@ int main() {
if(child_pid < 0) {
/* Could not start child process. Check why and error out. */
if(errno == EAGAIN) {
perror("Could not execute command: insufficient resources");
exit(1);
perror("Could not execute command: insufficient resources");
exit(1);
}
else if(errno == ENOMEM) {
/* Is this out of RAM or out of storage? */
perror("Could not execute command: insufficient storage space");
exit(1);
/* Is this out of RAM or out of storage? */
perror("Could not execute command: insufficient storage space");
exit(1);
}
else {
perror("Could not execute command: unknown failure");
exit(1);
perror("Could not execute command: unknown failure");
exit(1);
}
}
else if(child_pid == 0) {
/* We are the child, so allow ^C */
signal(SIGINT, SIG_DFL);
if(execvp(argv[0], argv) < 0) {
perror(argv[0]);
exit(1);
perror(argv[0]);
exit(1);
}
}
else {