Revision | e63024628868b87012863c832b200e858bfb3dcf (tree) |
---|---|
Zeit | 2008-07-01 22:55:47 |
Autor | Koji Arai <jca02266@gmai...> |
Commiter | Koji Arai |
ar.c was refined.
@@ -319,12 +319,12 @@ op_add(int cmd, char *archive_file, int argc, char **argv) | ||
319 | 319 | |
320 | 320 | nfiles = 0; |
321 | 321 | |
322 | - outfile = open_tempfile(); | |
323 | - wp->fp = outfile; | |
324 | - wp->buf = 0; | |
325 | 322 | if (*argv == 0) |
326 | 323 | error("archived files are not specified."); |
327 | 324 | |
325 | + wp->fp = outfile = open_tempfile(); | |
326 | + wp->buf = 0; | |
327 | + | |
328 | 328 | arcfile = fopen(archive_file, "rb"); |
329 | 329 | if (arcfile == NULL) |
330 | 330 | error("Can't open archive '%s'", archive_file); |
@@ -368,22 +368,22 @@ op_add(int cmd, char *archive_file, int argc, char **argv) | ||
368 | 368 | printf(" %d files\n", nfiles); |
369 | 369 | |
370 | 370 | if (nfiles > 0) { |
371 | - fputc(0, outfile); /* end of archive */ | |
372 | - if (ferror(outfile)) | |
371 | + if (fputc(0, outfile) == EOF) /* end of archive */ | |
373 | 372 | error("Can't write"); |
374 | 373 | |
374 | + fclose(arcfile); | |
375 | 375 | unlink(archive_file); |
376 | 376 | |
377 | - fclose(arcfile); | |
378 | 377 | rewind(outfile); |
379 | 378 | if (copy_stream_to_file(outfile, archive_file) == -1) |
380 | 379 | error("fail to copy_stream_to_file(): temp -> %s",archive_file); |
381 | 380 | } |
381 | + | |
382 | 382 | fclose(outfile); |
383 | 383 | } |
384 | 384 | |
385 | 385 | static void |
386 | -op_create(int cmd, char *archive_file, int argc, char **argv) | |
386 | +op_create_to_stdout(int cmd, char *archive_file, int argc, char **argv) | |
387 | 387 | { |
388 | 388 | int i; |
389 | 389 | struct lzh_ostream w, *wp; |
@@ -391,28 +391,49 @@ op_create(int cmd, char *archive_file, int argc, char **argv) | ||
391 | 391 | |
392 | 392 | wp = &w; |
393 | 393 | |
394 | - outfile = open_tempfile(); | |
395 | - wp->fp = outfile; | |
396 | 394 | wp->buf = 0; |
397 | 395 | if (*argv == 0) |
398 | 396 | error("archived files are not specified."); |
399 | 397 | |
400 | 398 | for (i = 0; i < argc; i++) { |
399 | + wp->fp = outfile = open_tempfile(); | |
400 | + | |
401 | 401 | add(wp, 0, argv[i], strlen(argv[i])); |
402 | - } | |
403 | 402 | |
404 | - fputc(0, outfile); /* end of archive */ | |
405 | - if (ferror(outfile)) | |
406 | - error("Can't write"); | |
407 | - rewind(outfile); | |
408 | - if (opts.archive_to_stdio) { | |
403 | + rewind(outfile); | |
409 | 404 | if (copy_stream(outfile, stdout) == -1) |
410 | 405 | error("fail to copy_stream(): temp -> %s","stdout"); |
406 | + fclose(outfile); | |
411 | 407 | } |
412 | - else { | |
413 | - if (copy_stream_to_file(outfile, archive_file) == -1) | |
414 | - error("fail to copy_stream_to_file(): temp -> %s",archive_file); | |
408 | + | |
409 | + if (fputc(0, stdout) == EOF) /* end of archive */ | |
410 | + error("Can't write"); | |
411 | +} | |
412 | + | |
413 | +static void | |
414 | +op_create(int cmd, char *archive_file, int argc, char **argv) | |
415 | +{ | |
416 | + int i; | |
417 | + struct lzh_ostream w, *wp; | |
418 | + FILE *outfile; | |
419 | + | |
420 | + wp = &w; | |
421 | + | |
422 | + wp->fp = outfile = open_tempfile(); | |
423 | + wp->buf = 0; | |
424 | + if (*argv == 0) | |
425 | + error("archived files are not specified."); | |
426 | + | |
427 | + for (i = 0; i < argc; i++) { | |
428 | + add(wp, 0, argv[i], strlen(argv[i])); | |
415 | 429 | } |
430 | + | |
431 | + if (fputc(0, outfile) == EOF) /* end of archive */ | |
432 | + error("Can't write"); | |
433 | + | |
434 | + rewind(outfile); | |
435 | + if (copy_stream_to_file(outfile, archive_file) == -1) | |
436 | + error("fail to copy_stream_to_file(): temp -> %s",archive_file); | |
416 | 437 | fclose(outfile); |
417 | 438 | } |
418 | 439 |
@@ -432,13 +453,7 @@ op_delete(int cmd, char *archive_file, int argc, char **argv) | ||
432 | 453 | } |
433 | 454 | outfile = open_tempfile(); |
434 | 455 | |
435 | - /* Open archive. */ | |
436 | - if (opts.archive_to_stdio) { | |
437 | - arcfile = stdin; | |
438 | - } | |
439 | - else { | |
440 | - arcfile = fopen(archive_file, "rb"); | |
441 | - } | |
456 | + arcfile = fopen(archive_file, "rb"); | |
442 | 457 | if (arcfile == NULL) |
443 | 458 | error("Can't open archive '%s'", archive_file); |
444 | 459 |
@@ -462,16 +477,14 @@ op_delete(int cmd, char *archive_file, int argc, char **argv) | ||
462 | 477 | printf(" %d files\n", nfiles); |
463 | 478 | |
464 | 479 | if (nfiles > 0) { |
465 | - fputc(0, outfile); /* end of archive */ | |
466 | - if (ferror(outfile)) | |
480 | + if (fputc(0, outfile) == EOF) /* end of archive */ | |
467 | 481 | error("Can't write"); |
468 | - if (!opts.archive_to_stdio) | |
469 | - unlink(archive_file); | |
470 | 482 | |
471 | 483 | fclose(arcfile); |
472 | - rewind(outfile); | |
484 | + unlink(archive_file); | |
473 | 485 | |
474 | 486 | if (arc_nfiles > 0) { |
487 | + rewind(outfile); | |
475 | 488 | if (copy_stream_to_file(outfile, archive_file) == -1) |
476 | 489 | error("fail to copy_stream_to_file(): temp -> %s",archive_file); |
477 | 490 | } |
@@ -479,6 +492,7 @@ op_delete(int cmd, char *archive_file, int argc, char **argv) | ||
479 | 492 | message("The archive file \"%s\" was removed because it would be empty.", archive_file); |
480 | 493 | } |
481 | 494 | } |
495 | + | |
482 | 496 | fclose(outfile); |
483 | 497 | } |
484 | 498 |
@@ -494,13 +508,11 @@ op_extract(int cmd, char *archive_file, int argc, char **argv) | ||
494 | 508 | |
495 | 509 | nfiles = 0; |
496 | 510 | |
497 | - /* Open archive. */ | |
498 | - if (opts.archive_to_stdio) { | |
511 | + if (opts.archive_to_stdio) | |
499 | 512 | arcfile = stdin; |
500 | - } | |
501 | - else { | |
513 | + else | |
502 | 514 | arcfile = fopen(archive_file, "rb"); |
503 | - } | |
515 | + | |
504 | 516 | if (arcfile == NULL) |
505 | 517 | error("Can't open archive '%s'", archive_file); |
506 | 518 |
@@ -549,13 +561,11 @@ op_list(int cmd, char *archive_file, int argc, char **argv) | ||
549 | 561 | |
550 | 562 | nfiles = 0; |
551 | 563 | |
552 | - /* Open archive. */ | |
553 | - if (opts.archive_to_stdio) { | |
564 | + if (opts.archive_to_stdio) | |
554 | 565 | arcfile = stdin; |
555 | - } | |
556 | - else { | |
566 | + else | |
557 | 567 | arcfile = fopen(archive_file, "rb"); |
558 | - } | |
568 | + | |
559 | 569 | if (arcfile == NULL) |
560 | 570 | error("Can't open archive '%s'", archive_file); |
561 | 571 |
@@ -645,17 +655,24 @@ main(int argc, char *argv[]) | ||
645 | 655 | switch (cmd) { |
646 | 656 | case 'a': |
647 | 657 | case 'u': |
648 | - if (opts.archive_to_stdio || !file_exists(archive_file)) | |
658 | + if (opts.archive_to_stdio) | |
659 | + op_create_to_stdout(cmd, archive_file, argc, argv); | |
660 | + else if (!file_exists(archive_file)) | |
649 | 661 | op_create(cmd, archive_file, argc, argv); |
650 | 662 | else |
651 | 663 | op_add(cmd, archive_file, argc, argv); |
652 | 664 | break; |
653 | 665 | |
654 | 666 | case 'c': |
655 | - op_create(cmd, archive_file, argc, argv); | |
667 | + if (opts.archive_to_stdio) | |
668 | + op_create_to_stdout(cmd, archive_file, argc, argv); | |
669 | + else | |
670 | + op_create(cmd, archive_file, argc, argv); | |
656 | 671 | break; |
657 | 672 | |
658 | 673 | case 'd': |
674 | + if (opts.archive_to_stdio) | |
675 | + error("should be specified the archive file"); | |
659 | 676 | op_delete(cmd, archive_file, argc, argv); |
660 | 677 | break; |
661 | 678 |