logo

utils-std

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

cmd/rm: unify error message formatting

Diffstat:

Mcmd/rm.c30+++++++++++++++++-------------
Mtest-cmd/rm.t14+++++++-------
2 files changed, 24 insertions(+), 20 deletions(-)

diff --git a/cmd/rm.c b/cmd/rm.c @@ -44,7 +44,7 @@ do_unlinkat(int fd, char *name, char *acc_path) return 0; } - fprintf(stderr, "rm: Failed getting status for '%s': %s\n", acc_path, strerror(errno)); + fprintf(stderr, "rm: error: Failed getting status for '%s': %s\n", acc_path, strerror(errno)); errno = 0; return 1; } @@ -55,7 +55,7 @@ do_unlinkat(int fd, char *name, char *acc_path) { if(!recurse) { - fprintf(stderr, "rm: Is a directory, pass -r or -d to remove: %s\n", acc_path); + fprintf(stderr, "rm: error: Is a directory, pass -r or -d to remove: %s\n", acc_path); return 1; } @@ -66,7 +66,8 @@ do_unlinkat(int fd, char *name, char *acc_path) int dir = openat(fd, name, O_RDONLY | O_DIRECTORY | O_CLOEXEC); if(dir == -1) { - fprintf(stderr, "rm: Couldn't open '%s' as directory: %s\n", acc_path, strerror(errno)); + fprintf( + stderr, "rm: error: Couldn't open '%s' as directory: %s\n", acc_path, strerror(errno)); errno = 0; return 1; } @@ -75,8 +76,10 @@ do_unlinkat(int fd, char *name, char *acc_path) DIR *dirp = fdopendir(dir); if(dirp == NULL) { - fprintf( - stderr, "rm: Couldn't get DIR entry for opened '%s': %s\n", acc_path, strerror(errno)); + fprintf(stderr, + "rm: error: Couldn't get DIR entry for opened '%s': %s\n", + acc_path, + strerror(errno)); errno = 0; return 1; } @@ -89,7 +92,8 @@ do_unlinkat(int fd, char *name, char *acc_path) { if(errno == 0) break; - fprintf(stderr, "rm: Failed reading directory '%s': %s\n", acc_path, strerror(errno)); + fprintf( + stderr, "rm: error: Failed reading directory '%s': %s\n", acc_path, strerror(errno)); closedir(dirp); // error ignored errno = 0; return 1; @@ -103,7 +107,7 @@ do_unlinkat(int fd, char *name, char *acc_path) if(snprintf(new_path, PATH_MAX, "%s/%s", acc_path, dp->d_name) < 0) { fprintf(stderr, - "rm: Couldn't concatenate '%s' into parent '%s', skipping to next entry: %s", + "rm: error: Couldn't concatenate '%s' into parent '%s', skipping to next entry: %s", name, acc_path, strerror(errno)); @@ -122,7 +126,7 @@ do_unlinkat(int fd, char *name, char *acc_path) if(closedir(dirp) != 0) { fprintf(stderr, - "rm: Deallocating directory entry for '%s' failed: %s\n", + "rm: error: Deallocating directory entry for '%s' failed: %s\n", acc_path, strerror(errno)); errno = 0; @@ -152,7 +156,7 @@ do_unlinkat(int fd, char *name, char *acc_path) assert(errno == 0); if(unlinkat(fd, name, is_dir ? AT_REMOVEDIR : 0) != 0) { - fprintf(stderr, "rm: Couldn't remove '%s': %s\n", acc_path, strerror(errno)); + fprintf(stderr, "rm: error: Couldn't remove '%s': %s\n", acc_path, strerror(errno)); errno = 0; return 1; } @@ -197,11 +201,11 @@ main(int argc, char *argv[]) verbose = true; break; case ':': - fprintf(stderr, "rm: Error: Missing operand for option: '-%c'\n", optopt); + fprintf(stderr, "rm: error: Missing operand for option: '-%c'\n", optopt); usage(); return 1; case '?': - fprintf(stderr, "rm: Error: Unrecognised option: '-%c'\n", optopt); + fprintf(stderr, "rm: error: Unrecognised option: '-%c'\n", optopt); usage(); return 1; default: @@ -216,7 +220,7 @@ main(int argc, char *argv[]) setlocale(LC_ALL, ""); if(errno != 0) { - fprintf(stderr, "%s: Warning: Failed to initialize locales: %s\n", argv0, strerror(errno)); + fprintf(stderr, "%s: warning: Failed to initialize locales: %s\n", argv0, strerror(errno)); errno = 0; } @@ -224,7 +228,7 @@ main(int argc, char *argv[]) if(argc == 0 && !force) { - fprintf(stderr, "rm: missing operand\n"); + fprintf(stderr, "rm: error: missing operand\n"); usage(); return 1; } diff --git a/test-cmd/rm.t b/test-cmd/rm.t @@ -15,7 +15,7 @@ POSIX rm(1p) step 1a, no -f option $ touch exists $ test -f exists $ rm enoent exists - rm: Failed getting status for 'enoent': No such file or directory + rm: error: Failed getting status for 'enoent': No such file or directory [1] $ test ! -e exists @@ -33,7 +33,7 @@ POSIX rm(1p) step 2a: $ touch no_rR.f $ test -f no_rR.f $ rm no_rR.d no_rR.f - rm: Is a directory, pass -r or -d to remove: no_rR.d + rm: error: Is a directory, pass -r or -d to remove: no_rR.d [1] $ test -d no_rR.d $ test ! -e no_rR.f @@ -122,13 +122,13 @@ Verbose POSIX, -f shouldn't return an error when non-existant files are passed $ rm -f /var/empty/e/no/ent $ rm /var/empty/e/no/ent - rm: Failed getting status for '/var/empty/e/no/ent': No such file or directory + rm: error: Failed getting status for '/var/empty/e/no/ent': No such file or directory [1] POSIX, -f shouldn't return an error when no operands are passed $ rm -f $ rm - rm: missing operand + rm: error: missing operand Usage: rm [-firRv] [files ...] [1] @@ -139,7 +139,7 @@ POSIX 2024/D4.1, -d $ mkdir non_empty_dir $ touch non_empty_dir/.keep $ rm -d non_empty_dir - rm: Couldn't remove 'non_empty_dir': Directory not empty + rm: error: Couldn't remove 'non_empty_dir': Directory not empty [1] $ test -e non_empty_dir $ rm -fr non_empty_dir @@ -156,7 +156,7 @@ Correct path when multiple files are given $ touch path_v_foo path_v_bar $ rm -v path_v_foo path_v_enoent path_v_bar rm: Removed: path_v_foo - rm: Failed getting status for 'path_v_enoent': No such file or directory + rm: error: Failed getting status for 'path_v_enoent': No such file or directory rm: Removed: path_v_bar [1] $ test ! -e path_v_foo @@ -167,7 +167,7 @@ Correct path when multiple files are given rm: Removed: dir_v_foo/ba[zr] (re) rm: Removed: dir_v_foo/ba[zr] (re) rm: Removed: dir_v_foo - rm: Failed getting status for 'dir_v_foo/baz': No such file or directory + rm: error: Failed getting status for 'dir_v_foo/baz': No such file or directory rm: Removed: dir_v_bar/foo rm: Removed: dir_v_bar rm: Removed: dir_v_dir/foo.d