xref: /original-bsd/sbin/umount/umount.c (revision c7de680c)
1 static char *sccsid = "@(#)umount.c	4.4 (Berkeley) 02/28/82";
2 
3 #include <stdio.h>
4 #include <fstab.h>
5 
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 char	*rindex();
19 int	vflag, all, errs;
20 
21 main(argc, argv)
22 	int argc;
23 	char **argv;
24 {
25 	register struct mtab *mp;
26 	register char *p1, *p2;
27 	int mf;
28 
29 	argc--, argv++;
30 	sync();
31 	mf = open("/etc/mtab", 0);
32 	read(mf, (char *)mtab, sizeof (mtab));
33 again:
34 	if (!strcmp(*argv, "-v")) {
35 		vflag++;
36 		argc--, argv++;
37 		goto again;
38 	}
39 	if (!strcmp(*argv, "-a")) {
40 		all++;
41 		argc--, argv++;
42 		goto again;
43 	}
44 	if (argc == 0 && !all) {
45 		fprintf(stderr, "Usage: umount [ -a ] [ -v ] [ dev ... ]\n");
46 		exit(1);
47 	}
48 	if (all) {
49 		if (setfsent() == 0)
50 			perror(FSTAB), exit(1);
51 		umountall();
52 		exit(0);
53 	}
54 	while (argc > 0) {
55 		if (umountfs(*argv++) == 0)
56 			errs++;
57 		argc--;
58 	}
59 	exit(errs);
60 }
61 
62 umountall()
63 {
64 	struct fstab fs, *fsp;
65 
66 	if ((fsp = getfsent()) == 0)
67 		return;
68 	fs = *fsp;	/* save info locally; it is static from getfsent() */
69 	umountall();
70 	if (strcmp(fs.fs_file, "/") == 0)
71 		return;
72 	if (strcmp(fs.fs_type, FSTAB_RW) && strcmp(fs.fs_type, FSTAB_RO))
73 		return;
74 	if (umountfs(fs.fs_spec) < 0) {
75 		perror(fs.fs_spec);
76 		return;
77 	}
78 }
79 
80 struct	mtab zeromtab;
81 
82 umountfs(name)
83 	char *name;
84 {
85 	register char *p1, *p2;
86 	register struct	mtab *mp;
87 	int mf;
88 
89 	if (umount(name) < 0) {
90 		perror(name);
91 		return (0);
92 	}
93 	if (vflag)
94 		fprintf(stderr, "%s: Unmounted\n", name);
95 	while ((p1 = rindex(name, '/')) && p1[1] == 0)
96 		*p1 = 0;
97 	if (p1)
98 		name = p1 + 1;
99 	for (mp = mtab; mp < &mtab[NMOUNT]; mp++) {
100 		if (strncmp(mp->spec, name, sizeof (mp->spec)))
101 			continue;
102 		*mp = zeromtab;
103 		for (mp = &mtab[NMOUNT]; mp >= mtab; mp--)
104 			if (mp->file[0])
105 				break;
106 		mp++;
107 		mf = creat("/etc/mtab", 0644);
108 		write(mf, (char *)mtab, (mp-mtab) * sizeof (struct mtab));
109 		return (1);
110 	}
111 	fprintf(stderr, "%s: Not mounted\n", name);
112 	return (0);
113 }
114