Implement mount -a

This code needs refactoring.
This commit is contained in:
sin 2014-03-07 13:43:42 +00:00
parent a3e437c7ef
commit 4879ea07b4

27
mount.c
View File

@ -1,4 +1,5 @@
/* See LICENSE file for copyright and license details. */ /* See LICENSE file for copyright and license details. */
#include <errno.h>
#include <mntent.h> #include <mntent.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
@ -36,7 +37,7 @@ static struct option {
static void static void
usage(void) usage(void)
{ {
eprintf("usage: %s [-BMRdn] [-t fstype] [-o options] source target\n", eprintf("usage: %s [-BMRadn] [-t fstype] [-o options] [source] [target]\n",
argv0); argv0);
} }
@ -44,7 +45,7 @@ int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
int i, validopt; int i, validopt;
int oflag = 0; int oflag = 0, aflag = 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;
@ -65,6 +66,9 @@ main(int argc, char *argv[])
case 'R': case 'R':
flags |= MS_REC; flags |= MS_REC;
break; break;
case 'a':
aflag = 1;
break;
case 'd': case 'd':
data = EARGF(usage()); data = EARGF(usage());
break; break;
@ -91,7 +95,7 @@ main(int argc, char *argv[])
usage(); usage();
} ARGEND; } ARGEND;
if (argc < 1) if (argc < 1 && aflag == 0)
usage(); usage();
for (opt = opthead; opt; opt = opt->next) { for (opt = opthead; opt; opt = opt->next) {
@ -120,6 +124,9 @@ main(int argc, char *argv[])
if (oflag && !validopt) if (oflag && !validopt)
eprintf("unknown option: %s\n", opt->name); eprintf("unknown option: %s\n", opt->name);
if (aflag == 1)
goto domount;
source = argv[0]; source = argv[0];
target = argv[1]; target = argv[1];
@ -131,7 +138,7 @@ main(int argc, char *argv[])
fp = setmntent("/proc/mounts", "r"); fp = setmntent("/proc/mounts", "r");
if (!fp) if (!fp)
eprintf("setmntent %s:", "/proc/mounts"); eprintf("setmntent %s:", "/proc/mounts");
while ((me = getmntent(fp)) != NULL) { while ((me = getmntent(fp))) {
if (stat(me->mnt_dir, &st2) < 0) if (stat(me->mnt_dir, &st2) < 0)
eprintf("stat %s:", me->mnt_dir); eprintf("stat %s:", me->mnt_dir);
if (st1.st_dev == st2.st_dev && if (st1.st_dev == st2.st_dev &&
@ -148,6 +155,18 @@ 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:");
domount:
if (aflag == 1) {
fp = setmntent("/etc/fstab", "r");
if (!fp)
eprintf("setmntent %s:", "/etc/fstab");
while ((me = getmntent(fp)))
if (mount(me->mnt_fsname, me->mnt_dir, me->mnt_type,
0, me->mnt_opts) < 0)
fprintf(stderr, "mount: %s\n", strerror(errno));
endmntent(fp);
}
opt = opthead; opt = opthead;
while (opt) { while (opt) {
tmp = opt->next; tmp = opt->next;