pci: don't do sanity check for missing pci bus, the check can misfire.
[minix.git] / commands / simple / umount.c
blobbf2fe1c6fbd53c956c2076b9b3817ecd40ce2f84
1 /* umount - unmount a file system Author: Andy Tanenbaum */
3 #define _MINIX 1 /* for proto of the non-POSIX umount() */
4 #define _POSIX_SOURCE 1 /* for PATH_MAX from limits.h */
6 #include <minix/type.h>
7 #include <sys/types.h>
8 #include <sys/stat.h>
9 #include <fcntl.h>
10 #include <errno.h>
11 #include <limits.h>
12 #include <minix/minlib.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include <unistd.h>
16 #include <sys/mount.h>
17 #include <stdio.h>
19 _PROTOTYPE(int main, (int argc, char **argv));
20 _PROTOTYPE(int find_mtab_entry, (char *name));
21 _PROTOTYPE(void update_mtab, (void));
22 _PROTOTYPE(void usage, (void));
24 static char device[PATH_MAX+1], mountpoint[PATH_MAX+1], vs[10], rw[10];
26 int main(argc, argv)
27 int argc;
28 char *argv[];
30 int found;
32 if (argc != 2) usage();
33 found = find_mtab_entry(argv[1]);
34 if (umount(argv[1]) < 0) {
35 if (errno == EINVAL)
36 std_err("umount: Device not mounted\n");
37 else if (errno == ENOTBLK)
38 std_err("unount: Not a mountpoint\n");
39 else
40 perror("umount");
41 exit(1);
43 if (found) {
44 printf("%s unmounted from %s\n", device, mountpoint);
45 update_mtab();
47 else printf("%s unmounted (mtab not updated)\n", argv[1]);
48 return(0);
51 int find_mtab_entry(name)
52 char *name;
54 /* Find a matching mtab entry for 'name' which may be a special or a path,
55 * and generate a new mtab file without this entry on the fly. Do not write
56 * out the result yet. Return whether we found a matching entry.
58 char special[PATH_MAX+1], mounted_on[PATH_MAX+1], version[10], rw_flag[10];
59 struct stat nstat, mstat;
60 int n, found;
62 if (load_mtab("umount") < 0) return 0;
64 if (stat(name, &nstat) != 0) return 0;
66 found = 0;
67 while (1) {
68 n = get_mtab_entry(special, mounted_on, version, rw_flag);
69 if (n < 0) break;
70 if (strcmp(name, special) == 0 || (stat(mounted_on, &mstat) == 0 &&
71 mstat.st_dev == nstat.st_dev && mstat.st_ino == nstat.st_ino))
73 /* If we found an earlier match, keep that one. Mountpoints
74 * may be stacked on top of each other, and unmounting should
75 * take place in the reverse order of mounting.
77 if (found) {
78 (void) put_mtab_entry(device, mountpoint, vs, rw);
81 strcpy(device, special);
82 strcpy(mountpoint, mounted_on);
83 strcpy(vs, version);
84 strcpy(rw, rw_flag);
85 found = 1;
86 continue;
88 (void) put_mtab_entry(special, mounted_on, version, rw_flag);
91 return found;
94 void update_mtab()
96 /* Write out the new mtab file. */
97 int n;
99 n = rewrite_mtab("umount");
100 if (n < 0) {
101 std_err("/etc/mtab not updated.\n");
102 exit(1);
106 void usage()
108 std_err("Usage: umount name\n");
109 exit(1);