mirror of git://git.suckless.org/sbase
Convert mount(8) to mntent and kill grabmntinfo.[ch]
This commit is contained in:
parent
fa96b14fbc
commit
3f98a7abc8
3
Makefile
3
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 \
|
||||
|
|
|
@ -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
37
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;
|
||||
|
|
|
@ -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;
|
||||
}
|
Loading…
Reference in New Issue