xref: /original-bsd/sbin/umount/umount.c (revision 6c57d260)
1 static char *sccsid = "@(#)umount.c	4.3 (Berkeley) 10/16/80";
2 #include <stdio.h>
3 #include <fstab.h>
4 /*
5  * umount
6  */
7 
8 #define	NMOUNT	16
9 #define	NAMSIZ	32
10 
11 struct mtab {
12 	char	file[NAMSIZ];
13 	char	spec[NAMSIZ];
14 } mtab[NMOUNT];
15 
16 main(argc, argv)
17 char **argv;
18 {
19 	register struct mtab *mp;
20 	register char *p1, *p2;
21 	int mf;
22 
23 	sync();
24 	mf = open("/etc/mtab", 0);
25 	read(mf, (char *)mtab, NMOUNT*2*NAMSIZ);
26 	if(argc != 2) {
27 		printf("arg count\n");
28 		return(1);
29 	}
30 	if (strcmp(argv[1], "-a") == 0){
31 		if (setfsent() == 0)
32 			perror(FSTAB), exit(1);
33 		umountall();
34 		endfsent();
35 	} else {
36 		int	back;
37 		if (back = umountfs(argv[1])){
38 			if (back < 0)
39 				perror("umount");
40 			exit(1);
41 		}
42 	}
43 	exit(0);
44 }
45 /*
46  *	It is important to unmount the files in
47  *	reverse! order from the order they were mounted,
48  *	so that file systems mounted as children to other
49  *	file systems get removed in the right order.
50  */
51 umountall()
52 {
53 	struct	fstab	fs;
54 	struct	fstab	*fsp;
55 	if ( (fsp = getfsent()) == 0)
56 		return;
57 	fs = *fsp;	/* save info locally; it is static from getfsent() */
58 	umountall();
59 	if (strcmp(fs.fs_file, "/") == 0)
60 		return;
61 	if (strcmp(fs.fs_type, FSTAB_RW) &&
62 	    strcmp(fs.fs_type, FSTAB_RO))
63 		return;
64 	if (umountfs(fs.fs_spec) < 0)
65 		fprintf(stdout, "Unmount of special file %s FAILED\n", fs.fs_spec);
66 	else
67 		fprintf(stdout, "Unmounted special file %s\n", fs.fs_spec);
68 	fflush(stdout);
69 }
70 
71 int umountfs(name)
72 	char	*name;
73 {
74 	register	char	*p1, *p2;
75 	register	struct	mtab	*mp;
76 	int	mf;
77 
78 	if (umount(name) < 0) {
79 		return(-1);
80 	}
81 	p1 = name;
82 	while(*p1++)
83 		;
84 	p1--;
85 	while(*--p1 == '/')
86 		*p1 = '\0';
87 	while(p1 > name && *--p1 != '/')
88 		;
89 	if(*p1 == '/')
90 		p1++;
91 	name = p1;
92 	for (mp = mtab; mp < &mtab[NMOUNT]; mp++) {
93 		p1 = name;
94 		p2 = &mp->spec[0];
95 		while (*p1++ == *p2)
96 			if (*p2++ == 0) {
97 				for (p1 = mp->file; p1 < &mp->file[NAMSIZ*2];)
98 					*p1++ = 0;
99 				mp = &mtab[NMOUNT];
100 				while ((--mp)->file[0] == 0);
101 				mf = creat("/etc/mtab", 0644);
102 				write(mf, (char *)mtab, (mp-mtab+1)*2*NAMSIZ);
103 				return(0);
104 			}
105 	}
106 	printf("%s not in mount table\n", name);
107 	return(1);
108 }
109