logo

utils-std

Collection of commonly available Unix tools
commit: d63fe3715f147d03848136380ffcf4b757f818af
parent 653994e94021bd28b942697a6a332d1899582f0c
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date:   Sat,  9 Mar 2024 15:28:30 +0100

cmd/df: Add -T and -x options

Diffstat:

Mcmd/df.112+++++++++++-
Mcmd/df.c73+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--------------
2 files changed, 70 insertions(+), 15 deletions(-)

diff --git a/cmd/df.1 b/cmd/df.1 @@ -9,7 +9,8 @@ .Nd display mounted filesystems usage .Sh SYNOPSIS .Nm -.Op Fl ahPk +.Op Fl ahPkT +.Op Fl x Ar mnt_type .Sh DESCRIPTION .Nm displays the current usage and mountpoints of mounted filesystems in a space-separated list containing: @@ -33,6 +34,13 @@ Forces printing in blocks of 1024. .Fl k is also passed, disables .Fl h . +.It Fl T +Print filesystem type right after the Filesystem column. +.It Fl x Ar mnt_type +Exclude filesystems types matching +.Ar mnt_type . +For example +.Qq nfs4 . .El .Sh EXIT STATUS .Ex -std @@ -43,5 +51,7 @@ is also passed, disables lacks getting a list of filesystems as argument to be compliant with the .St -p1003.1-2008 specification. +.Fl x +is a GNU extension. .Sh AUTHORS .An Haelwenn (lanodan) Monnier Aq Mt contact@hacktivis.me diff --git a/cmd/df.c b/cmd/df.c @@ -31,10 +31,12 @@ static_escape(char *str) int main(int argc, char *argv[]) { - bool opt_P = false, opt_h = false, opt_a = false; + bool opt_P = false, opt_h = false, opt_a = false, opt_T = false; + size_t excluded_count = 0; + char *excluded[4096]; int c = EOF; - while((c = getopt(argc, argv, ":ahPk")) != EOF) + while((c = getopt(argc, argv, ":ahPkTx:")) != EOF) { switch(c) { @@ -52,6 +54,12 @@ main(int argc, char *argv[]) case 'h': opt_h = true; break; + case 'T': + opt_T = true; + break; + case 'x': + excluded[excluded_count++] = optarg; + break; case ':': fprintf(stderr, "df: Error: Missing operand for option: '-%c'\n", optopt); return 1; @@ -61,17 +69,34 @@ main(int argc, char *argv[]) } } + if(opt_T && opt_P) + { + fprintf(stderr, "df: Options -T and -P are incompatible\n"); + return 1; + } + argc -= optind; argv += optind; + // Begin: Print header + + printf("Filesystem "); + + if(opt_T) printf("Type "); + if(forced_bsize != 0) - { - printf("Filesystem %zd-blocks Used Available Capacity Mounted on\n", forced_bsize); - } + printf("%zd-blocks ", forced_bsize); else - { - printf("Filesystem Total Used Available Use%% Mountpoint\n"); - } + printf("Total "); + + printf("Used Available "); + + if(opt_P) + printf("Capacity Mounted on\n"); + else + printf("Used%% Mountpoint\n"); + + // End: Print header FILE *mounted = setmntent(MOUNTED, "r"); if(mounted == NULL) @@ -129,12 +154,27 @@ main(int argc, char *argv[]) if(dupe) continue; if(devices_found >= 4096) - fprintf(stderr, "df: Warning: Reached maximum amount of devices which can be deduplicated\n"); + fprintf(stderr, + "df: Warning: Reached maximum amount of devices which can be deduplicated\n"); devices[devices_found++] = file_stats.st_dev; } } + if(excluded_count > 0) + { + bool exclude = false; + + for(size_t i = 0; i < excluded_count; i++) + if(strcmp(excluded[i], mntent->mnt_type) == 0) + { + exclude = true; + break; + } + + if(exclude) continue; + } + // Needs to be done after calling statvfs(3) and stat(3) static_escape(mntent->mnt_fsname); static_escape(mntent->mnt_dir); @@ -157,6 +197,10 @@ main(int argc, char *argv[]) used /= forced_bsize; } + printf("%s ", mntent->mnt_fsname); + + if(opt_T) printf("%s ", mntent->mnt_type); + if(opt_h && !opt_P) { struct si_scale total_scl = dtosi(total, true); @@ -164,8 +208,7 @@ main(int argc, char *argv[]) struct si_scale free_scl = dtosi(free, true); // clang-format off - printf("%s %.2f%s %.2f%s %.2f%s %zd%% %s\n", - mntent->mnt_fsname, + printf("%.2f%s %.2f%s %.2f%s %zd%% %s\n", total_scl.number, total_scl.prefix, used_scl.number, used_scl.prefix, free_scl.number, free_scl.prefix, @@ -176,13 +219,15 @@ main(int argc, char *argv[]) } else { - printf("%s %zd %zd %zd %zd%% %s\n", - mntent->mnt_fsname, + // clang-format off + printf("%zd %zd %zd %zd%% %s\n", total, used, free, percent, - mntent->mnt_dir); + mntent->mnt_dir + ); + // clang-format on } }