commit: bf37254b6d8190d852e5c89eda28c579f810ccd4
parent 958ed3917a9baad63a296dbe1067393afa32fe56
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date: Mon, 2 Sep 2024 03:21:20 +0200
cmd/df: add support for -i option
Diffstat:
M | cmd/df.1 | 5 | ++++- |
M | cmd/df.c | 71 | ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++----------- |
2 files changed, 64 insertions(+), 12 deletions(-)
diff --git a/cmd/df.1 b/cmd/df.1
@@ -9,7 +9,7 @@
.Nd display mounted filesystems usage
.Sh SYNOPSIS
.Nm
-.Op Fl ahlPkT
+.Op Fl ahilPkT
.Op Fl t Ar mnt_type
.Op Fl x Ar mnt_type
.Op Ar file...
@@ -37,6 +37,8 @@ Display all mounted filesystems, otherwise
deduplicates filesystems and skips ones with a blocksize of zero.
.It Fl h
Print human readable sizes.
+.It Fl i
+Print inode usage statistics.
.It Fl l
Exclude known remote filesystems.
.It Fl P
@@ -71,6 +73,7 @@ should be compliant with the
IEEE Std 1003.1-2024 (“POSIX.1”)
specification.
.Pp
+.Fl i ,
.Fl l ,
.Fl t
and
diff --git a/cmd/df.c b/cmd/df.c
@@ -41,7 +41,7 @@ static_escape(char *str)
int
main(int argc, char *argv[])
{
- bool opt_P = false, opt_h = false, opt_a = false, opt_T = false, opt_l = false;
+ bool opt_P = false, opt_h = false, opt_a = false, opt_T = false, opt_l = false, opt_i = false;
int fs_col_width = 20;
size_t excluded_count = 0;
@@ -51,7 +51,7 @@ main(int argc, char *argv[])
char *only[4096];
int c = -1;
- while((c = getopt(argc, argv, ":ahlPkTt:x:")) != -1)
+ while((c = getopt(argc, argv, ":ahilPkTt:x:")) != -1)
{
switch(c)
{
@@ -70,6 +70,9 @@ main(int argc, char *argv[])
opt_h = true;
fs_col_width = 30;
break;
+ case 'i':
+ opt_i = true;
+ break;
case 'l':
opt_l = true;
break;
@@ -145,18 +148,29 @@ main(int argc, char *argv[])
if(opt_T) printf("%*s ", col_width, "Type");
- if(forced_bsize != 0)
- printf("%zd-blocks ", forced_bsize);
- else
- printf("%*s ", col_width, "Total");
-
- printf("%*s %*s ", col_width, "Used", col_width, "Available");
+ if(opt_i)
+ {
+ printf("%*s %*s %*s ", col_width, "Inodes", col_width, "Iused", col_width, "IFree");
- if(opt_P)
- printf("Capacity Mounted on\n");
+ if(opt_P)
+ printf("IUse%% Mounted on\n");
+ else
+ printf("IUse%% Mountpoint\n");
+ }
else
- printf("Use%% Mountpoint\n");
+ {
+ if(forced_bsize != 0)
+ printf("%zd-blocks ", forced_bsize);
+ else
+ printf("%*s ", col_width, "Total");
+ printf("%*s %*s ", col_width, "Used", col_width, "Available");
+
+ if(opt_P)
+ printf("Capacity Mounted on\n");
+ else
+ printf("Use%% Mountpoint\n");
+ }
// End: Print header
assert(errno == 0);
@@ -300,6 +314,41 @@ main(int argc, char *argv[])
static_escape(mntent->mnt_fsname);
static_escape(mntent->mnt_dir);
+ if(opt_i)
+ {
+ fsfilcnt_t used = stats.f_files - stats.f_ffree;
+ fsfilcnt_t percent = (used / stats.f_files) * 100;
+
+ printf("%-*s ", fs_col_width, mntent->mnt_fsname);
+
+ if(opt_T) printf("%*s ", col_width, mntent->mnt_type);
+
+ if(opt_h && !opt_P)
+ {
+ struct si_scale total_scl = dtosi(stats.f_files, false);
+ struct si_scale used_scl = dtosi(used, false);
+ struct si_scale free_scl = dtosi(stats.f_ffree, false);
+
+ int width_num = tty_out ? col_width - 1 : 0;
+ int width_pre = tty_out ? 1 : 0;
+
+ printf("%*.2f%-*s ", width_num, total_scl.number, width_pre, total_scl.prefix);
+ printf("%*.2f%-*s ", width_num, used_scl.number, width_pre, used_scl.prefix);
+ printf("%*.2f%-*s ", width_num, free_scl.number, width_pre, free_scl.prefix);
+ }
+ else
+ {
+ printf("%*zd ", col_width, stats.f_files);
+ printf("%*zd ", col_width, used);
+ printf("%*zd ", col_width, stats.f_ffree);
+ }
+
+ printf("%*zd%% ", tty_out ? 4 : 0, percent);
+ printf("%s\n", mntent->mnt_dir);
+
+ continue;
+ }
+
off_t percent = 0;
off_t total = stats.f_frsize * (stats.f_blocks != 0 ? stats.f_blocks : 1);
off_t free = stats.f_bfree * (stats.f_bsize != 0 ? stats.f_bsize : 1);