Commit | Line | Data |
---|---|---|
83dfa04a | 1 | static char *sccsid = "@(#)umount.c 4.3 (Berkeley) %G%"; |
cfbe3c25 BJ |
2 | #include <stdio.h> |
3 | #include <fstab.h> | |
4 | /* | |
f0861f40 | 5 | * umount |
cfbe3c25 BJ |
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){ | |
83dfa04a BJ |
31 | if (setfsent() == 0) |
32 | perror(FSTAB), exit(1); | |
33 | umountall(); | |
34 | endfsent(); | |
cfbe3c25 | 35 | } else { |
83dfa04a BJ |
36 | int back; |
37 | if (back = umountfs(argv[1])){ | |
38 | if (back < 0) | |
39 | perror("umount"); | |
cfbe3c25 | 40 | exit(1); |
83dfa04a | 41 | } |
cfbe3c25 BJ |
42 | } |
43 | exit(0); | |
44 | } | |
83dfa04a BJ |
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 | } | |
cfbe3c25 BJ |
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) { | |
83dfa04a | 79 | return(-1); |
cfbe3c25 BJ |
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 | } |