[hackers] [ubase] disable all swap devices || Carlos J. Torres

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

commit 9938320eeabc1845110d7e7dfadeb74f274b51aa
Author: Carlos J. Torres <vlaadbrain_AT_gmail.com>
Date: Sat Feb 8 12:54:04 2014 -0500

    disable all swap devices

diff --git a/swapoff.c b/swapoff.c
index 88762fe..68b1f02 100644
--- a/swapoff.c
+++ b/swapoff.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 device
", argv0);
+ eprintf("usage: %s [-a] device
", argv0);
 }
 
 int
_AT_@ -17,21 +18,41 @@ main(int argc, char *argv[])
 {
         int i;
         int ret = EXIT_SUCCESS;
+ int all = 0;
 
         ARGBEGIN {
+ case 'a':
+ all = 1;
+ break;
         default:
                 usage();
         } ARGEND;
 
- if (argc < 1)
+ if (!all && argc < 1)
                 usage();
 
- for (i = 0; i < argc; i++) {
- ret = swapoff(argv[i]);
- if (ret < 0) {
- fprintf(stderr, "swapoff %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) {
+ if (swapoff(me->mnt_fsname) < 0) {
+ fprintf(stderr, "swapoff %s: %s
",
+ me->mnt_fsname, strerror(errno));
+ ret = EXIT_FAILURE;
+ }
+ }
+ }
+ endmntent(fp);
+ } else {
+ for (i = 0; i < argc; i++) {
+ if (swapoff(argv[i]) < 0) {
+ fprintf(stderr, "swapoff %s: %s
",
+ argv[i], strerror(errno));
+ ret = EXIT_FAILURE;
+ }
                 }
         }
         return ret;
Received on Sun Feb 09 2014 - 12:08:43 CET

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