lost the SCCS id's...
[unix-history] / usr / src / bin / df / df.c
index bfb7684..3227804 100644 (file)
@@ -1,28 +1,18 @@
 /*
 /*
- * Copyright (c) 1980 The Regents of the University of California.
+ * Copyright (c) 1980, 1990 The Regents of the University of California.
  * All rights reserved.
  *
  * All rights reserved.
  *
- * Redistribution and use in source and binary forms are permitted
- * provided that the above copyright notice and this paragraph are
- * duplicated in all such forms and that any documentation,
- * advertising materials, and other materials related to such
- * distribution and use acknowledge that the software was developed
- * by the University of California, Berkeley.  The name of the
- * University may not be used to endorse or promote products derived
- * from this software without specific prior written permission.
- * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
- * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
- * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
+ * %sccs.include.redist.c%
  */
 
 #ifndef lint
 char copyright[] =
  */
 
 #ifndef lint
 char copyright[] =
-"@(#) Copyright (c) 1980 The Regents of the University of California.\n\
+"@(#) Copyright (c) 1980, 1990 The Regents of the University of California.\n\
  All rights reserved.\n";
 #endif /* not lint */
 
 #ifndef lint
  All rights reserved.\n";
 #endif /* not lint */
 
 #ifndef lint
-static char sccsid[] = "@(#)df.c       5.11 (Berkeley) %G%";
+static char sccsid[] = "@(#)df.c       5.24 (Berkeley) %G%";
 #endif /* not lint */
 
 /*
 #endif /* not lint */
 
 /*
@@ -31,31 +21,30 @@ static char sccsid[] = "@(#)df.c    5.11 (Berkeley) %G%";
 #include <sys/param.h>
 #include <sys/stat.h>
 #include <sys/mount.h>
 #include <sys/param.h>
 #include <sys/stat.h>
 #include <sys/mount.h>
-#include <sys/file.h>
+#include <fcntl.h>
 #include <stdio.h>
 #include <stdio.h>
-#include <strings.h>
+#include <stdlib.h>
+#include <string.h>
 #include <unistd.h>
 #include <unistd.h>
-#include "pathnames.h"
 
 char   *getmntpt();
 
 char   *getmntpt();
-int    iflag, kflag;
-#ifdef COMPAT_43
-int    oflag;
-#endif /* COMPAT_43 */
+void   ufs_df(), prtstat();
+int    iflag, kflag, nflag;
+struct ufs_args mdev;
 
 
+int
 main(argc, argv)
        int argc;
        char **argv;
 {
        extern int errno, optind;
        int err, ch, i;
 main(argc, argv)
        int argc;
        char **argv;
 {
        extern int errno, optind;
        int err, ch, i;
-       long mntsize, getmntinfo();
+       long width, maxwidth, mntsize;
        char *mntpt, *mktemp();
        struct stat stbuf;
        struct statfs statfsbuf, *mntbuf;
        char *mntpt, *mktemp();
        struct stat stbuf;
        struct statfs statfsbuf, *mntbuf;
-       struct ufs_args mdev;
 
 
-       while ((ch = getopt(argc, argv, "iko")) != EOF)
+       while ((ch = getopt(argc, argv, "ikn")) != EOF)
                switch(ch) {
                case 'i':
                        iflag = 1;
                switch(ch) {
                case 'i':
                        iflag = 1;
@@ -63,35 +52,29 @@ main(argc, argv)
                case 'k':
                        kflag = 1;
                        break;
                case 'k':
                        kflag = 1;
                        break;
-#ifdef COMPAT_43
-               case 'o':
-                       oflag = 1;
+               case 'n':
+                       nflag = 1;
                        break;
                        break;
-#endif /* COMPAT_43 */
                case '?':
                default:
                        fprintf(stderr,
                case '?':
                default:
                        fprintf(stderr,
-                           "usage: df [-ik] [file | file_system ...]\n");
+                           "usage: df [-ikn] [file | file_system ...]\n");
                        exit(1);
                }
        argc -= optind;
        argv += optind;
 
                        exit(1);
                }
        argc -= optind;
        argv += optind;
 
-       printf("Filesystem  %s    used   avail capacity",
-           kflag ? "  kbytes" : "512-blks");
-       if (iflag)
-               printf(" iused   ifree  %%iused");
-       printf("  Mounted on\n");
-#ifdef COMPAT_43
-       if (oflag) {
-               olddf(argv);
-               exit(0);
+       mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
+       maxwidth = 0;
+       for (i = 0; i < mntsize; i++) {
+               width = strlen(mntbuf[i].f_mntfromname);
+               if (width > maxwidth)
+                       maxwidth = width;
        }
        }
-#endif /* COMPAT_43 */
        if (!*argv) {
        if (!*argv) {
-               mntsize = getmntinfo(&mntbuf);
+               mntsize = getmntinfo(&mntbuf, (nflag ? MNT_NOWAIT : MNT_WAIT));
                for (i = 0; i < mntsize; i++)
                for (i = 0; i < mntsize; i++)
-                       prtstat(&mntbuf[i]);
+                       prtstat(&mntbuf[i], maxwidth);
                exit(0);
        }
        for (; *argv; argv++) {
                exit(0);
        }
        for (; *argv; argv++) {
@@ -102,67 +85,49 @@ main(argc, argv)
                                    strerror(err));
                                continue;
                        }
                                    strerror(err));
                                continue;
                        }
+               } else if ((stbuf.st_mode & S_IFMT) == S_IFCHR) {
+                       ufs_df(*argv, maxwidth);
+                       continue;
                } else if ((stbuf.st_mode & S_IFMT) == S_IFBLK) {
                } else if ((stbuf.st_mode & S_IFMT) == S_IFBLK) {
-                       if ((mntpt = getmntpt(*argv)) == 0)
-                               mntpt = mktemp("/df.XXXXXX");
+                       if ((mntpt = getmntpt(*argv)) == 0) {
+                               mntpt = mktemp(strdup("/tmp/df.XXXXXX"));
                                mdev.fspec = *argv;
                                mdev.fspec = *argv;
-                               if (!mkdir(mntpt) &&
-                                   !mount(MOUNT_UFS, mntpt, M_RDONLY, &mdev) &&
-                                   !statfs(mntpt, &statfsbuf)) {
+                               if (mkdir(mntpt, DEFFILEMODE) != 0) {
+                                       fprintf(stderr, "df: %s: %s\n",
+                                           mntpt, strerror(errno));
+                                       continue;
+                               }
+                               if (mount(MOUNT_UFS, mntpt, MNT_RDONLY,
+                                   &mdev) != 0) {
+                                       ufs_df(*argv, maxwidth);
+                                       (void)rmdir(mntpt);
+                                       continue;
+                               } else if (statfs(mntpt, &statfsbuf)) {
                                        statfsbuf.f_mntonname[0] = '\0';
                                        statfsbuf.f_mntonname[0] = '\0';
-                                       prtstat(&statfsbuf);
+                                       prtstat(&statfsbuf, maxwidth);
                                } else
                                        fprintf(stderr, "df: %s: %s\n",
                                            *argv, strerror(errno));
                                } else
                                        fprintf(stderr, "df: %s: %s\n",
                                            *argv, strerror(errno));
-                               (void)umount(mntpt, MNT_NOFORCE);
+                               (void)unmount(mntpt, MNT_NOFORCE);
                                (void)rmdir(mntpt);
                                continue;
                                (void)rmdir(mntpt);
                                continue;
+                       }
                } else
                        mntpt = *argv;
                } else
                        mntpt = *argv;
+               /*
+                * Statfs does not take a `wait' flag, so we cannot
+                * implement nflag here.
+                */
                if (statfs(mntpt, &statfsbuf) < 0) {
                        fprintf(stderr,
                            "df: %s: %s\n", mntpt, strerror(errno));
                        continue;
                }
                if (statfs(mntpt, &statfsbuf) < 0) {
                        fprintf(stderr,
                            "df: %s: %s\n", mntpt, strerror(errno));
                        continue;
                }
-               prtstat(&statfsbuf);
-       }
-       exit(0);
-}
-
-long
-getmntinfo(mntbufp)
-       struct statfs **mntbufp;
-{
-       int i;
-       static long mntsize;
-       static struct statfs *mntbuf = 0;
-       char *malloc();
-
-       if (mntbuf) {
-               *mntbufp = mntbuf;
-               return (mntsize);
-       }
-       if ((mntsize = getfsstat((struct statfs *)0, 0)) < 0) {
-               perror("df");
-               exit(1);
+               if (argc == 1)
+                       maxwidth = strlen(statfsbuf.f_mntfromname) + 1;
+               prtstat(&statfsbuf, maxwidth);
        }
        }
-       mntbuf = 0;
-       do {
-               if (mntbuf)
-                       free((char *)mntbuf);
-               i = (mntsize + 1) * sizeof(struct statfs);
-               if ((mntbuf = (struct statfs *)malloc((unsigned)i)) == 0) {
-                       fprintf(stderr,
-                           "df: no space for mount table buffer\n");
-                       exit(1);
-               }
-               if ((mntsize = getfsstat(mntbuf, i)) < 0) {
-                       perror("df");
-                       exit(1);
-               }
-       } while (i == mntsize * sizeof(struct statfs));
-       *mntbufp = mntbuf;
-       return (mntsize);
+       return (0);
 }
 
 char *
 }
 
 char *
@@ -172,7 +137,7 @@ getmntpt(name)
        long mntsize, i;
        struct statfs *mntbuf;
 
        long mntsize, i;
        struct statfs *mntbuf;
 
-       mntsize = getmntinfo(&mntbuf);
+       mntsize = getmntinfo(&mntbuf, (nflag ? MNT_NOWAIT : MNT_WAIT));
        for (i = 0; i < mntsize; i++) {
                if (!strcmp(mntbuf[i].f_mntfromname, name))
                        return (mntbuf[i].f_mntonname);
        for (i = 0; i < mntsize; i++) {
                if (!strcmp(mntbuf[i].f_mntfromname, name))
                        return (mntbuf[i].f_mntonname);
@@ -183,12 +148,25 @@ getmntpt(name)
 /*
  * Print out status about a filesystem.
  */
 /*
  * Print out status about a filesystem.
  */
-prtstat(sfsp)
+void
+prtstat(sfsp, maxwidth)
        register struct statfs *sfsp;
        register struct statfs *sfsp;
+       long maxwidth;
 {
        long used, availblks, inodes;
 {
        long used, availblks, inodes;
+       static int timesthrough;
 
 
-       printf("%-12.12s", sfsp->f_mntfromname);
+       if (maxwidth < 11)
+               maxwidth = 11;
+       if (++timesthrough == 1) {
+               printf("%-*.*s%s    used   avail capacity",
+                   maxwidth, maxwidth, "Filesystem",
+                   kflag ? "  kbytes" : "512-blks");
+               if (iflag)
+                       printf(" iused   ifree  %%iused");
+               printf("  Mounted on\n");
+       }
+       printf("%-*.*s", maxwidth, maxwidth, sfsp->f_mntfromname);
        used = sfsp->f_blocks - sfsp->f_bfree;
        availblks = sfsp->f_bavail + used;
        printf("%8ld%8ld%8ld",
        used = sfsp->f_blocks - sfsp->f_bfree;
        availblks = sfsp->f_bavail + used;
        printf("%8ld%8ld%8ld",
@@ -207,7 +185,6 @@ prtstat(sfsp)
        printf("  %s\n", sfsp->f_mntonname);
 }
 
        printf("  %s\n", sfsp->f_mntonname);
 }
 
-#ifdef COMPAT_43
 /*
  * This code constitutes the old df code for extracting
  * information from filesystem superblocks.
 /*
  * This code constitutes the old df code for extracting
  * information from filesystem superblocks.
@@ -216,8 +193,6 @@ prtstat(sfsp)
 #include <errno.h>
 #include <fstab.h>
 
 #include <errno.h>
 #include <fstab.h>
 
-char   root[MAXPATHLEN];
-
 union {
        struct fs iu_fs;
        char dummy[SBSIZE];
 union {
        struct fs iu_fs;
        char dummy[SBSIZE];
@@ -225,68 +200,22 @@ union {
 #define sblock sb.iu_fs
 
 int    fi;
 #define sblock sb.iu_fs
 
 int    fi;
-char   *strcpy();
+int    bread();
 
 
-olddf(argv)
-       char *argv[];
-{
-       struct fstab *fsp;
-
-       sync();
-       if (!*argv) {
-               if (setfsent() == 0)
-                       perror(_PATH_FSTAB), exit(1);
-               while (fsp = getfsent()) {
-                       if (strcmp(fsp->fs_type, FSTAB_RW) &&
-                           strcmp(fsp->fs_type, FSTAB_RO) &&
-                           strcmp(fsp->fs_type, FSTAB_RQ))
-                               continue;
-                       if (root[0] == 0)
-                               (void) strcpy(root, fsp->fs_spec);
-                       dfree(fsp->fs_spec, 1);
-               }
-               (void)endfsent();
-               exit(0);
-       }
-       while (*argv)
-               dfree(*argv++, 0);
-       exit(0);
-}
-
-dfree(file, infsent)
+void
+ufs_df(file, maxwidth)
        char *file;
        char *file;
-       int infsent;
+       long maxwidth;
 {
        extern int errno;
 {
        extern int errno;
-       struct stat stbuf;
        struct statfs statfsbuf;
        register struct statfs *sfsp;
        struct statfs statfsbuf;
        register struct statfs *sfsp;
-       struct fstab *fsp;
        char *mntpt;
        char *mntpt;
+       static int synced;
 
 
-       if (stat(file, &stbuf) == 0 &&
-           (stbuf.st_mode&S_IFMT) != S_IFCHR &&
-           (stbuf.st_mode&S_IFMT) != S_IFBLK) {
-               if (infsent) {
-                       fprintf(stderr, "df: %s: screwy fstab entry\n", file);
-                       return;
-               }
-               (void)setfsent();
-               while (fsp = getfsent()) {
-                       struct stat stb;
+       if (synced++ == 0)
+               sync();
 
 
-                       if (stat(fsp->fs_spec, &stb) == 0 &&
-                           stb.st_rdev == stbuf.st_dev) {
-                               file = fsp->fs_spec;
-                               (void)endfsent();
-                               goto found;
-                       }
-               }
-               (void)endfsent();
-               fprintf(stderr, "df: %s: mounted on unknown device\n", file);
-               return;
-       }
-found:
        if ((fi = open(file, O_RDONLY)) < 0) {
                fprintf(stderr, "df: %s: %s\n", file, strerror(errno));
                return;
        if ((fi = open(file, O_RDONLY)) < 0) {
                fprintf(stderr, "df: %s: %s\n", file, strerror(errno));
                return;
@@ -315,15 +244,17 @@ found:
                mntpt = "";
        bcopy((caddr_t)mntpt, (caddr_t)&sfsp->f_mntonname[0], MNAMELEN);
        bcopy((caddr_t)file, (caddr_t)&sfsp->f_mntfromname[0], MNAMELEN);
                mntpt = "";
        bcopy((caddr_t)mntpt, (caddr_t)&sfsp->f_mntonname[0], MNAMELEN);
        bcopy((caddr_t)file, (caddr_t)&sfsp->f_mntfromname[0], MNAMELEN);
-       prtstat(sfsp);
+       prtstat(sfsp, maxwidth);
        (void) close(fi);
 }
 
 long lseek();
 
        (void) close(fi);
 }
 
 long lseek();
 
+int
 bread(off, buf, cnt)
        long off;
        char *buf;
 bread(off, buf, cnt)
        long off;
        char *buf;
+       int cnt;
 {
        int n;
        extern errno;
 {
        int n;
        extern errno;
@@ -339,4 +270,3 @@ bread(off, buf, cnt)
        }
        return (1);
 }
        }
        return (1);
 }
-#endif /* COMPAT_43 */