minix/commands/umount/umount.c

105 lines
2.2 KiB
C
Raw Normal View History

2005-04-21 16:53:53 +02:00
/* umount - unmount a file system Author: Andy Tanenbaum */
#include <minix/type.h>
2005-04-21 16:53:53 +02:00
#include <sys/types.h>
#include <sys/stat.h>
2005-04-21 16:53:53 +02:00
#include <fcntl.h>
#include <getopt.h>
2005-04-21 16:53:53 +02:00
#include <errno.h>
#include <limits.h>
#include <minix/minlib.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/mount.h>
2005-04-21 16:53:53 +02:00
#include <stdio.h>
int main(int argc, char **argv);
int find_mtab_entry(char *name);
void update_mtab(void);
void usage(void);
2005-04-21 16:53:53 +02:00
static char device[PATH_MAX], mount_point[PATH_MAX], type[MNTNAMELEN],
flags[MNTFLAGLEN];
2005-04-21 16:53:53 +02:00
int main(argc, argv)
int argc;
char *argv[];
{
int found;
int umount_flags = 0UL;
int c;
char *name;
while ((c = getopt (argc, argv, "e")) != -1)
{
switch (c) {
case 'e': umount_flags |= MS_EXISTING; break;
default: break;
}
}
if (argc - optind != 1) {
usage();
}
name = argv[optind];
found = find_mtab_entry(name);
if (minix_umount(name, umount_flags) < 0) {
2005-04-21 16:53:53 +02:00
if (errno == EINVAL)
std_err("umount: Device not mounted\n");
else if (errno == ENOTBLK)
2010-06-07 15:46:05 +02:00
std_err("umount: Not a mountpoint\n");
2005-04-21 16:53:53 +02:00
else
perror("umount");
exit(1);
}
if (found) {
printf("%s unmounted from %s\n", device, mount_point);
2005-04-21 16:53:53 +02:00
}
return(0);
}
int find_mtab_entry(name)
char *name;
2005-04-21 16:53:53 +02:00
{
/* Find a matching mtab entry for 'name' which may be a special or a path,
* and generate a new mtab file without this entry on the fly. Do not write
* out the result yet. Return whether we found a matching entry.
*/
char e_dev[PATH_MAX], e_mount_point[PATH_MAX], e_type[MNTNAMELEN],
e_flags[MNTFLAGLEN];
struct stat nstat, mstat;
int n, found;
2005-04-21 16:53:53 +02:00
if (load_mtab("umount") < 0) return 0;
if (stat(name, &nstat) != 0) return 0;
found = 0;
2005-04-21 16:53:53 +02:00
while (1) {
n = get_mtab_entry(e_dev, e_mount_point, e_type, e_flags);
2005-04-21 16:53:53 +02:00
if (n < 0) break;
if (strcmp(name, e_dev) == 0 || (stat(e_mount_point, &mstat) == 0 &&
mstat.st_dev == nstat.st_dev && mstat.st_ino == nstat.st_ino))
{
strlcpy(device, e_dev, PATH_MAX);
strlcpy(mount_point, e_mount_point, PATH_MAX);
strlcpy(type, e_type, MNTNAMELEN);
strlcpy(flags, e_flags, MNTFLAGLEN);
found = 1;
break;
2005-04-21 16:53:53 +02:00
}
}
return found;
}
2005-04-21 16:53:53 +02:00
void usage()
{
std_err("Usage: umount [-e] name\n");
2005-04-21 16:53:53 +02:00
exit(1);
}