summaryrefslogblamecommitdiffstats
path: root/02_exercise/shell.c
blob: c960680af23ab973cc5e684f6ee6f734abe1d2f9 (plain) (tree)
1
2
3
4
5
6
7
8
9
                  


                   
                     
                   

                  
                    
 
                    
 
                                                                    
 

                                                                         
 
                                  

                      
                          

                          
 
                       
                                                         
                                                         
                     
         
 

                                                     

                                
                                                      
 









                                                       
                


                                                               
         

              
                           




                                            



                                
                                                                     


                       


                                                                

                                                            
       
 






                                          


                                              

                                


                                
         
     




                                        
 
 







                                                   


     











                                                                          
                                                            










                                     


                                                     





                             

                               
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <sys/wait.h>
#include <unistd.h>

#include "array.h"
#include <stdbool.h>

void print_prompt();

int parse_line(char const *line, char ***parts, size_t *part_count);

// returns the return code of the executed program
int exec_command(const char *path, char *const argv[], unsigned timeout);

int main(int argc, char* argv[]) {
    bool done = false;
    while (!done) {
        char *line = NULL;
        size_t cap = 0;
        size_t length = 0;

        print_prompt();
        if ((length = getline(&line, &cap, stdin)) < 0) {
            fprintf(stderr, "Failed to read from STDIN");
            exit(-1);
        }

        line[length - 1] = '\0'; // cut the line feed

        char **arguments = NULL;
        size_t argument_count;
        parse_line(line, &arguments, &argument_count);

        if (strcmp(arguments[0], "cd") == 0) {
            if (arrayLen(arguments) != 3) {
                fprintf(stderr, "usage: cd <path>");
                goto clean;
            }
            int ret = chdir(arguments[1]);
            if (ret)
                printf("[%i] ", ret);
        } else if (strcmp(arguments[0], "exit") == 0) {
            done = true;
        } else {
            int ret = exec_command(arguments[0], arguments, 0);
            if (ret)
                printf("[%i] ", ret);
        }

        clean:
        free((void *)line);
        while (arrayLen(arguments) > 0) {
            char *tmp = arrayPop(arguments);
            if (tmp)
                arrayRelease(tmp);
        }
        arrayRelease(arguments);
    }
}

int parse_line(char const *line, char ***parts, size_t *part_count) {
    char *part;
    char **local_parts;

    arrayInit(part);
    arrayInit(local_parts);
/*    if (arrayInit(part) != 0 || arrayInit(local_parts) != 0) {
        fprintf(stderr, "Failed to prepare new part array");
        exit(-1);
    }*/

    char c;
    int i = 0;
    while (true) {
        c = line[i++];
        if (c == ' ' || c == '\0') {
            arrayPush(part) = '\0';
            arrayPush(local_parts) = part;
            if (c == '\0') {
                arrayPush(local_parts) = NULL;
                break;
            } else {
                arrayInit(part);
            }
        } else {
            arrayPush(part) = c;
        }
    }

    *part_count = arrayLen(local_parts);
    *parts = local_parts;

    return 0;
}

void print_prompt() {
    size_t length  = 1024;
    char   *buffer = malloc(length * sizeof(char));
    getcwd(buffer, length);
    if (buffer == NULL) {
        printf(".> ");
    } else {
        printf("%s > ", buffer);
    }
}

int exec_command(const char *path, char *const argv[], unsigned timeout) {
    int  pid;
    int  pipefd[2];
    int  status;
    char buf[512];

    pipe(pipefd);
    if ((pid = fork()) == 0) {
        close(pipefd[0]);
        dup2(pipefd[1], 1); // includes close(1);
        close(pipefd[1]);
        execvp(path, argv);
        fprintf(stderr, "could not execute \"%s\"\n", path);
        exit(-1);
    }

    if (pid < 0) {
        fprintf(stderr, "no fork\n");
        exit(-2);
    }

    close(pipefd[1]);

    size_t length = 0;
    while ((length = read(pipefd[0], buf, 10)) > 0) {
        buf[length] = '\0';
        printf("%s", buf);
    }

    waitpid(pid, &status, 0);

    close(pipefd[0]);

    return WEXITSTATUS(status);
}