Fix for GNU project, replace Single Line Comments

This commit is contained in:
Gitea 2020-12-01 20:33:33 -06:00
parent d392abebc7
commit 442e001dc6
1 changed files with 41 additions and 18 deletions

59
sh.c
View File

@ -3,11 +3,25 @@
#include <string.h>
#include <unistd.h>
#include <sys/wait.h>
#include <linux/limits.h>
#include <signal.h>
#include <libgen.h>
#include <errno.h>
#define ARG_SEP " " //Argument separator for commands
/*
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 */
#define PROMPT " $ "
char** create_argv(char* command);
@ -16,12 +30,12 @@ char* prompt();
int cd(char* path);
int main() {
char** argv; //The command with arguments
char** argv; /* The command with arguments */
char* user_input;
pid_t child_pid;
int stat_loc; //??? (Needed for waitpid)
int stat_loc; /* ??? (Needed for waitpid) */
signal(SIGINT, SIG_IGN); //Tell vsh to ignore SIGINT (^C)
signal(SIGINT, SIG_IGN); /* Tell vsh to ignore SIGINT (^C) */
while(1) {
user_input = prompt();
@ -31,7 +45,7 @@ int main() {
if(cd(&user_input[3]) < 0) {
perror(argv[1]);
}
//causes the fork to get skipped
/* causes the fork to get skipped */
continue;
}
else if(strcmp(argv[0], "exit") == 0 ||
@ -41,12 +55,23 @@ int main() {
child_pid = fork();
if(child_pid < 0) {
//If the child couldn't run, assume no memory
perror("Out of memory");
exit(1);
/* Could not start child process. Check why and error out. */
if(errno == EAGAIN) {
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);
}
else {
perror("Could not execute command: unknown failure");
exit(1);
}
}
else if(child_pid == 0) {
//If we are the child, allow ^C
/* We are the child, so allow ^C */
signal(SIGINT, SIG_DFL);
if(execvp(argv[0], argv) < 0) {
perror(argv[0]);
@ -54,6 +79,7 @@ int main() {
}
}
else {
/* Wait for the child to finish */
waitpid(child_pid, &stat_loc, WUNTRACED);
}
@ -71,8 +97,7 @@ char** create_argv(char* command) {
char** argv = calloc(argc, sizeof(*argv));
if(argv == NULL) {
//If couldn't allocate argv, assume no memory
perror("Out of memory");
perror("Unable to parse command: could not allocate memory");
exit(1);
}
@ -82,7 +107,7 @@ char** create_argv(char* command) {
i++;
}
//Set last item in argv to NULL, since execvp requires it
/* Set last item in argv to NULL, since execvp requires it */
argv[i] = NULL;
return argv;
}
@ -94,15 +119,13 @@ char* prompt() {
char prompt[PATH_MAX + 5] = "";
strcat(prompt, bname);
strcat(prompt, PROMPT);
char* input = calloc(512, sizeof(*input));
char* input = calloc(LINE_MAX, sizeof(*input));
if(input == NULL) {
//If input allocate failed, assume no memory
perror("Out of memory");
perror("Unable to read command: could not allocate memory");
exit(1);
}
printf("%s", prompt);
fgets(input, 512, stdin);
//Is 512 enough? What about really long commands?
fgets(input, LINE_MAX, stdin);
input[strcspn(input, "\r\n")] = '\0';
return input;
}