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


                   

                   
                     

                      
                   

                  
                  
                    
 
                    
 

                                                                         
 







                                                                




                             
 
                       

                                                            
                     
         



















                                                        


                                            
                                                    
                    
                
                                                                       


                              
 
 







                                                   


     





































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

#include <fcntl.h>
#include <signal.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>

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

void print_prompt();

// 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[]) {

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

    while (true) {
        char *command = NULL;
        size_t cap = 0;
        size_t length = 0;

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

        command[length - 1] = '\0'; // cut the line feed
/*
        char c;
        int i = 0;
        while (true) {
            c = command[i++];
            if (c == ' ') {
                arrayPush(argument) = '\0';
                arrayPush(arguments) = argument;
                printf("+ %s\n", argument);
                arrayRelease(argument);
                arrayInit(argument);
            } else if (c == '\0') {

            } else {
                arrayPush(argument) = c;
            }
        }
*/
        if (strstr(command, "cd") != NULL) {
            printf("Changing dirs \n");
            chdir("beispiele");
        } else if (strcmp(command, "exit\n") == 0) {
            exit(0);
        } else {
            exec_command(command, (char *const []) {command, NULL}, 0);
        }
        free((void *)command);
    }
}

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, "no program\n");
        exit(-1);
    }

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

    close(pipefd[1]);

    size_t length = 0;
    if ((length = read(pipefd[0], buf, 512)) <= 0) {
        fprintf(stderr, "error\n");
        exit(-3);
    }

    waitpid(pid, &status, 0);

    close(pipefd[0]);

    buf[length] = '\0';

    printf("%s", buf);
    return WEXITSTATUS(status);
}