A few minor updates. ;-)
[oweals/busybox.git] / util-linux / swaponoff.c
1 /*
2  * Mini swapon/swapoff implementation for busybox
3  *
4  *
5  * Copyright (C) 1999 by Lineo, inc.
6  * Written by Erik Andersen <andersen@lineo.com>, <andersee@debian.org>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21  *
22  */
23
24 #include "internal.h"
25 #include <stdio.h>
26 #include <sys/mount.h>
27 #include <sys/swap.h>
28 #include <mntent.h>
29 #include <dirent.h>
30 #include <fstab.h>
31 #include <errno.h>
32
33
34 static int whichApp;
35 static const char* appName;
36
37 static const char swapoff_usage[] = 
38 "swapoff device\n"
39 "\nStop swapping virtual memory pages on the given device.\n";
40 static const char swapon_usage[] = 
41 "swapon device\n"
42 "\nStart swapping virtual memory pages on the given device.\n";
43
44
45 #define SWAPON_APP   1
46 #define SWAPOFF_APP  2
47
48
49 static void
50 swap_enable_disable( char *device)
51 {
52     int status;
53     if ( whichApp == SWAPON_APP )
54         status = swapon(device, 0);
55     else 
56         status = swapoff(device);
57
58     if ( status != 0 ) { 
59         perror(appName);
60         exit( FALSE);
61     }
62 }
63
64 static void
65 do_em_all()
66 {
67         struct mntent *m;
68         FILE *f = setmntent ("/etc/fstab", "r");
69
70         if (f == NULL) {
71             perror("/etc/fstab");
72             exit( FALSE); 
73         }
74         while ((m = getmntent (f)) != NULL) {
75             if (!strstr (m->mnt_type, MNTTYPE_SWAP)) {
76                     swap_enable_disable( m->mnt_fsname);
77             }
78         }
79         endmntent (f);
80         exit( TRUE);
81 }
82
83
84 extern int
85 swap_on_off_main(int argc, char * * argv)
86 {
87     struct stat statBuf;
88     if (stat("/etc/fstab", &statBuf) < 0) 
89         fprintf(stderr, "/etc/fstab file missing -- Please install one.\n\n");
90
91     if (strcmp(*argv, "swapon")==0) {
92         appName = *argv;
93         whichApp = SWAPON_APP;
94
95     } else {
96         appName = *argv;
97         whichApp = SWAPOFF_APP;
98     }
99
100     if (argc < 2)
101         goto usage_and_exit;
102     argc--;
103     argv++;
104
105     /* Parse any options */
106     while (**argv == '-') {
107         while (*++(*argv)) switch (**argv) {
108             case 'a':
109                 do_em_all();
110                 break;
111             default:
112                 goto usage_and_exit;
113         }
114     }
115     swap_enable_disable(*argv);
116     exit( TRUE);
117
118 usage_and_exit:
119     usage( (whichApp==SWAPON_APP)? swapon_usage : swapoff_usage);
120     exit( FALSE);
121 }
122