1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
|
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#define count 20
#define start 5
typedef enum boolean { false, true } bool;
char** parse(char* line, int length) {
//brute force!
int sizeofarray = 1;
char **array = (char**) malloc(sizeof(char*) * sizeofarray * start);
char *token = (char*) strtok(line, " ");
array[0] = token;
int i = 1;
while (token != NULL) {
token = (char*) strtok(NULL, " \n");
array[i] = token;
i++;
if (i >= start*sizeofarray) {
sizeofarray++;
array = realloc(array, sizeof(char*) * sizeofarray * start);
}
}
return array;
}
int main(int argc, char **argv) {
bool quit = false;
size_t size = sizeof(char) * count;
char *command = (char*) malloc(size);
while (!quit) {
printf("\nVincent's Shell> ");
int size = getline(&command, &size, stdin);
if (!strcmp(command, "quit\n") || !strcmp(command, "exit\n")) {
quit = true;
} else if (size > 0) {
char** output = parse(command, size);
pid_t pid = fork();
if (pid < 0) {
printf("Could not fork!");
} else if (pid == 0) {
//child
execvp(*output, output);
//printf("That's not good! I haven't transformed!\n");
printf("%s\n", (char*) strerror(errno));
exit(1);
} else {
//parent
int status = 0;
pid = wait(&status);
printf("Process %d returned with status %d.", pid, status);
}
}
}
return 0;
}
|