swapoff.c (965B)
1 /* See LICENSE file for copyright and license details. */ 2 #include <sys/swap.h> 3 4 #include <mntent.h> 5 #include <stdio.h> 6 #include <stdlib.h> 7 #include <string.h> 8 9 #include "util.h" 10 11 static void 12 usage(void) 13 { 14 eprintf("usage: %s -a | device\n", argv0); 15 } 16 17 int 18 main(int argc, char *argv[]) 19 { 20 int i; 21 int ret = 0; 22 int all = 0; 23 struct mntent *me; 24 FILE *fp; 25 26 ARGBEGIN { 27 case 'a': 28 all = 1; 29 break; 30 default: 31 usage(); 32 } ARGEND; 33 34 if ((!all && argc < 1) || (all && argc > 0)) 35 usage(); 36 37 if (all) { 38 fp = setmntent("/etc/fstab", "r"); 39 if (!fp) 40 eprintf("setmntent %s:", "/etc/fstab"); 41 while ((me = getmntent(fp)) != NULL) { 42 if (strcmp(me->mnt_type, MNTTYPE_SWAP) == 0) { 43 if (swapoff(me->mnt_fsname) < 0) { 44 weprintf("swapoff %s:", me->mnt_fsname); 45 ret = 1; 46 } 47 } 48 } 49 endmntent(fp); 50 } else { 51 for (i = 0; i < argc; i++) { 52 if (swapoff(argv[i]) < 0) { 53 weprintf("swapoff %s:", argv[i]); 54 ret = 1; 55 } 56 } 57 } 58 return ret; 59 }