Révision | cedcbc30c698c8b4ab0334bebf92b0d5add7295c (tree) |
---|---|
l'heure | 2008-06-26 15:57:47 |
Auteur | Koji Arai <jca02266@gmai...> |
Commiter | Koji Arai |
ar.c was refined.
@@ -46,11 +46,8 @@ Structure of archive block (low order byte first): | ||
46 | 46 | |
47 | 47 | #include <stdlib.h> |
48 | 48 | #include <string.h> |
49 | -#include <ctype.h> | |
50 | 49 | #include <errno.h> |
51 | 50 | #include <sys/stat.h> |
52 | -#include <time.h> | |
53 | -#include <utime.h> | |
54 | 51 | #include <unistd.h> |
55 | 52 | #include "ar.h" |
56 | 53 |
@@ -311,15 +308,14 @@ open_tempfile() | ||
311 | 308 | static void |
312 | 309 | op_add(int cmd, char *archive_file, int argc, char **argv) |
313 | 310 | { |
314 | - int i, count, found, done; | |
311 | + int i, nfiles, found; | |
315 | 312 | struct lzh_header h; |
316 | 313 | struct lzh_ostream w, *wp; |
317 | - FILE *arcfile = NULL; | |
318 | - FILE *outfile = NULL; | |
314 | + FILE *arcfile, *outfile; | |
319 | 315 | |
320 | 316 | wp = &w; |
321 | 317 | |
322 | - count = done = 0; | |
318 | + nfiles = 0; | |
323 | 319 | |
324 | 320 | outfile = open_tempfile(); |
325 | 321 | wp->fp = outfile; |
@@ -331,54 +327,57 @@ op_add(int cmd, char *archive_file, int argc, char **argv) | ||
331 | 327 | if (arcfile == NULL) |
332 | 328 | error("Can't open archive '%s'", archive_file); |
333 | 329 | |
334 | - while (!done && read_header(arcfile, &h)) { | |
330 | + while (read_header(arcfile, &h)) { | |
335 | 331 | |
336 | 332 | found = search(argc, argv, &h); |
337 | - if (found>0) { | |
338 | - argv[found-1] = 0; | |
333 | + if (found <= 0) { | |
334 | + copy(arcfile, outfile, &h); | |
335 | + continue; | |
336 | + } | |
339 | 337 | |
340 | - if (cmd == 'u') { | |
341 | - time_t mtime; | |
338 | + /* found */ | |
339 | + argv[found-1] = 0; | |
342 | 340 | |
343 | - if (file_mtime(h.filename, &mtime) == -1 || h.mtime > mtime) { | |
344 | - copy(arcfile, outfile, &h); | |
345 | - continue; | |
346 | - } | |
347 | - } | |
341 | + if (cmd == 'u') { | |
342 | + time_t mtime; | |
348 | 343 | |
349 | - if (add(wp, 1, h.filename, h.namelen)) { | |
350 | - skip(arcfile, &h); | |
351 | - count++; | |
352 | - } | |
353 | - else | |
344 | + if (file_mtime(h.filename, &mtime) == -1 || h.mtime > mtime) { | |
345 | + /* no update */ | |
354 | 346 | copy(arcfile, outfile, &h); |
347 | + continue; | |
348 | + } | |
355 | 349 | } |
356 | - else | |
357 | - copy(arcfile, outfile, &h); | |
350 | + | |
351 | + /* replace */ | |
352 | + add(wp, 1, h.filename, h.namelen); | |
353 | + skip(arcfile, &h); | |
354 | + nfiles++; | |
358 | 355 | } |
359 | 356 | |
357 | + /* add non replacement files */ | |
360 | 358 | for (i = 0; i < argc; i++) { |
361 | 359 | if (argv[i]) { |
362 | - count++; | |
360 | + nfiles++; | |
363 | 361 | add(wp, 0, argv[i], strlen(argv[i])); |
364 | 362 | } |
365 | 363 | } |
366 | 364 | |
367 | 365 | if (opts.quiet < 2) |
368 | - printf(" %d files\n", count); | |
366 | + printf(" %d files\n", nfiles); | |
369 | 367 | |
370 | - if (count > 0) { | |
368 | + if (nfiles > 0) { | |
371 | 369 | fputc(0, outfile); /* end of archive */ |
372 | 370 | if (ferror(outfile)) |
373 | 371 | error("Can't write"); |
374 | - if (!opts.archive_to_stdio) | |
375 | - unlink(archive_file); | |
372 | + | |
373 | + unlink(archive_file); | |
376 | 374 | |
377 | 375 | fclose(arcfile); |
378 | 376 | rewind(outfile); |
379 | 377 | if (copy_stream_to_file(outfile, archive_file) == -1) |
380 | 378 | error("fail to copy_stream_to_file(): temp -> %s",archive_file); |
381 | 379 | } |
380 | + fclose(outfile); | |
382 | 381 | } |
383 | 382 | |
384 | 383 | static void |
@@ -386,7 +385,7 @@ op_create(int cmd, char *archive_file, int argc, char **argv) | ||
386 | 385 | { |
387 | 386 | int i; |
388 | 387 | struct lzh_ostream w, *wp; |
389 | - FILE *outfile = NULL; | |
388 | + FILE *outfile; | |
390 | 389 | |
391 | 390 | wp = &w; |
392 | 391 |
@@ -412,23 +411,18 @@ op_create(int cmd, char *archive_file, int argc, char **argv) | ||
412 | 411 | if (copy_stream_to_file(outfile, archive_file) == -1) |
413 | 412 | error("fail to copy_stream_to_file(): temp -> %s",archive_file); |
414 | 413 | } |
415 | - | |
416 | - return; | |
414 | + fclose(outfile); | |
417 | 415 | } |
418 | 416 | |
419 | 417 | static void |
420 | 418 | op_delete(int cmd, char *archive_file, int argc, char **argv) |
421 | 419 | { |
422 | - int count, found, done; | |
420 | + int nfiles, found; | |
423 | 421 | struct lzh_header h; |
424 | - int arc_count; | |
425 | - struct lzh_ostream w, *wp; | |
426 | - FILE *arcfile = NULL; | |
427 | - FILE *outfile = NULL; | |
428 | - | |
429 | - wp = &w; | |
422 | + int arc_nfiles; | |
423 | + FILE *arcfile, *outfile; | |
430 | 424 | |
431 | - count = done = 0; | |
425 | + nfiles = 0; | |
432 | 426 | |
433 | 427 | if (argc == 0) { |
434 | 428 | message("No files given in argument, do nothing."); |
@@ -446,26 +440,26 @@ op_delete(int cmd, char *archive_file, int argc, char **argv) | ||
446 | 440 | if (arcfile == NULL) |
447 | 441 | error("Can't open archive '%s'", archive_file); |
448 | 442 | |
449 | - arc_count = 0; | |
450 | - | |
451 | - while (!done && read_header(arcfile, &h)) { | |
443 | + arc_nfiles = 0; | |
452 | 444 | |
453 | - arc_count++; | |
445 | + while (read_header(arcfile, &h)) { | |
454 | 446 | |
455 | 447 | found = search(argc, argv, &h); |
456 | 448 | if (found) { |
457 | - count++; | |
449 | + nfiles++; | |
458 | 450 | message("'%s' deleted", h.filename); |
459 | 451 | skip(arcfile, &h); |
460 | 452 | } |
461 | - else | |
453 | + else { | |
454 | + arc_nfiles++; | |
462 | 455 | copy(arcfile, outfile, &h); |
456 | + } | |
463 | 457 | } |
464 | 458 | |
465 | 459 | if (opts.quiet < 2) |
466 | - printf(" %d files\n", count); | |
460 | + printf(" %d files\n", nfiles); | |
467 | 461 | |
468 | - if (count > 0) { | |
462 | + if (nfiles > 0) { | |
469 | 463 | fputc(0, outfile); /* end of archive */ |
470 | 464 | if (ferror(outfile)) |
471 | 465 | error("Can't write"); |
@@ -475,7 +469,7 @@ op_delete(int cmd, char *archive_file, int argc, char **argv) | ||
475 | 469 | fclose(arcfile); |
476 | 470 | rewind(outfile); |
477 | 471 | |
478 | - if (arc_count > count) { | |
472 | + if (arc_nfiles > 0) { | |
479 | 473 | if (copy_stream_to_file(outfile, archive_file) == -1) |
480 | 474 | error("fail to copy_stream_to_file(): temp -> %s",archive_file); |
481 | 475 | } |
@@ -483,19 +477,20 @@ op_delete(int cmd, char *archive_file, int argc, char **argv) | ||
483 | 477 | message("The archive file \"%s\" was removed because it would be empty.", archive_file); |
484 | 478 | } |
485 | 479 | } |
480 | + fclose(outfile); | |
486 | 481 | } |
487 | 482 | |
488 | 483 | static void |
489 | 484 | op_extract(int cmd, char *archive_file, int argc, char **argv) |
490 | 485 | { |
491 | - int count, nfiles, found, done; | |
486 | + int nfiles, found; | |
492 | 487 | struct lzh_header h; |
493 | 488 | struct lzh_istream r, *rp; |
494 | - FILE *arcfile = NULL; | |
489 | + FILE *arcfile; | |
495 | 490 | |
496 | 491 | rp = &r; |
497 | 492 | |
498 | - count = done = nfiles = 0; | |
493 | + nfiles = 0; | |
499 | 494 | |
500 | 495 | /* Open archive. */ |
501 | 496 | if (opts.archive_to_stdio) { |
@@ -520,15 +515,15 @@ op_extract(int cmd, char *archive_file, int argc, char **argv) | ||
520 | 515 | } |
521 | 516 | } |
522 | 517 | |
523 | - while (!done && read_header(arcfile, &h)) { | |
518 | + while (read_header(arcfile, &h)) { | |
524 | 519 | |
525 | 520 | found = search(argc, argv, &h); |
526 | 521 | if (found != 0) { |
527 | 522 | rp->fp = arcfile; |
528 | 523 | rp->compsize = h.compsize; |
529 | 524 | extract(rp, cmd == 'x', &h); |
530 | - if (++count == nfiles) | |
531 | - done = 1; | |
525 | + if (++nfiles == argc) | |
526 | + break; | |
532 | 527 | } |
533 | 528 | else |
534 | 529 | skip(arcfile, &h); |
@@ -536,21 +531,18 @@ op_extract(int cmd, char *archive_file, int argc, char **argv) | ||
536 | 531 | |
537 | 532 | if (cmd != 'p') { |
538 | 533 | if (opts.quiet < 2) |
539 | - printf(" %d files\n", count); | |
534 | + printf(" %d files\n", nfiles); | |
540 | 535 | } |
541 | 536 | } |
542 | 537 | |
543 | 538 | static void |
544 | 539 | op_list(int cmd, char *archive_file, int argc, char **argv) |
545 | 540 | { |
546 | - int count, nfiles, found, done; | |
541 | + int nfiles, found; | |
547 | 542 | struct lzh_header h; |
548 | - struct lzh_istream r, *rp; | |
549 | - FILE *arcfile = NULL; | |
550 | - | |
551 | - rp = &r; | |
543 | + FILE *arcfile; | |
552 | 544 | |
553 | - count = done = nfiles = 0; | |
545 | + nfiles = 0; | |
554 | 546 | |
555 | 547 | /* Open archive. */ |
556 | 548 | if (opts.archive_to_stdio) { |
@@ -562,22 +554,22 @@ op_list(int cmd, char *archive_file, int argc, char **argv) | ||
562 | 554 | if (arcfile == NULL) |
563 | 555 | error("Can't open archive '%s'", archive_file); |
564 | 556 | |
565 | - while (!done && read_header(arcfile, &h)) { | |
557 | + while (read_header(arcfile, &h)) { | |
566 | 558 | |
567 | 559 | found = search(argc, argv, &h); |
568 | 560 | |
569 | 561 | if (found != 0) { |
570 | - if (count == 0) | |
562 | + if (nfiles == 0) | |
571 | 563 | list_start(); |
572 | 564 | list(&h); |
573 | - if (++count == nfiles) | |
574 | - done = 1; | |
565 | + if (++nfiles == argc) | |
566 | + break; | |
575 | 567 | } |
576 | 568 | skip(arcfile, &h); |
577 | 569 | } |
578 | 570 | |
579 | 571 | if (opts.quiet < 2) |
580 | - printf(" %d files\n", count); | |
572 | + printf(" %d files\n", nfiles); | |
581 | 573 | } |
582 | 574 | |
583 | 575 | int |
@@ -632,12 +624,10 @@ main(int argc, char *argv[]) | ||
632 | 624 | switch (cmd) { |
633 | 625 | case 'a': |
634 | 626 | case 'u': |
635 | - if (opts.archive_to_stdio || !file_exists(archive_file)) { | |
627 | + if (opts.archive_to_stdio || !file_exists(archive_file)) | |
636 | 628 | op_create(cmd, archive_file, argc, argv); |
637 | - } | |
638 | - else { | |
629 | + else | |
639 | 630 | op_add(cmd, archive_file, argc, argv); |
640 | - } | |
641 | 631 | break; |
642 | 632 | |
643 | 633 | case 'c': |