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 *);
 
-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;
                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 @@ bsdprint(struct statfs *mntbuf, long mntsize, int
maxwidth)
                        headerlen = strlen(header);
                } else
                        header = getbsize(&headerlen, &blocksize);
-               (void)printf("%-*.*s %s      Used     Avail Capacity",
+               (void)printf("%-*.*s %s       Used      Avail
Capacity",
                             maxwidth, maxwidth, "Filesystem",
header);
        }
+       if (Tflag)
+               (void)printf("  Type  ");
        if (iflag)
-               (void)printf(" iused   ifree  %%iused");
-       (void)printf("  Mounted on\n");
+               (void)printf("    iused     ifree   %%iused ");
+       (void)printf("    Mounted on\n");
 
 
        for (i = 0; i < mntsize; i++)
@@ -455,7 +481,7 @@ static __dead void
 usage(void)
 {
        (void)fprintf(stderr,
-           "usage: %s [-hiklnP] [-t type] [[file | file_system]
...]\n",
+           "usage: %s [-hiklnPT] [-t type] [[file | file_system]
...]\n",
            getprogname());
        exit(1);
 }


Reply via email to