summary refs log tree commit diff
path: root/progs/shel/shel.c
blob: d0f4dae3cbb37d5f94ada5c632a0a9bcebfb702c (plain)
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/wait.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <stdbool.h>
#include <string.h>
#include <stdlib.h>

#define P_ALL 0
#define P_PID 1
#define P_PGID 2
#define P_PIDFD 3
#if !WEXITED
#define WEXITED		0x00000004
#endif

void parseInput(char *input, char *command, char **argv) {
    bool reachedArgv = false;
    char arg[32] = {0};
    int idx = 0;
    int argvIdx = 1;
    
    for (int i = 0; i < strlen(input); i++)
    {
        char *a = {&(input[i])};
        if (reachedArgv) {
            if (input[i] == ' ') {
                arg[idx] = '\0';
                argv[argvIdx] = strdup(arg);
                memset(arg, 0, sizeof(arg));
                argvIdx++;
                idx = 0;
                continue;
            }
            arg[idx] = input[i];
            idx++;
            continue;
        }
        if (input[i] == ' ') {
            arg[idx] = '\0';
            strcpy(command, arg);
            reachedArgv = true;
            memset(arg, 0, sizeof(arg));
            idx = 0;
            continue;
        }
        arg[idx] = input[i];
        idx++;
    }
    argv[0] = command;
    if (reachedArgv) {
        arg[idx] = '\0';
        argv[argvIdx] = strdup(arg);
    } else {
        arg[idx] = '\0';
        strcpy(command, arg);
    }
}

void joinPath(char *result, const char *base, const char *rel) {
    strcpy(result, base);
    strcat(result, "/");
    strcat(result, rel);
}

const char *get_env_var(const char *name, char *const *env) {
    size_t len = strlen(name);

    for (char *const *e = env; *e; e++) { // loop over every env
        if (strncmp(*e, name, len) == 0 && (*e)[len] == '=') { //if starts with name
            return *e + len + 1;  // return shit after =
        }
    }
    return NULL;
}

int main() {
    char input[255] = {0};
    char command[128] = {0};
    char *argv[32] = {0};
    char *env[32] = {"PATH=/bin"};
    char path[128] = "/";

    while (!false) {
        memset(input, 0, sizeof(input));
        memset(command, 0, sizeof(command));
        memset(argv, 0, sizeof(argv));
        write(1, path, strlen(path));
        write(1, " $ ", 3);
        int len = read(1, input, sizeof(input));
        input[len - 1] = '\0';
        parseInput(input, command, argv);

        if (!strcmp(command, "cd")) {
            char newpath[128] = {0};
            char newresolvedpath[128] = {0};
            if (argv[1][0] == '/') {
                strcpy(newresolvedpath, argv[1]);
            } else {
                joinPath(newpath, path, argv[1]);
                realpath(newpath, newresolvedpath);
            }
            if (!access(newresolvedpath, F_OK)) {
                struct stat statResult;
                stat(newresolvedpath, &statResult);
                if (S_ISDIR(statResult.st_mode)) {
                    strcpy(path, newresolvedpath);
                } else {
                    write(1, "uuuuh das not a dir\n", 21);
                }
            } else {
                write(1, "uuuuh das not a dir\n", 21);
            }
            continue;
        }

        chdir(path);
        __pid_t forkResult = fork();
        if (forkResult == 0) {
            // algorithm from old shel since i made it on my own
            const char* pathV = get_env_var("PATH", env);
            struct stat path_stat;
            if (stat(command, &path_stat) == 0 && access(command, X_OK)){
                execve(command, argv, 0);
            } else {
                bool found = false;
                char currPath[32] = {0};
                int i = 0;
                int c = 0;
                char newPath[128] = {0};
                while (i < strlen(pathV) && !found)
                {
                    if (pathV[i] == ':') {
                        currPath[i] = '\0';
                        memset(newPath, 0, sizeof(newPath));
                        joinPath(newPath, currPath, command);
                        realpath(newPath, newPath);
                        if(stat(newPath, &path_stat) == 0 && path_stat.st_mode & S_IXUSR) {
                            found = true;
                            continue;
                        }
                        c = 0;

                    } else {
                        currPath[c] = pathV[i];
                    }
                    i++;c++;
                }
                if (!found) {
                    currPath[i] = '\0';
                    memset(newPath, 0, sizeof(newPath));
                    joinPath(newPath, currPath, command);
                    realpath(newPath, newPath);
                    if(stat(newPath, &path_stat) == 0 && path_stat.st_mode & S_IXUSR) {
                        found = true;
                    }
                }
                if (found) {
                    execve(newPath, argv, 0);
                }
            }
        } else {
            waitid(P_ALL, 0, 0, WEXITED);
        }
    }
}