commit: 1eb5a9c6620375c6b07a0a477baa225a6397efb8
parent cc4aeba01e3c72b05ffdac49095a5467f8f35665
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date:   Tue, 19 Apr 2022 08:53:29 +0200
bin/env: Fix exit status
Diffstat:
1 file changed, 18 insertions(+), 6 deletions(-)
diff --git a/bin/env.c b/bin/env.c
@@ -4,6 +4,7 @@
 
 #define _POSIX_C_SOURCE 200809L
 #include <assert.h>  // assert
+#include <errno.h>   // errno
 #include <stdbool.h> // bool, true, false
 #include <stdio.h>   // puts, perror, fprintf
 #include <stdlib.h>  // putenv
@@ -55,11 +56,13 @@ main(int argc, char *argv[])
 		case ':':
 			fprintf(stderr, "Error: Missing operand for option: '-%c'\n", optopt);
 			usage();
-			return 127;
+			return 1;
 		case '?':
 			fprintf(stderr, "Error: Unrecognised option: '-%c'\n", optopt);
 			usage();
-			return 127;
+			return 1;
+		default:
+			assert(false);
 		}
 	}
 
@@ -86,7 +89,7 @@ main(int argc, char *argv[])
 		if(setenv(argv[0], sep, 1))
 		{
 			perror("env: setenv:");
-			return 127;
+			return 1;
 		}
 	}
 
@@ -96,12 +99,21 @@ main(int argc, char *argv[])
 	}
 
 	assert(argv[0]);
+	errno = 0;
 	/* flawfinder: ignore. No restrictions on commands is intended */
 	if(execvp(argv[0], argv) < 0)
 	{
-		perror("env: execve");
-		return 127;
+		if(errno == ENOENT)
+		{
+			perror("env: execve");
+			return 127;
+		}
+		else
+		{
+			perror("env: execve");
+			return 126;
+		}
 	}
 
-	return 0;
+	assert(false);
 }