date and time created 87/02/15 16:03:39 by lepreau
[unix-history] / usr / src / sbin / umount / umount.c
CommitLineData
8c5eec2f
DF
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
8char copyright[] =
9"@(#) Copyright (c) 1980 Regents of the University of California.\n\
10 All rights reserved.\n";
11#endif not lint
12
7a6d69cb 13#ifndef lint
8c5eec2f
DF
14static char sccsid[] = "@(#)umount.c 5.1 (Berkeley) %G%";
15#endif not lint
15de4e1e 16
cfbe3c25 17/*
f0861f40 18 * umount
cfbe3c25 19 */
7a6d69cb 20#include <sys/param.h>
cfbe3c25 21
7a6d69cb
SL
22#include <stdio.h>
23#include <fstab.h>
24#include <mtab.h>
cfbe3c25 25
7a6d69cb 26struct mtab mtab[NMOUNT];
cfbe3c25 27
15de4e1e
BJ
28char *rindex();
29int vflag, all, errs;
30
cfbe3c25 31main(argc, argv)
15de4e1e
BJ
32 int argc;
33 char **argv;
cfbe3c25
BJ
34{
35 register struct mtab *mp;
36 register char *p1, *p2;
37 int mf;
38
15de4e1e 39 argc--, argv++;
cfbe3c25
BJ
40 sync();
41 mf = open("/etc/mtab", 0);
15de4e1e
BJ
42 read(mf, (char *)mtab, sizeof (mtab));
43again:
e2d561ad 44 if (argc > 0 && !strcmp(*argv, "-v")) {
15de4e1e
BJ
45 vflag++;
46 argc--, argv++;
47 goto again;
48 }
e2d561ad 49 if (argc > 0 && !strcmp(*argv, "-a")) {
15de4e1e
BJ
50 all++;
51 argc--, argv++;
52 goto again;
cfbe3c25 53 }
15de4e1e
BJ
54 if (argc == 0 && !all) {
55 fprintf(stderr, "Usage: umount [ -a ] [ -v ] [ dev ... ]\n");
56 exit(1);
57 }
58 if (all) {
83dfa04a
BJ
59 if (setfsent() == 0)
60 perror(FSTAB), exit(1);
61 umountall();
15de4e1e
BJ
62 exit(0);
63 }
64 while (argc > 0) {
65 if (umountfs(*argv++) == 0)
66 errs++;
67 argc--;
cfbe3c25 68 }
15de4e1e 69 exit(errs);
cfbe3c25 70}
15de4e1e 71
83dfa04a
BJ
72umountall()
73{
7a6d69cb 74 struct fstab *fs, *allocfsent();
15de4e1e 75
7a6d69cb 76 if ((fs = getfsent()) == 0)
83dfa04a 77 return;
7a6d69cb 78 fs = allocfsent(fs);
83dfa04a 79 umountall();
7a6d69cb
SL
80 if (strcmp(fs->fs_file, "/") == 0) {
81 freefsent(fs);
83dfa04a 82 return;
7a6d69cb
SL
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);
15de4e1e
BJ
88 return;
89 }
7a6d69cb
SL
90 if (umountfs(fs->fs_spec) < 0)
91 perror(fs->fs_spec);
92 freefsent(fs);
93}
94
95struct fstab *
96allocfsent(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
118freefsent(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);
83dfa04a 129}
cfbe3c25 130
15de4e1e
BJ
131struct mtab zeromtab;
132
133umountfs(name)
134 char *name;
cfbe3c25 135{
15de4e1e
BJ
136 register char *p1, *p2;
137 register struct mtab *mp;
138 int mf;
7a6d69cb 139 struct fstab *fs;
cfbe3c25 140
7a6d69cb
SL
141 fs = getfsfile(name);
142 if (fs != NULL)
143 name = fs->fs_spec;
631a8885 144 if (umount(name) < 0) {
15de4e1e
BJ
145 perror(name);
146 return (0);
cfbe3c25 147 }
15de4e1e
BJ
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;
cfbe3c25 154 for (mp = mtab; mp < &mtab[NMOUNT]; mp++) {
7a6d69cb 155 if (strncmp(mp->m_dname, name, sizeof (mp->m_dname)))
15de4e1e
BJ
156 continue;
157 *mp = zeromtab;
158 for (mp = &mtab[NMOUNT]; mp >= mtab; mp--)
7a6d69cb 159 if (mp->m_path[0])
15de4e1e
BJ
160 break;
161 mp++;
162 mf = creat("/etc/mtab", 0644);
163 write(mf, (char *)mtab, (mp-mtab) * sizeof (struct mtab));
164 return (1);
cfbe3c25 165 }
15de4e1e
BJ
166 fprintf(stderr, "%s: Not mounted\n", name);
167 return (0);
cfbe3c25 168}