aboutsummaryrefslogtreecommitdiff
path: root/coreutils/test.c
diff options
context:
space:
mode:
authorDenis Vlasenko <vda.linux@googlemail.com>2007-05-01 20:07:29 +0000
committerDenis Vlasenko <vda.linux@googlemail.com>2007-05-01 20:07:29 +0000
commitdcf4de20a1904bb31d498b6cc999a37f20b2783d (patch)
tree9c4c939bb250f37bd4f405b74723824cde1e8d7b /coreutils/test.c
parentf20de5bb42f9a4f2c8417f6a1a2db7e2f2cafd5b (diff)
downloadbusybox-w32-dcf4de20a1904bb31d498b6cc999a37f20b2783d.tar.gz
busybox-w32-dcf4de20a1904bb31d498b6cc999a37f20b2783d.tar.bz2
busybox-w32-dcf4de20a1904bb31d498b6cc999a37f20b2783d.zip
test: code size saving, no logic changes
ps: fix warning, make a bit smaller kill -l: make smaller & know much more signals function old new delta get_signum 121 153 +32 kill_main 826 843 +17 get_signame 44 36 -8 signals 252 224 -28 .rodata 131955 131923 -32 ------------------------------------------------------------------------------ (add/remove: 0/0 grow/shrink: 2/3 up/down: 49/-68) Total: -19 bytes
Diffstat (limited to 'coreutils/test.c')
-rw-r--r--coreutils/test.c177
1 files changed, 90 insertions, 87 deletions
diff --git a/coreutils/test.c b/coreutils/test.c
index e9b627638..70b392f38 100644
--- a/coreutils/test.c
+++ b/coreutils/test.c
@@ -85,12 +85,12 @@ enum token {
85 RPAREN, 85 RPAREN,
86 OPERAND 86 OPERAND
87}; 87};
88#define __is_int_op(a) (((unsigned char)((a) - INTEQ)) <= 5) 88#define is_int_op(a) (((unsigned char)((a) - INTEQ)) <= 5)
89#define __is_str_op(a) (((unsigned char)((a) - STREZ)) <= 5) 89#define is_str_op(a) (((unsigned char)((a) - STREZ)) <= 5)
90#define __is_file_op(a) (((unsigned char)((a) - FILNT)) <= 2) 90#define is_file_op(a) (((unsigned char)((a) - FILNT)) <= 2)
91#define __is_file_access(a) (((unsigned char)((a) - FILRD)) <= 2) 91#define is_file_access(a) (((unsigned char)((a) - FILRD)) <= 2)
92#define __is_file_type(a) (((unsigned char)((a) - FILREG)) <= 5) 92#define is_file_type(a) (((unsigned char)((a) - FILREG)) <= 5)
93#define __is_file_bit(a) (((unsigned char)((a) - FILSUID)) <= 2) 93#define is_file_bit(a) (((unsigned char)((a) - FILSUID)) <= 2)
94enum token_types { 94enum token_types {
95 UNOP, 95 UNOP,
96 BINOP, 96 BINOP,
@@ -100,64 +100,67 @@ enum token_types {
100}; 100};
101 101
102static const struct t_op { 102static const struct t_op {
103 const char * const op_text; 103 char op_text[4];
104 unsigned char op_num, op_type; 104 unsigned char op_num, op_type;
105} ops[] = { 105} ops[] = {
106 { 106 { "-r", FILRD , UNOP },
107 "-r", FILRD, UNOP}, { 107 { "-w", FILWR , UNOP },
108 "-w", FILWR, UNOP}, { 108 { "-x", FILEX , UNOP },
109 "-x", FILEX, UNOP}, { 109 { "-e", FILEXIST, UNOP },
110 "-e", FILEXIST, UNOP}, { 110 { "-f", FILREG , UNOP },
111 "-f", FILREG, UNOP}, { 111 { "-d", FILDIR , UNOP },
112 "-d", FILDIR, UNOP}, { 112 { "-c", FILCDEV , UNOP },
113 "-c", FILCDEV, UNOP}, { 113 { "-b", FILBDEV , UNOP },
114 "-b", FILBDEV, UNOP}, { 114 { "-p", FILFIFO , UNOP },
115 "-p", FILFIFO, UNOP}, { 115 { "-u", FILSUID , UNOP },
116 "-u", FILSUID, UNOP}, { 116 { "-g", FILSGID , UNOP },
117 "-g", FILSGID, UNOP}, { 117 { "-k", FILSTCK , UNOP },
118 "-k", FILSTCK, UNOP}, { 118 { "-s", FILGZ , UNOP },
119 "-s", FILGZ, UNOP}, { 119 { "-t", FILTT , UNOP },
120 "-t", FILTT, UNOP}, { 120 { "-z", STREZ , UNOP },
121 "-z", STREZ, UNOP}, { 121 { "-n", STRNZ , UNOP },
122 "-n", STRNZ, UNOP}, { 122 { "-h", FILSYM , UNOP }, /* for backwards compat */
123 "-h", FILSYM, UNOP}, /* for backwards compat */ 123
124 { 124 { "-O" , FILUID , UNOP },
125 "-O", FILUID, UNOP}, { 125 { "-G" , FILGID , UNOP },
126 "-G", FILGID, UNOP}, { 126 { "-L" , FILSYM , UNOP },
127 "-L", FILSYM, UNOP}, { 127 { "-S" , FILSOCK, UNOP },
128 "-S", FILSOCK, UNOP}, { 128 { "=" , STREQ , BINOP },
129 "=", STREQ, BINOP}, { 129 { "==" , STREQ , BINOP },
130 "==", STREQ, BINOP}, { 130 { "!=" , STRNE , BINOP },
131 "!=", STRNE, BINOP}, { 131 { "<" , STRLT , BINOP },
132 "<", STRLT, BINOP}, { 132 { ">" , STRGT , BINOP },
133 ">", STRGT, BINOP}, { 133 { "-eq", INTEQ , BINOP },
134 "-eq", INTEQ, BINOP}, { 134 { "-ne", INTNE , BINOP },
135 "-ne", INTNE, BINOP}, { 135 { "-ge", INTGE , BINOP },
136 "-ge", INTGE, BINOP}, { 136 { "-gt", INTGT , BINOP },
137 "-gt", INTGT, BINOP}, { 137 { "-le", INTLE , BINOP },
138 "-le", INTLE, BINOP}, { 138 { "-lt", INTLT , BINOP },
139 "-lt", INTLT, BINOP}, { 139 { "-nt", FILNT , BINOP },
140 "-nt", FILNT, BINOP}, { 140 { "-ot", FILOT , BINOP },
141 "-ot", FILOT, BINOP}, { 141 { "-ef", FILEQ , BINOP },
142 "-ef", FILEQ, BINOP}, { 142 { "!" , UNOT , BUNOP },
143 "!", UNOT, BUNOP}, { 143 { "-a" , BAND , BBINOP },
144 "-a", BAND, BBINOP}, { 144 { "-o" , BOR , BBINOP },
145 "-o", BOR, BBINOP}, { 145 { "(" , LPAREN , PAREN },
146 "(", LPAREN, PAREN}, { 146 { ")" , RPAREN , PAREN },
147 ")", RPAREN, PAREN}, {
148 0, 0, 0}
149}; 147};
150 148
151#ifdef CONFIG_FEATURE_TEST_64 149enum { NUM_OPS = sizeof(ops) / sizeof(ops[0]) };
150
151#if ENABLE_FEATURE_TEST_64
152typedef int64_t arith_t; 152typedef int64_t arith_t;
153#else 153#else
154typedef int arith_t; 154typedef int arith_t;
155#endif 155#endif
156 156
157/* Cannot eliminate these static data (do the G trick)
158 * because of bb_test usage from other applets */
157static char **t_wp; 159static char **t_wp;
158static struct t_op const *t_wp_op; 160static struct t_op const *t_wp_op;
159static gid_t *group_array; 161static gid_t *group_array;
160static int ngroups; 162static int ngroups;
163static jmp_buf leaving;
161 164
162static enum token t_lex(char *s); 165static enum token t_lex(char *s);
163static arith_t oexpr(enum token n); 166static arith_t oexpr(enum token n);
@@ -176,8 +179,6 @@ static int test_eaccess(char *path, int mode);
176static int is_a_group_member(gid_t gid); 179static int is_a_group_member(gid_t gid);
177static void initialize_group_array(void); 180static void initialize_group_array(void);
178 181
179static jmp_buf leaving;
180
181int bb_test(int argc, char **argv) 182int bb_test(int argc, char **argv)
182{ 183{
183 int res; 184 int res;
@@ -210,7 +211,7 @@ int bb_test(int argc, char **argv)
210 * isn't likely in the case of a shell. paranoia 211 * isn't likely in the case of a shell. paranoia
211 * prevails... 212 * prevails...
212 */ 213 */
213 ngroups = 0; 214 ngroups = 0;
214 215
215 /* Implement special cases from POSIX.2, section 4.62.4 */ 216 /* Implement special cases from POSIX.2, section 4.62.4 */
216 if (argc == 1) 217 if (argc == 1)
@@ -223,8 +224,9 @@ int bb_test(int argc, char **argv)
223 if (argc == 3) 224 if (argc == 3)
224 return *argv[2] != '\0'; 225 return *argv[2] != '\0';
225 _off = argc - 4; 226 _off = argc - 4;
226 if (t_lex(argv[2+_off]), t_wp_op && t_wp_op->op_type == BINOP) { 227 t_lex(argv[2 + _off]);
227 t_wp = &argv[1+_off]; 228 if (t_wp_op && t_wp_op->op_type == BINOP) {
229 t_wp = &argv[1 + _off];
228 return binop() == 0; 230 return binop() == 0;
229 } 231 }
230 } 232 }
@@ -238,6 +240,7 @@ int bb_test(int argc, char **argv)
238 return res; 240 return res;
239} 241}
240 242
243static void syntax(const char *op, const char *msg) ATTRIBUTE_NORETURN;
241static void syntax(const char *op, const char *msg) 244static void syntax(const char *op, const char *msg)
242{ 245{
243 if (op && *op) { 246 if (op && *op) {
@@ -296,20 +299,20 @@ static arith_t primary(enum token n)
296 if (*++t_wp == NULL) 299 if (*++t_wp == NULL)
297 syntax(t_wp_op->op_text, "argument expected"); 300 syntax(t_wp_op->op_text, "argument expected");
298 if (n == STREZ) 301 if (n == STREZ)
299 return strlen(*t_wp) == 0; 302 return t_wp[0][0] == '\0';
300 else if (n == STRNZ) 303 if (n == STRNZ)
301 return strlen(*t_wp) != 0; 304 return t_wp[0][0] != '\0';
302 else if (n == FILTT) 305 if (n == FILTT)
303 return isatty(getn(*t_wp)); 306 return isatty(getn(*t_wp));
304 else 307 return filstat(*t_wp, n);
305 return filstat(*t_wp, n);
306 } 308 }
307 309
308 if (t_lex(t_wp[1]), t_wp_op && t_wp_op->op_type == BINOP) { 310 t_lex(t_wp[1]);
311 if (t_wp_op && t_wp_op->op_type == BINOP) {
309 return binop(); 312 return binop();
310 } 313 }
311 314
312 return strlen(*t_wp) > 0; 315 return t_wp[0][0] != '\0';
313} 316}
314 317
315static int binop(void) 318static int binop(void)
@@ -322,10 +325,11 @@ static int binop(void)
322 (void) t_lex(*++t_wp); 325 (void) t_lex(*++t_wp);
323 op = t_wp_op; 326 op = t_wp_op;
324 327
325 if ((opnd2 = *++t_wp) == (char *) 0) 328 opnd2 = *++t_wp;
329 if (opnd2 == NULL)
326 syntax(op->op_text, "argument expected"); 330 syntax(op->op_text, "argument expected");
327 331
328 if (__is_int_op(op->op_num)) { 332 if (is_int_op(op->op_num)) {
329 val1 = getn(opnd1); 333 val1 = getn(opnd1);
330 val2 = getn(opnd2); 334 val2 = getn(opnd2);
331 if (op->op_num == INTEQ) 335 if (op->op_num == INTEQ)
@@ -341,7 +345,7 @@ static int binop(void)
341 if (op->op_num == INTLT) 345 if (op->op_num == INTLT)
342 return val1 < val2; 346 return val1 < val2;
343 } 347 }
344 if (__is_str_op(op->op_num)) { 348 if (is_str_op(op->op_num)) {
345 val1 = strcmp(opnd1, opnd2); 349 val1 = strcmp(opnd1, opnd2);
346 if (op->op_num == STREQ) 350 if (op->op_num == STREQ)
347 return val1 == 0; 351 return val1 == 0;
@@ -355,7 +359,7 @@ static int binop(void)
355 /* We are sure that these three are by now the only binops we didn't check 359 /* We are sure that these three are by now the only binops we didn't check
356 * yet, so we do not check if the class is correct: 360 * yet, so we do not check if the class is correct:
357 */ 361 */
358/* if (__is_file_op(op->op_num)) */ 362/* if (is_file_op(op->op_num)) */
359 { 363 {
360 struct stat b1, b2; 364 struct stat b1, b2;
361 365
@@ -390,7 +394,7 @@ static int filstat(char *nm, enum token mode)
390 return 0; 394 return 0;
391 if (mode == FILEXIST) 395 if (mode == FILEXIST)
392 return 1; 396 return 1;
393 else if (__is_file_access(mode)) { 397 if (is_file_access(mode)) {
394 if (mode == FILRD) 398 if (mode == FILRD)
395 i = R_OK; 399 i = R_OK;
396 if (mode == FILWR) 400 if (mode == FILWR)
@@ -399,7 +403,7 @@ static int filstat(char *nm, enum token mode)
399 i = X_OK; 403 i = X_OK;
400 return test_eaccess(nm, i) == 0; 404 return test_eaccess(nm, i) == 0;
401 } 405 }
402 else if (__is_file_type(mode)) { 406 if (is_file_type(mode)) {
403 if (mode == FILREG) 407 if (mode == FILREG)
404 i = S_IFREG; 408 i = S_IFREG;
405 if (mode == FILDIR) 409 if (mode == FILDIR)
@@ -422,10 +426,10 @@ static int filstat(char *nm, enum token mode)
422 return 0; 426 return 0;
423#endif 427#endif
424 } 428 }
425filetype: 429 filetype:
426 return ((s.st_mode & S_IFMT) == i); 430 return ((s.st_mode & S_IFMT) == i);
427 } 431 }
428 else if (__is_file_bit(mode)) { 432 if (is_file_bit(mode)) {
429 if (mode == FILSUID) 433 if (mode == FILSUID)
430 i = S_ISUID; 434 i = S_ISUID;
431 if (mode == FILSGID) 435 if (mode == FILSGID)
@@ -434,33 +438,33 @@ filetype:
434 i = S_ISVTX; 438 i = S_ISVTX;
435 return ((s.st_mode & i) != 0); 439 return ((s.st_mode & i) != 0);
436 } 440 }
437 else if (mode == FILGZ) 441 if (mode == FILGZ)
438 return s.st_size > 0L; 442 return s.st_size > 0L;
439 else if (mode == FILUID) 443 if (mode == FILUID)
440 return s.st_uid == geteuid(); 444 return s.st_uid == geteuid();
441 else if (mode == FILGID) 445 if (mode == FILGID)
442 return s.st_gid == getegid(); 446 return s.st_gid == getegid();
443 else 447 return 1; /* NOTREACHED */
444 return 1; /* NOTREACHED */
445
446} 448}
447 449
448static enum token t_lex(char *s) 450static enum token t_lex(char *s)
449{ 451{
450 struct t_op const *op = ops; 452 const struct t_op *op;
451 453
452 if (s == 0) { 454 t_wp_op = NULL;
453 t_wp_op = (struct t_op *) 0; 455 if (s == NULL) {
454 return EOI; 456 return EOI;
455 } 457 }
456 while (op->op_text) { 458
459 op = ops;
460 do {
457 if (strcmp(s, op->op_text) == 0) { 461 if (strcmp(s, op->op_text) == 0) {
458 t_wp_op = op; 462 t_wp_op = op;
459 return op->op_num; 463 return op->op_num;
460 } 464 }
461 op++; 465 op++;
462 } 466 } while (op < ops + NUM_OPS);
463 t_wp_op = (struct t_op *) 0; 467
464 return OPERAND; 468 return OPERAND;
465} 469}
466 470
@@ -469,14 +473,14 @@ static enum token t_lex(char *s)
469static arith_t getn(const char *s) 473static arith_t getn(const char *s)
470{ 474{
471 char *p; 475 char *p;
472#ifdef CONFIG_FEATURE_TEST_64 476#if ENABLE_FEATURE_TEST_64
473 long long r; 477 long long r;
474#else 478#else
475 long r; 479 long r;
476#endif 480#endif
477 481
478 errno = 0; 482 errno = 0;
479#ifdef CONFIG_FEATURE_TEST_64 483#if ENABLE_FEATURE_TEST_64
480 r = strtoll(s, &p, 10); 484 r = strtoll(s, &p, 10);
481#else 485#else
482 r = strtol(s, &p, 10); 486 r = strtol(s, &p, 10);
@@ -591,4 +595,3 @@ int test_main(int argc, char **argv)
591{ 595{
592 return bb_test(argc, argv); 596 return bb_test(argc, argv);
593} 597}
594