summaryrefslogtreecommitdiffstats
path: root/02_exercise/shell.c
blob: 75e77b73b88cca2c5915e62fe22cc5af661f5e10 (plain) (blame)
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#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);
}