mkfs: symlink support
[minix.git] / commands / umount / umount.c
blob9234f620145f79093d24644a67511c725f392a51
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 <getopt.h>
11 #include <errno.h>
12 #include <limits.h>
13 #include <minix/minlib.h>
14 #include <stdlib.h>
15 #include <string.h>
16 #include <unistd.h>
17 #include <sys/mount.h>
18 #include <stdio.h>
20 int main(int argc, char **argv);
21 int find_mtab_entry(char *name);
22 void update_mtab(void);
23 void usage(void);
25 static char device[PATH_MAX+1], mountpoint[PATH_MAX+1], vs[10], rw[10];
27 int main(argc, argv)
28 int argc;
29 char *argv[];
31 int found;
32 int flags = 0UL;
33 int i;
34 char c;
35 char *name;
37 while ((c = getopt (argc, argv, "e")) != -1)
39 switch (c) {
40 case 'e': flags |= MS_EXISTING; break;
41 default: break;
45 if (argc - optind != 1) {
46 usage();
49 name = argv[optind];
52 found = find_mtab_entry(name);
55 if (umount2(name, flags) < 0) {
56 if (errno == EINVAL)
57 std_err("umount: Device not mounted\n");
58 else if (errno == ENOTBLK)
59 std_err("umount: Not a mountpoint\n");
60 else
61 perror("umount");
62 exit(1);
64 if (found) {
65 printf("%s unmounted from %s\n", device, mountpoint);
66 update_mtab();
68 else printf("%s unmounted (mtab not updated)\n", name);
69 return(0);
72 int find_mtab_entry(name)
73 char *name;
75 /* Find a matching mtab entry for 'name' which may be a special or a path,
76 * and generate a new mtab file without this entry on the fly. Do not write
77 * out the result yet. Return whether we found a matching entry.
79 char special[PATH_MAX+1], mounted_on[PATH_MAX+1], version[10], rw_flag[10];
80 struct stat nstat, mstat;
81 int n, found;
83 if (load_mtab("umount") < 0) return 0;
85 if (stat(name, &nstat) != 0) return 0;
87 found = 0;
88 while (1) {
89 n = get_mtab_entry(special, mounted_on, version, rw_flag);
90 if (n < 0) break;
91 if (strcmp(name, special) == 0 || (stat(mounted_on, &mstat) == 0 &&
92 mstat.st_dev == nstat.st_dev && mstat.st_ino == nstat.st_ino))
94 /* If we found an earlier match, keep that one. Mountpoints
95 * may be stacked on top of each other, and unmounting should
96 * take place in the reverse order of mounting.
98 if (found) {
99 (void) put_mtab_entry(device, mountpoint, vs, rw);
102 strcpy(device, special);
103 strcpy(mountpoint, mounted_on);
104 strcpy(vs, version);
105 strcpy(rw, rw_flag);
106 found = 1;
107 continue;
109 (void) put_mtab_entry(special, mounted_on, version, rw_flag);
112 return found;
115 void update_mtab()
117 /* Write out the new mtab file. */
118 int n;
120 n = rewrite_mtab("umount");
121 if (n < 0) {
122 std_err("/etc/mtab not updated.\n");
123 exit(1);
127 void usage()
129 std_err("Usage: umount [-e] name\n");
130 exit(1);