2000-02-18 03:38:33 +08:00
|
|
|
/* umount.cc
|
|
|
|
|
2013-01-22 00:28:27 +08:00
|
|
|
Copyright 1996, 1998, 1999, 2000, 2001, 2002, 2003, 2008, 2011 Red Hat, Inc.
|
2000-02-18 03:38:33 +08:00
|
|
|
|
|
|
|
This file is part of Cygwin.
|
|
|
|
|
|
|
|
This software is a copyrighted work licensed under the terms of the
|
|
|
|
Cygwin license. Please consult the file "CYGWIN_LICENSE" for
|
|
|
|
details. */
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/mount.h>
|
|
|
|
#include <mntent.h>
|
2000-06-06 02:43:54 +08:00
|
|
|
#include <stdlib.h>
|
2000-06-08 20:54:12 +08:00
|
|
|
#include <errno.h>
|
2001-04-03 10:41:54 +08:00
|
|
|
#include <getopt.h>
|
2011-10-10 22:57:48 +08:00
|
|
|
#include <cygwin/version.h>
|
2000-02-18 03:38:33 +08:00
|
|
|
|
|
|
|
static void remove_all_user_mounts ();
|
|
|
|
|
|
|
|
static const char *progname;
|
|
|
|
|
2001-04-03 10:41:54 +08:00
|
|
|
struct option longopts[] =
|
|
|
|
{
|
2001-04-16 10:51:03 +08:00
|
|
|
{"help", no_argument, NULL, 'h' },
|
2001-04-03 10:41:54 +08:00
|
|
|
{"remove-user-mounts", no_argument, NULL, 'U'},
|
2011-10-10 22:57:48 +08:00
|
|
|
{"version", no_argument, NULL, 'V'},
|
2001-04-03 10:41:54 +08:00
|
|
|
{NULL, 0, NULL, 0}
|
|
|
|
};
|
|
|
|
|
2011-10-10 22:57:48 +08:00
|
|
|
char opts[] = "hUV";
|
2001-04-03 10:41:54 +08:00
|
|
|
|
2000-02-18 03:38:33 +08:00
|
|
|
static void
|
2002-06-04 09:31:28 +08:00
|
|
|
usage (FILE *where = stderr)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2002-06-04 09:31:28 +08:00
|
|
|
fprintf (where, "\
|
|
|
|
Usage: %s [OPTION] [<posixpath>]\n\
|
2011-10-10 22:57:48 +08:00
|
|
|
\n\
|
2003-04-27 05:52:03 +08:00
|
|
|
Unmount filesystems\n\
|
|
|
|
\n\
|
2002-06-04 09:31:28 +08:00
|
|
|
-h, --help output usage information and exit\n\
|
2001-04-03 10:41:54 +08:00
|
|
|
-U, --remove-user-mounts remove all user mounts\n\
|
2011-10-10 22:57:48 +08:00
|
|
|
-V, --version output version information and exit\n\
|
|
|
|
\n", progname);
|
2002-06-04 09:31:28 +08:00
|
|
|
exit (where == stderr ? 1 : 0);
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
|
2000-06-08 20:54:12 +08:00
|
|
|
static void
|
2000-07-29 06:34:24 +08:00
|
|
|
error (const char *path)
|
2000-06-08 20:54:12 +08:00
|
|
|
{
|
|
|
|
fprintf (stderr, "%s: %s: %s\n", progname, path, strerror (errno));
|
|
|
|
exit (1);
|
|
|
|
}
|
|
|
|
|
2002-06-04 09:31:28 +08:00
|
|
|
static void
|
|
|
|
print_version ()
|
|
|
|
{
|
2011-10-10 22:57:48 +08:00
|
|
|
printf ("umount (cygwin) %d.%d.%d\n"
|
2011-12-18 07:39:47 +08:00
|
|
|
"Unmount filesystem utility\n"
|
|
|
|
"Copyright (C) 1996 - %s Red Hat, Inc.\n"
|
|
|
|
"This is free software; see the source for copying conditions. There is NO\n"
|
2011-10-10 22:57:48 +08:00
|
|
|
"warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n",
|
2011-12-18 07:39:47 +08:00
|
|
|
CYGWIN_VERSION_DLL_MAJOR / 1000,
|
|
|
|
CYGWIN_VERSION_DLL_MAJOR % 1000,
|
|
|
|
CYGWIN_VERSION_DLL_MINOR,
|
|
|
|
strrchr (__DATE__, ' ') + 1);
|
2002-06-04 09:31:28 +08:00
|
|
|
}
|
|
|
|
|
2000-02-18 03:38:33 +08:00
|
|
|
int
|
|
|
|
main (int argc, char **argv)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
int flags = 0;
|
2002-05-12 12:29:54 +08:00
|
|
|
int default_flag = MOUNT_SYSTEM;
|
2001-04-03 10:41:54 +08:00
|
|
|
enum do_what
|
|
|
|
{
|
|
|
|
nada,
|
|
|
|
saw_remove_all_user_mounts
|
|
|
|
} do_what = nada;
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2011-10-10 22:57:48 +08:00
|
|
|
progname = program_invocation_short_name;
|
2002-06-04 09:31:28 +08:00
|
|
|
|
2000-02-18 03:38:33 +08:00
|
|
|
if (argc == 1)
|
|
|
|
usage ();
|
|
|
|
|
2001-04-03 10:41:54 +08:00
|
|
|
while ((i = getopt_long (argc, argv, opts, longopts, NULL)) != EOF)
|
|
|
|
switch (i)
|
|
|
|
{
|
2002-06-04 09:31:28 +08:00
|
|
|
case 'h':
|
|
|
|
usage (stdout);
|
2001-04-03 10:41:54 +08:00
|
|
|
case 'U':
|
|
|
|
if (do_what != nada)
|
|
|
|
usage ();
|
|
|
|
do_what = saw_remove_all_user_mounts;
|
|
|
|
break;
|
2011-10-10 22:57:48 +08:00
|
|
|
case 'V':
|
2002-06-04 09:31:28 +08:00
|
|
|
print_version ();
|
|
|
|
exit (0);
|
2001-04-03 10:41:54 +08:00
|
|
|
default:
|
2011-10-10 22:57:48 +08:00
|
|
|
fprintf (stderr, "Try `%s --help' for more information.\n", progname);
|
|
|
|
exit (1);
|
2001-04-03 10:41:54 +08:00
|
|
|
}
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2001-04-03 10:41:54 +08:00
|
|
|
switch (do_what)
|
|
|
|
{
|
|
|
|
case saw_remove_all_user_mounts:
|
|
|
|
if (optind != argc)
|
2000-02-18 03:38:33 +08:00
|
|
|
usage ();
|
2001-04-03 10:41:54 +08:00
|
|
|
remove_all_user_mounts ();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
if (optind != argc - 1)
|
|
|
|
usage ();
|
2002-05-12 12:29:54 +08:00
|
|
|
if (cygwin_umount (argv[optind], flags | default_flag) != 0)
|
2001-04-03 10:41:54 +08:00
|
|
|
error (argv[optind]);
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* remove_all_user_mounts: Unmount all user mounts. */
|
|
|
|
static void
|
|
|
|
remove_all_user_mounts ()
|
|
|
|
{
|
|
|
|
FILE *m = setmntent ("/-not-used-", "r");
|
|
|
|
struct mntent *p;
|
|
|
|
|
|
|
|
while ((p = getmntent (m)) != NULL)
|
|
|
|
{
|
|
|
|
/* Remove the mount if it's a user mount. */
|
2001-06-15 12:50:57 +08:00
|
|
|
if (strncmp (p->mnt_type, "user", 4) == 0 &&
|
|
|
|
strstr (p->mnt_opts, "noumount") == NULL)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2000-06-08 20:54:12 +08:00
|
|
|
if (cygwin_umount (p->mnt_dir, 0))
|
|
|
|
error (p->mnt_dir);
|
2000-02-18 03:38:33 +08:00
|
|
|
|
|
|
|
/* We've modified the table so we need to start over. */
|
|
|
|
endmntent (m);
|
|
|
|
m = setmntent ("/-not-used-", "r");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
endmntent (m);
|
|
|
|
}
|