diff options
author | Glenn L McGrath <bug1@ihug.co.nz> | 2002-08-23 06:05:11 +0000 |
---|---|---|
committer | Glenn L McGrath <bug1@ihug.co.nz> | 2002-08-23 06:05:11 +0000 |
commit | acfc0d872e159590376f0f11d555326e1e052f58 (patch) | |
tree | f72a7e9f5f7d4a3cd35ec856cf1affd9f82d3d02 | |
parent | 03195fc16f400798fe27640ccb46ed39f4ee7f4a (diff) | |
download | busybox-w32-acfc0d872e159590376f0f11d555326e1e052f58.tar.gz busybox-w32-acfc0d872e159590376f0f11d555326e1e052f58.tar.bz2 busybox-w32-acfc0d872e159590376f0f11d555326e1e052f58.zip |
Run through indent, fix comments
-rw-r--r-- | coreutils/test.c | 238 |
1 files changed, 103 insertions, 135 deletions
diff --git a/coreutils/test.c b/coreutils/test.c index 3404b02b4..83e31ea5c 100644 --- a/coreutils/test.c +++ b/coreutils/test.c | |||
@@ -110,47 +110,49 @@ enum token_types { | |||
110 | static const struct t_op { | 110 | static const struct t_op { |
111 | const char *op_text; | 111 | const char *op_text; |
112 | short op_num, op_type; | 112 | short op_num, op_type; |
113 | } ops [] = { | 113 | } ops[] = { |
114 | {"-r", FILRD, UNOP}, | 114 | { |
115 | {"-w", FILWR, UNOP}, | 115 | "-r", FILRD, UNOP}, { |
116 | {"-x", FILEX, UNOP}, | 116 | "-w", FILWR, UNOP}, { |
117 | {"-e", FILEXIST,UNOP}, | 117 | "-x", FILEX, UNOP}, { |
118 | {"-f", FILREG, UNOP}, | 118 | "-e", FILEXIST, UNOP}, { |
119 | {"-d", FILDIR, UNOP}, | 119 | "-f", FILREG, UNOP}, { |
120 | {"-c", FILCDEV,UNOP}, | 120 | "-d", FILDIR, UNOP}, { |
121 | {"-b", FILBDEV,UNOP}, | 121 | "-c", FILCDEV, UNOP}, { |
122 | {"-p", FILFIFO,UNOP}, | 122 | "-b", FILBDEV, UNOP}, { |
123 | {"-u", FILSUID,UNOP}, | 123 | "-p", FILFIFO, UNOP}, { |
124 | {"-g", FILSGID,UNOP}, | 124 | "-u", FILSUID, UNOP}, { |
125 | {"-k", FILSTCK,UNOP}, | 125 | "-g", FILSGID, UNOP}, { |
126 | {"-s", FILGZ, UNOP}, | 126 | "-k", FILSTCK, UNOP}, { |
127 | {"-t", FILTT, UNOP}, | 127 | "-s", FILGZ, UNOP}, { |
128 | {"-z", STREZ, UNOP}, | 128 | "-t", FILTT, UNOP}, { |
129 | {"-n", STRNZ, UNOP}, | 129 | "-z", STREZ, UNOP}, { |
130 | {"-h", FILSYM, UNOP}, /* for backwards compat */ | 130 | "-n", STRNZ, UNOP}, { |
131 | {"-O", FILUID, UNOP}, | 131 | "-h", FILSYM, UNOP}, /* for backwards compat */ |
132 | {"-G", FILGID, UNOP}, | 132 | { |
133 | {"-L", FILSYM, UNOP}, | 133 | "-O", FILUID, UNOP}, { |
134 | {"-S", FILSOCK,UNOP}, | 134 | "-G", FILGID, UNOP}, { |
135 | {"=", STREQ, BINOP}, | 135 | "-L", FILSYM, UNOP}, { |
136 | {"!=", STRNE, BINOP}, | 136 | "-S", FILSOCK, UNOP}, { |
137 | {"<", STRLT, BINOP}, | 137 | "=", STREQ, BINOP}, { |
138 | {">", STRGT, BINOP}, | 138 | "!=", STRNE, BINOP}, { |
139 | {"-eq", INTEQ, BINOP}, | 139 | "<", STRLT, BINOP}, { |
140 | {"-ne", INTNE, BINOP}, | 140 | ">", STRGT, BINOP}, { |
141 | {"-ge", INTGE, BINOP}, | 141 | "-eq", INTEQ, BINOP}, { |
142 | {"-gt", INTGT, BINOP}, | 142 | "-ne", INTNE, BINOP}, { |
143 | {"-le", INTLE, BINOP}, | 143 | "-ge", INTGE, BINOP}, { |
144 | {"-lt", INTLT, BINOP}, | 144 | "-gt", INTGT, BINOP}, { |
145 | {"-nt", FILNT, BINOP}, | 145 | "-le", INTLE, BINOP}, { |
146 | {"-ot", FILOT, BINOP}, | 146 | "-lt", INTLT, BINOP}, { |
147 | {"-ef", FILEQ, BINOP}, | 147 | "-nt", FILNT, BINOP}, { |
148 | {"!", UNOT, BUNOP}, | 148 | "-ot", FILOT, BINOP}, { |
149 | {"-a", BAND, BBINOP}, | 149 | "-ef", FILEQ, BINOP}, { |
150 | {"-o", BOR, BBINOP}, | 150 | "!", UNOT, BUNOP}, { |
151 | {"(", LPAREN, PAREN}, | 151 | "-a", BAND, BBINOP}, { |
152 | {")", RPAREN, PAREN}, | 152 | "-o", BOR, BBINOP}, { |
153 | {0, 0, 0} | 153 | "(", LPAREN, PAREN}, { |
154 | ")", RPAREN, PAREN}, { | ||
155 | 0, 0, 0} | ||
154 | }; | 156 | }; |
155 | 157 | ||
156 | static char **t_wp; | 158 | static char **t_wp; |
@@ -158,7 +160,7 @@ static struct t_op const *t_wp_op; | |||
158 | static gid_t *group_array = NULL; | 160 | static gid_t *group_array = NULL; |
159 | static int ngroups; | 161 | static int ngroups; |
160 | 162 | ||
161 | static enum token t_lex(char* s); | 163 | static enum token t_lex(char *s); |
162 | static int oexpr(enum token n); | 164 | static int oexpr(enum token n); |
163 | static int aexpr(enum token n); | 165 | static int aexpr(enum token n); |
164 | static int nexpr(enum token n); | 166 | static int nexpr(enum token n); |
@@ -174,10 +176,9 @@ static int test_eaccess(char *path, int mode); | |||
174 | static int is_a_group_member(gid_t gid); | 176 | static int is_a_group_member(gid_t gid); |
175 | static void initialize_group_array(void); | 177 | static void initialize_group_array(void); |
176 | 178 | ||
177 | extern int | 179 | extern int test_main(int argc, char **argv) |
178 | test_main(int argc, char** argv) | ||
179 | { | 180 | { |
180 | int res; | 181 | int res; |
181 | 182 | ||
182 | if (strcmp(applet_name, "[") == 0) { | 183 | if (strcmp(applet_name, "[") == 0) { |
183 | if (strcmp(argv[--argc], "]")) | 184 | if (strcmp(argv[--argc], "]")) |
@@ -187,29 +188,27 @@ test_main(int argc, char** argv) | |||
187 | /* Implement special cases from POSIX.2, section 4.62.4 */ | 188 | /* Implement special cases from POSIX.2, section 4.62.4 */ |
188 | switch (argc) { | 189 | switch (argc) { |
189 | case 1: | 190 | case 1: |
190 | exit( 1); | 191 | exit(1); |
191 | case 2: | 192 | case 2: |
192 | exit (*argv[1] == '\0'); | 193 | exit(*argv[1] == '\0'); |
193 | case 3: | 194 | case 3: |
194 | if (argv[1][0] == '!' && argv[1][1] == '\0') { | 195 | if (argv[1][0] == '!' && argv[1][1] == '\0') { |
195 | exit (!(*argv[2] == '\0')); | 196 | exit(!(*argv[2] == '\0')); |
196 | } | 197 | } |
197 | break; | 198 | break; |
198 | case 4: | 199 | case 4: |
199 | if (argv[1][0] != '!' || argv[1][1] != '\0') { | 200 | if (argv[1][0] != '!' || argv[1][1] != '\0') { |
200 | if (t_lex(argv[2]), | 201 | if (t_lex(argv[2]), t_wp_op && t_wp_op->op_type == BINOP) { |
201 | t_wp_op && t_wp_op->op_type == BINOP) { | ||
202 | t_wp = &argv[1]; | 202 | t_wp = &argv[1]; |
203 | exit (binop() == 0); | 203 | exit(binop() == 0); |
204 | } | 204 | } |
205 | } | 205 | } |
206 | break; | 206 | break; |
207 | case 5: | 207 | case 5: |
208 | if (argv[1][0] == '!' && argv[1][1] == '\0') { | 208 | if (argv[1][0] == '!' && argv[1][1] == '\0') { |
209 | if (t_lex(argv[3]), | 209 | if (t_lex(argv[3]), t_wp_op && t_wp_op->op_type == BINOP) { |
210 | t_wp_op && t_wp_op->op_type == BINOP) { | ||
211 | t_wp = &argv[2]; | 210 | t_wp = &argv[2]; |
212 | exit (!(binop() == 0)); | 211 | exit(!(binop() == 0)); |
213 | } | 212 | } |
214 | } | 213 | } |
215 | break; | 214 | break; |
@@ -221,36 +220,31 @@ test_main(int argc, char** argv) | |||
221 | if (*t_wp != NULL && *++t_wp != NULL) | 220 | if (*t_wp != NULL && *++t_wp != NULL) |
222 | syntax(*t_wp, "unknown operand"); | 221 | syntax(*t_wp, "unknown operand"); |
223 | 222 | ||
224 | return( res); | 223 | return (res); |
225 | } | 224 | } |
226 | 225 | ||
227 | static void | 226 | static void syntax(const char *op, const char *msg) |
228 | syntax(op, msg) | ||
229 | const char *op; | ||
230 | const char *msg; | ||
231 | { | 227 | { |
232 | if (op && *op) | 228 | if (op && *op) { |
233 | error_msg_and_die("%s: %s", op, msg); | 229 | error_msg_and_die("%s: %s", op, msg); |
234 | else | 230 | } else { |
235 | error_msg_and_die("%s", msg); | 231 | error_msg_and_die("%s", msg); |
232 | } | ||
236 | } | 233 | } |
237 | 234 | ||
238 | static int | 235 | static int oexpr(enum token n) |
239 | oexpr(n) | ||
240 | enum token n; | ||
241 | { | 236 | { |
242 | int res; | 237 | int res; |
243 | 238 | ||
244 | res = aexpr(n); | 239 | res = aexpr(n); |
245 | if (t_lex(*++t_wp) == BOR) | 240 | if (t_lex(*++t_wp) == BOR) { |
246 | return oexpr(t_lex(*++t_wp)) || res; | 241 | return oexpr(t_lex(*++t_wp)) || res; |
242 | } | ||
247 | t_wp--; | 243 | t_wp--; |
248 | return res; | 244 | return res; |
249 | } | 245 | } |
250 | 246 | ||
251 | static int | 247 | static int aexpr(enum token n) |
252 | aexpr(n) | ||
253 | enum token n; | ||
254 | { | 248 | { |
255 | int res; | 249 | int res; |
256 | 250 | ||
@@ -261,23 +255,20 @@ aexpr(n) | |||
261 | return res; | 255 | return res; |
262 | } | 256 | } |
263 | 257 | ||
264 | static int | 258 | static int nexpr(enum token n) |
265 | nexpr(n) | ||
266 | enum token n; /* token */ | ||
267 | { | 259 | { |
268 | if (n == UNOT) | 260 | if (n == UNOT) |
269 | return !nexpr(t_lex(*++t_wp)); | 261 | return !nexpr(t_lex(*++t_wp)); |
270 | return primary(n); | 262 | return primary(n); |
271 | } | 263 | } |
272 | 264 | ||
273 | static int | 265 | static int primary(enum token n) |
274 | primary(n) | ||
275 | enum token n; | ||
276 | { | 266 | { |
277 | int res; | 267 | int res; |
278 | 268 | ||
279 | if (n == EOI) | 269 | if (n == EOI) { |
280 | syntax(NULL, "argument expected"); | 270 | syntax(NULL, "argument expected"); |
271 | } | ||
281 | if (n == LPAREN) { | 272 | if (n == LPAREN) { |
282 | res = oexpr(t_lex(*++t_wp)); | 273 | res = oexpr(t_lex(*++t_wp)); |
283 | if (t_lex(*++t_wp) != RPAREN) | 274 | if (t_lex(*++t_wp) != RPAREN) |
@@ -302,13 +293,12 @@ primary(n) | |||
302 | 293 | ||
303 | if (t_lex(t_wp[1]), t_wp_op && t_wp_op->op_type == BINOP) { | 294 | if (t_lex(t_wp[1]), t_wp_op && t_wp_op->op_type == BINOP) { |
304 | return binop(); | 295 | return binop(); |
305 | } | 296 | } |
306 | 297 | ||
307 | return strlen(*t_wp) > 0; | 298 | return strlen(*t_wp) > 0; |
308 | } | 299 | } |
309 | 300 | ||
310 | static int | 301 | static int binop() |
311 | binop() | ||
312 | { | 302 | { |
313 | const char *opnd1, *opnd2; | 303 | const char *opnd1, *opnd2; |
314 | struct t_op const *op; | 304 | struct t_op const *op; |
@@ -317,9 +307,9 @@ binop() | |||
317 | (void) t_lex(*++t_wp); | 307 | (void) t_lex(*++t_wp); |
318 | op = t_wp_op; | 308 | op = t_wp_op; |
319 | 309 | ||
320 | if ((opnd2 = *++t_wp) == (char *)0) | 310 | if ((opnd2 = *++t_wp) == (char *) 0) |
321 | syntax(op->op_text, "argument expected"); | 311 | syntax(op->op_text, "argument expected"); |
322 | 312 | ||
323 | switch (op->op_num) { | 313 | switch (op->op_num) { |
324 | case STREQ: | 314 | case STREQ: |
325 | return strcmp(opnd1, opnd2) == 0; | 315 | return strcmp(opnd1, opnd2) == 0; |
@@ -342,20 +332,17 @@ binop() | |||
342 | case INTLT: | 332 | case INTLT: |
343 | return getn(opnd1) < getn(opnd2); | 333 | return getn(opnd1) < getn(opnd2); |
344 | case FILNT: | 334 | case FILNT: |
345 | return newerf (opnd1, opnd2); | 335 | return newerf(opnd1, opnd2); |
346 | case FILOT: | 336 | case FILOT: |
347 | return olderf (opnd1, opnd2); | 337 | return olderf(opnd1, opnd2); |
348 | case FILEQ: | 338 | case FILEQ: |
349 | return equalf (opnd1, opnd2); | 339 | return equalf(opnd1, opnd2); |
350 | } | 340 | } |
351 | /* NOTREACHED */ | 341 | /* NOTREACHED */ |
352 | return 1; | 342 | return 1; |
353 | } | 343 | } |
354 | 344 | ||
355 | static int | 345 | static int filstat(char *nm, enum token mode) |
356 | filstat(nm, mode) | ||
357 | char *nm; | ||
358 | enum token mode; | ||
359 | { | 346 | { |
360 | struct stat s; | 347 | struct stat s; |
361 | unsigned int i; | 348 | unsigned int i; |
@@ -370,7 +357,7 @@ filstat(nm, mode) | |||
370 | return 0; | 357 | return 0; |
371 | } | 358 | } |
372 | 359 | ||
373 | if (stat(nm, &s) != 0) | 360 | if (stat(nm, &s) != 0) |
374 | return 0; | 361 | return 0; |
375 | 362 | ||
376 | switch (mode) { | 363 | switch (mode) { |
@@ -427,21 +414,19 @@ filstat(nm, mode) | |||
427 | return 1; | 414 | return 1; |
428 | } | 415 | } |
429 | 416 | ||
430 | filetype: | 417 | filetype: |
431 | return ((s.st_mode & S_IFMT) == i); | 418 | return ((s.st_mode & S_IFMT) == i); |
432 | 419 | ||
433 | filebit: | 420 | filebit: |
434 | return ((s.st_mode & i) != 0); | 421 | return ((s.st_mode & i) != 0); |
435 | } | 422 | } |
436 | 423 | ||
437 | static enum token | 424 | static enum token t_lex(char *s) |
438 | t_lex(s) | ||
439 | char *s; | ||
440 | { | 425 | { |
441 | struct t_op const *op = ops; | 426 | struct t_op const *op = ops; |
442 | 427 | ||
443 | if (s == 0) { | 428 | if (s == 0) { |
444 | t_wp_op = (struct t_op *)0; | 429 | t_wp_op = (struct t_op *) 0; |
445 | return EOI; | 430 | return EOI; |
446 | } | 431 | } |
447 | while (op->op_text) { | 432 | while (op->op_text) { |
@@ -451,14 +436,12 @@ t_lex(s) | |||
451 | } | 436 | } |
452 | op++; | 437 | op++; |
453 | } | 438 | } |
454 | t_wp_op = (struct t_op *)0; | 439 | t_wp_op = (struct t_op *) 0; |
455 | return OPERAND; | 440 | return OPERAND; |
456 | } | 441 | } |
457 | 442 | ||
458 | /* atoi with error detection */ | 443 | /* atoi with error detection */ |
459 | static int | 444 | static int getn(const char *s) |
460 | getn(s) | ||
461 | const char *s; | ||
462 | { | 445 | { |
463 | char *p; | 446 | char *p; |
464 | long r; | 447 | long r; |
@@ -467,69 +450,57 @@ getn(s) | |||
467 | r = strtol(s, &p, 10); | 450 | r = strtol(s, &p, 10); |
468 | 451 | ||
469 | if (errno != 0) | 452 | if (errno != 0) |
470 | error_msg_and_die("%s: out of range", s); | 453 | error_msg_and_die("%s: out of range", s); |
471 | 454 | ||
472 | while (isspace(*p)) | 455 | while (isspace(*p)) |
473 | p++; | 456 | p++; |
474 | 457 | ||
475 | if (*p) | 458 | if (*p) |
476 | error_msg_and_die("%s: bad number", s); | 459 | error_msg_and_die("%s: bad number", s); |
477 | 460 | ||
478 | return (int) r; | 461 | return (int) r; |
479 | } | 462 | } |
480 | 463 | ||
481 | static int | 464 | static int newerf(const char *f1, const char *f2) |
482 | newerf (f1, f2) | ||
483 | const char *f1, *f2; | ||
484 | { | 465 | { |
485 | struct stat b1, b2; | 466 | struct stat b1, b2; |
486 | 467 | ||
487 | return (stat (f1, &b1) == 0 && | 468 | return (stat(f1, &b1) == 0 && |
488 | stat (f2, &b2) == 0 && | 469 | stat(f2, &b2) == 0 && b1.st_mtime > b2.st_mtime); |
489 | b1.st_mtime > b2.st_mtime); | ||
490 | } | 470 | } |
491 | 471 | ||
492 | static int | 472 | static int olderf(const char *f1, const char *f2) |
493 | olderf (f1, f2) | ||
494 | const char *f1, *f2; | ||
495 | { | 473 | { |
496 | struct stat b1, b2; | 474 | struct stat b1, b2; |
497 | 475 | ||
498 | return (stat (f1, &b1) == 0 && | 476 | return (stat(f1, &b1) == 0 && |
499 | stat (f2, &b2) == 0 && | 477 | stat(f2, &b2) == 0 && b1.st_mtime < b2.st_mtime); |
500 | b1.st_mtime < b2.st_mtime); | ||
501 | } | 478 | } |
502 | 479 | ||
503 | static int | 480 | static int equalf(const char *f1, const char *f2) |
504 | equalf (f1, f2) | ||
505 | const char *f1, *f2; | ||
506 | { | 481 | { |
507 | struct stat b1, b2; | 482 | struct stat b1, b2; |
508 | 483 | ||
509 | return (stat (f1, &b1) == 0 && | 484 | return (stat(f1, &b1) == 0 && |
510 | stat (f2, &b2) == 0 && | 485 | stat(f2, &b2) == 0 && |
511 | b1.st_dev == b2.st_dev && | 486 | b1.st_dev == b2.st_dev && b1.st_ino == b2.st_ino); |
512 | b1.st_ino == b2.st_ino); | ||
513 | } | 487 | } |
514 | 488 | ||
515 | /* Do the same thing access(2) does, but use the effective uid and gid, | 489 | /* Do the same thing access(2) does, but use the effective uid and gid, |
516 | and don't make the mistake of telling root that any file is | 490 | and don't make the mistake of telling root that any file is |
517 | executable. */ | 491 | executable. */ |
518 | static int | 492 | static int test_eaccess(char *path, int mode) |
519 | test_eaccess (path, mode) | ||
520 | char *path; | ||
521 | int mode; | ||
522 | { | 493 | { |
523 | struct stat st; | 494 | struct stat st; |
524 | unsigned int euid = geteuid(); | 495 | unsigned int euid = geteuid(); |
525 | 496 | ||
526 | if (stat (path, &st) < 0) | 497 | if (stat(path, &st) < 0) |
527 | return (-1); | 498 | return (-1); |
528 | 499 | ||
529 | if (euid == 0) { | 500 | if (euid == 0) { |
530 | /* Root can read or write any file. */ | 501 | /* Root can read or write any file. */ |
531 | if (mode != X_OK) | 502 | if (mode != X_OK) |
532 | return (0); | 503 | return (0); |
533 | 504 | ||
534 | /* Root can execute any file that has any one of the execute | 505 | /* Root can execute any file that has any one of the execute |
535 | bits set. */ | 506 | bits set. */ |
@@ -537,9 +508,9 @@ int mode; | |||
537 | return (0); | 508 | return (0); |
538 | } | 509 | } |
539 | 510 | ||
540 | if (st.st_uid == euid) /* owner */ | 511 | if (st.st_uid == euid) /* owner */ |
541 | mode <<= 6; | 512 | mode <<= 6; |
542 | else if (is_a_group_member (st.st_gid)) | 513 | else if (is_a_group_member(st.st_gid)) |
543 | mode <<= 3; | 514 | mode <<= 3; |
544 | 515 | ||
545 | if (st.st_mode & mode) | 516 | if (st.st_mode & mode) |
@@ -548,8 +519,7 @@ int mode; | |||
548 | return (-1); | 519 | return (-1); |
549 | } | 520 | } |
550 | 521 | ||
551 | static void | 522 | static void initialize_group_array() |
552 | initialize_group_array () | ||
553 | { | 523 | { |
554 | ngroups = getgroups(0, NULL); | 524 | ngroups = getgroups(0, NULL); |
555 | group_array = xrealloc(group_array, ngroups * sizeof(gid_t)); | 525 | group_array = xrealloc(group_array, ngroups * sizeof(gid_t)); |
@@ -557,9 +527,7 @@ initialize_group_array () | |||
557 | } | 527 | } |
558 | 528 | ||
559 | /* Return non-zero if GID is one that we have in our groups list. */ | 529 | /* Return non-zero if GID is one that we have in our groups list. */ |
560 | static int | 530 | static int is_a_group_member(gid_t gid) |
561 | is_a_group_member (gid) | ||
562 | gid_t gid; | ||
563 | { | 531 | { |
564 | register int i; | 532 | register int i; |
565 | 533 | ||
@@ -568,7 +536,7 @@ gid_t gid; | |||
568 | return (1); | 536 | return (1); |
569 | 537 | ||
570 | if (ngroups == 0) | 538 | if (ngroups == 0) |
571 | initialize_group_array (); | 539 | initialize_group_array(); |
572 | 540 | ||
573 | /* Search through the list looking for GID. */ | 541 | /* Search through the list looking for GID. */ |
574 | for (i = 0; i < ngroups; i++) | 542 | for (i = 0; i < ngroups; i++) |