logo

utils-std

Collection of commonly available Unix tools git clone https://anongit.hacktivis.me/git/utils-std.git
commit: 14f50813969fd871c4a5d86e369be3714d3433cd
parent fb039c94ddc84e1f2ec5dcb38e7d1b507d0dfd97
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date:   Fri, 20 Sep 2024 02:26:03 +0200

cmd/env: unify error message formatting

Diffstat:

Mcmd/env.c29+++++++++++++++++------------
Mtest-cmd/env.sh4++--
2 files changed, 19 insertions(+), 14 deletions(-)

diff --git a/cmd/env.c b/cmd/env.c @@ -29,7 +29,7 @@ do_export(void) { if(puts(environ[i]) < 0) { - perror("env: puts(environ[i])"); + fprintf(stderr, "env: error: Failed writing environ: %s\n", strerror(errno)); return 1; } } @@ -84,31 +84,31 @@ main(int argc, char *argv[]) case 0: break; case WRDE_BADCHAR: - fputs("env: Error: wordexpr returned WRDE_BADCHAR\n", stderr); + fputs("env: error: wordexpr returned WRDE_BADCHAR\n", stderr); return 1; case WRDE_BADVAL: - fputs("env: Error: Undefined shell variable (WRDE_BADVAL)\n", stderr); + fputs("env: error: Undefined shell variable (WRDE_BADVAL)\n", stderr); return 1; case WRDE_CMDSUB: - fputs("env: Error: Command substitution forbidden (WRDE_CMDSUB)\n", stderr); + fputs("env: error: Command substitution forbidden (WRDE_CMDSUB)\n", stderr); return 1; case WRDE_NOSPACE: - fputs("env: Error: Out of memory (WRDE_NOSPACE)\n", stderr); + fputs("env: error: Out of memory (WRDE_NOSPACE)\n", stderr); return 1; case WRDE_SYNTAX: - fputs("env: Error: Syntax Error (WRDE_SYNTAX)\n", stderr); + fputs("env: error: Syntax Error (WRDE_SYNTAX)\n", stderr); return 1; default: - fprintf(stderr, "env: Unknown error %d from wordexp\n", ret); + fprintf(stderr, "env: error: Unknown error %d from wordexp\n", ret); return 1; } break; case ':': - fprintf(stderr, "env: Error: Missing operand for option: '-%c'\n", optopt); + fprintf(stderr, "env: error: Missing operand for option: '-%c'\n", optopt); usage(); return 1; case '?': - fprintf(stderr, "env: Error: Unrecognised option: '-%c'\n", optopt); + fprintf(stderr, "env: error: Unrecognised option: '-%c'\n", optopt); usage(); return 1; default: @@ -138,7 +138,11 @@ main(int argc, char *argv[]) if(setenv(argv[0], sep, 1)) { - fprintf(stderr, "env: setenv(%s, %s, 1): %s\n", argv[0], sep, strerror(errno)); + fprintf(stderr, + "env: error: Faild setting environment variable '%s' to '%s': %s\n", + argv[0], + sep, + strerror(errno)); return 1; } } @@ -148,7 +152,8 @@ main(int argc, char *argv[]) errno = 0; if(execvp(opt_S.we_wordv[0], opt_S.we_wordv) < 0) { - fprintf(stderr, "env: execvp(\"%s\", ...): %s\n", opt_S.we_wordv[0], strerror(errno)); + fprintf( + stderr, "env: error: Failed executing '%s': %s\n", opt_S.we_wordv[0], strerror(errno)); return (errno == ENOENT) ? 127 : 126; } @@ -164,7 +169,7 @@ main(int argc, char *argv[]) errno = 0; if(execvp(argv[0], argv) < 0) { - fprintf(stderr, "env: execvp(\"%s\", ...): %s\n", argv[0], strerror(errno)); + fprintf(stderr, "env: error: Failed executing '%s': %s\n", argv[0], strerror(errno)); return (errno == ENOENT) ? 127 : 126; } diff --git a/test-cmd/env.sh b/test-cmd/env.sh @@ -15,8 +15,8 @@ t exec_true 'true' '' t exec_echo 'echo' ' ' t --exit=1 exec_false 'false' '' -t --exit=127 enoent '/var/empty/e/no/ent' 'env: execvp("/var/empty/e/no/ent", ...): No such file or directory -' +t --exit=127 enoent '/var/empty/e/no/ent' "env: error: Failed executing '/var/empty/e/no/ent': No such file or directory +" t posix_me_harder "-i FOO=BAR sh -c echo" ' '