コミットメタ情報

リビジョンcedcbc30c698c8b4ab0334bebf92b0d5add7295c (tree)
日時2008-06-26 15:57:47
作者Koji Arai <jca02266@gmai...>
コミッターKoji Arai

ログメッセージ

ar.c was refined.

変更サマリ

差分

--- a/ar.c
+++ b/ar.c
@@ -46,11 +46,8 @@ Structure of archive block (low order byte first):
4646
4747 #include <stdlib.h>
4848 #include <string.h>
49-#include <ctype.h>
5049 #include <errno.h>
5150 #include <sys/stat.h>
52-#include <time.h>
53-#include <utime.h>
5451 #include <unistd.h>
5552 #include "ar.h"
5653
@@ -311,15 +308,14 @@ open_tempfile()
311308 static void
312309 op_add(int cmd, char *archive_file, int argc, char **argv)
313310 {
314- int i, count, found, done;
311+ int i, nfiles, found;
315312 struct lzh_header h;
316313 struct lzh_ostream w, *wp;
317- FILE *arcfile = NULL;
318- FILE *outfile = NULL;
314+ FILE *arcfile, *outfile;
319315
320316 wp = &w;
321317
322- count = done = 0;
318+ nfiles = 0;
323319
324320 outfile = open_tempfile();
325321 wp->fp = outfile;
@@ -331,54 +327,57 @@ op_add(int cmd, char *archive_file, int argc, char **argv)
331327 if (arcfile == NULL)
332328 error("Can't open archive '%s'", archive_file);
333329
334- while (!done && read_header(arcfile, &h)) {
330+ while (read_header(arcfile, &h)) {
335331
336332 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+ }
339337
340- if (cmd == 'u') {
341- time_t mtime;
338+ /* found */
339+ argv[found-1] = 0;
342340
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;
348343
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 */
354346 copy(arcfile, outfile, &h);
347+ continue;
348+ }
355349 }
356- else
357- copy(arcfile, outfile, &h);
350+
351+ /* replace */
352+ add(wp, 1, h.filename, h.namelen);
353+ skip(arcfile, &h);
354+ nfiles++;
358355 }
359356
357+ /* add non replacement files */
360358 for (i = 0; i < argc; i++) {
361359 if (argv[i]) {
362- count++;
360+ nfiles++;
363361 add(wp, 0, argv[i], strlen(argv[i]));
364362 }
365363 }
366364
367365 if (opts.quiet < 2)
368- printf(" %d files\n", count);
366+ printf(" %d files\n", nfiles);
369367
370- if (count > 0) {
368+ if (nfiles > 0) {
371369 fputc(0, outfile); /* end of archive */
372370 if (ferror(outfile))
373371 error("Can't write");
374- if (!opts.archive_to_stdio)
375- unlink(archive_file);
372+
373+ unlink(archive_file);
376374
377375 fclose(arcfile);
378376 rewind(outfile);
379377 if (copy_stream_to_file(outfile, archive_file) == -1)
380378 error("fail to copy_stream_to_file(): temp -> %s",archive_file);
381379 }
380+ fclose(outfile);
382381 }
383382
384383 static void
@@ -386,7 +385,7 @@ op_create(int cmd, char *archive_file, int argc, char **argv)
386385 {
387386 int i;
388387 struct lzh_ostream w, *wp;
389- FILE *outfile = NULL;
388+ FILE *outfile;
390389
391390 wp = &w;
392391
@@ -412,23 +411,18 @@ op_create(int cmd, char *archive_file, int argc, char **argv)
412411 if (copy_stream_to_file(outfile, archive_file) == -1)
413412 error("fail to copy_stream_to_file(): temp -> %s",archive_file);
414413 }
415-
416- return;
414+ fclose(outfile);
417415 }
418416
419417 static void
420418 op_delete(int cmd, char *archive_file, int argc, char **argv)
421419 {
422- int count, found, done;
420+ int nfiles, found;
423421 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;
430424
431- count = done = 0;
425+ nfiles = 0;
432426
433427 if (argc == 0) {
434428 message("No files given in argument, do nothing.");
@@ -446,26 +440,26 @@ op_delete(int cmd, char *archive_file, int argc, char **argv)
446440 if (arcfile == NULL)
447441 error("Can't open archive '%s'", archive_file);
448442
449- arc_count = 0;
450-
451- while (!done && read_header(arcfile, &h)) {
443+ arc_nfiles = 0;
452444
453- arc_count++;
445+ while (read_header(arcfile, &h)) {
454446
455447 found = search(argc, argv, &h);
456448 if (found) {
457- count++;
449+ nfiles++;
458450 message("'%s' deleted", h.filename);
459451 skip(arcfile, &h);
460452 }
461- else
453+ else {
454+ arc_nfiles++;
462455 copy(arcfile, outfile, &h);
456+ }
463457 }
464458
465459 if (opts.quiet < 2)
466- printf(" %d files\n", count);
460+ printf(" %d files\n", nfiles);
467461
468- if (count > 0) {
462+ if (nfiles > 0) {
469463 fputc(0, outfile); /* end of archive */
470464 if (ferror(outfile))
471465 error("Can't write");
@@ -475,7 +469,7 @@ op_delete(int cmd, char *archive_file, int argc, char **argv)
475469 fclose(arcfile);
476470 rewind(outfile);
477471
478- if (arc_count > count) {
472+ if (arc_nfiles > 0) {
479473 if (copy_stream_to_file(outfile, archive_file) == -1)
480474 error("fail to copy_stream_to_file(): temp -> %s",archive_file);
481475 }
@@ -483,19 +477,20 @@ op_delete(int cmd, char *archive_file, int argc, char **argv)
483477 message("The archive file \"%s\" was removed because it would be empty.", archive_file);
484478 }
485479 }
480+ fclose(outfile);
486481 }
487482
488483 static void
489484 op_extract(int cmd, char *archive_file, int argc, char **argv)
490485 {
491- int count, nfiles, found, done;
486+ int nfiles, found;
492487 struct lzh_header h;
493488 struct lzh_istream r, *rp;
494- FILE *arcfile = NULL;
489+ FILE *arcfile;
495490
496491 rp = &r;
497492
498- count = done = nfiles = 0;
493+ nfiles = 0;
499494
500495 /* Open archive. */
501496 if (opts.archive_to_stdio) {
@@ -520,15 +515,15 @@ op_extract(int cmd, char *archive_file, int argc, char **argv)
520515 }
521516 }
522517
523- while (!done && read_header(arcfile, &h)) {
518+ while (read_header(arcfile, &h)) {
524519
525520 found = search(argc, argv, &h);
526521 if (found != 0) {
527522 rp->fp = arcfile;
528523 rp->compsize = h.compsize;
529524 extract(rp, cmd == 'x', &h);
530- if (++count == nfiles)
531- done = 1;
525+ if (++nfiles == argc)
526+ break;
532527 }
533528 else
534529 skip(arcfile, &h);
@@ -536,21 +531,18 @@ op_extract(int cmd, char *archive_file, int argc, char **argv)
536531
537532 if (cmd != 'p') {
538533 if (opts.quiet < 2)
539- printf(" %d files\n", count);
534+ printf(" %d files\n", nfiles);
540535 }
541536 }
542537
543538 static void
544539 op_list(int cmd, char *archive_file, int argc, char **argv)
545540 {
546- int count, nfiles, found, done;
541+ int nfiles, found;
547542 struct lzh_header h;
548- struct lzh_istream r, *rp;
549- FILE *arcfile = NULL;
550-
551- rp = &r;
543+ FILE *arcfile;
552544
553- count = done = nfiles = 0;
545+ nfiles = 0;
554546
555547 /* Open archive. */
556548 if (opts.archive_to_stdio) {
@@ -562,22 +554,22 @@ op_list(int cmd, char *archive_file, int argc, char **argv)
562554 if (arcfile == NULL)
563555 error("Can't open archive '%s'", archive_file);
564556
565- while (!done && read_header(arcfile, &h)) {
557+ while (read_header(arcfile, &h)) {
566558
567559 found = search(argc, argv, &h);
568560
569561 if (found != 0) {
570- if (count == 0)
562+ if (nfiles == 0)
571563 list_start();
572564 list(&h);
573- if (++count == nfiles)
574- done = 1;
565+ if (++nfiles == argc)
566+ break;
575567 }
576568 skip(arcfile, &h);
577569 }
578570
579571 if (opts.quiet < 2)
580- printf(" %d files\n", count);
572+ printf(" %d files\n", nfiles);
581573 }
582574
583575 int
@@ -632,12 +624,10 @@ main(int argc, char *argv[])
632624 switch (cmd) {
633625 case 'a':
634626 case 'u':
635- if (opts.archive_to_stdio || !file_exists(archive_file)) {
627+ if (opts.archive_to_stdio || !file_exists(archive_file))
636628 op_create(cmd, archive_file, argc, argv);
637- }
638- else {
629+ else
639630 op_add(cmd, archive_file, argc, argv);
640- }
641631 break;
642632
643633 case 'c':
旧リポジトリブラウザで表示