logo

utils-std

Collection of commonly available Unix tools git clone https://anongit.hacktivis.me/git/utils-std.git
commit: 0ff4ea00499ddff85d59330535d928e3c0a97bc4
parent 6462828c8a803e12e80be3706c577811fcca731c
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date:   Thu, 19 Sep 2024 19:42:06 +0200

cmd/df: unify error message formatting

Diffstat:

Mcmd/df.c35++++++++++++++++++++++++++---------
1 file changed, 26 insertions(+), 9 deletions(-)

diff --git a/cmd/df.c b/cmd/df.c @@ -29,6 +29,7 @@ static const char *net_fs_list[] = { // clang-format on blksize_t forced_bsize = 0; +const char *argv0 = "df"; // Replaces control characters and whitespaces with '?' in-place (no allocation) static void @@ -86,17 +87,17 @@ main(int argc, char *argv[]) excluded[excluded_count++] = optarg; break; case ':': - fprintf(stderr, "df: Error: Missing operand for option: '-%c'\n", optopt); + fprintf(stderr, "%s: error: Missing operand for option: '-%c'\n", argv0, optopt); return 1; case '?': - fprintf(stderr, "df: Error: Unrecognised option: '-%c'\n", optopt); + fprintf(stderr, "%s: error: Unrecognised option: '-%c'\n", argv0, optopt); return 1; } } if(opt_P && opt_T) { - fprintf(stderr, "df: Options -P and -T are incompatible\n"); + fprintf(stderr, "%s: error: Options -P and -T are incompatible\n", argv0); return 1; } @@ -123,7 +124,7 @@ main(int argc, char *argv[]) arg_devs = calloc(argc, sizeof(dev_t)); if(arg_devs == NULL) { - fprintf(stderr, "df: Failed to allocate arg_devs array: %s\n", strerror(errno)); + fprintf(stderr, "%s: error: Failed to allocate arg_devs array: %s\n", argv0, strerror(errno)); return 1; } } @@ -135,7 +136,11 @@ main(int argc, char *argv[]) struct stat file_stats; if(stat(argv[i], &file_stats) != 0) { - fprintf(stderr, "df: Error stat(\"%s\", _): %s\n", argv[i], strerror(errno)); + fprintf(stderr, + "%s: error: Failed getting status for file '%s': %s\n", + argv0, + argv[i], + strerror(errno)); goto error; } @@ -177,7 +182,10 @@ main(int argc, char *argv[]) FILE *mounted = setmntent(MOUNTED, "r"); if(mounted == NULL) { - fprintf(stderr, "df: Error opening setmntent(\"" MOUNTED "\", \"r\"): %s", strerror(errno)); + fprintf(stderr, + "%s: error: Failed opening setmntent(\"" MOUNTED "\", \"r\"): %s\n", + argv0, + strerror(errno)); goto error; } @@ -239,7 +247,11 @@ main(int argc, char *argv[]) { if(stat(mntent->mnt_dir, &file_stats) != 0) { - fprintf(stderr, "df: Warning stat(\"%s\", _): %s\n", mntent->mnt_dir, strerror(errno)); + fprintf(stderr, + "%s: warning: Failed getting status for file '%s': %s\n", + argv0, + mntent->mnt_dir, + strerror(errno)); errno = 0; } } @@ -273,7 +285,8 @@ main(int argc, char *argv[]) if(devices_found >= 4096) fprintf(stderr, - "df: Warning: Reached maximum amount of devices which can be deduplicated\n"); + "%s: warning: Reached maximum amount of devices which can be deduplicated\n", + argv0); devices[devices_found++] = file_stats.st_dev; } @@ -285,7 +298,11 @@ main(int argc, char *argv[]) struct statvfs stats; if(statvfs(mntent->mnt_dir, &stats) != 0) { - fprintf(stderr, "df: Warning: statvfs(\"%s\", _): %s\n", mntent->mnt_dir, strerror(errno)); + fprintf(stderr, + "%s: warning: Failed getting statistics for filesystem '%s': %s\n", + argv0, + mntent->mnt_dir, + strerror(errno)); errno = 0; static_escape(mntent->mnt_fsname);