|
|
1.1 root 1: #include <stdio.h>
2: #include <fstab.h>
3:
4: struct mtab *mtab;
5:
6: int nmtab;
7: int mdirty;
8: int errs;
9:
10: char mfile[] = MTAB;
11:
12: main(argc, argv)
13: int argc;
14: char **argv;
15: {
16:
17: if (argc < 2) {
18: fprintf(stderr, "usage: umount [-a] [file ...]\n");
19: exit(1);
20: }
21: readmtab();
22: sync();
23: if (argc == 2 && strcmp(argv[1], "-a") == 0)
24: umountall();
25: else {
26: while (--argc > 0)
27: umountone(*++argv);
28: }
29: writmtab();
30: sync();
31: exit(errs);
32: }
33:
34: /*
35: * unmount everything
36: * in reverse order in case of nesting
37: */
38: umountall()
39: {
40: register int i;
41:
42: for (i = nmtab - 1; i >= 0; i--) {
43: if (mtab[i].file[0] == 0)
44: continue;
45: if (funmount(mtab[i].file) < 0) {
46: perror(mtab[i].file);
47: errs++;
48: continue;
49: }
50: mtab[i].file[0] = 0;
51: mdirty++;
52: }
53: }
54:
55: umountone(f)
56: char *f;
57: {
58: register int i;
59:
60: if (funmount(f) < 0) {
61: perror(f);
62: errs++;
63: return;
64: }
65: for (i = nmtab - 1; i >= 0; i--)
66: if (strcmp(mtab[i].file, f) == 0) {
67: mtab[i].file[0] = 0;
68: mdirty++;
69: break;
70: }
71: }
72:
73: #define CHUNK (20*sizeof(struct mtab))
74:
75: readmtab()
76: {
77: char *base, *buf;
78: int n, nr, tot;
79: int fd;
80: char *malloc(), *realloc();
81:
82: if ((base = malloc(CHUNK)) == NULL) {
83: fprintf(stderr, "umount: no mem\n");
84: exit(1);
85: }
86: if ((fd = open(mfile, 0)) < 0) {
87: perror("read /etc/mtab");
88: exit(1);
89: }
90: buf = base;
91: nr = CHUNK;
92: tot = 0;
93: while ((n = read(fd, buf, nr)) > 0) {
94: tot += n;
95: buf += n;
96: if ((nr -= n) <= 0) {
97: if ((base = realloc(base, CHUNK)) == NULL) {
98: fprintf(stderr, "umount: no mem\n");
99: exit(1);
100: }
101: buf = base + tot;
102: nr = CHUNK;
103: }
104: }
105: mtab = (struct mtab *)base;
106: nmtab = tot / sizeof(struct mtab);
107: close(fd);
108: }
109:
110: writmtab()
111: {
112: int fd;
113: register int i;
114:
115: if (mdirty == 0)
116: return;
117: for (i = nmtab - 1; i >= 0; i--)
118: if (mtab[i].file[0])
119: break;
120: if ((fd = creat(mfile, 0666)) < 0) {
121: perror("write /etc/mtab");
122: errs++;
123: return;
124: }
125: i = (char *)&mtab[i+1] - (char *)mtab;
126: if (write(fd, (char *)mtab, i) != i) {
127: perror("write /etc/mtab");
128: errs++;
129: return;
130: }
131: close(fd);
132: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.