386BSD 0.1 development
[unix-history] / usr / src / bin / df / df.c
index df073ab..1ed82c7 100644 (file)
@@ -1,28 +1,44 @@
 /*
 /*
- * 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.
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. All advertising materials mentioning features or use of this software
+ *    must display the following acknowledgement:
+ *     This product includes software developed by the University of
+ *     California, Berkeley and its contributors.
+ * 4. Neither the name of the University nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
  */
 
 #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.18 (Berkeley) %G%";
+static char sccsid[] = "@(#)df.c       5.24 (Berkeley) 3/6/91";
 #endif /* not lint */
 
 /*
 #endif /* not lint */
 
 /*
@@ -31,30 +47,30 @@ static char sccsid[] = "@(#)df.c    5.18 (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>
 
 char   *getmntpt();
 #include <unistd.h>
 
 char   *getmntpt();
+void   ufs_df(), prtstat();
 int    iflag, kflag, nflag;
 int    iflag, kflag, nflag;
-#ifdef COMPAT_43
-int    oflag;
-#endif /* COMPAT_43 */
+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 width, maxwidth, 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, "ikon")) != EOF)
+       while ((ch = getopt(argc, argv, "ikn")) != EOF)
                switch(ch) {
                case 'i':
                        iflag = 1;
                switch(ch) {
                case 'i':
                        iflag = 1;
@@ -65,11 +81,6 @@ main(argc, argv)
                case 'n':
                        nflag = 1;
                        break;
                case 'n':
                        nflag = 1;
                        break;
-#ifdef COMPAT_43
-               case 'o':
-                       oflag = 1;
-                       break;
-#endif /* COMPAT_43 */
                case '?':
                default:
                        fprintf(stderr,
                case '?':
                default:
                        fprintf(stderr,
@@ -86,12 +97,6 @@ main(argc, argv)
                if (width > maxwidth)
                        maxwidth = width;
        }
                if (width > maxwidth)
                        maxwidth = width;
        }
-#ifdef COMPAT_43
-       if (oflag) {
-               olddf(argv, maxwidth);
-               exit(0);
-       }
-#endif /* COMPAT_43 */
        if (!*argv) {
                mntsize = getmntinfo(&mntbuf, (nflag ? MNT_NOWAIT : MNT_WAIT));
                for (i = 0; i < mntsize; i++)
        if (!*argv) {
                mntsize = getmntinfo(&mntbuf, (nflag ? MNT_NOWAIT : MNT_WAIT));
                for (i = 0; i < mntsize; i++)
@@ -106,13 +111,24 @@ 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) {
                        if ((mntpt = getmntpt(*argv)) == 0) {
                } else if ((stbuf.st_mode & S_IFMT) == S_IFBLK) {
                        if ((mntpt = getmntpt(*argv)) == 0) {
-                               mntpt = mktemp("/tmp/df.XXXXXX");
+                               mntpt = mktemp(strdup("/tmp/df.XXXXXX"));
                                mdev.fspec = *argv;
                                mdev.fspec = *argv;
-                               if (!mkdir(mntpt) &&
-                                   !mount(MOUNT_UFS, mntpt, MNT_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';
                                        prtstat(&statfsbuf, maxwidth);
                                } else
                                        statfsbuf.f_mntonname[0] = '\0';
                                        prtstat(&statfsbuf, maxwidth);
                                } else
@@ -137,7 +153,7 @@ main(argc, argv)
                        maxwidth = strlen(statfsbuf.f_mntfromname) + 1;
                prtstat(&statfsbuf, maxwidth);
        }
                        maxwidth = strlen(statfsbuf.f_mntfromname) + 1;
                prtstat(&statfsbuf, maxwidth);
        }
-       exit(0);
+       return (0);
 }
 
 char *
 }
 
 char *
@@ -158,6 +174,7 @@ getmntpt(name)
 /*
  * Print out status about a filesystem.
  */
 /*
  * Print out status about a filesystem.
  */
+void
 prtstat(sfsp, maxwidth)
        register struct statfs *sfsp;
        long maxwidth;
 prtstat(sfsp, maxwidth)
        register struct statfs *sfsp;
        long maxwidth;
@@ -194,7 +211,6 @@ prtstat(sfsp, maxwidth)
        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.
@@ -203,8 +219,6 @@ prtstat(sfsp, maxwidth)
 #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];
@@ -212,70 +226,22 @@ union {
 #define sblock sb.iu_fs
 
 int    fi;
 #define sblock sb.iu_fs
 
 int    fi;
-char   *strcpy();
-
-olddf(argv, maxwidth)
-       char *argv[];
-       long maxwidth;
-{
-       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, maxwidth);
-               }
-               (void)endfsent();
-               exit(0);
-       }
-       while (*argv)
-               dfree(*argv++, 0, maxwidth);
-       exit(0);
-}
+int    bread();
 
 
-dfree(file, infsent, maxwidth)
+void
+ufs_df(file, maxwidth)
        char *file;
        char *file;
-       int infsent;
        long maxwidth;
 {
        extern int errno;
        long maxwidth;
 {
        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;
@@ -310,9 +276,11 @@ found:
 
 long lseek();
 
 
 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;
@@ -328,4 +296,3 @@ bread(off, buf, cnt)
        }
        return (1);
 }
        }
        return (1);
 }
-#endif /* COMPAT_43 */