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;
- COMMAND parseInput(char* input);
- char** splitter(char *token, int *count);
- 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 for exit command
- if (strcmp(comm.prog, "exit") == 0) {
- printf("Bye...\n");
- exit(0);
- }
- if (strcmp(comm.prog, "pwd") == 0) {
- char cwd[512];
- getcwd(cwd, sizeof(cwd));
- printf("%s\n", cwd);
- }
- 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);
- //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", input, WEXITSTATUS(status));
- }
- }
- }
- return EXIT_SUCCESS;
- }
- COMMAND parseInput(char *input) {
- COMMAND comm;
- int i = 1;
- char *token;
- token = strtok(input, " ");
- //printf("Got a token: %s\n", token);
- 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 = 0;
- for (j; 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;
- return comm;
- }
- char** splitter(char *token, int *count) {
- char **tokens = malloc(sizeof(char*) * 512);
- char *temp;
- int i = 0;
- int j = 0;
- while (token[i] != '\0') {
- if (token[i] == '<' || token[i] == '>' || token[i] == '|') {
- 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;
- temp[0] = 0; //will hopefully clear the string
- j = 0;
- i++;
- }
- else {
- temp[j] = (char)malloc(sizeof(char));
- temp[j] = token[i];
- i++;
- j++;
- }
- }
- while (*tokens) {
- printf("%s ", *tokens);
- printf("\n");
- ++tokens;
- }
- return tokens;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement