1: /*
2: * Copyright (c) 1980 Regents of the University of California.
3: * All rights reserved. The Berkeley software License Agreement
4: * specifies the terms and conditions for redistribution.
5: */
6:
7: #ifndef lint
8: char copyright[] =
9: "@(#) Copyright (c) 1980 Regents of the University of California.\n\
10: All rights reserved.\n";
11: #endif not lint
12:
13: #ifndef lint
14: static char sccsid[] = "@(#)umount.c 5.1 (Berkeley) 5/28/85";
15: #endif not lint
16:
17: /*
18: * umount
19: */
20: #include <sys/param.h>
21:
22: #include <stdio.h>
23: #include <fstab.h>
24: #include <mtab.h>
25:
26: struct mtab mtab[NMOUNT];
27:
28: char *rindex();
29: int vflag, all, errs;
30:
31: main(argc, argv)
32: int argc;
33: char **argv;
34: {
35: register struct mtab *mp;
36: register char *p1, *p2;
37: int mf;
38:
39: argc--, argv++;
40: sync();
41: mf = open("/etc/mtab", 0);
42: read(mf, (char *)mtab, sizeof (mtab));
43: again:
44: if (argc > 0 && !strcmp(*argv, "-v")) {
45: vflag++;
46: argc--, argv++;
47: goto again;
48: }
49: if (argc > 0 && !strcmp(*argv, "-a")) {
50: all++;
51: argc--, argv++;
52: goto again;
53: }
54: if (argc == 0 && !all) {
55: fprintf(stderr, "Usage: umount [ -a ] [ -v ] [ dev ... ]\n");
56: exit(1);
57: }
58: if (all) {
59: if (setfsent() == 0)
60: perror(FSTAB), exit(1);
61: umountall();
62: exit(0);
63: }
64: while (argc > 0) {
65: if (umountfs(*argv++) == 0)
66: errs++;
67: argc--;
68: }
69: exit(errs);
70: }
71:
72: umountall()
73: {
74: struct fstab *fs, *allocfsent();
75:
76: if ((fs = getfsent()) == 0)
77: return;
78: fs = allocfsent(fs);
79: umountall();
80: if (strcmp(fs->fs_file, "/") == 0) {
81: freefsent(fs);
82: return;
83: }
84: if (strcmp(fs->fs_type, FSTAB_RW) &&
85: strcmp(fs->fs_type, FSTAB_RO) &&
86: strcmp(fs->fs_type, FSTAB_RQ)) {
87: freefsent(fs);
88: return;
89: }
90: if (umountfs(fs->fs_spec) < 0)
91: perror(fs->fs_spec);
92: freefsent(fs);
93: }
94:
95: struct fstab *
96: allocfsent(fs)
97: register struct fstab *fs;
98: {
99: register struct fstab *new;
100: register char *cp;
101: char *malloc();
102:
103: new = (struct fstab *)malloc(sizeof (*fs));
104: cp = malloc(strlen(fs->fs_file) + 1);
105: strcpy(cp, fs->fs_file);
106: new->fs_file = cp;
107: cp = malloc(strlen(fs->fs_type) + 1);
108: strcpy(cp, fs->fs_type);
109: new->fs_type = cp;
110: cp = malloc(strlen(fs->fs_spec) + 1);
111: strcpy(cp, fs->fs_spec);
112: new->fs_spec = cp;
113: new->fs_passno = fs->fs_passno;
114: new->fs_freq = fs->fs_freq;
115: return (new);
116: }
117:
118: freefsent(fs)
119: register struct fstab *fs;
120: {
121:
122: if (fs->fs_file)
123: free(fs->fs_file);
124: if (fs->fs_spec)
125: free(fs->fs_spec);
126: if (fs->fs_type)
127: free(fs->fs_type);
128: free((char *)fs);
129: }
130:
131: struct mtab zeromtab;
132:
133: umountfs(name)
134: char *name;
135: {
136: register char *p1, *p2;
137: register struct mtab *mp;
138: int mf;
139: struct fstab *fs;
140:
141: fs = getfsfile(name);
142: if (fs != NULL)
143: name = fs->fs_spec;
144: if (umount(name) < 0) {
145: perror(name);
146: return (0);
147: }
148: if (vflag)
149: fprintf(stderr, "%s: Unmounted\n", name);
150: while ((p1 = rindex(name, '/')) && p1[1] == 0)
151: *p1 = 0;
152: if (p1)
153: name = p1 + 1;
154: for (mp = mtab; mp < &mtab[NMOUNT]; mp++) {
155: if (strncmp(mp->m_dname, name, sizeof (mp->m_dname)))
156: continue;
157: *mp = zeromtab;
158: for (mp = &mtab[NMOUNT]; mp >= mtab; mp--)
159: if (mp->m_path[0])
160: break;
161: mp++;
162: mf = creat("/etc/mtab", 0644);
163: write(mf, (char *)mtab, (mp-mtab) * sizeof (struct mtab));
164: return (1);
165: }
166: fprintf(stderr, "%s: Not mounted\n", name);
167: return (0);
168: }
Defined functions
main
defined in line
31;
never used
Defined variables
all
defined in line
29; used 3 times
errs
defined in line
29; used 2 times
mtab
defined in line
26; used 8 times
vflag
defined in line
29; used 2 times