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

This commit is contained in:
sin 2014-02-24 13:39:05 +00:00
parent fa96b14fbc
commit 3f98a7abc8
4 changed files with 18 additions and 61 deletions

View File

@ -3,13 +3,12 @@ include config.mk
.POSIX: .POSIX:
.SUFFIXES: .c .o .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 = \ LIB = \
util/agetcwd.o \ util/agetcwd.o \
util/apathmax.o \ util/apathmax.o \
util/eprintf.o \ util/eprintf.o \
util/estrtol.o \ util/estrtol.o \
util/grabmntinfo.o \
util/proc.o \ util/proc.o \
util/putword.o \ util/putword.o \
util/recurse.o \ util/recurse.o \

View File

@ -1,8 +0,0 @@
/* See LICENSE file for copyright and license details. */
struct mntinfo {
char *fsname;
char *mntdir;
};
int grabmntinfo(struct mntinfo **minfo);

37
mount.c
View File

@ -1,12 +1,12 @@
/* See LICENSE file for copyright and license details. */ /* See LICENSE file for copyright and license details. */
#include <sys/mount.h> #include <mntent.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.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" #include "util.h"
struct { struct {
@ -43,14 +43,16 @@ usage(void)
int int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
int i, validopt, siz = 0, oflag = 0; int i, validopt;
int oflag = 0;
unsigned long flags = 0; unsigned long flags = 0;
char *types = NULL, *arg = NULL, *p; char *types = NULL, *arg = NULL, *p;
const char *source; const char *source;
const char *target; const char *target;
struct stat st1, st2; struct stat st1, st2;
void *data = NULL; void *data = NULL;
struct mntinfo *minfo = NULL; struct mntent *me = NULL;
FILE *fp;
struct option *opt, *tmp; struct option *opt, *tmp;
ARGBEGIN { ARGBEGIN {
@ -126,18 +128,19 @@ main(int argc, char *argv[])
source = NULL; source = NULL;
if (stat(target, &st1) < 0) if (stat(target, &st1) < 0)
eprintf("stat %s:", target); eprintf("stat %s:", target);
siz = grabmntinfo(&minfo); fp = setmntent("/proc/mounts", "r");
if (!siz) if (!fp)
eprintf("grabmntinfo:"); eprintf("setmntent %s:", "/proc/mounts");
for (i = 0; i < siz; i++) { while ((me = getmntent(fp)) != NULL) {
if (stat(minfo[i].mntdir, &st2) < 0) if (stat(me->mnt_dir, &st2) < 0)
eprintf("stat %s:", minfo[i].mntdir); eprintf("stat %s:", me->mnt_dir);
if (st1.st_dev == st2.st_dev && if (st1.st_dev == st2.st_dev &&
st1.st_ino == st2.st_ino) { st1.st_ino == st2.st_ino) {
source = minfo[i].fsname; source = strdup(me->mnt_fsname);
break; break;
} }
} }
endmntent(fp);
if (!source) if (!source)
enprintf(EXIT_FAILURE, "can't find %s mountpoint\n", enprintf(EXIT_FAILURE, "can't find %s mountpoint\n",
target); target);
@ -146,12 +149,6 @@ main(int argc, char *argv[])
if (mount(source, target, types, flags, data) < 0) if (mount(source, target, types, flags, data) < 0)
eprintf("mount:"); eprintf("mount:");
for (i = 0; i < siz; i++) {
free(minfo[i].fsname);
free(minfo[i].mntdir);
}
free(minfo);
opt = opthead; opt = opthead;
while (opt) { while (opt) {
tmp = opt->next; tmp = opt->next;

View File

@ -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;
}