Pārlūkot izejas kodu

Shell: Explicitly check if command is a directory

This is a bit nicer than getting "Exec format error" after trying to
execvp() a directory.
Linus Groh 5 gadi atpakaļ
vecāks
revīzija
fc09767872
1 mainītis faili ar 6 papildinājumiem un 0 dzēšanām
  1. 6 0
      Shell/main.cpp

+ 6 - 0
Shell/main.cpp

@@ -885,6 +885,12 @@ static int run_command(const String& cmd)
             if (handle_builtin(argv.size() - 1, argv.data(), retval))
                 return retval;
 
+            struct stat st;
+            if (stat(argv[0], &st) == 0 && S_ISDIR(st.st_mode)) {
+                fprintf(stderr, "Shell: %s: Is a directory\n", argv[0]);
+                return 126;
+            }
+
             pid_t child = fork();
             if (!child) {
                 setpgid(0, 0);