ubase/umount.c

70 lines
1.1 KiB
C
Raw Normal View History

/* See LICENSE file for copyright and license details. */
2014-03-07 13:49:40 +00:00
#include <mntent.h>
2013-08-07 08:52:10 +00:00
#include <stdio.h>
2013-10-07 18:11:40 +00:00
#include <stdlib.h>
2014-03-12 14:35:20 +00:00
#include <string.h>
2014-03-07 13:49:40 +00:00
#include <sys/mount.h>
2013-08-07 08:52:10 +00:00
#include "util.h"
static void
usage(void)
{
2014-03-12 14:28:56 +00:00
weprintf("usage: %s [-lfn] target\n", argv0);
weprintf("usage: %s -a [-lfn]\n", argv0);
exit(EXIT_FAILURE);
2013-08-07 08:52:10 +00:00
}
int
main(int argc, char *argv[])
{
2013-08-07 08:52:10 +00:00
int i;
2014-03-07 13:49:40 +00:00
int aflag = 0;
int flags = 0;
2013-10-07 18:11:40 +00:00
int ret = EXIT_SUCCESS;
2014-03-07 13:49:40 +00:00
FILE *fp;
struct mntent *me;
2013-08-07 08:52:10 +00:00
ARGBEGIN {
2014-03-07 13:49:40 +00:00
case 'a':
aflag = 1;
break;
2013-08-07 08:52:10 +00:00
case 'f':
flags |= MNT_FORCE;
break;
case 'l':
flags |= MNT_DETACH;
2013-08-07 08:52:10 +00:00
break;
case 'n':
break;
2013-08-07 08:52:10 +00:00
default:
usage();
} ARGEND;
2013-08-10 10:38:30 +00:00
2014-03-07 13:49:40 +00:00
if (argc < 1 && aflag == 0)
2013-08-07 08:52:10 +00:00
usage();
2013-08-10 10:38:30 +00:00
2014-03-07 13:49:40 +00:00
if (aflag == 1) {
2014-03-12 14:33:04 +00:00
fp = setmntent("/etc/mtab", "r");
2014-03-07 13:49:40 +00:00
if (!fp)
2014-03-12 14:33:04 +00:00
eprintf("setmntent %s:", "/etc/mtab");
2014-03-07 13:49:40 +00:00
while ((me = getmntent(fp))) {
2014-03-12 14:35:20 +00:00
if (strcmp(me->mnt_type, "proc") == 0)
continue;
2014-03-07 13:49:40 +00:00
if (umount2(me->mnt_dir, flags) < 0) {
2014-03-12 14:20:35 +00:00
weprintf("umount2:");
2014-03-07 13:49:40 +00:00
ret = EXIT_FAILURE;
}
}
endmntent(fp);
return ret;
}
2013-08-07 08:52:10 +00:00
for (i = 0; i < argc; i++) {
if (umount2(argv[i], flags) < 0) {
2014-03-12 14:20:35 +00:00
weprintf("umount2:");
ret = EXIT_FAILURE;
}
2013-08-07 08:52:10 +00:00
}
return ret;
}