Annotation of researchv9/cmd/umount.c, revision 1.1.1.1

1.1       root        1: static char *sccsid = "@(#)umount.c    4.3 (Berkeley) 10/15/80";
                      2: #include <stdio.h>
                      3: #include <fstab.h>
                      4: #include <errno.h>
                      5: extern int errno;
                      6: /*
                      7:  * umount
                      8:  */
                      9: 
                     10: #define        NMOUNT  16
                     11: #define        NAMSIZ  32
                     12: 
                     13: struct mtab {
                     14:        char    file[NAMSIZ];
                     15:        char    spec[NAMSIZ];
                     16: } mtab[NMOUNT];
                     17: 
                     18: main(argc, argv)
                     19: char **argv;
                     20: {
                     21:        register struct mtab *mp;
                     22:        register char *p1, *p2;
                     23:        int mf;
                     24: 
                     25:        sync();
                     26:        mf = open("/etc/mtab", 0);
                     27:        read(mf, (char *)mtab, NMOUNT*2*NAMSIZ);
                     28:        if(argc != 2) {
                     29:                fprintf(stderr, "usage: umount [filsys] [-a]\n");
                     30:                exit(1);
                     31:        }
                     32:        if (strcmp(argv[1], "-a") == 0){
                     33:                if (setfsent() == 0)
                     34:                        perror(FSTAB), exit(1);
                     35:                umountall();
                     36:                endfsent();
                     37:        } else {
                     38:                int     back;
                     39:                if (back = umountfs(argv[1])){
                     40:                        if (back < 0)
                     41:                                perror(argv[1]);
                     42:                        exit(1);
                     43:                }
                     44:        }
                     45:        exit(0);
                     46: }
                     47: /*
                     48:  *     It is important to unmount the files in
                     49:  *     reverse! order from the order they were mounted,
                     50:  *     so that file systems mounted as children to other
                     51:  *     file systems get removed in the right order.
                     52:  */
                     53: umountall()
                     54: {
                     55:        struct  fstab   fs;
                     56:        struct  fstab   *fsp;
                     57:        if ( (fsp = getfsent()) == 0)
                     58:                return;
                     59:        fs = *fsp;      /* save info locally; it is static from getfsent() */
                     60:        umountall();
                     61:        if (strcmp(fs.fs_file, "/") == 0)
                     62:                return;
                     63:        if (strcmp(fs.fs_type, FSTAB_RW) &&
                     64:            strcmp(fs.fs_type, FSTAB_RO))
                     65:                return;
                     66:        if (umountfs(fs.fs_spec) < 0
                     67:        &&  errno != EINVAL)    /* wasn't mounted, don't babble */
                     68:                fprintf(stdout, "can't unmount %s\n", fs.fs_spec);
                     69:        fflush(stdout);
                     70: }
                     71: 
                     72: int umountfs(name)
                     73:        char    *name;
                     74: {
                     75:        register        char    *p1, *p2;
                     76:        register        struct  mtab    *mp;
                     77:        int     mf;
                     78: 
                     79:        if (umount(name) < 0)
                     80:                return(-1);
                     81:        p1 = name;
                     82:        while(*p1++)
                     83:                ;
                     84:        p1--;
                     85:        while(*--p1 == '/')
                     86:                *p1 = '\0';
                     87:        while(p1 > name && *--p1 != '/')
                     88:                ;
                     89:        if(*p1 == '/')
                     90:                p1++;
                     91:        name = p1;
                     92:        for (mp = mtab; mp < &mtab[NMOUNT]; mp++) {
                     93:                p1 = name;
                     94:                p2 = &mp->spec[0];
                     95:                while (*p1++ == *p2)
                     96:                        if (*p2++ == 0) {
                     97:                                for (p1 = mp->file; p1 < &mp->file[NAMSIZ*2];)
                     98:                                        *p1++ = 0;
                     99:                                mp = &mtab[NMOUNT];
                    100:                                while ((--mp)->file[0] == 0);
                    101:                                mf = creat("/etc/mtab", 0644);
                    102:                                write(mf, (char *)mtab, (mp-mtab+1)*2*NAMSIZ);
                    103:                                return(0);
                    104:                        }
                    105:        }
                    106:        printf("%s not in mount table\n", name);
                    107:        return(1);
                    108: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.