OSDN Git Service

should include sys/types.h for mode_t
[lha/olha.git] / add.c
diff --git a/add.c b/add.c
index 29643b0..ef77eff 100644 (file)
--- a/add.c
+++ b/add.c
@@ -21,25 +21,26 @@ store(FILE *rfp, FILE *wfp, unsigned int *crc)
 int
 add_dir(struct lzh_ostream *wp, int replace_flag, struct lzh_header *h)
 {
-    long headerpos, arcpos;
-    uint r;
-
     h->origsize = h->compsize = 0;
     h->file_crc = INIT_CRC;
 
-    headerpos = ftell(wp->fp);
     write_header(wp->fp, h);
-    arcpos = ftell(wp->fp);
 
-    if (opts.quiet < 2)
-        printf(" %d.%d%%\n", r / 10, r % 10);
+    if (opts.quiet < 2) {
+        if (replace_flag)
+            printf("Replacing %s/ ", h->filename);
+        else
+            printf("Adding %s/ ", h->filename);
+
+        printf("(directory)\n");
+    }
     return 1;                   /* success */
 }
 
 static int
 add_1(struct lzh_ostream *wp, int replace_flag, struct lzh_header *h)
 {
-    long headerpos, arcpos;
+    long headerpos, arcpos, arcendpos;
     uint r;
     FILE *rfp;
 
@@ -47,12 +48,10 @@ add_1(struct lzh_ostream *wp, int replace_flag, struct lzh_header *h)
         fprintf(stderr, "Can't open %s\n", h->filename);
         return 0;               /* failure */
     }
-    if (replace_flag) {
-        if (opts.quiet < 2)
+    if (opts.quiet < 2) {
+        if (replace_flag)
             printf("Replacing %s ", h->filename);
-    }
-    else {
-        if (opts.quiet < 2)
+        else
             printf("Adding %s ", h->filename);
     }
 
@@ -83,9 +82,12 @@ add_1(struct lzh_ostream *wp, int replace_flag, struct lzh_header *h)
     h->file_crc = wp->crc ^ INIT_CRC;
     fclose(rfp);
 
+    /* rewrite compsize and origsize to the header */
+    arcendpos = ftell(wp->fp);
     fseek(wp->fp, headerpos, SEEK_SET);
     write_header(wp->fp, h);
-    fseek(wp->fp, 0L, SEEK_END);
+    fseek(wp->fp, arcendpos, SEEK_SET);
+
     r = ratio(wp->compsize, wp->origsize);
     if (opts.quiet < 2)
         printf(" %d.%d%%\n", r / 10, r % 10);
@@ -109,12 +111,14 @@ add(struct lzh_ostream *wp, int replace_flag, char *filename, int namelen)
 
     h.mtime = st.st_mtime;
     if (S_ISDIR(st.st_mode)) {
+        /* directory */
         DIR *dir;
         struct dirent *ent;
 
         memcpy(h.method, "-lhd-", sizeof(h.method));  /* directory */
         add_dir(wp, replace_flag, &h);
 
+        /* recursive call */
         dir = opendir(h.filename);
         if (dir == NULL)
             error("cannot open directory: \"%s\"", h.filename);
@@ -136,7 +140,8 @@ add(struct lzh_ostream *wp, int replace_flag, char *filename, int namelen)
         closedir(dir);
     }
     else {
-        memcpy(h.method, opts.method->id, sizeof(h.method));  /* compress */
+        /* regular file */
+        memcpy(h.method, opts.method->id, sizeof(h.method));
         add_1(wp, replace_flag, &h);
     }