Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <sys/wait.h>
- #include <string.h>
- typedef struct {
- char *prog;
- char **args;
- } COMMAND;
- // parseInput splits command line input based on spaces and calls
- // splitter to divide input that didn't have spaces
- COMMAND parseInput(char* input);
- char** splitter(char *token, int *count);
- // finds position of left carrot within the arguments array
- // returns -1 if not found
- int whereLeftCarrot(char **args);
- // finds position of right carrot within the arguments array
- // returns -1 if not found
- int whereRightCarrot(char **args);
- // removes special symbol from comm.args by taking in the pos
- // of the special char and shifting from that spot over
- void shiftArgs(COMMAND *comm, int pos);
- int main(int argc, char *argv[])
- {
- while (1) {
- char input[512]; // store input line
- char inputcpy[512];
- printf("$sshell "); // shell prompt
- fgets(input, 512, stdin); // read line into string
- input[strlen(input) - 1] = '\0';
- strcpy(inputcpy, input);
- COMMAND comm = parseInput(input);
- //printf("INPUT: %s\n", inputcpy);
- //check if input is empty or whitespace
- if (comm.prog == NULL) {
- continue;
- }
- // check for exit command
- if (strcmp(comm.prog, "exit") == 0) {
- printf("Bye...\n");
- exit(0);
- } else
- if (strcmp(comm.prog, "pwd") == 0) {
- char cwd[512];
- getcwd(cwd, sizeof(cwd));
- printf("%s\n", cwd);
- } else
- if (strcmp(comm.prog, "cd") == 0) {
- if (chdir(comm.args[1]) == -1) {
- fprintf(stderr, "Error: no such directory.\n");
- }
- }
- else {
- // create new process
- pid_t PID = fork();
- // if child process
- if (PID == 0) {
- int status;
- // printf("Exec call.\n");
- status = execvp(comm.prog, comm.args);
- printf("STATUS: %d", status);
- // if exec fails
- if (status != 0) {
- fprintf(stderr, "Error: command not found.\n");
- exit(42);
- // else exit the process
- }
- else {
- exit(0);
- }
- }
- else {
- int status;
- wait(&status);
- fprintf(stderr, "+ completed '%s' [%d]\n", inputcpy, WEXITSTATUS(status));
- }
- }
- }
- return EXIT_SUCCESS;
- }
- COMMAND parseInput(char *input) {
- COMMAND comm;
- int i = 1;
- char *token;
- token = strtok(input, " \n");
- // printf("Got a token: %s\n", token);
- // is empty or whitespace
- if (token == NULL) {
- comm.prog = malloc(sizeof(NULL));
- comm.prog = NULL;
- return comm;
- }
- comm.prog = (char*)malloc(strlen(token) + 1);
- strcpy(comm.prog, token);
- comm.args = (char**)malloc(sizeof(char*) * 512);
- comm.args[0] = (char*)malloc(strlen(token) + 1);
- strcpy(comm.args[0], token);
- while (1) {
- token = strtok(NULL, " \n");
- if (token == NULL) {
- break;
- }
- if (strpbrk(token, "<>|") != NULL) {
- int count = 0;
- char **split = splitter(token, &count);
- int j;
- for (j = 0; j < count; j++) {
- comm.args[i + j] = malloc(sizeof(split[j] + 1));
- strcpy(comm.args[i + j], split[j]);
- }
- i = i + j;
- }
- else {
- comm.args[i] = (char*)malloc(strlen(token) + 1);
- strcpy(comm.args[i], token);
- i++;
- }
- }
- comm.args[i + 1] = (char*)malloc(sizeof((char*) NULL));
- comm.args[i + 1] = (char*) NULL;
- /*
- for(int g = 0; g < i; g++) {
- printf("ARGS: %s\n", comm.args[g]);
- }
- */
- return comm;
- }
- char** splitter(char *token, int *count) {
- char **tokens = malloc(sizeof(char*) * 512);
- char temp[512];
- int i = 0;
- int j = 0;
- while (token[i] != '\0') {
- if (token[i] == '<' || token[i] == '>' || token[i] == '|') {
- temp[j] = '\0';
- tokens[*count] = (char*)malloc(strlen(temp) + 1);
- strcpy(tokens[*count], temp);
- ++*count;
- if (token[i] == '<') {
- tokens[*count] = (char*)malloc(strlen("<") + 1);
- strcpy(tokens[*count], "<");
- }
- else if (token[i] == '>') {
- tokens[*count] = (char*)malloc(strlen(">") + 1);
- strcpy(tokens[*count], ">");
- }
- else if (token[i] == '|') {
- tokens[*count] = (char*)malloc(strlen("|") + 1);
- strcpy(tokens[*count], "|");
- }
- ++*count;
- j = 0;
- i++;
- } else {
- temp[j] = token[i];
- i++;
- j++;
- }
- }
- tokens[*count] = malloc(strlen(temp) + 1);
- strcpy(tokens[*count], temp);
- ++*count;
- // printf("# of tokens: %d\n", *count);
- /*
- for(int g = 0; g < *count; g++) {
- printf("Token Stored: %s\n", tokens[g]);
- }
- */
- return tokens;
- }
- int whereLeftCarrot(char **args) {
- int pos = 0;
- while(strcmp(args[pos], "NULL") != 0) {
- if (strcmp(args[pos], "<") == 0) {
- return pos;
- }
- }
- return -1;
- }
- int whereRightCarrot(char **args) {
- int pos = 0;
- while(strcmp(args[pos], "NULL") != 0) {
- if (strcmp(args[pos], ">") == 0) {
- return pos;
- }
- }
- return -1;
- }
- void shiftArgs(COMMAND *comm, int pos) {
- while ((*comm).args[pos + 1] != NULL) {
- (*comm).args[pos] = realloc((*comm).args[pos], sizeof((*comm).args[pos + 1]));
- strcpy((*comm).args[pos], (*comm).args[pos + 1]);
- pos++;
- }
- (*comm).args[pos] = malloc(sizeof( NULL ));
- (*comm).args[pos] = NULL;
- free((*comm).args[pos + 1]);
- return;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement