aboutsummaryrefslogtreecommitdiff
path: root/busybox.c
blob: 15e1bef87ce54a174ba206f2f4732ca9580df6b9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
#include "internal.h"
#include <stdio.h>
#include <string.h>
#include <errno.h>

static int been_there_done_that = 0;

static const struct Applet applets[] = {

#ifdef BB_BUSYBOX		//bin
    {"busybox", busybox_main},
#endif
#ifdef BB_BLOCK_DEVICE		//sbin
    {"block_device", block_device_main},
#endif
#ifdef BB_CAT			//bin
    {"cat", cat_more_main},
#endif
#ifdef BB_CHMOD			//bin
    {"chmod", chmod_main},
#endif
#ifdef BB_CHOWN			//bin
    {"chown", chown_main},
    {"chgrp", chown_main},
#endif
#ifdef BB_CHROOT		//sbin
    {"chroot", chroot_main},
#endif
#ifdef BB_CLEAR			//usr/bin
    {"clear", clear_main},
#endif
#ifdef BB_CP			//bin
    {"cp", dyadic_main},
#endif
#ifdef BB_DATE			//bin
    {"date", date_main},
#endif
#ifdef BB_DD			//bin
    {"dd", dd_main},
#endif
#ifdef BB_DF			//bin
    {"df", df_main},
#endif
#ifdef BB_DMESG			//bin
    {"dmesg", dmesg_main},
#endif
#ifdef BB_DUTMP			//usr/sbin
    {"dutmp", cat_more_main},
#endif
#ifdef BB_FALSE			//bin
    {"false", false_main},
#endif
#ifdef BB_FDFLUSH		//bin
    {"fdflush", monadic_main},
#endif
#ifdef BB_FIND			//usr/bin
    {"find", find_main},
#endif
#ifdef BB_GREP			//bin
    {"grep", grep_main},
#endif
#ifdef BB_HALT			//sbin
    {"halt", halt_main},
#endif
#ifdef BB_INIT			//sbin
    {"init", init_main},
#endif
#ifdef BB_KILL			//bin
    {"kill", kill_main},
#endif
#ifdef BB_LENGTH		//usr/bin
    {"length", length_main},
#endif
#ifdef BB_LN			//bin
    {"ln", dyadic_main},
#endif
#ifdef BB_LOADKMAP		//sbin
    {"loadkmap", loadkmap_main},
#endif
#ifdef BB_LOSETUP		//sbin
    {"losetup", losetup_main},
#endif
#ifdef BB_LS			//bin
    {"ls", ls_main},
#endif
#ifdef BB_MAKEDEVS		//sbin
    {"makedevs", makedevs_main},
#endif
#ifdef BB_MATH			//usr/bin
    {"math", math_main},
#endif
#ifdef BB_MKDIR			//bin
    {"mkdir", monadic_main},
#endif
#ifdef BB_MKNOD			//bin
    {"mknod", mknod_main},
#endif
#ifdef BB_MKSWAP		//sbin
    {"mkswap", mkswap_main},
#endif
#ifdef BB_MNC			//usr/bin
    {"mnc", mnc_main},
#endif
#ifdef BB_MORE			//bin
    {"more", more_main},
#endif
#ifdef BB_MOUNT			//bin
    {"mount", mount_main},
#endif
#ifdef BB_MT			//bin
    {"mt", mt_main},
#endif
#ifdef BB_MV			//bin
    {"mv", mv_main},
#endif
#ifdef BB_PRINTF		//usr/bin
    {"printf", printf_main},
#endif
#ifdef BB_PWD			//bin
    {"pwd", pwd_main},
#endif
#ifdef BB_REBOOT		//sbin
    {"reboot", reboot_main},
#endif
#ifdef BB_RM			//bin
    {"rm", rm_main},
#endif
#ifdef BB_RMDIR			//bin
    {"rmdir", monadic_main},
#endif
#ifdef BB_SLEEP			//bin
    {"sleep", sleep_main},
#endif
#ifdef BB_TAR			//bin
    {"tar", tar_main},
#endif
#ifdef BB_SWAPOFF		//sbin
    {"swapoff", monadic_main},
#endif
#ifdef BB_SWAPON		//sbin
    {"swapon", monadic_main},
#endif
#ifdef BB_SYNC			//bin
    {"sync", sync_main},
#endif
#ifdef BB_TOUCH			//usr/bin
    {"touch", touch_main},
#endif
#ifdef BB_TRUE			//bin
    {"true", true_main},
#endif
#ifdef BB_UMOUNT		//bin
    {"umount", umount_main},
#endif
#ifdef BB_UPDATE		//sbin
    {"update", update_main},
#endif
#ifdef BB_ZCAT			//bin
    {"zcat", zcat_main},
    {"gunzip", zcat_main},
#endif
#ifdef BB_GZIP			//bin
    {"gzip", gzip_main},
#endif
    {0}
};

int main(int argc, char **argv)
{
    char *s = argv[0];
    char *name = argv[0];
    const struct Applet *a = applets;

    while (*s != '\0') {
	if (*s++ == '/')
	    name = s;
    }

    while (a->name != 0) {
	if (strcmp(name, a->name) == 0) {
	    int status;

	    status = ((*(a->main)) (argc, argv));
	    if (status < 0) {
		fprintf(stderr, "%s: %s\n", a->name, strerror(errno));
	    }
	    fprintf(stderr, "\n");
	    exit(status);
	}
	a++;
    }
    return (busybox_main(argc, argv));
}


int busybox_main(int argc, char **argv)
{
    int col=0;
    argc--;
    argv++;

    /* If we've already been here once, exit now */
    if (been_there_done_that == 1)
	return -1;
    been_there_done_that = 1;

    if (argc < 1) {
	const struct Applet *a = applets;
	fprintf(stderr, "BusyBox v%s (%s) multi-call binary -- GPL2\n",
		BB_VER, BB_BT);
	fprintf(stderr, "\nUsage:\t[function] [arguments]...\n");
	fprintf(stderr, "\tbusybox [function] [arguments]...\n");
	fprintf(stderr,
		"\n\tMost people will create a symlink to busybox for each\n"
		"\tfunction name, and busybox will act like whatever you invoke it as.\n");
	fprintf(stderr, "\nCurrently defined functions:\n");

	while (a->name != 0) {
	    col+=fprintf(stderr, "%s%s", ((col==0)? "\t":", "), a->name);
	    a++;
	    if (col>60) {
		fprintf(stderr, ",\n");
		col=0;
	    }
	}
	fprintf(stderr, "\n\n");
	exit(-1);
    } else
	return (main(argc, argv));
}