ubase

suckless linux base utils
git clone git://git.2f30.org/ubase
Log | Files | Refs | README | LICENSE

commit 3f98a7abc81f12513f1c9e09fbdc57550da84990
parent fa96b14fbce5a50e811008e9bb80797e40f1da62
Author: sin <sin@2f30.org>
Date:   Mon, 24 Feb 2014 13:39:05 +0000

Convert mount(8) to mntent and kill grabmntinfo.[ch]

Diffstat:
MMakefile | 3+--
Dgrabmntinfo.h | 8--------
Mmount.c | 37+++++++++++++++++--------------------
Dutil/grabmntinfo.c | 31-------------------------------
4 files changed, 18 insertions(+), 61 deletions(-)

diff --git 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 @@ -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 @@ -1,12 +1,12 @@ /* See LICENSE file for copyright and license details. */ -#include <sys/mount.h> -#include <sys/types.h> -#include <sys/stat.h> -#include <unistd.h> +#include <mntent.h> #include <stdio.h> #include <stdlib.h> #include <string.h> -#include "grabmntinfo.h" +#include <sys/mount.h> +#include <sys/stat.h> +#include <sys/types.h> +#include <unistd.h> #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 @@ -1,31 +0,0 @@ -/* See LICENSE file for copyright and license details. */ -#include <stdio.h> -#include <stdlib.h> -#include <string.h> -#include <mntent.h> -#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; -}