commit: c559884b323d53fa10058598ed4be7ed930295ee
parent d632a0f3a2e8f5f8ebfa141fb48acd7539dc18e6
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date: Tue, 12 Sep 2023 07:19:24 +0200
cmd/env: Improve error messages
Diffstat:
2 files changed, 9 insertions(+), 16 deletions(-)
diff --git a/cmd/env.c b/cmd/env.c
@@ -6,9 +6,9 @@
#include <assert.h> // assert
#include <errno.h> // errno
#include <stdbool.h> // bool, true, false
-#include <stdio.h> // puts, perror, fprintf
+#include <stdio.h> // puts, fprintf
#include <stdlib.h> // putenv
-#include <string.h> // strchr
+#include <string.h> // strchr, strerror
#include <unistd.h> // getopt, opt*
extern char **environ;
@@ -22,7 +22,7 @@ int export()
{
if(puts(environ[i]) < 0)
{
- perror("env: puts");
+ perror("env: puts(environ[i])");
return 1;
}
}
@@ -108,7 +108,7 @@ main(int argc, char *argv[])
if(setenv(argv[0], sep, 1))
{
- perror("env: setenv:");
+ fprintf(stderr, "env: setenv(%s, %s, 1): %s\n", argv[0], sep, strerror(errno));
return 1;
}
}
@@ -123,16 +123,9 @@ main(int argc, char *argv[])
/* flawfinder: ignore. No restrictions on commands is intended */
if(execvp(argv[0], argv) < 0)
{
- if(errno == ENOENT)
- {
- perror("env: execve");
- return 127;
- }
- else
- {
- perror("env: execve");
- return 126;
- }
+ fprintf(stderr, "env: execvp(\"%s\", ...): %s\n", argv[0], strerror(errno));
+
+ return (errno == ENOENT) ? 127 : 126;
}
assert(false);
diff --git a/test-cmd/env b/test-cmd/env
@@ -47,12 +47,12 @@ devfull_body() {
[ "$(uname -s)" = "NetBSD" ] && atf_expect_fail "NetBSD ignoring write errors for puts()"
[ "$(uname -s)" = "FreeBSD" ] && atf_expect_fail "FreeBSD ignoring write errors for puts()"
- atf_check -s exit:1 -e 'inline:env: puts: No space left on device\n' sh -c '../cmd/env >/dev/full'
+ atf_check -s exit:1 -e 'inline:env: puts(environ[i]): No space left on device\n' sh -c '../cmd/env >/dev/full'
}
atf_test_case noutil
noutil_body() {
- atf_check -s exit:127 -e 'inline:env: execve: No such file or directory\n' ../cmd/env /var/empty/e/no/ent
+ atf_check -s exit:127 -e 'inline:env: execvp("/var/empty/e/no/ent", ...): No such file or directory\n' ../cmd/env /var/empty/e/no/ent
}
atf_test_case false