umount: getopt return value is int, not char
[minix.git] / commands / umount / umount.c
blob6ec9e2da5ba1837c3425de05d55d25acb80645d0
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], mount_point[PATH_MAX], type[MNTNAMELEN],
26 flags[MNTFLAGLEN];
28 int main(argc, argv)
29 int argc;
30 char *argv[];
32 int found;
33 int umount_flags = 0UL;
34 int c;
35 char *name;
37 while ((c = getopt (argc, argv, "e")) != -1)
39 switch (c) {
40 case 'e': umount_flags |= MS_EXISTING; break;
41 default: break;
45 if (argc - optind != 1) {
46 usage();
49 name = argv[optind];
51 found = find_mtab_entry(name);
53 if (umount2(name, umount_flags) < 0) {
54 if (errno == EINVAL)
55 std_err("umount: Device not mounted\n");
56 else if (errno == ENOTBLK)
57 std_err("umount: Not a mountpoint\n");
58 else
59 perror("umount");
60 exit(1);
62 if (found) {
63 printf("%s unmounted from %s\n", device, mount_point);
65 return(0);
68 int find_mtab_entry(name)
69 char *name;
71 /* Find a matching mtab entry for 'name' which may be a special or a path,
72 * and generate a new mtab file without this entry on the fly. Do not write
73 * out the result yet. Return whether we found a matching entry.
75 char e_dev[PATH_MAX], e_mount_point[PATH_MAX], e_type[MNTNAMELEN],
76 e_flags[MNTFLAGLEN];
77 struct stat nstat, mstat;
78 int n, found;
80 if (load_mtab("umount") < 0) return 0;
82 if (stat(name, &nstat) != 0) return 0;
84 found = 0;
85 while (1) {
86 n = get_mtab_entry(e_dev, e_mount_point, e_type, e_flags);
87 if (n < 0) break;
88 if (strcmp(name, e_dev) == 0 || (stat(e_mount_point, &mstat) == 0 &&
89 mstat.st_dev == nstat.st_dev && mstat.st_ino == nstat.st_ino))
91 strlcpy(device, e_dev, PATH_MAX);
92 strlcpy(mount_point, e_mount_point, PATH_MAX);
93 strlcpy(type, e_type, MNTNAMELEN);
94 strlcpy(flags, e_flags, MNTFLAGLEN);
95 found = 1;
96 break;
100 return found;
103 void usage()
105 std_err("Usage: umount [-e] name\n");
106 exit(1);