Jens Michaelsen pointed out that Linksys' firmware download side moved.
[oweals/busybox.git] / util-linux / swaponoff.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * Mini swapon/swapoff implementation for busybox
4  *
5  * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
6  *
7  * Licensed under the GPL v2, see the file LICENSE in this tarball.
8  */
9
10 #include "busybox.h"
11 #include <mntent.h>
12 #include <dirent.h>
13 #include <errno.h>
14 #include <string.h>
15 #include <sys/swap.h>
16
17
18 static int swap_enable_disable(const char *device)
19 {
20         int status;
21         struct stat st;
22
23         xstat(device, &st);
24
25         /* test for holes */
26         if (S_ISREG(st.st_mode))
27                 if (st.st_blocks * 512 < st.st_size)
28                         bb_error_msg_and_die("swap file has holes");
29
30         if (bb_applet_name[5] == 'n')
31                 status = swapon(device, 0);
32         else
33                 status = swapoff(device);
34
35         if (status != 0) {
36                 bb_perror_msg("%s", device);
37                 return 1;
38         }
39
40         return 0;
41 }
42
43 static int do_em_all(void)
44 {
45         struct mntent *m;
46         FILE *f;
47         int err;
48
49         f = setmntent("/etc/fstab", "r");
50         if (f == NULL)
51                 bb_perror_msg_and_die("/etc/fstab");
52
53         err = 0;
54         while ((m = getmntent(f)) != NULL)
55                 if (strcmp(m->mnt_type, MNTTYPE_SWAP) == 0)
56                         err += swap_enable_disable(m->mnt_fsname);
57
58         endmntent(f);
59
60         return err;
61 }
62
63 #define DO_ALL    0x01
64
65 int swap_on_off_main(int argc, char **argv)
66 {
67         int ret;
68
69         if (argc == 1)
70                 bb_show_usage();
71
72         ret = bb_getopt_ulflags(argc, argv, "a");
73         if (ret & DO_ALL)
74                 return do_em_all();
75
76         ret = 0;
77         while (*++argv)
78                 ret += swap_enable_disable(*argv);
79         return ret;
80 }