ostensibly make it work with new signals; lots of reformatting done as well
[unix-history] / usr / src / sbin / umount / umount.c
index b97436e..197a8d5 100644 (file)
@@ -1,19 +1,17 @@
-static char *sccsid = "@(#)umount.c    4.4 (Berkeley) %G%";
-
-#include <stdio.h>
-#include <fstab.h>
+#ifndef lint
+static char *sccsid = "@(#)umount.c    4.8 (Berkeley) %G%";
+#endif
 
 /*
  * umount
  */
 
 /*
  * umount
  */
+#include <sys/param.h>
 
 
-#define        NMOUNT  16
-#define        NAMSIZ  32
+#include <stdio.h>
+#include <fstab.h>
+#include <mtab.h>
 
 
-struct mtab {
-       char    file[NAMSIZ];
-       char    spec[NAMSIZ];
-} mtab[NMOUNT];
+struct mtab mtab[NMOUNT];
 
 char   *rindex();
 int    vflag, all, errs;
 
 char   *rindex();
 int    vflag, all, errs;
@@ -31,12 +29,12 @@ main(argc, argv)
        mf = open("/etc/mtab", 0);
        read(mf, (char *)mtab, sizeof (mtab));
 again:
        mf = open("/etc/mtab", 0);
        read(mf, (char *)mtab, sizeof (mtab));
 again:
-       if (!strcmp(*argv, "-v")) {
+       if (argc > 0 && !strcmp(*argv, "-v")) {
                vflag++;
                argc--, argv++;
                goto again;
        }
                vflag++;
                argc--, argv++;
                goto again;
        }
-       if (!strcmp(*argv, "-a")) {
+       if (argc > 0 && !strcmp(*argv, "-a")) {
                all++;
                argc--, argv++;
                goto again;
                all++;
                argc--, argv++;
                goto again;
@@ -61,20 +59,61 @@ again:
 
 umountall()
 {
 
 umountall()
 {
-       struct fstab fs, *fsp;
+       struct fstab *fs, *allocfsent();
 
 
-       if ((fsp = getfsent()) == 0)
+       if ((fs = getfsent()) == 0)
                return;
                return;
-       fs = *fsp;      /* save info locally; it is static from getfsent() */
+       fs = allocfsent(fs);
        umountall();
        umountall();
-       if (strcmp(fs.fs_file, "/") == 0)
-               return;
-       if (strcmp(fs.fs_type, FSTAB_RW) && strcmp(fs.fs_type, FSTAB_RO))
+       if (strcmp(fs->fs_file, "/") == 0) {
+               freefsent(fs);
                return;
                return;
-       if (umountfs(fs.fs_spec) < 0) {
-               perror(fs.fs_spec);
+       }
+       if (strcmp(fs->fs_type, FSTAB_RW) &&
+           strcmp(fs->fs_type, FSTAB_RO) &&
+           strcmp(fs->fs_type, FSTAB_RQ)) {
+               freefsent(fs);
                return;
        }
                return;
        }
+       if (umountfs(fs->fs_spec) < 0)
+               perror(fs->fs_spec);
+       freefsent(fs);
+}
+
+struct fstab *
+allocfsent(fs)
+       register struct fstab *fs;
+{
+       register struct fstab *new;
+       register char *cp;
+       char *malloc();
+
+       new = (struct fstab *)malloc(sizeof (*fs));
+       cp = malloc(strlen(fs->fs_file) + 1);
+       strcpy(cp, fs->fs_file);
+       new->fs_file = cp;
+       cp = malloc(strlen(fs->fs_type) + 1);
+       strcpy(cp, fs->fs_type);
+       new->fs_type = cp;
+       cp = malloc(strlen(fs->fs_spec) + 1);
+       strcpy(cp, fs->fs_spec);
+       new->fs_spec = cp;
+       new->fs_passno = fs->fs_passno;
+       new->fs_freq = fs->fs_freq;
+       return (new);
+}
+
+freefsent(fs)
+       register struct fstab *fs;
+{
+
+       if (fs->fs_file)
+               free(fs->fs_file);
+       if (fs->fs_spec)
+               free(fs->fs_spec);
+       if (fs->fs_type)
+               free(fs->fs_type);
+       free((char *)fs);
 }
 
 struct mtab zeromtab;
 }
 
 struct mtab zeromtab;
@@ -85,7 +124,11 @@ umountfs(name)
        register char *p1, *p2;
        register struct mtab *mp;
        int mf;
        register char *p1, *p2;
        register struct mtab *mp;
        int mf;
+       struct fstab *fs;
 
 
+       fs = getfsfile(name);
+       if (fs != NULL)
+               name = fs->fs_spec;
        if (umount(name) < 0) {
                perror(name);
                return (0);
        if (umount(name) < 0) {
                perror(name);
                return (0);
@@ -97,11 +140,11 @@ umountfs(name)
        if (p1)
                name = p1 + 1;
        for (mp = mtab; mp < &mtab[NMOUNT]; mp++) {
        if (p1)
                name = p1 + 1;
        for (mp = mtab; mp < &mtab[NMOUNT]; mp++) {
-               if (strncmp(mp->spec, name, sizeof (mp->spec)))
+               if (strncmp(mp->m_dname, name, sizeof (mp->m_dname)))
                        continue;
                *mp = zeromtab;
                for (mp = &mtab[NMOUNT]; mp >= mtab; mp--)
                        continue;
                *mp = zeromtab;
                for (mp = &mtab[NMOUNT]; mp >= mtab; mp--)
-                       if (mp->file[0])
+                       if (mp->m_path[0])
                                break;
                mp++;
                mf = creat("/etc/mtab", 0644);
                                break;
                mp++;
                mf = creat("/etc/mtab", 0644);