Some minor tweaks
This commit is contained in:
parent
6731edbf57
commit
ae31e82b64
43
sh.c
43
sh.c
|
@ -1,3 +1,5 @@
|
||||||
|
#define _POSIX_C_SOURCE 200809L
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
@ -6,19 +8,6 @@
|
||||||
#include <signal.h>
|
#include <signal.h>
|
||||||
#include <libgen.h>
|
#include <libgen.h>
|
||||||
#include <errno.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>
|
#include <limits.h>
|
||||||
|
|
||||||
#define ARG_SEP " " /* Argument separator for commands */
|
#define ARG_SEP " " /* Argument separator for commands */
|
||||||
|
@ -30,26 +19,26 @@ char* prompt();
|
||||||
int cd(char* path);
|
int cd(char* path);
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
char** argv; /* The command with arguments */
|
char** argv;
|
||||||
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);
|
||||||
|
|
||||||
while(1) {
|
while(1) {
|
||||||
user_input = prompt();
|
user_input = prompt();
|
||||||
argv = create_argv(user_input);
|
argv = create_argv(user_input);
|
||||||
|
|
||||||
if(strcmp(argv[0], "cd") == 0) {
|
if(strcmp(argv[0], "cd") == 0) {
|
||||||
if(cd(&user_input[3]) < 0) {
|
if(cd(argv[1]) < 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 ||
|
||||||
strcmp(argv[0], "quit") == 0) {
|
strcmp(argv[0], "quit") == 0) {
|
||||||
exit(0);
|
exit(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -57,25 +46,25 @@ int main() {
|
||||||
if(child_pid < 0) {
|
if(child_pid < 0) {
|
||||||
/* Could not start child process. Check why and error out. */
|
/* Could not start child process. Check why and error out. */
|
||||||
if(errno == EAGAIN) {
|
if(errno == EAGAIN) {
|
||||||
perror("Could not execute command: insufficient resources");
|
perror("Could not execute command: insufficient resources");
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
else if(errno == ENOMEM) {
|
else if(errno == ENOMEM) {
|
||||||
/* Is this out of RAM or out of storage? */
|
/* Is this out of RAM or out of storage? */
|
||||||
perror("Could not execute command: insufficient storage space");
|
perror("Could not execute command: insufficient storage space");
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
perror("Could not execute command: unknown failure");
|
perror("Could not execute command: unknown failure");
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if(child_pid == 0) {
|
else if(child_pid == 0) {
|
||||||
/* We are the child, so 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]);
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
|
Loading…
Reference in New Issue