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