summaryrefslogtreecommitdiffstats
path: root/02_exercise/shell.c
blob: 88b17fd01e92395e4481b7c6e1abd5218150b25c (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
#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 "stdbool.h"

void print_prompt(char const *const original_wd, char const *current_wd);

int main(void) {

    char const *const original_wd = get_current_dir_name();
    char const *current_wd = get_current_dir_name();

    while (true) {
        char *command = NULL;
        size_t cap = 0;
        size_t length = 0;
        print_prompt(original_wd, current_wd);
        if ((length = getline(&command, &cap, stdin)) < 0) {
            fprintf(stderr, "Failed to read from STDIN");
        }
        if (strcmp(command, "exit\n") == 0) {
            exit(0);
        }
        free((void *)command);
    }
    // Gotta cast to void otherwise we discard qualifiers
    free((void *)original_wd);
    free((void *)current_wd);
}

void print_prompt(char const *const original_wd, char const *current_wd) {

    int result = strcmp(original_wd, current_wd);
    if (result == 0) {
        printf("./ > ");
    }
}