logo

utils-std

Collection of commonly available Unix tools git clone https://anongit.hacktivis.me/git/utils-std.git
commit: 66161caafde656d8c4075d6250b327cbabc9c851
parent 916c618bd99370c1a44a1592a542cf553b2c7256
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date:   Fri, 20 Sep 2024 03:50:03 +0200

cmd/realpath: unify error message formatting

Diffstat:

Mcmd/realpath.c27++++++++++++++-------------
Mtest-cmd/readlink.sh8++++----
Mtest-cmd/realpath.sh30+++++++++++++++---------------
3 files changed, 33 insertions(+), 32 deletions(-)

diff --git a/cmd/realpath.c b/cmd/realpath.c @@ -28,7 +28,7 @@ print_realpath(char *path) { if(printf("%s", file) < 0) { - fprintf(stderr, "%s: Error writing to stdtout: %s\n", argv0, strerror(errno)); + fprintf(stderr, "%s: error: Failed writing to stdout: %s\n", argv0, strerror(errno)); free(file); return 1; } @@ -40,7 +40,7 @@ print_realpath(char *path) if(must_exists || errno != ENOENT) { - fprintf(stderr, "%s: Failed canonilizing \"%s\": %s\n", argv0, path, strerror(errno)); + fprintf(stderr, "%s: error: Failed canonilizing \"%s\": %s\n", argv0, path, strerror(errno)); return 1; } @@ -49,7 +49,7 @@ print_realpath(char *path) if(child == NULL) { // Return as if realpath just failed - fprintf(stderr, "%s: Failed canonilizing \"%s\": %s\n", argv0, path, strerror(errno)); + fprintf(stderr, "%s: error: Failed canonilizing \"%s\": %s\n", argv0, path, strerror(errno)); return 1; } @@ -59,7 +59,7 @@ print_realpath(char *path) if(!parent) { fprintf(stderr, - "%s: Failed canonilizing parent of full path \"%s/%s\": %s\n", + "%s: error: Failed canonilizing parent of full path \"%s/%s\": %s\n", argv0, path, child, @@ -69,7 +69,7 @@ print_realpath(char *path) if(printf("%s/%s", parent, child) < 0) { - fprintf(stderr, "%s: Error writing to stdtout: %s\n", argv0, strerror(errno)); + fprintf(stderr, "%s: error: Failed writing to stdout: %s\n", argv0, strerror(errno)); free(parent); return 1; } @@ -109,11 +109,11 @@ main_realpath(int argc, char *argv[]) sep = '\0'; break; case ':': - fprintf(stderr, "realpath: Error: Missing operand for option: '-%c'\n", optopt); + fprintf(stderr, "%s: error: Missing operand for option: '-%c'\n", argv0, optopt); usage_realpath(); return 1; case '?': - fprintf(stderr, "realpath: Error: Unrecognised option: '-%c'\n", optopt); + fprintf(stderr, "%s: error: Unrecognised option: '-%c'\n", argv0, optopt); usage_realpath(); return 1; } @@ -124,7 +124,7 @@ main_realpath(int argc, char *argv[]) if(argc == 0) { - fprintf(stderr, "%s: Expected one file as argument, got 0\n", argv0); + fprintf(stderr, "%s: error: Expected one file as argument, got 0\n", argv0); usage_realpath(); return 1; } @@ -172,11 +172,11 @@ main_readlink(int argc, char *argv[]) sep = '\0'; break; case ':': - fprintf(stderr, "readlink: Error: Missing operand for option: '-%c'\n", optopt); + fprintf(stderr, "%s: error: Missing operand for option: '-%c'\n", argv0, optopt); usage_readlink(); return 1; case '?': - fprintf(stderr, "readlink: Error: Unrecognised option: '-%c'\n", optopt); + fprintf(stderr, "%s: error: Unrecognised option: '-%c'\n", argv0, optopt); usage_readlink(); return 1; } @@ -187,7 +187,7 @@ main_readlink(int argc, char *argv[]) if(argc == 0) { - fprintf(stderr, "%s: Expected one file as argument, got 0\n", argv0); + fprintf(stderr, "%s: error: Expected one file as argument, got 0\n", argv0); usage_readlink(); return 1; } @@ -208,7 +208,8 @@ main_readlink(int argc, char *argv[]) if(readlink(path, buf, sizeof(buf) - 1) < 0) { fprintf(stderr, - "readlink: Error: Failed reading symbolic link of '%s': %s\n", + "%s: error: Failed reading symbolic link of '%s': %s\n", + argv0, path, strerror(errno)); return 1; @@ -228,7 +229,7 @@ main(int argc, char *argv[]) if(strcmp(argv0, "realpath") == 0) return main_realpath(argc, argv); if(strcmp(argv0, "readlink") == 0) return main_readlink(argc, argv); - fprintf(stderr, "Unknown utility '%s', expected realpath or readlink\n", argv0); + fprintf(stderr, "%s: error: Unknown utility '%s', expected realpath or readlink\n", argv0, argv0); return 1; } diff --git a/test-cmd/readlink.sh b/test-cmd/readlink.sh @@ -6,7 +6,7 @@ target="$(dirname "$0")/../cmd/readlink" plans=12 . "$(dirname "$0")/tap.sh" -t --exit=1 noargs '' 'readlink: Expected one file as argument, got 0 +t --exit=1 noargs '' 'readlink: error: Expected one file as argument, got 0 Usage: readlink [-f|-e] [-n|-z] file... ' @@ -19,15 +19,15 @@ t read_foobar_no_nl '-n foobar' '//example.org/' t_cmd rm:foobar '' rm foobar -t --exit=1 enoent /var/empty/e/no/ent "readlink: Error: Failed reading symbolic link of '/var/empty/e/no/ent': No such file or directory +t --exit=1 enoent /var/empty/e/no/ent "readlink: error: Failed reading symbolic link of '/var/empty/e/no/ent': No such file or directory " t_cmd touch:regular '' touch regular -t --exit=1 readlink:regular regular "readlink: Error: Failed reading symbolic link of 'regular': Invalid argument +t --exit=1 readlink:regular regular "readlink: error: Failed reading symbolic link of 'regular': Invalid argument " t_cmd rm:regular '' rm regular t_cmd mkdir:dir '' mkdir dir -t --exit=1 readlink:dir dir "readlink: Error: Failed reading symbolic link of 'dir': Invalid argument +t --exit=1 readlink:dir dir "readlink: error: Failed reading symbolic link of 'dir': Invalid argument " t_cmd rm:dir '' rm -r dir diff --git a/test-cmd/realpath.sh b/test-cmd/realpath.sh @@ -15,28 +15,28 @@ t /var /var '/var ' t /var/empty /var/empty '/var/empty ' -t --exit=1 /var/empty/foo/bar /var/empty/foo/bar 'realpath: Failed canonilizing parent of full path "/var/empty/foo/bar": No such file or directory +t --exit=1 /var/empty/foo/bar /var/empty/foo/bar 'realpath: error: Failed canonilizing parent of full path "/var/empty/foo/bar": No such file or directory ' -t --exit=1 /var/empty/foo/bar/ /var/empty/foo/bar/ 'realpath: Failed canonilizing parent of full path "/var/empty/foo/bar": No such file or directory +t --exit=1 /var/empty/foo/bar/ /var/empty/foo/bar/ 'realpath: error: Failed canonilizing parent of full path "/var/empty/foo/bar": No such file or directory ' -t --exit=1 /var/empty/foo/bar// /var/empty/foo/bar// 'realpath: Failed canonilizing parent of full path "/var/empty/foo/bar": No such file or directory +t --exit=1 /var/empty/foo/bar// /var/empty/foo/bar// 'realpath: error: Failed canonilizing parent of full path "/var/empty/foo/bar": No such file or directory ' t e:/ '-e /' '/ ' t e:/var '-e /var' '/var ' -t --exit=1 e:/var/empty/foo '-e /var/empty/foo' 'realpath: Failed canonilizing "/var/empty/foo": No such file or directory +t --exit=1 e:/var/empty/foo '-e /var/empty/foo' 'realpath: error: Failed canonilizing "/var/empty/foo": No such file or directory ' -t --exit=1 e:/var/empty/foo/ '-e /var/empty/foo/' 'realpath: Failed canonilizing "/var/empty/foo/": No such file or directory +t --exit=1 e:/var/empty/foo/ '-e /var/empty/foo/' 'realpath: error: Failed canonilizing "/var/empty/foo/": No such file or directory ' -t --exit=1 e:/var/empty/foo// '-e /var/empty/foo//' 'realpath: Failed canonilizing "/var/empty/foo//": No such file or directory +t --exit=1 e:/var/empty/foo// '-e /var/empty/foo//' 'realpath: error: Failed canonilizing "/var/empty/foo//": No such file or directory ' -t --exit=1 e:/var/empty/foo/bar '-e /var/empty/foo/bar' 'realpath: Failed canonilizing "/var/empty/foo/bar": No such file or directory +t --exit=1 e:/var/empty/foo/bar '-e /var/empty/foo/bar' 'realpath: error: Failed canonilizing "/var/empty/foo/bar": No such file or directory ' -t --exit=1 e:/var/empty/foo/bar/ '-e /var/empty/foo/bar/' 'realpath: Failed canonilizing "/var/empty/foo/bar/": No such file or directory +t --exit=1 e:/var/empty/foo/bar/ '-e /var/empty/foo/bar/' 'realpath: error: Failed canonilizing "/var/empty/foo/bar/": No such file or directory ' -t --exit=1 e:/var/empty/foo/bar// '-e /var/empty/foo/bar//' 'realpath: Failed canonilizing "/var/empty/foo/bar//": No such file or directory +t --exit=1 e:/var/empty/foo/bar// '-e /var/empty/foo/bar//' 'realpath: error: Failed canonilizing "/var/empty/foo/bar//": No such file or directory ' # Non-directory @@ -48,18 +48,18 @@ t e:/dev/null '-e /dev/null' '/dev/null t E:/dev/null '-E /dev/null' '/dev/null ' -t --exit=1 /dev/null/ /dev/null/ 'realpath: Failed canonilizing "/dev/null/": Not a directory +t --exit=1 /dev/null/ /dev/null/ 'realpath: error: Failed canonilizing "/dev/null/": Not a directory ' -t --exit=1 e:/dev/null/ '-e /dev/null/' 'realpath: Failed canonilizing "/dev/null/": Not a directory +t --exit=1 e:/dev/null/ '-e /dev/null/' 'realpath: error: Failed canonilizing "/dev/null/": Not a directory ' -t --exit=1 E:/dev/null/ '-E /dev/null/' 'realpath: Failed canonilizing "/dev/null/": Not a directory +t --exit=1 E:/dev/null/ '-E /dev/null/' 'realpath: error: Failed canonilizing "/dev/null/": Not a directory ' -t --exit=1 /dev/null/.. /dev/null/.. 'realpath: Failed canonilizing "/dev/null/..": Not a directory +t --exit=1 /dev/null/.. /dev/null/.. 'realpath: error: Failed canonilizing "/dev/null/..": Not a directory ' -t --exit=1 e:/dev/null/.. '-e /dev/null/..' 'realpath: Failed canonilizing "/dev/null/..": Not a directory +t --exit=1 e:/dev/null/.. '-e /dev/null/..' 'realpath: error: Failed canonilizing "/dev/null/..": Not a directory ' -t --exit=1 E:/dev/null/.. '-E /dev/null/..' 'realpath: Failed canonilizing "/dev/null/..": Not a directory +t --exit=1 E:/dev/null/.. '-E /dev/null/..' 'realpath: error: Failed canonilizing "/dev/null/..": Not a directory ' # Zero-separation