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