ubase

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

commit 99a95f2734a3cd0e6e14c93b4a2ae245ec45f450
parent 30824954f8097195f82072b45e82c1a0bb5538b0
Author: Carlos J. Torres <vlaadbrain@gmail.com>
Date:   Sat Feb  8 12:44:50 -0500

add -a flag to enable all swap devices unless noauto opt is set

Diffstat:
swapon.c | 38++++++++++++++++++++++++++++++--------
1 file changed, 30 insertions(+), 8 deletions(-)
diff --git a/swapon.c b/swapon.c @@ -1,5 +1,6 @@ /* See LICENSE file for copyright and license details. */ #include <sys/swap.h> +#include <mntent.h> #include <errno.h> #include <stdio.h> #include <stdlib.h> @@ -9,7 +10,7 @@ static void usage(void) { - eprintf("usage: %s [-d] device\n", argv0); + eprintf("usage: %s [-d] [-a] device\n", argv0); } int @@ -18,8 +19,12 @@ main(int argc, char *argv[]) int i; int ret = EXIT_SUCCESS; int flags = 0; + int all = 0; ARGBEGIN { + case 'a': + all = 1; + break; case 'd': flags |= SWAP_FLAG_DISCARD; break; @@ -27,15 +32,32 @@ main(int argc, char *argv[]) usage(); } ARGEND; - if (argc < 1) + if (!all && argc < 1) usage(); - for (i = 0; i < argc; i++) { - ret = swapon(argv[i], flags); - if (ret < 0) { - fprintf(stderr, "swapon %s: %s\n", - argv[i], strerror(errno)); - ret = EXIT_FAILURE; + if (all) { + struct mntent *me = NULL; + FILE *fp; + + fp = setmntent("/etc/fstab", "r"); + while ((me = getmntent(fp)) != NULL) { + if (strcmp(me->mnt_type, MNTTYPE_SWAP) == 0 + && (hasmntopt(me, MNTOPT_NOAUTO) == NULL)) { + if (swapon(me->mnt_fsname, flags) < 0) { + fprintf(stderr, "swapon %s: %s\n", + me->mnt_fsname, strerror(errno)); + ret = EXIT_FAILURE; + } + } + } + endmntent(fp); + } else { + for (i = 0; i < argc; i++) { + if (swapon(argv[i], flags) < 0) { + fprintf(stderr, "swapon %s: %s\n", + argv[i], strerror(errno)); + ret = EXIT_FAILURE; + } } } return ret;