[hackers] [ubase] Convert mount(8) to mntent and kill grabmntinfo.[ch] || sin
commit 3f98a7abc81f12513f1c9e09fbdc57550da84990
Author: sin <sin_AT_2f30.org>
Date: Mon Feb 24 13:39:05 2014 +0000
Convert mount(8) to mntent and kill grabmntinfo.[ch]
diff --git a/Makefile b/Makefile
index f7c3cf8..63d4aae 100644
--- a/Makefile
+++ b/Makefile
_AT_@ -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
_AT_@ -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
_AT_@ -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 {
_AT_@ -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 {
_AT_@ -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
",
target);
_AT_@ -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
_AT_@ -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;
-}
Received on Mon Feb 24 2014 - 14:44:21 CET
This archive was generated by hypermail 2.3.0
: Mon Feb 24 2014 - 14:48:16 CET