micro-utils/src/umount.c

71 lines
1.2 KiB
C
Raw Normal View History

2024-07-01 10:23:00 +00:00
#define _MOUNT_C
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <mntent.h>
#include <sys/mount.h>
#include "config.h"
2024-07-09 12:43:55 +00:00
static int parse_fstab(unsigned long flag) {
2024-07-01 10:23:00 +00:00
FILE *fp = setmntent(MOUNT_CFG, "r");
if (fp == NULL) {
fprintf(stderr, "umount: %s\n", strerror(errno));
return 1;
}
struct mntent *me;
int ret = 0;
while ((me = getmntent(fp))) {
puts(me->mnt_dir);
if (umount2(me->mnt_dir, flag) < 0) {
fprintf(stderr, "umount: %s: %s\n", me->mnt_dir, strerror(errno));
ret = 1;
}
}
endmntent(fp);
return ret;
}
int main(int argc, char **argv) {
unsigned long flag = 0;
int opt;
while ((opt = getopt(argc, argv, "afl")) != -1) {
switch (opt) {
case 'a':
return parse_fstab(flag);
case 'f':
flag |= MNT_FORCE;
break;
case 'l':
flag |= MNT_DETACH;
break;
default:
2024-07-03 14:22:50 +00:00
puts("umount [afl] [dst1 dst2...]\n\t-a Umount all\n\t-f Force umount\n\t-l Lazy umount");
2024-07-01 10:23:00 +00:00
return 0;
}
}
argv += optind;
argc -= optind;
int ret = 0;
for (int i = 0; i < argc; i++) {
if (umount2(argv[i], flag) < 0) {
fprintf(stderr, "umount: %s: %s\n", argv[i], strerror(errno));
ret = 1;
}
}
return ret;
}