BSD 4 release
[unix-history] / usr / src / cmd / umount.c
index 008c2d1..a242280 100644 (file)
@@ -1,3 +1,10 @@
+static char *sccsid = "@(#)umount.c    4.3 (Berkeley) 10/15/80";
+#include <stdio.h>
+#include <fstab.h>
+/*
+ * umount
+ */
+
 #define        NMOUNT  16
 #define        NAMSIZ  32
 
 #define        NMOUNT  16
 #define        NAMSIZ  32
 
@@ -20,23 +27,70 @@ char **argv;
                printf("arg count\n");
                return(1);
        }
                printf("arg count\n");
                return(1);
        }
-       if (umount(argv[1]) < 0) {
-               perror("umount");
-               return(1);
+       if (strcmp(argv[1], "-a") == 0){
+               if (setfsent() == 0)
+                       perror(FSTAB), exit(1);
+               umountall();
+               endfsent();
+       } else {
+               int     back;
+               if (back = umountfs(argv[1])){
+                       if (back < 0)
+                               perror("umount");
+                       exit(1);
+               }
+       }
+       exit(0);
+}
+/*
+ *     It is important to unmount the files in
+ *     reverse! order from the order they were mounted,
+ *     so that file systems mounted as children to other
+ *     file systems get removed in the right order.
+ */
+umountall()
+{
+       struct  fstab   fs;
+       struct  fstab   *fsp;
+       if ( (fsp = getfsent()) == 0)
+               return;
+       fs = *fsp;      /* save info locally; it is static from getfsent() */
+       umountall();
+       if (strcmp(fs.fs_file, "/") == 0)
+               return;
+       if (strcmp(fs.fs_type, FSTAB_RW) &&
+           strcmp(fs.fs_type, FSTAB_RO))
+               return;
+       if (umountfs(fs.fs_spec) < 0)
+               fprintf(stdout, "Unmount of special file %s FAILED\n", fs.fs_spec);
+       else
+               fprintf(stdout, "Unmounted special file %s\n", fs.fs_spec);
+       fflush(stdout);
+}
+
+int umountfs(name)
+       char    *name;
+{
+       register        char    *p1, *p2;
+       register        struct  mtab    *mp;
+       int     mf;
+
+       if (umount(name) < 0) {
+               return(-1);
        }
        }
-       p1 = argv[1];
+       p1 = name;
        while(*p1++)
                ;
        p1--;
        while(*--p1 == '/')
                *p1 = '\0';
        while(*p1++)
                ;
        p1--;
        while(*--p1 == '/')
                *p1 = '\0';
-       while(p1 > argv[1] && *--p1 != '/')
+       while(p1 > name && *--p1 != '/')
                ;
        if(*p1 == '/')
                p1++;
                ;
        if(*p1 == '/')
                p1++;
-       argv[1] = p1;
+       name = p1;
        for (mp = mtab; mp < &mtab[NMOUNT]; mp++) {
        for (mp = mtab; mp < &mtab[NMOUNT]; mp++) {
-               p1 = argv[1];
+               p1 = name;
                p2 = &mp->spec[0];
                while (*p1++ == *p2)
                        if (*p2++ == 0) {
                p2 = &mp->spec[0];
                while (*p1++ == *p2)
                        if (*p2++ == 0) {
@@ -49,6 +103,6 @@ char **argv;
                                return(0);
                        }
        }
                                return(0);
                        }
        }
-       printf("%s not in mount table\n", argv[1]);
+       printf("%s not in mount table\n", name);
        return(1);
 }
        return(1);
 }