commit: f5211f99058183bea84be9463f3e579981881d33
parent 2ccd22098e8af5a19f408abf62d0936e5b493f91
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date: Thu, 19 Sep 2024 18:43:33 +0200
cmd/chmod: unify error message formatting
Diffstat:
2 files changed, 41 insertions(+), 19 deletions(-)
diff --git a/cmd/chmod.c b/cmd/chmod.c
@@ -25,6 +25,7 @@
#include <getopt.h>
#endif
+const char *argv0 = "chmod";
bool opt_c = false, opt_v = false;
static int
@@ -36,7 +37,11 @@ do_fchmodat(int fd, char *mode_arg, char *name, char *acc_path, bool recursive)
assert(errno == 0);
if(fstatat(fd, name, &stats, AT_SYMLINK_NOFOLLOW) != 0)
{
- fprintf(stderr, "chmod: Failed getting status for '%s': %s\n", acc_path, strerror(errno));
+ fprintf(stderr,
+ "%s: error: Failed getting status for '%s': %s\n",
+ argv0,
+ acc_path,
+ strerror(errno));
errno = 0;
return 1;
}
@@ -45,7 +50,7 @@ do_fchmodat(int fd, char *mode_arg, char *name, char *acc_path, bool recursive)
mode_t mode = new_mode(mode_arg, stats.st_mode, &errstr);
if(errstr != NULL)
{
- fprintf(stderr, "chmod: Failed parsing mode '%s': %s\n", mode_arg, errstr);
+ fprintf(stderr, "%s: error: Failed parsing mode '%s': %s\n", argv0, mode_arg, errstr);
return 1;
}
@@ -58,7 +63,8 @@ do_fchmodat(int fd, char *mode_arg, char *name, char *acc_path, bool recursive)
if(fchmodat(fd, name, mode, 0) != 0)
{
fprintf(stderr,
- "chmod: Failed setting permissions to 0%04o for '%s': %s\n",
+ "%s: error: Failed setting permissions to 0%04o for '%s': %s\n",
+ argv0,
mode,
acc_path,
strerror(errno));
@@ -71,7 +77,8 @@ do_fchmodat(int fd, char *mode_arg, char *name, char *acc_path, bool recursive)
char mode_to[11] = "";
symbolize_mode(mode, mode_to);
- printf("chmod: Permissions changed from 0%04o/%s to 0%04o/%s for '%s'\n",
+ printf("%s: Permissions changed from 0%04o/%s to 0%04o/%s for '%s'\n",
+ argv0,
stats.st_mode & 07777,
mode_from,
mode & 07777,
@@ -80,7 +87,8 @@ do_fchmodat(int fd, char *mode_arg, char *name, char *acc_path, bool recursive)
}
}
else if(opt_v)
- printf("chmod: Permissions already set to 0%04o/%s for '%s'\n",
+ printf("%s: Permissions already set to 0%04o/%s for '%s'\n",
+ argv0,
stats.st_mode & 07777,
mode_from,
acc_path);
@@ -91,7 +99,11 @@ do_fchmodat(int fd, char *mode_arg, char *name, char *acc_path, bool recursive)
int dir = openat(fd, name, O_RDONLY | O_DIRECTORY | O_CLOEXEC);
if(dir == -1)
{
- fprintf(stderr, "chmod: Couldn't open '%s' as directory: %s\n", acc_path, strerror(errno));
+ fprintf(stderr,
+ "%s: error: Couldn't open '%s' as directory: %s\n",
+ argv0,
+ acc_path,
+ strerror(errno));
errno = 0;
return 1;
}
@@ -100,8 +112,11 @@ do_fchmodat(int fd, char *mode_arg, char *name, char *acc_path, bool recursive)
DIR *dirp = fdopendir(dir);
if(dirp == NULL)
{
- fprintf(
- stderr, "chmod: Couldn't get DIR entry for opened '%s': %s\n", acc_path, strerror(errno));
+ fprintf(stderr,
+ "%s: error: Couldn't get DIR entry for opened '%s': %s\n",
+ argv0,
+ acc_path,
+ strerror(errno));
errno = 0;
return 1;
}
@@ -114,7 +129,11 @@ do_fchmodat(int fd, char *mode_arg, char *name, char *acc_path, bool recursive)
{
if(errno == 0) break;
- fprintf(stderr, "chmod: Failed reading directory '%s': %s\n", acc_path, strerror(errno));
+ fprintf(stderr,
+ "%s: error: Failed reading directory '%s': %s\n",
+ argv0,
+ acc_path,
+ strerror(errno));
closedir(dirp); // FIXME: unhandled error
errno = 0;
return 1;
@@ -128,7 +147,8 @@ do_fchmodat(int fd, char *mode_arg, char *name, char *acc_path, bool recursive)
if(snprintf(new_path, PATH_MAX, "%s/%s", acc_path, dp->d_name) < 0)
{
fprintf(stderr,
- "chmod: Couldn't concatenate '%s' into parent '%s', skipping to next entry: %s",
+ "%s: Couldn't concatenate '%s' into parent '%s', skipping to next entry: %s\n",
+ argv0,
name,
acc_path,
strerror(errno));
@@ -147,7 +167,8 @@ do_fchmodat(int fd, char *mode_arg, char *name, char *acc_path, bool recursive)
if(closedir(dirp) != 0)
{
fprintf(stderr,
- "chmod: Deallocating directory entry for '%s' failed: %s\n",
+ "%s: Deallocating directory entry for '%s' failed: %s\n",
+ argv0,
acc_path,
strerror(errno));
errno = 0;
@@ -189,8 +210,9 @@ main(int argc, char *argv[])
if(argv[optind][0] == '-' && strchr("rwxugoa", argv[optind][1]))
{
fprintf(stderr,
- "chmod: Portability Warning: Pass -- before a mode with a leading dash(-) to "
- "separate it from options\n");
+ "%s: warning: (portability) Pass -- before a mode with a leading dash(-) to "
+ "separate it from options\n",
+ argv0);
break;
}
@@ -214,11 +236,11 @@ main(int argc, char *argv[])
opt_v = true;
break;
case ':':
- fprintf(stderr, "chmod: Error: Missing operand for option: '-%c'\n", optopt);
+ fprintf(stderr, "%s: error: Missing operand for option: '-%c'\n", argv0, optopt);
usage();
return 1;
case '?': // GNU
- fprintf(stderr, "chmod: Error: Unrecognised option: '-%c'\n", optopt);
+ fprintf(stderr, "%s: error: Unrecognised option: '-%c'\n", argv0, optopt);
usage();
return 1;
}
@@ -229,7 +251,7 @@ main(int argc, char *argv[])
if(argc < 2)
{
- fprintf(stderr, "chmod: Expects >=2 arguments, %d given\n", argc);
+ fprintf(stderr, "%s: error: Expects >=2 arguments, %d given\n", argv0, argc);
usage();
return 1;
}
diff --git a/test-cmd/chmod.sh b/test-cmd/chmod.sh
@@ -25,14 +25,14 @@ t '0444' "-v 0444 $tmpfile" "chmod: Permissions changed from 00000/---------- to
t '0777' "-v 0777 $tmpfile" "chmod: Permissions changed from 00444/-r--r--r-- to 00777/-rwxrwxrwx for '${tmpfile}'
"
-t --exit=1 'invalid 0888' "-v 0888 $tmpfile" "chmod: Failed parsing mode '0888': contains digit outside of [0-7]
+t --exit=1 'invalid 0888' "-v 0888 $tmpfile" "chmod: error: Failed parsing mode '0888': contains digit outside of [0-7]
"
-t 'mode:-w,+x' "-v -w,+x $tmpfile" "chmod: Portability Warning: Pass -- before a mode with a leading dash(-) to separate it from options
+t 'mode:-w,+x' "-v -w,+x $tmpfile" "chmod: warning: (portability) Pass -- before a mode with a leading dash(-) to separate it from options
chmod: Permissions changed from 00777/-rwxrwxrwx to 00577/-r-xrwxrwx for '${tmpfile}'
"
-t 'mode:-r' "-v -r $tmpfile" "chmod: Portability Warning: Pass -- before a mode with a leading dash(-) to separate it from options
+t 'mode:-r' "-v -r $tmpfile" "chmod: warning: (portability) Pass -- before a mode with a leading dash(-) to separate it from options
chmod: Permissions changed from 00577/-r-xrwxrwx to 00133/---x-wx-wx for '${tmpfile}'
"