Re: df(1): formatting adjustments and -T support

2021-01-25 Thread Katherine Rohl
On Sat, 2021-01-23 at 20:37 +1100, Jonathan Gray wrote:
> On Fri, Jan 22, 2021 at 11:11:27PM -0600, Katherine Rohl wrote:
> > I noticed that large disk volumes cause problems with the formatting of
> > numerical columns in df(1), particularly when using -i. Here's a patch
> > that pads out their width a bit and raises the maximum width of
> > numerical columns before fields start running into each other.
> > 
> > I also added support for the -T argument, which is present in many
> > other df(1) implementations and shows the filesystem's type.
> 
> The type is already available in the output of mount(8).  Flags being
> available elsewhere isn't normally a good reason to add them here.

Hmm, that's fair. I was thinking about it increasing the
portability of scripts that rely on the -T option between BSDs
after running into that issue recently.

> 
> > 
> > POSIX-style -P output is not affected by this since it has a specific
> > format. The affected flags don't support -P anyway.
> > 
> > I believe this follows the KNF style (at least that's what my editor
> > says) but if there are any problems please let me know.
> 
> Your mail client has line wrapped the patch.  If you can disable that
> for future patches it would make them easier to apply.

Ah, apologies. I thought I had disabled this in my client.

> 
> > 
> > diff --git bin/df/df.1 bin/df/df.1
> > index 89dd51aac8d..4a9a549c492 100644
> > --- bin/df/df.1
> > +++ bin/df/df.1
> > @@ -100,6 +100,10 @@ with the possibly stale statistics that were
> > previously obtained.
> >  .It Fl P
> >  Print out information in a stricter format designed to be parsed
> >  by portable scripts.
> > +.It Fl T
> > +Include the filesystem type. This option is incompatible with the
> > +.Fl P
> > +option.
> >  .It Fl t Ar type
> >  Indicate the actions should only be taken on
> >  file systems of the specified
> > diff --git bin/df/df.c bin/df/df.c
> > index fd51f906f89..427d774e802 100644
> > --- bin/df/df.c
> > +++ bin/df/df.c
> > @@ -63,7 +63,7 @@ extern int e2fs_df(int, char *, struct statfs
> > *);
> >  extern int  ffs_df(int, char *, struct statfs *);
> >  static int  raw_df(char *, struct statfs *);
> >  
> > -inthflag, iflag, kflag, lflag, nflag, Pflag;
> > +inthflag, iflag, kflag, lflag, nflag, Pflag, Tflag;
> >  char   **typelist = NULL;
> >  
> >  int
> > @@ -79,7 +79,7 @@ main(int argc, char *argv[])
> > if (pledge("stdio rpath", NULL) == -1)
> > err(1, "pledge");
> >  
> > -   while ((ch = getopt(argc, argv, "hiklnPt:")) != -1)
> > +   while ((ch = getopt(argc, argv, "hiklnPTt:")) != -1)
> > switch (ch) {
> > case 'h':
> > hflag = 1;
> > @@ -106,14 +106,17 @@ main(int argc, char *argv[])
> > errx(1, "only one -t option may be
> > specified.");
> > maketypelist(optarg);
> > break;
> > +   case 'T':
> > +   Tflag = 1;
> > +   break;
> > default:
> > usage();
> > }
> > argc -= optind;
> > argv += optind;
> >  
> > -   if ((iflag || hflag) && Pflag) {
> > -   warnx("-h and -i are incompatible with -P");
> > +   if ((iflag || hflag || Tflag) && Pflag) {
> > +   warnx("-h, -i, and -T are incompatible with -P");
> > usage();
> > }
> >  
> > @@ -159,13 +162,17 @@ main(int argc, char *argv[])
> > }
> >  
> > if (mntsize) {
> > -   maxwidth = 11;
> > +   maxwidth = 13;
> > for (i = 0; i < mntsize; i++) {
> > width = strlen(mntbuf[i].f_mntfromname);
> > if (width > maxwidth)
> > maxwidth = width;
> > +   if (Tflag) {
> > +   width =
> > strlen(mntbuf[i].f_fstypename);
> > +   if (width > maxwidth)
> > +   maxwidth = width;
> > +   }
> > }
> > -
> > if (Pflag)
> > posixprint(mntbuf, mntsize, maxwidth);
> > else
> > @@ -189,6 +196,20 @@ getmntpt(char *name)
> > return (0);
> >  }
> >  
> > +char *
> > +getfsname(char *name)
> > +{
> > +   long mntsize, i;
> > +   struct statfs *mntbuf;
> > +
> > +   mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
> > +   for (i = 0; i < mntsize; i++) {
> > +   if (!strcmp(mntbuf[i].f_mntfromname, name))
> > +   return (mntbuf[i].f_fstypename);
> > +   }
> > +   return (0);
> > +}
> > +
> >  static enum { IN_LIST, NOT_IN_LIST } which;
> >  
> >  static int
> > @@ -319,20 +340,23 @@ prtstat(struct statfs *sfsp, int maxwidth, int
> > headerlen, int blocksize)
> > if (hflag)
> >   

Re: df(1): formatting adjustments and -T support

2021-01-23 Thread Jonathan Gray
On Fri, Jan 22, 2021 at 11:11:27PM -0600, Katherine Rohl wrote:
> I noticed that large disk volumes cause problems with the formatting of
> numerical columns in df(1), particularly when using -i. Here's a patch
> that pads out their width a bit and raises the maximum width of
> numerical columns before fields start running into each other.
> 
> I also added support for the -T argument, which is present in many
> other df(1) implementations and shows the filesystem's type.

The type is already available in the output of mount(8).  Flags being
available elsewhere isn't normally a good reason to add them here.

> 
> POSIX-style -P output is not affected by this since it has a specific
> format. The affected flags don't support -P anyway.
> 
> I believe this follows the KNF style (at least that's what my editor
> says) but if there are any problems please let me know.

Your mail client has line wrapped the patch.  If you can disable that
for future patches it would make them easier to apply.

> 
> diff --git bin/df/df.1 bin/df/df.1
> index 89dd51aac8d..4a9a549c492 100644
> --- bin/df/df.1
> +++ bin/df/df.1
> @@ -100,6 +100,10 @@ with the possibly stale statistics that were
> previously obtained.
>  .It Fl P
>  Print out information in a stricter format designed to be parsed
>  by portable scripts.
> +.It Fl T
> +Include the filesystem type. This option is incompatible with the
> +.Fl P
> +option.
>  .It Fl t Ar type
>  Indicate the actions should only be taken on
>  file systems of the specified
> diff --git bin/df/df.c bin/df/df.c
> index fd51f906f89..427d774e802 100644
> --- bin/df/df.c
> +++ bin/df/df.c
> @@ -63,7 +63,7 @@ extern int   e2fs_df(int, char *, struct statfs
> *);
>  extern intffs_df(int, char *, struct statfs *);
>  static intraw_df(char *, struct statfs *);
>  
> -int  hflag, iflag, kflag, lflag, nflag, Pflag;
> +int  hflag, iflag, kflag, lflag, nflag, Pflag, Tflag;
>  char **typelist = NULL;
>  
>  int
> @@ -79,7 +79,7 @@ main(int argc, char *argv[])
>   if (pledge("stdio rpath", NULL) == -1)
>   err(1, "pledge");
>  
> - while ((ch = getopt(argc, argv, "hiklnPt:")) != -1)
> + while ((ch = getopt(argc, argv, "hiklnPTt:")) != -1)
>   switch (ch) {
>   case 'h':
>   hflag = 1;
> @@ -106,14 +106,17 @@ main(int argc, char *argv[])
>   errx(1, "only one -t option may be
> specified.");
>   maketypelist(optarg);
>   break;
> + case 'T':
> + Tflag = 1;
> + break;
>   default:
>   usage();
>   }
>   argc -= optind;
>   argv += optind;
>  
> - if ((iflag || hflag) && Pflag) {
> - warnx("-h and -i are incompatible with -P");
> + if ((iflag || hflag || Tflag) && Pflag) {
> + warnx("-h, -i, and -T are incompatible with -P");
>   usage();
>   }
>  
> @@ -159,13 +162,17 @@ main(int argc, char *argv[])
>   }
>  
>   if (mntsize) {
> - maxwidth = 11;
> + maxwidth = 13;
>   for (i = 0; i < mntsize; i++) {
>   width = strlen(mntbuf[i].f_mntfromname);
>   if (width > maxwidth)
>   maxwidth = width;
> + if (Tflag) {
> + width =
> strlen(mntbuf[i].f_fstypename);
> + if (width > maxwidth)
> + maxwidth = width;
> + }
>   }
> -
>   if (Pflag)
>   posixprint(mntbuf, mntsize, maxwidth);
>   else
> @@ -189,6 +196,20 @@ getmntpt(char *name)
>   return (0);
>  }
>  
> +char *
> +getfsname(char *name)
> +{
> + long mntsize, i;
> + struct statfs *mntbuf;
> +
> + mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
> + for (i = 0; i < mntsize; i++) {
> + if (!strcmp(mntbuf[i].f_mntfromname, name))
> + return (mntbuf[i].f_fstypename);
> + }
> + return (0);
> +}
> +
>  static enum { IN_LIST, NOT_IN_LIST } which;
>  
>  static int
> @@ -319,20 +340,23 @@ prtstat(struct statfs *sfsp, int maxwidth, int
> headerlen, int blocksize)
>   if (hflag)
>   prthuman(sfsp, used);
>   else
> - (void)printf(" %*llu %9llu %9lld", headerlen,
> + (void)printf(" %*llu %10llu %10lld", headerlen,
>   fsbtoblk(sfsp->f_blocks, sfsp->f_bsize,
> blocksize),
>   fsbtoblk(used, sfsp->f_bsize, blocksize),
>   fsbtoblk(sfsp->f_bavail, sfsp->f_bsize,
> blocksize));
>   (void)printf(" %5.0f%%",
>   availblks == 0 ? 100.0 : (double)used / (double)availblks
> * 100.0);
> + if (Tflag) {
> + (void)printf("%4s", sfsp->f_fstypename);
> + }
>   if (iflag) {
>   inodes = sfsp->f_files;
>  

df(1): formatting adjustments and -T support

2021-01-22 Thread Katherine Rohl
I noticed that large disk volumes cause problems with the formatting of
numerical columns in df(1), particularly when using -i. Here's a patch
that pads out their width a bit and raises the maximum width of
numerical columns before fields start running into each other.

I also added support for the -T argument, which is present in many
other df(1) implementations and shows the filesystem's type.

POSIX-style -P output is not affected by this since it has a specific
format. The affected flags don't support -P anyway.

I believe this follows the KNF style (at least that's what my editor
says) but if there are any problems please let me know.

diff --git bin/df/df.1 bin/df/df.1
index 89dd51aac8d..4a9a549c492 100644
--- bin/df/df.1
+++ bin/df/df.1
@@ -100,6 +100,10 @@ with the possibly stale statistics that were
previously obtained.
 .It Fl P
 Print out information in a stricter format designed to be parsed
 by portable scripts.
+.It Fl T
+Include the filesystem type. This option is incompatible with the
+.Fl P
+option.
 .It Fl t Ar type
 Indicate the actions should only be taken on
 file systems of the specified
diff --git bin/df/df.c bin/df/df.c
index fd51f906f89..427d774e802 100644
--- bin/df/df.c
+++ bin/df/df.c
@@ -63,7 +63,7 @@ extern int e2fs_df(int, char *, struct statfs
*);
 extern int  ffs_df(int, char *, struct statfs *);
 static int  raw_df(char *, struct statfs *);
 
-inthflag, iflag, kflag, lflag, nflag, Pflag;
+inthflag, iflag, kflag, lflag, nflag, Pflag, Tflag;
 char   **typelist = NULL;
 
 int
@@ -79,7 +79,7 @@ main(int argc, char *argv[])
if (pledge("stdio rpath", NULL) == -1)
err(1, "pledge");
 
-   while ((ch = getopt(argc, argv, "hiklnPt:")) != -1)
+   while ((ch = getopt(argc, argv, "hiklnPTt:")) != -1)
switch (ch) {
case 'h':
hflag = 1;
@@ -106,14 +106,17 @@ main(int argc, char *argv[])
errx(1, "only one -t option may be
specified.");
maketypelist(optarg);
break;
+   case 'T':
+   Tflag = 1;
+   break;
default:
usage();
}
argc -= optind;
argv += optind;
 
-   if ((iflag || hflag) && Pflag) {
-   warnx("-h and -i are incompatible with -P");
+   if ((iflag || hflag || Tflag) && Pflag) {
+   warnx("-h, -i, and -T are incompatible with -P");
usage();
}
 
@@ -159,13 +162,17 @@ main(int argc, char *argv[])
}
 
if (mntsize) {
-   maxwidth = 11;
+   maxwidth = 13;
for (i = 0; i < mntsize; i++) {
width = strlen(mntbuf[i].f_mntfromname);
if (width > maxwidth)
maxwidth = width;
+   if (Tflag) {
+   width =
strlen(mntbuf[i].f_fstypename);
+   if (width > maxwidth)
+   maxwidth = width;
+   }
}
-
if (Pflag)
posixprint(mntbuf, mntsize, maxwidth);
else
@@ -189,6 +196,20 @@ getmntpt(char *name)
return (0);
 }
 
+char *
+getfsname(char *name)
+{
+   long mntsize, i;
+   struct statfs *mntbuf;
+
+   mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
+   for (i = 0; i < mntsize; i++) {
+   if (!strcmp(mntbuf[i].f_mntfromname, name))
+   return (mntbuf[i].f_fstypename);
+   }
+   return (0);
+}
+
 static enum { IN_LIST, NOT_IN_LIST } which;
 
 static int
@@ -319,20 +340,23 @@ prtstat(struct statfs *sfsp, int maxwidth, int
headerlen, int blocksize)
if (hflag)
prthuman(sfsp, used);
else
-   (void)printf(" %*llu %9llu %9lld", headerlen,
+   (void)printf(" %*llu %10llu %10lld", headerlen,
fsbtoblk(sfsp->f_blocks, sfsp->f_bsize,
blocksize),
fsbtoblk(used, sfsp->f_bsize, blocksize),
fsbtoblk(sfsp->f_bavail, sfsp->f_bsize,
blocksize));
(void)printf(" %5.0f%%",
availblks == 0 ? 100.0 : (double)used / (double)availblks
* 100.0);
+   if (Tflag) {
+   (void)printf("%4s", sfsp->f_fstypename);
+   }
if (iflag) {
inodes = sfsp->f_files;
used = inodes - sfsp->f_ffree;
-   (void)printf(" %7llu %7llu %5.0f%% ", used, sfsp-
>f_ffree,
+   (void)printf("  %9llu %9llu %5.0f%% ", used, sfsp-
>f_ffree,
   inodes == 0 ? 100.0 : (double)used / (double)inodes
* 100.0);
} else
(void)printf("  ");
-   (void)printf("  %s\n", sfsp->f_mntonname);
+   (void)printf("%s\n", sfsp->f_mntonname);
 }
 
 /*
@@ -359,12 +383,14