xref: /original-bsd/sbin/umount/umount.c (revision 552e81d8)
1 static char *sccsid = "@(#)umount.c	4.1 (Berkeley) 10/01/80";
2 #include <stdio.h>
3 #include <fstab.h>
4 /*
5  *	umount name
6  *
7  *	umount -a
8  *		This tries to unmount all of the block special file names
9  *		as given in /etc/fstab.
10  */
11 
12 #define	NMOUNT	16
13 #define	NAMSIZ	32
14 
15 struct mtab {
16 	char	file[NAMSIZ];
17 	char	spec[NAMSIZ];
18 } mtab[NMOUNT];
19 
20 main(argc, argv)
21 char **argv;
22 {
23 	register struct mtab *mp;
24 	register char *p1, *p2;
25 	int mf;
26 
27 	sync();
28 	mf = open("/etc/mtab", 0);
29 	read(mf, (char *)mtab, NMOUNT*2*NAMSIZ);
30 	if(argc != 2) {
31 		printf("arg count\n");
32 		return(1);
33 	}
34 	if (strcmp(argv[1], "-a") == 0){
35 		FILE	*fs_file;
36 		struct	fstab	fs;
37 		if ( (fs_file = fopen(FSTAB, "r")) == NULL){
38 			perror(FSTAB);
39 			exit(1);
40 		}
41 		while (!feof(fs_file)){
42 			fscanf(fs_file, FSTABFMT, FSTABARG(&fs));
43 			if (strcmp(fs.fs_file, "/") == 0)
44 				continue;
45 			fprintf(stderr, "Unmounting special file %s\n",
46 				fs.fs_spec);
47 			fflush(stderr);
48 			if (umountfs(fs.fs_spec))
49 				continue;
50 		}
51 		fclose(fs_file);
52 	} else {
53 		if (umountfs(argv[1]))
54 			exit(1);
55 	}
56 	exit(0);
57 }
58 
59 int umountfs(name)
60 	char	*name;
61 {
62 	register	char	*p1, *p2;
63 	register	struct	mtab	*mp;
64 	int	mf;
65 
66 	if (umount(name) < 0) {
67 		perror("umount");
68 		return(1);
69 	}
70 	p1 = name;
71 	while(*p1++)
72 		;
73 	p1--;
74 	while(*--p1 == '/')
75 		*p1 = '\0';
76 	while(p1 > name && *--p1 != '/')
77 		;
78 	if(*p1 == '/')
79 		p1++;
80 	name = p1;
81 	for (mp = mtab; mp < &mtab[NMOUNT]; mp++) {
82 		p1 = name;
83 		p2 = &mp->spec[0];
84 		while (*p1++ == *p2)
85 			if (*p2++ == 0) {
86 				for (p1 = mp->file; p1 < &mp->file[NAMSIZ*2];)
87 					*p1++ = 0;
88 				mp = &mtab[NMOUNT];
89 				while ((--mp)->file[0] == 0);
90 				mf = creat("/etc/mtab", 0644);
91 				write(mf, (char *)mtab, (mp-mtab+1)*2*NAMSIZ);
92 				return(0);
93 			}
94 	}
95 	printf("%s not in mount table\n", name);
96 	return(1);
97 }
98