コミットメタ情報

リビジョンe63024628868b87012863c832b200e858bfb3dcf (tree)
日時2008-07-01 22:55:47
作者Koji Arai <jca02266@gmai...>
コミッターKoji Arai

ログメッセージ

ar.c was refined.

変更サマリ

差分

--- a/ar.c
+++ b/ar.c
@@ -319,12 +319,12 @@ op_add(int cmd, char *archive_file, int argc, char **argv)
319319
320320 nfiles = 0;
321321
322- outfile = open_tempfile();
323- wp->fp = outfile;
324- wp->buf = 0;
325322 if (*argv == 0)
326323 error("archived files are not specified.");
327324
325+ wp->fp = outfile = open_tempfile();
326+ wp->buf = 0;
327+
328328 arcfile = fopen(archive_file, "rb");
329329 if (arcfile == NULL)
330330 error("Can't open archive '%s'", archive_file);
@@ -368,22 +368,22 @@ op_add(int cmd, char *archive_file, int argc, char **argv)
368368 printf(" %d files\n", nfiles);
369369
370370 if (nfiles > 0) {
371- fputc(0, outfile); /* end of archive */
372- if (ferror(outfile))
371+ if (fputc(0, outfile) == EOF) /* end of archive */
373372 error("Can't write");
374373
374+ fclose(arcfile);
375375 unlink(archive_file);
376376
377- fclose(arcfile);
378377 rewind(outfile);
379378 if (copy_stream_to_file(outfile, archive_file) == -1)
380379 error("fail to copy_stream_to_file(): temp -> %s",archive_file);
381380 }
381+
382382 fclose(outfile);
383383 }
384384
385385 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)
387387 {
388388 int i;
389389 struct lzh_ostream w, *wp;
@@ -391,28 +391,49 @@ op_create(int cmd, char *archive_file, int argc, char **argv)
391391
392392 wp = &w;
393393
394- outfile = open_tempfile();
395- wp->fp = outfile;
396394 wp->buf = 0;
397395 if (*argv == 0)
398396 error("archived files are not specified.");
399397
400398 for (i = 0; i < argc; i++) {
399+ wp->fp = outfile = open_tempfile();
400+
401401 add(wp, 0, argv[i], strlen(argv[i]));
402- }
403402
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);
409404 if (copy_stream(outfile, stdout) == -1)
410405 error("fail to copy_stream(): temp -> %s","stdout");
406+ fclose(outfile);
411407 }
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]));
415429 }
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);
416437 fclose(outfile);
417438 }
418439
@@ -432,13 +453,7 @@ op_delete(int cmd, char *archive_file, int argc, char **argv)
432453 }
433454 outfile = open_tempfile();
434455
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");
442457 if (arcfile == NULL)
443458 error("Can't open archive '%s'", archive_file);
444459
@@ -462,16 +477,14 @@ op_delete(int cmd, char *archive_file, int argc, char **argv)
462477 printf(" %d files\n", nfiles);
463478
464479 if (nfiles > 0) {
465- fputc(0, outfile); /* end of archive */
466- if (ferror(outfile))
480+ if (fputc(0, outfile) == EOF) /* end of archive */
467481 error("Can't write");
468- if (!opts.archive_to_stdio)
469- unlink(archive_file);
470482
471483 fclose(arcfile);
472- rewind(outfile);
484+ unlink(archive_file);
473485
474486 if (arc_nfiles > 0) {
487+ rewind(outfile);
475488 if (copy_stream_to_file(outfile, archive_file) == -1)
476489 error("fail to copy_stream_to_file(): temp -> %s",archive_file);
477490 }
@@ -479,6 +492,7 @@ op_delete(int cmd, char *archive_file, int argc, char **argv)
479492 message("The archive file \"%s\" was removed because it would be empty.", archive_file);
480493 }
481494 }
495+
482496 fclose(outfile);
483497 }
484498
@@ -494,13 +508,11 @@ op_extract(int cmd, char *archive_file, int argc, char **argv)
494508
495509 nfiles = 0;
496510
497- /* Open archive. */
498- if (opts.archive_to_stdio) {
511+ if (opts.archive_to_stdio)
499512 arcfile = stdin;
500- }
501- else {
513+ else
502514 arcfile = fopen(archive_file, "rb");
503- }
515+
504516 if (arcfile == NULL)
505517 error("Can't open archive '%s'", archive_file);
506518
@@ -549,13 +561,11 @@ op_list(int cmd, char *archive_file, int argc, char **argv)
549561
550562 nfiles = 0;
551563
552- /* Open archive. */
553- if (opts.archive_to_stdio) {
564+ if (opts.archive_to_stdio)
554565 arcfile = stdin;
555- }
556- else {
566+ else
557567 arcfile = fopen(archive_file, "rb");
558- }
568+
559569 if (arcfile == NULL)
560570 error("Can't open archive '%s'", archive_file);
561571
@@ -645,17 +655,24 @@ main(int argc, char *argv[])
645655 switch (cmd) {
646656 case 'a':
647657 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))
649661 op_create(cmd, archive_file, argc, argv);
650662 else
651663 op_add(cmd, archive_file, argc, argv);
652664 break;
653665
654666 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);
656671 break;
657672
658673 case 'd':
674+ if (opts.archive_to_stdio)
675+ error("should be specified the archive file");
659676 op_delete(cmd, archive_file, argc, argv);
660677 break;
661678
旧リポジトリブラウザで表示