diff options
Diffstat (limited to 'swaponoff.c')
-rw-r--r-- | swaponoff.c | 131 |
1 files changed, 67 insertions, 64 deletions
diff --git a/swaponoff.c b/swaponoff.c index 3c02bdd42..6bda22277 100644 --- a/swaponoff.c +++ b/swaponoff.c | |||
@@ -1,3 +1,4 @@ | |||
1 | /* vi: set sw=4 ts=4: */ | ||
1 | /* | 2 | /* |
2 | * Mini swapon/swapoff implementation for busybox | 3 | * Mini swapon/swapoff implementation for busybox |
3 | * | 4 | * |
@@ -32,91 +33,93 @@ | |||
32 | 33 | ||
33 | 34 | ||
34 | static int whichApp; | 35 | static int whichApp; |
35 | static const char* appName; | 36 | static const char *appName; |
36 | 37 | ||
37 | static const char swapoff_usage[] = | 38 | static const char swapoff_usage[] = |
38 | "swapoff device\n" | 39 | |
39 | "\nStop swapping virtual memory pages on the given device.\n"; | 40 | "swapoff device\n" |
40 | static const char swapon_usage[] = | 41 | "\nStop swapping virtual memory pages on the given device.\n"; |
41 | "swapon device\n" | 42 | static const char swapon_usage[] = |
42 | "\nStart swapping virtual memory pages on the given device.\n"; | 43 | |
44 | "swapon device\n" | ||
45 | "\nStart swapping virtual memory pages on the given device.\n"; | ||
43 | 46 | ||
44 | 47 | ||
45 | #define SWAPON_APP 1 | 48 | #define SWAPON_APP 1 |
46 | #define SWAPOFF_APP 2 | 49 | #define SWAPOFF_APP 2 |
47 | 50 | ||
48 | 51 | ||
49 | static void | 52 | static void swap_enable_disable(char *device) |
50 | swap_enable_disable( char *device) | ||
51 | { | 53 | { |
52 | int status; | 54 | int status; |
53 | if ( whichApp == SWAPON_APP ) | 55 | |
54 | status = swapon(device, 0); | 56 | if (whichApp == SWAPON_APP) |
55 | else | 57 | status = swapon(device, 0); |
56 | status = swapoff(device); | 58 | else |
57 | 59 | status = swapoff(device); | |
58 | if ( status != 0 ) { | 60 | |
59 | perror(appName); | 61 | if (status != 0) { |
60 | exit( FALSE); | 62 | perror(appName); |
61 | } | 63 | exit(FALSE); |
64 | } | ||
62 | } | 65 | } |
63 | 66 | ||
64 | static void | 67 | static void do_em_all() |
65 | do_em_all() | ||
66 | { | 68 | { |
67 | struct mntent *m; | 69 | struct mntent *m; |
68 | FILE *f = setmntent ("/etc/fstab", "r"); | 70 | FILE *f = setmntent("/etc/fstab", "r"); |
69 | 71 | ||
70 | if (f == NULL) { | 72 | if (f == NULL) { |
71 | perror("/etc/fstab"); | 73 | perror("/etc/fstab"); |
72 | exit( FALSE); | 74 | exit(FALSE); |
73 | } | 75 | } |
74 | while ((m = getmntent (f)) != NULL) { | 76 | while ((m = getmntent(f)) != NULL) { |
75 | if (!strstr (m->mnt_type, MNTTYPE_SWAP)) { | 77 | if (!strstr(m->mnt_type, MNTTYPE_SWAP)) { |
76 | swap_enable_disable( m->mnt_fsname); | 78 | swap_enable_disable(m->mnt_fsname); |
77 | } | 79 | } |
78 | } | 80 | } |
79 | endmntent (f); | 81 | endmntent(f); |
80 | exit( TRUE); | 82 | exit(TRUE); |
81 | } | 83 | } |
82 | 84 | ||
83 | 85 | ||
84 | extern int | 86 | extern int swap_on_off_main(int argc, char **argv) |
85 | swap_on_off_main(int argc, char * * argv) | ||
86 | { | 87 | { |
87 | struct stat statBuf; | 88 | struct stat statBuf; |
88 | if (stat("/etc/fstab", &statBuf) < 0) | 89 | |
89 | fprintf(stderr, "/etc/fstab file missing -- Please install one.\n\n"); | 90 | if (stat("/etc/fstab", &statBuf) < 0) |
90 | 91 | fprintf(stderr, | |
91 | if (strcmp(*argv, "swapon")==0) { | 92 | "/etc/fstab file missing -- Please install one.\n\n"); |
92 | appName = *argv; | 93 | |
93 | whichApp = SWAPON_APP; | 94 | if (strcmp(*argv, "swapon") == 0) { |
94 | 95 | appName = *argv; | |
95 | } else { | 96 | whichApp = SWAPON_APP; |
96 | appName = *argv; | 97 | |
97 | whichApp = SWAPOFF_APP; | 98 | } else { |
98 | } | 99 | appName = *argv; |
99 | 100 | whichApp = SWAPOFF_APP; | |
100 | if (argc < 2) | 101 | } |
101 | goto usage_and_exit; | 102 | |
102 | argc--; | 103 | if (argc < 2) |
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; | 104 | goto usage_and_exit; |
105 | argc--; | ||
106 | argv++; | ||
107 | |||
108 | /* Parse any options */ | ||
109 | while (**argv == '-') { | ||
110 | while (*++(*argv)) | ||
111 | switch (**argv) { | ||
112 | case 'a': | ||
113 | do_em_all(); | ||
114 | break; | ||
115 | default: | ||
116 | goto usage_and_exit; | ||
117 | } | ||
113 | } | 118 | } |
114 | } | 119 | swap_enable_disable(*argv); |
115 | swap_enable_disable(*argv); | 120 | exit(TRUE); |
116 | exit( TRUE); | ||
117 | 121 | ||
118 | usage_and_exit: | 122 | usage_and_exit: |
119 | usage( (whichApp==SWAPON_APP)? swapon_usage : swapoff_usage); | 123 | usage((whichApp == SWAPON_APP) ? swapon_usage : swapoff_usage); |
120 | exit( FALSE); | 124 | exit(FALSE); |
121 | } | 125 | } |
122 | |||