summary refs log tree commit diff
path: root/progs/shel
diff options
context:
space:
mode:
authorWlodekM <[email protected]>2025-02-15 15:07:02 +0200
committerWlodekM <[email protected]>2025-02-15 15:07:02 +0200
commit6f22816a9a00e4d62a1f7106edf6ba8225b7901a (patch)
tree3e0055f1f900aa56f04dabc4bb7bc67778532566 /progs/shel
parent68a54722c759cb50a955b0da2aec08a83a616a20 (diff)
add support for PATH env thing
Diffstat (limited to 'progs/shel')
-rw-r--r--progs/shel/shel.c54
1 files changed, 53 insertions, 1 deletions
diff --git a/progs/shel/shel.c b/progs/shel/shel.c
index dc51a92..d0f4dae 100644
--- a/progs/shel/shel.c
+++ b/progs/shel/shel.c
@@ -65,6 +65,17 @@ void joinPath(char *result, const char *base, const char *rel) {
     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};
@@ -108,7 +119,48 @@ int main() {
         chdir(path);
         __pid_t forkResult = fork();
         if (forkResult == 0) {
-            execve(command, argv, 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);
         }