xref: /original-bsd/sbin/umount/umount.c (revision 23a40993)
1 #ifndef lint
2 static char *sccsid = "@(#)umount.c	4.8 (Berkeley) 05/28/83";
3 #endif
4 
5 /*
6  * umount
7  */
8 #include <sys/param.h>
9 
10 #include <stdio.h>
11 #include <fstab.h>
12 #include <mtab.h>
13 
14 struct	mtab mtab[NMOUNT];
15 
16 char	*rindex();
17 int	vflag, all, errs;
18 
19 main(argc, argv)
20 	int argc;
21 	char **argv;
22 {
23 	register struct mtab *mp;
24 	register char *p1, *p2;
25 	int mf;
26 
27 	argc--, argv++;
28 	sync();
29 	mf = open("/etc/mtab", 0);
30 	read(mf, (char *)mtab, sizeof (mtab));
31 again:
32 	if (argc > 0 && !strcmp(*argv, "-v")) {
33 		vflag++;
34 		argc--, argv++;
35 		goto again;
36 	}
37 	if (argc > 0 && !strcmp(*argv, "-a")) {
38 		all++;
39 		argc--, argv++;
40 		goto again;
41 	}
42 	if (argc == 0 && !all) {
43 		fprintf(stderr, "Usage: umount [ -a ] [ -v ] [ dev ... ]\n");
44 		exit(1);
45 	}
46 	if (all) {
47 		if (setfsent() == 0)
48 			perror(FSTAB), exit(1);
49 		umountall();
50 		exit(0);
51 	}
52 	while (argc > 0) {
53 		if (umountfs(*argv++) == 0)
54 			errs++;
55 		argc--;
56 	}
57 	exit(errs);
58 }
59 
60 umountall()
61 {
62 	struct fstab *fs, *allocfsent();
63 
64 	if ((fs = getfsent()) == 0)
65 		return;
66 	fs = allocfsent(fs);
67 	umountall();
68 	if (strcmp(fs->fs_file, "/") == 0) {
69 		freefsent(fs);
70 		return;
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);
76 		return;
77 	}
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);
117 }
118 
119 struct	mtab zeromtab;
120 
121 umountfs(name)
122 	char *name;
123 {
124 	register char *p1, *p2;
125 	register struct	mtab *mp;
126 	int mf;
127 	struct fstab *fs;
128 
129 	fs = getfsfile(name);
130 	if (fs != NULL)
131 		name = fs->fs_spec;
132 	if (umount(name) < 0) {
133 		perror(name);
134 		return (0);
135 	}
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;
142 	for (mp = mtab; mp < &mtab[NMOUNT]; mp++) {
143 		if (strncmp(mp->m_dname, name, sizeof (mp->m_dname)))
144 			continue;
145 		*mp = zeromtab;
146 		for (mp = &mtab[NMOUNT]; mp >= mtab; mp--)
147 			if (mp->m_path[0])
148 				break;
149 		mp++;
150 		mf = creat("/etc/mtab", 0644);
151 		write(mf, (char *)mtab, (mp-mtab) * sizeof (struct mtab));
152 		return (1);
153 	}
154 	fprintf(stderr, "%s: Not mounted\n", name);
155 	return (0);
156 }
157