2020-12-13 20:15:04 +00:00
|
|
|
#define _POSIX_C_SOURCE 200809L
|
|
|
|
|
2020-02-12 05:30:50 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <libgen.h>
|
2020-12-02 02:33:33 +00:00
|
|
|
#include <errno.h>
|
|
|
|
#include <limits.h>
|
|
|
|
|
|
|
|
#define ARG_SEP " " /* Argument separator for commands */
|
2020-02-12 05:30:50 +00:00
|
|
|
#define PROMPT " $ "
|
|
|
|
|
|
|
|
char** create_argv(char* command);
|
|
|
|
int get_argc(char* command);
|
|
|
|
char* prompt();
|
|
|
|
int cd(char* path);
|
|
|
|
|
|
|
|
int main() {
|
2020-12-13 20:15:04 +00:00
|
|
|
char** argv;
|
2020-02-12 05:30:50 +00:00
|
|
|
char* user_input;
|
|
|
|
pid_t child_pid;
|
2020-12-02 02:33:33 +00:00
|
|
|
int stat_loc; /* ??? (Needed for waitpid) */
|
2020-02-12 05:30:50 +00:00
|
|
|
|
2020-12-13 20:15:04 +00:00
|
|
|
signal(SIGINT, SIG_IGN);
|
2020-02-12 05:30:50 +00:00
|
|
|
|
|
|
|
while(1) {
|
|
|
|
user_input = prompt();
|
|
|
|
argv = create_argv(user_input);
|
|
|
|
|
|
|
|
if(strcmp(argv[0], "cd") == 0) {
|
2020-12-13 20:15:04 +00:00
|
|
|
if(cd(argv[1]) < 0) {
|
|
|
|
perror(argv[1]);
|
2020-02-12 05:30:50 +00:00
|
|
|
}
|
2020-12-02 02:33:33 +00:00
|
|
|
/* causes the fork to get skipped */
|
2020-02-12 05:30:50 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else if(strcmp(argv[0], "exit") == 0 ||
|
2020-12-13 20:15:04 +00:00
|
|
|
strcmp(argv[0], "quit") == 0) {
|
2020-02-12 05:30:50 +00:00
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
child_pid = fork();
|
|
|
|
if(child_pid < 0) {
|
2020-12-02 02:33:33 +00:00
|
|
|
/* Could not start child process. Check why and error out. */
|
|
|
|
if(errno == EAGAIN) {
|
2020-12-13 20:15:04 +00:00
|
|
|
perror("Could not execute command: insufficient resources");
|
|
|
|
exit(1);
|
2020-12-02 02:33:33 +00:00
|
|
|
}
|
|
|
|
else if(errno == ENOMEM) {
|
2020-12-13 20:15:04 +00:00
|
|
|
/* Is this out of RAM or out of storage? */
|
|
|
|
perror("Could not execute command: insufficient storage space");
|
|
|
|
exit(1);
|
2020-12-02 02:33:33 +00:00
|
|
|
}
|
|
|
|
else {
|
2020-12-13 20:15:04 +00:00
|
|
|
perror("Could not execute command: unknown failure");
|
|
|
|
exit(1);
|
2020-12-02 02:33:33 +00:00
|
|
|
}
|
2020-02-12 05:30:50 +00:00
|
|
|
}
|
|
|
|
else if(child_pid == 0) {
|
2020-12-02 02:33:33 +00:00
|
|
|
/* We are the child, so allow ^C */
|
2020-02-12 05:30:50 +00:00
|
|
|
signal(SIGINT, SIG_DFL);
|
|
|
|
if(execvp(argv[0], argv) < 0) {
|
2020-12-13 20:15:04 +00:00
|
|
|
perror(argv[0]);
|
|
|
|
exit(1);
|
2020-02-12 05:30:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2020-12-02 02:33:33 +00:00
|
|
|
/* Wait for the child to finish */
|
2020-02-12 05:30:50 +00:00
|
|
|
waitpid(child_pid, &stat_loc, WUNTRACED);
|
|
|
|
}
|
|
|
|
|
|
|
|
free(user_input);
|
|
|
|
free(argv);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
char** create_argv(char* command) {
|
|
|
|
int argc = get_argc(command) + 1;
|
|
|
|
char* temp;
|
|
|
|
int i = 0;
|
|
|
|
|
|
|
|
char** argv = calloc(argc, sizeof(*argv));
|
|
|
|
if(argv == NULL) {
|
2020-12-02 02:33:33 +00:00
|
|
|
perror("Unable to parse command: could not allocate memory");
|
2020-02-12 05:30:50 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
for(temp = strtok(command, ARG_SEP); temp != NULL;
|
|
|
|
temp = strtok(NULL, ARG_SEP)) {
|
|
|
|
argv[i] = temp;
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
2020-12-02 02:33:33 +00:00
|
|
|
/* Set last item in argv to NULL, since execvp requires it */
|
2020-02-12 05:30:50 +00:00
|
|
|
argv[i] = NULL;
|
|
|
|
return argv;
|
|
|
|
}
|
|
|
|
|
|
|
|
char* prompt() {
|
|
|
|
char cwd[PATH_MAX];
|
|
|
|
getcwd(cwd, sizeof(cwd));
|
|
|
|
char* bname = basename(cwd);
|
|
|
|
char prompt[PATH_MAX + 5] = "";
|
|
|
|
strcat(prompt, bname);
|
|
|
|
strcat(prompt, PROMPT);
|
2020-12-02 02:33:33 +00:00
|
|
|
char* input = calloc(LINE_MAX, sizeof(*input));
|
2020-02-12 05:30:50 +00:00
|
|
|
if(input == NULL) {
|
2020-12-02 02:33:33 +00:00
|
|
|
perror("Unable to read command: could not allocate memory");
|
2020-02-12 05:30:50 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
printf("%s", prompt);
|
2020-12-02 02:33:33 +00:00
|
|
|
fgets(input, LINE_MAX, stdin);
|
2020-02-12 05:30:50 +00:00
|
|
|
input[strcspn(input, "\r\n")] = '\0';
|
|
|
|
return input;
|
|
|
|
}
|
|
|
|
|
|
|
|
int get_argc(char* command) {
|
|
|
|
int argc = 0, word = 0;
|
|
|
|
char* command_iter = command;
|
|
|
|
|
|
|
|
do {
|
|
|
|
switch(*command_iter) {
|
|
|
|
case '\0':
|
|
|
|
case ' ':
|
|
|
|
case '\t': if(word) { word = 0; argc++; }
|
|
|
|
default: word = 1;
|
|
|
|
}
|
|
|
|
} while(*(command_iter++));
|
|
|
|
|
|
|
|
return argc;
|
|
|
|
}
|
|
|
|
|
|
|
|
int cd(char* path) {
|
|
|
|
return chdir(path);
|
|
|
|
}
|