[hackers] [ubase] add -a flag to enable all swap devices unless noauto opt is set || Carlos J. Torres

From: <git_AT_suckless.org>
Date: Sun, 09 Feb 2014 12:08:24 +0100

commit 99a95f2734a3cd0e6e14c93b4a2ae245ec45f450
Author: Carlos J. Torres <vlaadbrain_AT_gmail.com>
Date: Sat Feb 8 12:44:50 2014 -0500

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

diff --git a/swapon.c b/swapon.c
index 0366aff..0610385 100644
--- a/swapon.c
+++ b/swapon.c
_AT_@ -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>
_AT_@ -9,7 +10,7 @@
 static void
 usage(void)
 {
- eprintf("usage: %s [-d] device
", argv0);
+ eprintf("usage: %s [-d] [-a] device
", argv0);
 }
 
 int
_AT_@ -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;
_AT_@ -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
",
- 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
",
+ 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
",
+ argv[i], strerror(errno));
+ ret = EXIT_FAILURE;
+ }
                 }
         }
         return ret;
Received on Sun Feb 09 2014 - 12:08:24 CET

This archive was generated by hypermail 2.3.0 : Sun Feb 09 2014 - 12:12:17 CET