morpheus-base

morpheus base system
git clone git://git.2f30.org/morpheus-base
Log | Files | Refs

swapoff.c (952B)


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