]> sourceware.org Git - newlib-cygwin.git/blame - winsup/utils/umount.cc
Cygwin: console: Use GetCurrentProcessId() instead of myself->dwProcessId
[newlib-cygwin.git] / winsup / utils / umount.cc
CommitLineData
1fd5e000
CF
1/* umount.cc
2
1fd5e000
CF
3This file is part of Cygwin.
4
5This software is a copyrighted work licensed under the terms of the
6Cygwin license. Please consult the file "CYGWIN_LICENSE" for
7details. */
8
9#include <stdio.h>
10#include <string.h>
11#include <sys/mount.h>
12#include <mntent.h>
b6e259b1 13#include <stdlib.h>
9500a3db 14#include <errno.h>
be61cf4d 15#include <getopt.h>
92b499ac 16#include <cygwin/version.h>
1fd5e000 17
1fd5e000 18static void remove_all_user_mounts ();
1fd5e000
CF
19
20static const char *progname;
21
be61cf4d
CF
22struct option longopts[] =
23{
9f425256 24 {"help", no_argument, NULL, 'h' },
be61cf4d 25 {"remove-user-mounts", no_argument, NULL, 'U'},
92b499ac 26 {"version", no_argument, NULL, 'V'},
be61cf4d
CF
27 {NULL, 0, NULL, 0}
28};
29
92b499ac 30char opts[] = "hUV";
be61cf4d 31
e7fca6f8 32static void __attribute__ ((__noreturn__))
e6cd2312 33usage (FILE *where = stderr)
1fd5e000 34{
e6cd2312
CF
35 fprintf (where, "\
36Usage: %s [OPTION] [<posixpath>]\n\
92b499ac 37\n\
aa275fe0
JDF
38Unmount filesystems\n\
39\n\
e6cd2312 40 -h, --help output usage information and exit\n\
be61cf4d 41 -U, --remove-user-mounts remove all user mounts\n\
92b499ac
CV
42 -V, --version output version information and exit\n\
43\n", progname);
e6cd2312 44 exit (where == stderr ? 1 : 0);
1fd5e000
CF
45}
46
9500a3db 47static void
959e1bac 48error (const char *path)
9500a3db
CV
49{
50 fprintf (stderr, "%s: %s: %s\n", progname, path, strerror (errno));
51 exit (1);
52}
53
e6cd2312
CF
54static void
55print_version ()
56{
92b499ac 57 printf ("umount (cygwin) %d.%d.%d\n"
1b23b30b 58 "Unmount filesystem utility\n"
6e623e93 59 "Copyright (C) 1996 - %s Cygwin Authors\n"
1b23b30b 60 "This is free software; see the source for copying conditions. There is NO\n"
92b499ac 61 "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n",
1b23b30b
CF
62 CYGWIN_VERSION_DLL_MAJOR / 1000,
63 CYGWIN_VERSION_DLL_MAJOR % 1000,
64 CYGWIN_VERSION_DLL_MINOR,
65 strrchr (__DATE__, ' ') + 1);
e6cd2312
CF
66}
67
1fd5e000
CF
68int
69main (int argc, char **argv)
70{
71 int i;
72 int flags = 0;
48897dfe 73 int default_flag = MOUNT_SYSTEM;
be61cf4d
CF
74 enum do_what
75 {
76 nada,
be61cf4d
CF
77 saw_remove_all_user_mounts
78 } do_what = nada;
1fd5e000 79
92b499ac 80 progname = program_invocation_short_name;
e6cd2312 81
1fd5e000
CF
82 if (argc == 1)
83 usage ();
84
be61cf4d
CF
85 while ((i = getopt_long (argc, argv, opts, longopts, NULL)) != EOF)
86 switch (i)
87 {
e6cd2312
CF
88 case 'h':
89 usage (stdout);
be61cf4d
CF
90 case 'U':
91 if (do_what != nada)
92 usage ();
93 do_what = saw_remove_all_user_mounts;
94 break;
92b499ac 95 case 'V':
e6cd2312
CF
96 print_version ();
97 exit (0);
be61cf4d 98 default:
92b499ac
CV
99 fprintf (stderr, "Try `%s --help' for more information.\n", progname);
100 exit (1);
be61cf4d 101 }
1fd5e000 102
be61cf4d
CF
103 switch (do_what)
104 {
be61cf4d
CF
105 case saw_remove_all_user_mounts:
106 if (optind != argc)
1fd5e000 107 usage ();
be61cf4d
CF
108 remove_all_user_mounts ();
109 break;
110 default:
111 if (optind != argc - 1)
112 usage ();
48897dfe 113 if (cygwin_umount (argv[optind], flags | default_flag) != 0)
be61cf4d 114 error (argv[optind]);
1fd5e000
CF
115 }
116
1fd5e000
CF
117 return 0;
118}
119
1fd5e000
CF
120/* remove_all_user_mounts: Unmount all user mounts. */
121static void
122remove_all_user_mounts ()
123{
124 FILE *m = setmntent ("/-not-used-", "r");
125 struct mntent *p;
1fd5e000
CF
126
127 while ((p = getmntent (m)) != NULL)
128 {
129 /* Remove the mount if it's a user mount. */
12a9c874
CF
130 if (strncmp (p->mnt_type, "user", 4) == 0 &&
131 strstr (p->mnt_opts, "noumount") == NULL)
1fd5e000 132 {
9500a3db
CV
133 if (cygwin_umount (p->mnt_dir, 0))
134 error (p->mnt_dir);
1fd5e000
CF
135
136 /* We've modified the table so we need to start over. */
137 endmntent (m);
138 m = setmntent ("/-not-used-", "r");
139 }
140 }
141
142 endmntent (m);
143}
This page took 0.376378 seconds and 6 git commands to generate.