From bd4c587409af6cfc4b5d6207458aa4b5ecceaf7d Mon Sep 17 00:00:00 2001
From: apio <blobs.trading@gmail.com>
Date: Thu, 20 Oct 2022 19:12:17 +0200
Subject: [PATCH] sh: Try to execute programs in /bin if they are not found

---
 apps/src/sh.c | 14 +++++++++++++-
 1 file changed, 13 insertions(+), 1 deletion(-)

diff --git a/apps/src/sh.c b/apps/src/sh.c
index a9fdc90c..8e2f5f5a 100644
--- a/apps/src/sh.c
+++ b/apps/src/sh.c
@@ -1,3 +1,4 @@
+#include <errno.h>
 #include <luna.h>
 #include <stdio.h>
 #include <stdlib.h>
@@ -104,7 +105,18 @@ void command_execute(command* cmd)
     }
     if (child == 0)
     {
-        execv(cmd->buffer, NULL);
+        if (cmd->buffer[0] != '/' && access(cmd->buffer, F_OK) < 0) // FIXME: Race condition.
+        {
+            if (errno == ENOENT)
+            { // Try in /bin
+                char* buf = malloc(cmd->size + 6);
+                strlcpy(buf, "/bin/", 6);
+                strncat(buf, cmd->buffer, cmd->size);
+                execv(buf, NULL);
+            }
+        }
+        else
+            execv(cmd->buffer, NULL);
         perror(cmd->buffer);
         exit(127);
     }