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