OSDN Git Service

* src/lhext.c (make_parent_path): missing braces.
[lha/lha.git] / src / lhext.c
index 32c5a25..bc49b9a 100644 (file)
@@ -35,7 +35,7 @@ inquire_extract(name)
        skip_flg = FALSE;
        if (stat(name, &stbuf) >= 0) {
                if (!is_regularfile(&stbuf)) {
-                       error("Already exist (not a file)", name);
+                       error("\"%s\" already exists (not a file)", name);
                        return FALSE;
                }
 
@@ -90,20 +90,19 @@ make_parent_path(name)
                }
 
        if (p == path) {
-               message("Why?", "ROOT");
+               message("invalid path name \"%s\"", name);
                return FALSE;   /* no more parent. */
        }
 
        if (GETSTAT(path, &stbuf) >= 0) {
                if (is_directory(&stbuf))
                        return TRUE;
-               error("Not a directory", path);
+               error("Not a directory \"%s\"", path);
                return FALSE;
        }
-       errno = 0;
 
        if (verbose)
-               printf("Making directory \"%s\".\n", path);
+               message("Making directory \"%s\".", path);
 
 #if defined __MINGW32__
     if (mkdir(path) >= 0)
@@ -112,18 +111,18 @@ make_parent_path(name)
        if (mkdir(path, 0777) >= 0)     /* try */
                return TRUE;    /* successful done. */
 #endif
-       errno = 0;
 
        if (!make_parent_path(path))
                return FALSE;
 
 #if defined __MINGW32__
-    if (mkdir(path) < 0)
-               message("Cannot make directory", path);
+    if (mkdir(path) < 0) {      /* try again */
+               error("Cannot make directory", path);
         return FALSE;
+    }
 #else
        if (mkdir(path, 0777) < 0) {    /* try again */
-               message("Cannot make directory", path);
+               error("Cannot make directory", path);
                return FALSE;
        }
 #endif
@@ -139,11 +138,9 @@ open_with_make_path(name)
        FILE           *fp;
 
        if ((fp = fopen(name, WRITE_BINARY)) == NULL) {
-               errno = 0;
                if (!make_parent_path(name) ||
                    (fp = fopen(name, WRITE_BINARY)) == NULL)
-                       error("Cannot extract", name);
-               errno = 0;
+                       error("Cannot extract a file \"%s\"", name);
        }
        return fp;
 }
@@ -154,13 +151,13 @@ adjust_info(name, hdr)
        char           *name;
        LzHeader       *hdr;
 {
-       time_t          utimebuf[2];
+    struct utimbuf utimebuf;
 
        /* adjust file stamp */
-       utimebuf[0] = utimebuf[1] = hdr->unix_last_modified_stamp;
+       utimebuf.actime = utimebuf.modtime = hdr->unix_last_modified_stamp;
 
        if ((hdr->unix_mode & UNIX_FILE_TYPEMASK) != UNIX_FILE_SYMLINK)
-               utime(name, utimebuf);
+               utime(name, &utimebuf);
 
        if (hdr->extend_type == EXTEND_UNIX
            || hdr->extend_type == EXTEND_OS68K
@@ -172,14 +169,27 @@ adjust_info(name, hdr)
                        chmod(name, hdr->unix_mode);
 #endif
                if (!getuid()){
+            uid_t uid = hdr->unix_uid;
+            gid_t gid = hdr->unix_gid;
+
+#if HAVE_GETPWNAM && HAVE_GETGRNAM
+            if (hdr->user[0]) {
+                struct passwd *ent = getpwnam(hdr->user);
+                if (ent) uid = ent->pw_uid;
+            }
+            if (hdr->group[0]) {
+                struct group *ent = getgrnam(hdr->group);
+                if (ent) gid = ent->gr_gid;
+            }
+#endif
+
 #if HAVE_LCHOWN
                        if ((hdr->unix_mode & UNIX_FILE_TYPEMASK) == UNIX_FILE_SYMLINK)
-                               lchown(name, hdr->unix_uid, hdr->unix_gid);
+                               lchown(name, uid, gid);
                        else
 #endif /* HAVE_LCHWON */
-                               chown(name, hdr->unix_uid, hdr->unix_gid);
+                               chown(name, uid, gid);
                }
-               errno = 0;
        }
 }
 
@@ -191,7 +201,7 @@ extract_one(afp, hdr)
 {
        FILE           *fp;     /* output file */
        struct stat     stbuf;
-       char            name[257];
+       char            name[FILENAME_LENGTH];
        int             crc;
        int             method;
        boolean         save_quiet, save_verbose, up_flag;
@@ -218,7 +228,7 @@ extract_one(afp, hdr)
        }
 
        if (extract_directory)
-               sprintf(name, "%s/%s", extract_directory, q);
+               xsnprintf(name, sizeof(name), "%s/%s", extract_directory, q);
        else
                strcpy(name, q);
 
@@ -227,10 +237,11 @@ extract_one(afp, hdr)
        /* 1999.4.30 t.okamoto */
        for (method = 0;; method++) {
                if (methods[method] == NULL) {
-                       error("Unknown method skiped ...", name);
+                       error("Unknown method \"%.*s\"; \"%s\" will be skiped ...",
+                  5, hdr->method, name);
                        return;
                }
-               if (bcmp(hdr->method, methods[method], 5) == 0)
+               if (memcmp(hdr->method, methods[method], 5) == 0)
                        break;
        }
 
@@ -239,16 +250,17 @@ extract_one(afp, hdr)
 #if 0
                for (method = 0;; method++) {
                        if (methods[method] == NULL) {
-                               error("Unknown method skiped ...", name);
+                error("Unknown method \"%.*s\"; \"%s\" will be skiped ...",
+                      5, hdr->method, name);
                                return;
                        }
-                       if (bcmp(hdr->method, methods[method], 5) == 0)
+                       if (memcmp(hdr->method, methods[method], 5) == 0)
                                break;
                }
 #endif
 
                reading_filename = archive_name;
-               writting_filename = name;
+               writing_filename = name;
                if (output_to_stdout || verify_mode) {
                        if (noexec) {
                                printf("%s %s\n", verify_mode ? "VERIFY" : "EXTRACT", name);
@@ -272,8 +284,20 @@ extract_one(afp, hdr)
                                verbose = TRUE;
                        }
 
+#if __MINGW32__
+            {
+                int old_mode;
+                fflush(stdout);
+                old_mode = setmode(fileno(stdout), O_BINARY);
+#endif
+
                        crc = decode_lzhuf
                                (afp, stdout, hdr->original_size, hdr->packed_size, name, method);
+#if __MINGW32__
+                fflush(stdout);
+                setmode(fileno(stdout), old_mode);
+            }
+#endif
                        quiet = save_quiet;
                        verbose = save_verbose;
                }
@@ -309,7 +333,6 @@ extract_one(afp, hdr)
 #endif
 
                        unlink(name);
-                       errno = 0;
                        remove_extracting_file_when_interrupt = TRUE;
 
                        if ((fp = open_with_make_path(name)) != NULL) {
@@ -326,14 +349,13 @@ extract_one(afp, hdr)
                                return;
                }
 
-               errno = 0;
                if (hdr->has_crc && crc != hdr->crc)
-                       error("CRC error", name);
+                       error("CRC error: \"%s\"", name);
        }
        else if ((hdr->unix_mode & UNIX_FILE_TYPEMASK) == UNIX_FILE_DIRECTORY
                         || (hdr->unix_mode & UNIX_FILE_TYPEMASK) == UNIX_FILE_SYMLINK
                         || method == LZHDIRS_METHOD_NUM) {
-               /* ¢¬¤³¤ì¤Ç¡¢Symblic Link ¤Ï¡¢Âç¾æÉפ«¡© */
+               /* ¢¬¤³¤ì¤Ç¡¢Symbolic Link ¤Ï¡¢Âç¾æÉפ«¡© */
                if (!ignore_directory && !verify_mode) {
                        if (noexec) {
                                if (quiet != TRUE)
@@ -368,15 +390,15 @@ extract_one(afp, hdr)
                                l_code = symlink(bb2, bb1);
                                if (l_code < 0) {
                                        if (quiet != TRUE)
-                                               warning("Can't make Symbolic Link : ");
+                                               warning("Can't make Symbolic Link \"%s\" -> \"%s\"",
+                                bb1, bb2);
                                }
                                if (quiet != TRUE) {
-                                       printf("Symbolic Link %s -> %s\n", bb1, bb2);
+                                       message("Symbolic Link %s -> %s", bb1, bb2);
                                }
                                strcpy(name, bb1);      /* Symbolic's name set */
 #else
-                               sprintf(buf, "%s -> %s", bb1, bb2);
-                               warning("Can't make Symbolic Link", buf);
+                               warning("Can't make Symbolic Link %s -> %s", bb1, bb2);
                                return;
 #endif
                        } else { /* make directory */
@@ -386,7 +408,7 @@ extract_one(afp, hdr)
                }
        }
        else {
-               error("Unknown information", name);
+               error("Unknown information: \"%s\"", name);
        }
 
        if (!output_to_stdout)
@@ -405,7 +427,7 @@ cmd_extract()
 
        /* open archive file */
        if ((afp = open_old_archive()) == NULL)
-               fatal_error(archive_name);
+               fatal_error("Cannot open archive file \"%s\"", archive_name);
 
        if (archive_is_msdos_sfx1(archive_name))
                skip_msdos_sfx1_code(afp);
@@ -415,7 +437,16 @@ cmd_extract()
                if (need_file(hdr.name)) {
                        pos = ftell(afp);
                        extract_one(afp, &hdr);
-                       fseek(afp, pos + hdr.packed_size, SEEK_SET);
+            /* when error occurred in extract_one(), should adjust
+               point of file stream */
+                       if (afp != stdin)
+                fseek(afp, pos + hdr.packed_size, SEEK_SET);
+            else {
+                /* FIXME: assume that the extract_one() has read
+                   packed_size bytes from stdin. */
+                long i = 0;
+                               while (i--) fgetc(afp);
+            }
                } else {
                        if (afp != stdin)
                                fseek(afp, hdr.packed_size, SEEK_CUR);
@@ -437,3 +468,4 @@ cmd_extract()
 /* mode:c */
 /* tab-width:4 */
 /* End: */
+/* vi: set tabstop=4: */