From 3f98a7abc81f12513f1c9e09fbdc57550da84990 Mon Sep 17 00:00:00 2001 From: sin Date: Mon, 24 Feb 2014 13:39:05 +0000 Subject: [PATCH] Convert mount(8) to mntent and kill grabmntinfo.[ch] --- Makefile | 3 +-- grabmntinfo.h | 8 -------- mount.c | 37 +++++++++++++++++-------------------- util/grabmntinfo.c | 31 ------------------------------- 4 files changed, 18 insertions(+), 61 deletions(-) delete mode 100644 grabmntinfo.h delete mode 100644 util/grabmntinfo.c diff --git a/Makefile b/Makefile index f7c3cf8..63d4aae 100644 --- a/Makefile +++ b/Makefile @@ -3,13 +3,12 @@ include config.mk .POSIX: .SUFFIXES: .c .o -HDR = arg.h config.def.h grabmntinfo.h proc.h reboot.h util.h +HDR = arg.h config.def.h proc.h reboot.h util.h LIB = \ util/agetcwd.o \ util/apathmax.o \ util/eprintf.o \ util/estrtol.o \ - util/grabmntinfo.o \ util/proc.o \ util/putword.o \ util/recurse.o \ diff --git a/grabmntinfo.h b/grabmntinfo.h deleted file mode 100644 index a13be67..0000000 --- a/grabmntinfo.h +++ /dev/null @@ -1,8 +0,0 @@ -/* See LICENSE file for copyright and license details. */ - -struct mntinfo { - char *fsname; - char *mntdir; -}; - -int grabmntinfo(struct mntinfo **minfo); diff --git a/mount.c b/mount.c index 0ebb453..9ae9787 100644 --- a/mount.c +++ b/mount.c @@ -1,12 +1,12 @@ /* See LICENSE file for copyright and license details. */ -#include -#include -#include -#include +#include #include #include #include -#include "grabmntinfo.h" +#include +#include +#include +#include #include "util.h" struct { @@ -43,14 +43,16 @@ usage(void) int main(int argc, char *argv[]) { - int i, validopt, siz = 0, oflag = 0; + int i, validopt; + int oflag = 0; unsigned long flags = 0; char *types = NULL, *arg = NULL, *p; const char *source; const char *target; struct stat st1, st2; void *data = NULL; - struct mntinfo *minfo = NULL; + struct mntent *me = NULL; + FILE *fp; struct option *opt, *tmp; ARGBEGIN { @@ -126,18 +128,19 @@ main(int argc, char *argv[]) source = NULL; if (stat(target, &st1) < 0) eprintf("stat %s:", target); - siz = grabmntinfo(&minfo); - if (!siz) - eprintf("grabmntinfo:"); - for (i = 0; i < siz; i++) { - if (stat(minfo[i].mntdir, &st2) < 0) - eprintf("stat %s:", minfo[i].mntdir); + fp = setmntent("/proc/mounts", "r"); + if (!fp) + eprintf("setmntent %s:", "/proc/mounts"); + while ((me = getmntent(fp)) != NULL) { + if (stat(me->mnt_dir, &st2) < 0) + eprintf("stat %s:", me->mnt_dir); if (st1.st_dev == st2.st_dev && st1.st_ino == st2.st_ino) { - source = minfo[i].fsname; + source = strdup(me->mnt_fsname); break; } } + endmntent(fp); if (!source) enprintf(EXIT_FAILURE, "can't find %s mountpoint\n", target); @@ -146,12 +149,6 @@ main(int argc, char *argv[]) if (mount(source, target, types, flags, data) < 0) eprintf("mount:"); - for (i = 0; i < siz; i++) { - free(minfo[i].fsname); - free(minfo[i].mntdir); - } - free(minfo); - opt = opthead; while (opt) { tmp = opt->next; diff --git a/util/grabmntinfo.c b/util/grabmntinfo.c deleted file mode 100644 index f59444c..0000000 --- a/util/grabmntinfo.c +++ /dev/null @@ -1,31 +0,0 @@ -/* See LICENSE file for copyright and license details. */ -#include -#include -#include -#include -#include "../grabmntinfo.h" -#include "../util.h" - -int -grabmntinfo(struct mntinfo **minfo) -{ - struct mntent *me; - struct mntinfo *mi = NULL; - int siz = 0; - FILE *fp; - - fp = setmntent("/proc/mounts", "r"); - if (!fp) - eprintf("setmntent:"); - while ((me = getmntent(fp))) { - mi = realloc(mi, (siz + 1) * sizeof(*mi)); - if (!mi) - eprintf("realloc:"); - mi[siz].fsname = strdup(me->mnt_fsname); - mi[siz].mntdir = strdup(me->mnt_dir); - siz++; - } - endmntent(fp); - *minfo = mi; - return siz; -}