OSDN Git Service

* cygpath.cc (get_device_name): Fix path length test.
[pf3gnuchains/pf3gnuchains4x.git] / winsup / utils / umount.cc
1 /* umount.cc
2
3    Copyright 1996, 1998, 1999, 2000, 2001, 2002, 2008 Red Hat, Inc.
4
5 This file is part of Cygwin.
6
7 This software is a copyrighted work licensed under the terms of the
8 Cygwin license.  Please consult the file "CYGWIN_LICENSE" for
9 details. */
10
11 #include <stdio.h>
12 #include <string.h>
13 #include <sys/mount.h>
14 #include <mntent.h>
15 #include <stdlib.h>
16 #include <errno.h>
17 #include <getopt.h>
18
19 static void remove_all_user_mounts ();
20
21 static const char version[] = "$Revision$";
22 static const char *progname;
23
24 struct option longopts[] =
25 {
26   {"help", no_argument, NULL, 'h' },
27   {"remove-user-mounts", no_argument, NULL, 'U'},
28   {"version", no_argument, NULL, 'v'},
29   {NULL, 0, NULL, 0}
30 };
31
32 char opts[] = "hUv";
33
34 static void
35 usage (FILE *where = stderr)
36 {
37   fprintf (where, "\
38 Usage: %s [OPTION] [<posixpath>]\n\
39 Unmount filesystems\n\
40 \n\
41   -h, --help                    output usage information and exit\n\
42   -U, --remove-user-mounts      remove all user mounts\n\
43   -v, --version                 output version information and exit\n\
44 ", progname);
45   exit (where == stderr ? 1 : 0);
46 }
47
48 static void
49 error (const char *path)
50 {
51   fprintf (stderr, "%s: %s: %s\n", progname, path, strerror (errno));
52   exit (1);
53 }
54
55 static void
56 print_version ()
57 {
58   const char *v = strchr (version, ':');
59   int len;
60   if (!v)
61     {
62       v = "?";
63       len = 1;
64     }
65   else
66     {
67       v += 2;
68       len = strchr (v, ' ') - v;
69     }
70   printf ("\
71 %s (cygwin) %.*s\n\
72 Filesystem Utility\n\
73 Copyright 1996, 1998, 1999, 2000, 2001, 2002\n\
74 Compiled on %s\n\
75 ", progname, len, v, __DATE__);
76 }
77
78 int
79 main (int argc, char **argv)
80 {
81   int i;
82   int flags = 0;
83   int default_flag = MOUNT_SYSTEM;
84   enum do_what
85   {
86     nada,
87     saw_remove_all_user_mounts
88   } do_what = nada;
89
90   progname = strrchr (argv[0], '/');
91   if (progname == NULL)
92     progname = strrchr (argv[0], '\\');
93   if (progname == NULL)
94     progname = argv[0];
95   else
96     progname++;
97
98   if (argc == 1)
99     usage ();
100
101   while ((i = getopt_long (argc, argv, opts, longopts, NULL)) != EOF)
102     switch (i)
103       {
104       case 'h':
105         usage (stdout);
106       case 'U':
107         if (do_what != nada)
108           usage ();
109         do_what = saw_remove_all_user_mounts;
110         break;
111       case 'v':
112         print_version ();
113         exit (0);
114       default:
115         usage ();
116       }
117
118   switch (do_what)
119     {
120     case saw_remove_all_user_mounts:
121       if (optind != argc)
122         usage ();
123       remove_all_user_mounts ();
124       break;
125     default:
126       if (optind != argc - 1)
127         usage ();
128       if (cygwin_umount (argv[optind], flags | default_flag) != 0)
129         error (argv[optind]);
130     }
131
132   return 0;
133 }
134
135 /* remove_all_user_mounts: Unmount all user mounts. */
136 static void
137 remove_all_user_mounts ()
138 {
139   FILE *m = setmntent ("/-not-used-", "r");
140   struct mntent *p;
141
142   while ((p = getmntent (m)) != NULL)
143     {
144       /* Remove the mount if it's a user mount. */
145       if (strncmp (p->mnt_type, "user", 4) == 0 &&
146           strstr (p->mnt_opts, "noumount") == NULL)
147         {
148           if (cygwin_umount (p->mnt_dir, 0))
149             error (p->mnt_dir);
150
151           /* We've modified the table so we need to start over. */
152           endmntent (m);
153           m = setmntent ("/-not-used-", "r");
154         }
155     }
156
157   endmntent (m);
158 }