commit: 195ac0be1ad30429cad0912da4a5d8268db9fa0b
parent c018f3aba6d3173301073ac0764f6fecc4377f13
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date: Mon, 25 Mar 2024 04:09:55 +0100
cmd/df: Add support for -t option
Also moves the mnt_type-based filtering before statvfs to avoid useless calls (and useless errors).
Diffstat:
M | cmd/df.1 | 14 | ++++++++++++-- |
M | cmd/df.c | 81 | ++++++++++++++++++++++++++++++++++++++++++++++++++----------------------------- |
2 files changed, 63 insertions(+), 32 deletions(-)
diff --git a/cmd/df.1 b/cmd/df.1
@@ -10,6 +10,7 @@
.Sh SYNOPSIS
.Nm
.Op Fl ahlPkT
+.Op Fl t Ar mnt_type
.Op Fl x Ar mnt_type
.Sh DESCRIPTION
.Nm
@@ -38,11 +39,18 @@ is also passed, disables
.Fl h .
.It Fl T
Print filesystem type right after the Filesystem column.
+.It Fl t Ar mnt_type
+Include only selected filesystems matching
+.Ar mnt_type .
+For example
+.Qq nfs4 .
+Option can be passed multiple times to include multiple types.
.It Fl x Ar mnt_type
Exclude filesystems types matching
.Ar mnt_type .
For example
.Qq nfs4 .
+Option can be passed multiple times to exclude multiple types.
.El
.Sh EXIT STATUS
.Ex -std
@@ -53,9 +61,11 @@ For example
lacks getting a list of filesystems as argument to be compliant with the
.St -p1003.1-2008
specification.
-.Fl l
+.Pp
+.Fl l ,
+.Fl t
and
.Fl x
-are GNU extensions.
+are extensions inspired by other implementations such as FreeBSD and GNU.
.Sh AUTHORS
.An Haelwenn (lanodan) Monnier Aq Mt contact+utils@hacktivis.me
diff --git a/cmd/df.c b/cmd/df.c
@@ -41,12 +41,16 @@ int
main(int argc, char *argv[])
{
bool opt_P = false, opt_h = false, opt_a = false, opt_T = false, opt_l = false;
+ int fs_col_width = 20;
+
size_t excluded_count = 0;
- int fs_col_width = 20;
char *excluded[4096];
+ size_t only_count = 0;
+ char *only[4096];
+
int c = EOF;
- while((c = getopt(argc, argv, ":ahlPkTx:")) != EOF)
+ while((c = getopt(argc, argv, ":ahlPkTt:x:")) != EOF)
{
switch(c)
{
@@ -71,6 +75,9 @@ main(int argc, char *argv[])
case 'T':
opt_T = true;
break;
+ case 't':
+ only[only_count++] = optarg;
+ break;
case 'x':
excluded[excluded_count++] = optarg;
break;
@@ -130,6 +137,48 @@ main(int argc, char *argv[])
struct mntent *mntent = getmntent(mounted);
if(mntent == NULL) break;
+ 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;
+ }
+
+ if(only_count > 0)
+ {
+ bool include = false;
+
+ for(size_t i = 0; i < only_count; i++)
+ if(strcmp(only[i], mntent->mnt_type) == 0)
+ {
+ include = true;
+ break;
+ }
+
+ if(!include) continue;
+ }
+
+ if(opt_l)
+ {
+ bool remote = false;
+
+ for(size_t i = 0; i < sizeof(net_fs_list) / sizeof(char *); i++)
+ if(strcmp(net_fs_list[i], mntent->mnt_type) == 0)
+ {
+ remote = true;
+ break;
+ }
+
+ if(remote) continue;
+ }
+
// Note: musl prior to 1.2.5 has broken getmntent when octal sequences and carriage return is used
// https://git.musl-libc.org/cgit/musl/commit/src/misc/mntent.c?id=f314e133929b6379eccc632bef32eaebb66a7335
// https://git.musl-libc.org/cgit/musl/commit/src/misc/mntent.c?id=ee1d39bc1573c1ae49ee6b658938b56bbef95a6c
@@ -184,34 +233,6 @@ main(int argc, char *argv[])
}
}
- 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;
- }
-
- if(opt_l)
- {
- bool remote = false;
-
- for(size_t i = 0; i < sizeof(net_fs_list) / sizeof(char *); i++)
- if(strcmp(net_fs_list[i], mntent->mnt_type) == 0)
- {
- remote = true;
- break;
- }
-
- if(remote) continue;
- }
-
// Needs to be done after calling statvfs(3) and stat(3)
static_escape(mntent->mnt_fsname);
static_escape(mntent->mnt_dir);