URI: 
       umount.c - ubase - suckless linux base utils
  HTML git clone git://git.suckless.org/ubase
   DIR Log
   DIR Files
   DIR Refs
   DIR README
   DIR LICENSE
       ---
       umount.c (1404B)
       ---
            1 /* See LICENSE file for copyright and license details. */
            2 #include <sys/mount.h>
            3 
            4 #include <mntent.h>
            5 #include <stdio.h>
            6 #include <stdlib.h>
            7 #include <string.h>
            8 
            9 #include "util.h"
           10 
           11 static int
           12 umountall(int flags)
           13 {
           14         FILE *fp;
           15         struct mntent *me;
           16         int ret = 0;
           17         char **mntdirs = NULL;
           18         int len = 0;
           19 
           20         fp = setmntent("/proc/mounts", "r");
           21         if (!fp)
           22                 eprintf("setmntent %s:", "/proc/mounts");
           23         while ((me = getmntent(fp))) {
           24                 if (strcmp(me->mnt_type, "proc") == 0)
           25                         continue;
           26                 mntdirs = erealloc(mntdirs, ++len * sizeof(*mntdirs));
           27                 mntdirs[len - 1] = estrdup(me->mnt_dir);
           28         }
           29         endmntent(fp);
           30         while (--len >= 0) {
           31                 if (umount2(mntdirs[len], flags) < 0) {
           32                         weprintf("umount2 %s:", mntdirs[len]);
           33                         ret = 1;
           34                 }
           35                 free(mntdirs[len]);
           36         }
           37         free(mntdirs);
           38         return ret;
           39 }
           40 
           41 static void
           42 usage(void)
           43 {
           44         weprintf("usage: %s [-lfn] target...\n", argv0);
           45         weprintf("usage: %s -a [-lfn]\n", argv0);
           46         exit(1);
           47 }
           48 
           49 int
           50 main(int argc, char *argv[])
           51 {
           52         int i;
           53         int aflag = 0;
           54         int flags = 0;
           55         int ret = 0;
           56 
           57         ARGBEGIN {
           58         case 'a':
           59                 aflag = 1;
           60                 break;
           61         case 'f':
           62                 flags |= MNT_FORCE;
           63                 break;
           64         case 'l':
           65                 flags |= MNT_DETACH;
           66                 break;
           67         case 'n':
           68                 break;
           69         default:
           70                 usage();
           71         } ARGEND;
           72 
           73         if (argc < 1 && aflag == 0)
           74                 usage();
           75 
           76         if (aflag == 1)
           77                 return umountall(flags);
           78 
           79         for (i = 0; i < argc; i++) {
           80                 if (umount2(argv[i], flags) < 0) {
           81                         weprintf("umount2 %s:", argv[i]);
           82                         ret = 1;
           83                 }
           84         }
           85         return ret;
           86 }