svlogd: fix compat problem: svlogd -tt should timestanp stderr too
[busybox.git] / util-linux / swaponoff.c
blob6858d2619c784d849bf77e940ff194681faae063
1 /* vi: set sw=4 ts=4: */
2 /*
3 * Mini swapon/swapoff implementation for busybox
5 * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
7 * Licensed under the GPL version 2, see the file LICENSE in this tarball.
8 */
10 #include "libbb.h"
11 #include <mntent.h>
12 #include <sys/swap.h>
14 static int swap_enable_disable(char *device)
16 int status;
17 struct stat st;
19 xstat(device, &st);
21 #if ENABLE_DESKTOP
22 /* test for holes */
23 if (S_ISREG(st.st_mode))
24 if (st.st_blocks * 512 < st.st_size)
25 bb_error_msg("warning: swap file has holes");
26 #endif
28 if (applet_name[5] == 'n')
29 status = swapon(device, 0);
30 else
31 status = swapoff(device);
33 if (status != 0) {
34 bb_simple_perror_msg(device);
35 return 1;
38 return 0;
41 static int do_em_all(void)
43 struct mntent *m;
44 FILE *f;
45 int err;
47 f = setmntent("/etc/fstab", "r");
48 if (f == NULL)
49 bb_perror_msg_and_die("/etc/fstab");
51 err = 0;
52 while ((m = getmntent(f)) != NULL)
53 if (strcmp(m->mnt_type, MNTTYPE_SWAP) == 0)
54 err += swap_enable_disable(m->mnt_fsname);
56 endmntent(f);
58 return err;
61 int swap_on_off_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
62 int swap_on_off_main(int argc, char **argv)
64 int ret;
66 if (argc == 1)
67 bb_show_usage();
69 ret = getopt32(argv, "a");
70 if (ret)
71 return do_em_all();
73 /* ret = 0; redundant */
74 while (*++argv)
75 ret += swap_enable_disable(*argv);
76 return ret;