OSDN Git Service

should include sys/types.h for mode_t
[lha/olha.git] / extract.c
index 75722d2..3096561 100644 (file)
--- a/extract.c
+++ b/extract.c
@@ -44,38 +44,44 @@ void
 extract_to_file(struct lzh_istream *rp, struct lzh_header *h)
 {
     FILE *outfile;
+    char filename[1024];
+
+    if (opts.outdir)
+        makepath(filename, sizeof(filename),
+                 opts.outdir, h->filename, NULL);
+    else
+        string_copy(filename, h->filename, sizeof(filename));
 
     if (memcmp(h->method, "-lhd-", sizeof(h->method)) == 0) {
         /* directory */
-        if (mkdir(h->filename, 0777) == -1) {
-            if (errno != EEXIST)
-                error("cannot make directory \"%s\"", opts.outdir);
-        }
         if (opts.quiet < 2)
-            printf("Extracting %s ", h->filename);
+            printf("Extracting %s\n", filename);
         return;
     }
 
     /* create regular file */
-    if (file_exists(h->filename)) {
+    if (file_exists(filename)) {
         if (!opts.force_extract) {
-            message("'%s' has been already exist. skip", h->filename);
+            message("'%s' has been already exist. skip", filename);
             skip(rp->fp, h);
             return;
         }
     }
-    while ((outfile = fopen(h->filename, "wb")) == NULL) {
-        fprintf(stderr, "Can't open %s\n");
-        fprintf(stderr, "New filename: ", h->filename);
-        if (get_line(h->filename, sizeof(h->filename)) == 0) {
-            fprintf(stderr, "Not extracted\n");
-            skip(rp->fp, h);
-            return;
-        }
-        h->namelen = strlen(h->filename);
+
+    if (mkdir_parent(filename) == -1) {
+        if (errno != EEXIST)
+            error("cannot make directory \"%s\"", opts.outdir);
+    }
+
+    outfile = fopen(filename, "wb");
+    if (outfile == NULL) {
+        fprintf(stderr, "Can't open %s (skip)\n", filename);
+
+        skip(rp->fp, h);
+        return;
     }
     if (opts.quiet < 2)
-        printf("Extracting %s ", h->filename);
+        printf("Extracting %s ", filename);
 
     extract(rp, outfile, h);
     fclose(outfile);
@@ -85,7 +91,7 @@ extract_to_file(struct lzh_istream *rp, struct lzh_header *h)
         struct utimbuf ut;
 
         ut.actime = ut.modtime = h->mtime;
-        utime(h->filename, &ut);
+        utime(filename, &ut);
     }
 
     if (opts.quiet < 2)