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

59
sh.c
View file

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