ubase

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

swapon.c (1145B)


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