logo

utils-std

Collection of commonly available Unix tools
commit: e71f5d453293684800f065ea5f03f2b98d12789c
parent f66efb9d62357412cabf688c9fbe0ae852f460b6
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date:   Thu, 14 Mar 2024 02:07:20 +0100

cmd/chmod: Fix path printed in error messages

Diffstat:

Mcmd/chmod.c56+++++++++++++++++++++++---------------------------------
1 file changed, 23 insertions(+), 33 deletions(-)

diff --git a/cmd/chmod.c b/cmd/chmod.c @@ -19,15 +19,14 @@ bool opt_c = false, opt_v = false; static int -do_fchmodat(int fd, char *fdname, char *mode_arg, char *path, bool recursive) +do_fchmodat(int fd, char *mode_arg, char *name, char *acc_path, bool recursive) { struct stat stats; int err = 0; - if(fstatat(fd, path, &stats, AT_SYMLINK_NOFOLLOW) != 0) + if(fstatat(fd, name, &stats, AT_SYMLINK_NOFOLLOW) != 0) { - fprintf( - stderr, "chmod: Failed getting status for '%s/%s': %s\n", fdname, path, strerror(errno)); + fprintf(stderr, "chmod: Failed getting status for '%s': %s\n", acc_path, strerror(errno)); return 1; } @@ -44,13 +43,12 @@ do_fchmodat(int fd, char *fdname, char *mode_arg, char *path, bool recursive) if(mode != stats.st_mode) { - if(fchmodat(fd, path, mode, 0) != 0) + if(fchmodat(fd, name, mode, 0) != 0) { fprintf(stderr, - "chmod: Failed setting permissions to 0%6o for '%s/%s': %s\n", + "chmod: Failed setting permissions to 0%6o for '%s': %s\n", mode, - fdname, - path, + acc_path, strerror(errno)); return 1; } @@ -60,40 +58,34 @@ do_fchmodat(int fd, char *fdname, char *mode_arg, char *path, bool recursive) char mode_to[11] = ""; symbolize_mode(mode, mode_to); - printf("chmod: Permissions changed from 0%o6/%s to 0%o6/%s for '%s/%s'\n", + printf("chmod: Permissions changed from 0%o6/%s to 0%o6/%s for '%s'\n", stats.st_mode & 07777, mode_from, mode & 07777, mode_to, - fdname, - path); + acc_path); } } else if(opt_v) - printf("chmod: Permissions already set to 0%o6/%s for '%s/%s'\n", + printf("chmod: Permissions already set to 0%o6/%s for '%s'\n", stats.st_mode & 07777, mode_from, - fdname, - path); + acc_path); if(recursive && S_ISDIR(stats.st_mode)) { - int dir = openat(fd, path, O_RDONLY | O_DIRECTORY | O_CLOEXEC); + int dir = openat(fd, name, O_RDONLY | O_DIRECTORY | O_CLOEXEC); if(dir == -1) { - fprintf( - stderr, "chmod: Couldn't open '%s/%s' as directory: %s\n", fdname, path, strerror(errno)); + fprintf(stderr, "chmod: Couldn't open '%s' as directory: %s\n", acc_path, strerror(errno)); return 1; } DIR *dirp = fdopendir(dir); if(dirp == NULL) { - fprintf(stderr, - "chmod: Couldn't get DIR entry for opened '%s/%s': %s\n", - fdname, - path, - strerror(errno)); + fprintf( + stderr, "chmod: Couldn't get DIR entry for opened '%s': %s\n", acc_path, strerror(errno)); return 1; } @@ -104,8 +96,7 @@ do_fchmodat(int fd, char *fdname, char *mode_arg, char *path, bool recursive) { if(errno == 0) break; - fprintf( - stderr, "chmod: Failed reading directory '%s/%s': %s\n", fdname, path, strerror(errno)); + fprintf(stderr, "chmod: Failed reading directory '%s': %s\n", acc_path, strerror(errno)); closedir(dirp); return 1; } @@ -113,20 +104,20 @@ do_fchmodat(int fd, char *fdname, char *mode_arg, char *path, bool recursive) if(strcmp(dp->d_name, ".") == 0) continue; if(strcmp(dp->d_name, "..") == 0) continue; - char destdir[PATH_MAX] = ""; - if(snprintf(destdir, PATH_MAX, "%s/%s", fdname, path) < 0) + char new_path[PATH_MAX] = ""; + 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", - fdname, - path, + name, + acc_path, strerror(errno)); err++; continue; } // No depth counter for now, unlikely to be a problem - int ret = do_fchmodat(dir, destdir, mode_arg, dp->d_name, true); + int ret = do_fchmodat(dir, mode_arg, dp->d_name, new_path, true); if(ret != 0) return ret; } @@ -134,9 +125,8 @@ do_fchmodat(int fd, char *fdname, char *mode_arg, char *path, bool recursive) if(closedir(dirp) != 0) { fprintf(stderr, - "chmod: Deallocating directory entry for '%s/%s' failed: %s\n", - fdname, - path, + "chmod: Deallocating directory entry for '%s' failed: %s\n", + acc_path, strerror(errno)); return 1; } @@ -211,7 +201,7 @@ getopt_out: for(int i = 1; i < argc; i++) { - int ret = do_fchmodat(AT_FDCWD, ".", argv[0], argv[i], opt_R); + int ret = do_fchmodat(AT_FDCWD, argv[0], argv[i], argv[i], opt_R); if(ret != 0) return ret; } }