1 /* ------------------------------------------------------------------------ */
3 /* lhext.c -- LHarc extract */
5 /* Copyright (C) MCMLXXXIX Yooichi.Tagawa */
6 /* Modified Nobutaka Watazaki */
8 /* Ver. 0.00 Original 1988.05.23 Y.Tagawa */
9 /* Ver. 1.00 Fixed 1989.09.22 Y.Tagawa */
10 /* Ver. 0.03 LHa for UNIX 1991.12.17 M.Oki */
11 /* Ver. 1.12 LHa for UNIX 1993.10.01 N.Watazaki */
12 /* Ver. 1.13b Symbolic Link Update Bug Fix 1994.06.21 N.Watazaki */
13 /* Ver. 1.14 Source All chagned 1995.01.14 N.Watazaki */
14 /* Ver. 1.14e bugfix 1999.04.30 T.Okamoto */
15 /* ------------------------------------------------------------------------ */
17 /* ------------------------------------------------------------------------ */
18 static int skip_flg = FALSE; /* FALSE..No Skip , TRUE..Skip */
19 static char *methods[] =
21 LZHUFF0_METHOD, LZHUFF1_METHOD, LZHUFF2_METHOD, LZHUFF3_METHOD,
22 LZHUFF4_METHOD, LZHUFF5_METHOD, LZHUFF6_METHOD, LZHUFF7_METHOD,
23 LARC_METHOD, LARC5_METHOD, LARC4_METHOD,
28 static void add_dirinfo(char* name, LzHeader* hdr);
29 static void adjust_dirinfo();
31 /* ------------------------------------------------------------------------ */
39 if (stat(name, &stbuf) >= 0) {
40 if (!is_regularfile(&stbuf)) {
41 error("\"%s\" already exists (not a file)", name);
46 printf("EXTRACT %s but file is exist.\n", name);
51 warning("skip to extract %s.", name);
55 switch (inquire("OverWrite ?(Yes/[No]/All/Skip)", name, "YyNnAaSs\n")) {
76 printf("EXTRACT %s\n", name);
81 /* ------------------------------------------------------------------------ */
83 make_parent_path(name)
86 char path[FILENAME_LENGTH];
90 /* make parent directory name into PATH for recursive call */
91 str_safe_copy(path, name, sizeof(path));
92 for (p = path + strlen(path); p > path; p--)
99 message("invalid path name \"%s\"", name);
100 return FALSE; /* no more parent. */
103 if (GETSTAT(path, &stbuf) >= 0) {
104 if (is_directory(&stbuf))
109 message("Making directory \"%s\".", path);
111 #if defined __MINGW32__
112 if (mkdir(path) >= 0)
115 if (mkdir(path, 0777) >= 0) /* try */
116 return TRUE; /* successful done. */
119 if (!make_parent_path(path))
122 #if defined __MINGW32__
123 if (mkdir(path) < 0) { /* try again */
124 error("Cannot make directory \"%s\"", path);
128 if (mkdir(path, 0777) < 0) { /* try again */
129 error("Cannot make directory \"%s\"", path);
137 /* ------------------------------------------------------------------------ */
139 open_with_make_path(name)
144 if ((fp = fopen(name, WRITE_BINARY)) == NULL) {
145 if (!make_parent_path(name) ||
146 (fp = fopen(name, WRITE_BINARY)) == NULL)
147 error("Cannot extract a file \"%s\"", name);
152 /* ------------------------------------------------------------------------ */
154 adjust_info(name, hdr)
158 struct utimbuf utimebuf;
160 /* adjust file stamp */
161 utimebuf.actime = utimebuf.modtime = hdr->unix_last_modified_stamp;
163 if ((hdr->unix_mode & UNIX_FILE_TYPEMASK) != UNIX_FILE_SYMLINK)
164 utime(name, &utimebuf);
166 if (hdr->extend_type == EXTEND_UNIX
167 || hdr->extend_type == EXTEND_OS68K
168 || hdr->extend_type == EXTEND_XOSK) {
169 #ifdef NOT_COMPATIBLE_MODE
170 Please need your modification in this space.
172 if ((hdr->unix_mode & UNIX_FILE_TYPEMASK) != UNIX_FILE_SYMLINK)
173 chmod(name, hdr->unix_mode);
176 uid_t uid = hdr->unix_uid;
177 gid_t gid = hdr->unix_gid;
179 #if HAVE_GETPWNAM && HAVE_GETGRNAM
181 struct passwd *ent = getpwnam(hdr->user);
182 if (ent) uid = ent->pw_uid;
185 struct group *ent = getgrnam(hdr->group);
186 if (ent) gid = ent->gr_gid;
191 if ((hdr->unix_mode & UNIX_FILE_TYPEMASK) == UNIX_FILE_SYMLINK)
192 lchown(name, uid, gid);
194 #endif /* HAVE_LCHWON */
195 chown(name, uid, gid);
200 /* On Cygwin, execute permission should be set for .exe or .dll. */
203 umask(m = umask(0)); /* get current umask */
204 chmod(name, 0777 & ~m);
209 /* ------------------------------------------------------------------------ */
211 extract_one(afp, hdr)
212 FILE *afp; /* archive file */
215 FILE *fp; /* output file */
217 char name[FILENAME_LENGTH];
220 boolean save_quiet, save_verbose, up_flag;
221 char *q = hdr->name, c;
222 size_t read_size = 0;
224 if (ignore_directory && strrchr(hdr->name, '/')) {
225 q = (char *) strrchr(hdr->name, '/') + 1;
228 if (is_directory_traversal(q)) {
229 fprintf(stderr, "Possible directory traversal hack attempt in %s\n", q);
234 while (*q == '/') { q++; }
237 * if OSK then strip device name
239 if (hdr->extend_type == EXTEND_OS68K
240 || hdr->extend_type == EXTEND_XOSK) {
243 while (c && c != '/');
245 q = "."; /* if device name only */
250 if (extract_directory)
251 xsnprintf(name, sizeof(name), "%s/%s", extract_directory, q);
253 str_safe_copy(name, q, sizeof(name));
255 /* LZHDIRS_METHOD¤ò»ý¤Ä¥Ø¥Ã¥À¤ò¥Á¥§¥Ã¥¯¤¹¤ë */
256 /* 1999.4.30 t.okamoto */
257 for (method = 0;; method++) {
258 if (methods[method] == NULL) {
259 error("Unknown method \"%.*s\"; \"%s\" will be skiped ...",
260 5, hdr->method, name);
263 if (memcmp(hdr->method, methods[method], 5) == 0)
267 if ((hdr->unix_mode & UNIX_FILE_TYPEMASK) == UNIX_FILE_REGULAR
268 && method != LZHDIRS_METHOD_NUM) {
271 for (method = 0;; method++) {
272 if (methods[method] == NULL) {
273 error("Unknown method \"%.*s\"; \"%s\" will be skiped ...",
274 5, hdr->method, name);
277 if (memcmp(hdr->method, methods[method], 5) == 0)
282 reading_filename = archive_name;
283 writing_filename = name;
284 if (output_to_stdout || verify_mode) {
286 printf("%s %s\n", verify_mode ? "VERIFY" : "EXTRACT", name);
291 save_verbose = verbose;
292 if (!quiet && output_to_stdout) {
293 printf("::::::::\n%s\n::::::::\n", name);
297 else if (verify_mode) {
306 old_mode = setmode(fileno(stdout), O_BINARY);
309 crc = decode_lzhuf(afp, stdout,
310 hdr->original_size, hdr->packed_size,
311 name, method, &read_size);
314 setmode(fileno(stdout), old_mode);
318 verbose = save_verbose;
321 if (skip_flg == FALSE) {
322 up_flag = inquire_extract(name);
323 if (up_flag == FALSE && force == FALSE) {
328 if (skip_flg == TRUE) { /* if skip_flg */
329 if (stat(name, &stbuf) == 0 && force != TRUE) {
330 if (stbuf.st_mtime >= hdr->unix_last_modified_stamp) {
332 printf("%s : Skipped...\n", name);
341 signal(SIGINT, interrupt);
343 signal(SIGHUP, interrupt);
347 remove_extracting_file_when_interrupt = TRUE;
349 if ((fp = open_with_make_path(name)) != NULL) {
350 crc = decode_lzhuf(afp, fp,
351 hdr->original_size, hdr->packed_size,
352 name, method, &read_size);
355 remove_extracting_file_when_interrupt = FALSE;
356 signal(SIGINT, SIG_DFL);
358 signal(SIGHUP, SIG_DFL);
364 if (hdr->has_crc && crc != hdr->crc)
365 error("CRC error: \"%s\"", name);
367 else if ((hdr->unix_mode & UNIX_FILE_TYPEMASK) == UNIX_FILE_DIRECTORY
368 || (hdr->unix_mode & UNIX_FILE_TYPEMASK) == UNIX_FILE_SYMLINK
369 || method == LZHDIRS_METHOD_NUM) {
370 /* ¢¬¤³¤ì¤Ç¡¢Symbolic Link ¤Ï¡¢Âç¾æÉפ«¡© */
371 if (!ignore_directory && !verify_mode) {
374 printf("EXTRACT %s (directory)\n", name);
377 /* NAME has trailing SLASH '/', (^_^) */
378 if ((hdr->unix_mode & UNIX_FILE_TYPEMASK) == UNIX_FILE_SYMLINK) {
382 if (skip_flg == FALSE) {
383 up_flag = inquire_extract(name);
384 if (up_flag == FALSE && force == FALSE) {
388 if (GETSTAT(name, &stbuf) == 0 && force != TRUE) {
389 if (stbuf.st_mtime >= hdr->unix_last_modified_stamp) {
391 printf("%s : Skipped...\n", name);
398 make_parent_path(name);
399 l_code = symlink(hdr->realname, name);
402 warning("Can't make Symbolic Link \"%s\" -> \"%s\"",
403 hdr->realname, name);
406 message("Symbolic Link %s -> %s",
407 hdr->realname, name);
410 warning("Can't make Symbolic Link %s -> %s",
411 hdr->realname, name);
414 } else { /* make directory */
415 if (!output_to_stdout && !make_parent_path(name))
417 /* save directory information */
418 add_dirinfo(name, hdr);
423 if (force) /* force extract */
424 goto extract_regular;
426 error("Unknown file type: \"%s\". use `f' option to force extract.", name);
429 if (!output_to_stdout)
430 adjust_info(name, hdr);
435 /* ------------------------------------------------------------------------ */
436 /* EXTRACT COMMAND MAIN */
437 /* ------------------------------------------------------------------------ */
446 /* open archive file */
447 if ((afp = open_old_archive()) == NULL)
448 fatal_error("Cannot open archive file \"%s\"", archive_name);
450 if (archive_is_msdos_sfx1(archive_name))
451 seek_lha_header(afp);
453 /* extract each files */
454 while (get_header(afp, &hdr)) {
455 if (need_file(hdr.name)) {
457 read_size = extract_one(afp, &hdr);
458 if (read_size != hdr.packed_size) {
459 /* when error occurred in extract_one(), should adjust
460 point of file stream */
461 if (pos != -1 && afp != stdin)
462 fseeko(afp, pos + hdr.packed_size - read_size, SEEK_SET);
464 size_t i = hdr.packed_size - read_size;
465 while (i--) fgetc(afp);
470 fseeko(afp, hdr.packed_size, SEEK_CUR);
472 size_t i = hdr.packed_size;
473 while (i--) fgetc(afp);
478 /* close archive file */
481 /* adjust directory information */
488 is_directory_traversal(char *string)
490 unsigned int type = 0; /* 0 = new, 1 = only dots, 2 = other chars than dots */
496 if (temp[0] == '/') {
497 if (type == 1) { return 1; }
503 if ((temp[0] == '.') && (type < 2))
515 * restore directory information (time stamp).
516 * added by A.Iriyama 2003.12.12
519 typedef struct lhdDirectoryInfo_t {
520 struct lhdDirectoryInfo_t *next;
524 static LzHeaderList *dirinfo;
526 static void add_dirinfo(char *name, LzHeader *hdr)
530 if (memcmp(hdr->method, LZHDIRS_METHOD, 5) != 0)
533 p = xmalloc(sizeof(LzHeaderList));
535 memcpy(&p->hdr, hdr, sizeof(LzHeader));
536 strncpy(p->hdr.name, name, sizeof(p->hdr.name));
537 p->hdr.name[sizeof(p->hdr.name)-1] = 0;
540 LzHeaderList *tmp = dirinfo;
546 static void adjust_dirinfo()
549 adjust_info(dirinfo->hdr.name, &dirinfo->hdr);
552 LzHeaderList *tmp = dirinfo;
553 dirinfo = dirinfo->next;