swapon.c (1132B)
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 [-dp] [-a] device\n", argv0); 15 } 16 17 int 18 main(int argc, char *argv[]) 19 { 20 int i; 21 int ret = 0; 22 int flags = 0; 23 int all = 0; 24 25 ARGBEGIN { 26 case 'a': 27 all = 1; 28 break; 29 case 'd': 30 flags |= SWAP_FLAG_DISCARD; 31 break; 32 case 'p': 33 flags |= SWAP_FLAG_PREFER; 34 break; 35 default: 36 usage(); 37 } ARGEND; 38 39 if (!all && argc < 1) 40 usage(); 41 42 if (all) { 43 struct mntent *me = NULL; 44 FILE *fp; 45 46 fp = setmntent("/etc/fstab", "r"); 47 if (!fp) 48 eprintf("setmntent %s:", "/etc/fstab"); 49 while ((me = getmntent(fp)) != NULL) { 50 if (strcmp(me->mnt_type, MNTTYPE_SWAP) == 0 51 && (hasmntopt(me, MNTOPT_NOAUTO) == NULL)) { 52 if (swapon(me->mnt_fsname, flags) < 0) { 53 weprintf("swapon %s:", me->mnt_fsname); 54 ret = 1; 55 } 56 } 57 } 58 endmntent(fp); 59 } else { 60 for (i = 0; i < argc; i++) { 61 if (swapon(argv[i], flags) < 0) { 62 weprintf("swapon %s:", argv[i]); 63 ret = 1; 64 } 65 } 66 } 67 return ret; 68 }