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 */
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;
231 * if OSK then strip device name
233 if (hdr->extend_type == EXTEND_OS68K
234 || hdr->extend_type == EXTEND_XOSK) {
237 while (c && c != '/');
239 q = "."; /* if device name only */
244 if (extract_directory)
245 xsnprintf(name, sizeof(name), "%s/%s", extract_directory, q);
250 /* LZHDIRS_METHOD¤ò»ý¤Ä¥Ø¥Ã¥À¤ò¥Á¥§¥Ã¥¯¤¹¤ë */
251 /* 1999.4.30 t.okamoto */
252 for (method = 0;; method++) {
253 if (methods[method] == NULL) {
254 error("Unknown method \"%.*s\"; \"%s\" will be skiped ...",
255 5, hdr->method, name);
258 if (memcmp(hdr->method, methods[method], 5) == 0)
262 if ((hdr->unix_mode & UNIX_FILE_TYPEMASK) == UNIX_FILE_REGULAR
263 && method != LZHDIRS_METHOD_NUM) {
266 for (method = 0;; method++) {
267 if (methods[method] == NULL) {
268 error("Unknown method \"%.*s\"; \"%s\" will be skiped ...",
269 5, hdr->method, name);
272 if (memcmp(hdr->method, methods[method], 5) == 0)
277 reading_filename = archive_name;
278 writing_filename = name;
279 if (output_to_stdout || verify_mode) {
281 printf("%s %s\n", verify_mode ? "VERIFY" : "EXTRACT", name);
286 save_verbose = verbose;
287 if (!quiet && output_to_stdout) {
288 printf("::::::::\n%s\n::::::::\n", name);
292 else if (verify_mode) {
301 old_mode = setmode(fileno(stdout), O_BINARY);
304 crc = decode_lzhuf(afp, stdout,
305 hdr->original_size, hdr->packed_size,
306 name, method, &read_size);
309 setmode(fileno(stdout), old_mode);
313 verbose = save_verbose;
316 if (skip_flg == FALSE) {
317 up_flag = inquire_extract(name);
318 if (up_flag == FALSE && force == FALSE) {
323 if (skip_flg == TRUE) { /* if skip_flg */
324 if (stat(name, &stbuf) == 0 && force != TRUE) {
325 if (stbuf.st_mtime >= hdr->unix_last_modified_stamp) {
327 printf("%s : Skipped...\n", name);
336 signal(SIGINT, interrupt);
338 signal(SIGHUP, interrupt);
342 remove_extracting_file_when_interrupt = TRUE;
344 if ((fp = open_with_make_path(name)) != NULL) {
345 crc = decode_lzhuf(afp, fp,
346 hdr->original_size, hdr->packed_size,
347 name, method, &read_size);
350 remove_extracting_file_when_interrupt = FALSE;
351 signal(SIGINT, SIG_DFL);
353 signal(SIGHUP, SIG_DFL);
359 if (hdr->has_crc && crc != hdr->crc)
360 error("CRC error: \"%s\"", name);
362 else if ((hdr->unix_mode & UNIX_FILE_TYPEMASK) == UNIX_FILE_DIRECTORY
363 || (hdr->unix_mode & UNIX_FILE_TYPEMASK) == UNIX_FILE_SYMLINK
364 || method == LZHDIRS_METHOD_NUM) {
365 /* ¢¬¤³¤ì¤Ç¡¢Symbolic Link ¤Ï¡¢Âç¾æÉפ«¡© */
366 if (!ignore_directory && !verify_mode) {
369 printf("EXTRACT %s (directory)\n", name);
372 /* NAME has trailing SLASH '/', (^_^) */
373 if ((hdr->unix_mode & UNIX_FILE_TYPEMASK) == UNIX_FILE_SYMLINK) {
377 if (skip_flg == FALSE) {
378 up_flag = inquire_extract(name);
379 if (up_flag == FALSE && force == FALSE) {
383 if (GETSTAT(name, &stbuf) == 0 && force != TRUE) {
384 if (stbuf.st_mtime >= hdr->unix_last_modified_stamp) {
386 printf("%s : Skipped...\n", name);
393 make_parent_path(name);
394 l_code = symlink(hdr->realname, name);
397 warning("Can't make Symbolic Link \"%s\" -> \"%s\"",
398 hdr->realname, name);
401 message("Symbolic Link %s -> %s",
402 hdr->realname, name);
405 warning("Can't make Symbolic Link %s -> %s",
406 hdr->realname, name);
409 } else { /* make directory */
410 if (!output_to_stdout && !make_parent_path(name))
412 /* save directory information */
413 add_dirinfo(name, hdr);
418 if (force) /* force extract */
419 goto extract_regular;
421 error("Unknown file type: \"%s\". use `f' option to force extract.", name);
424 if (!output_to_stdout)
425 adjust_info(name, hdr);
430 /* ------------------------------------------------------------------------ */
431 /* EXTRACT COMMAND MAIN */
432 /* ------------------------------------------------------------------------ */
441 /* open archive file */
442 if ((afp = open_old_archive()) == NULL)
443 fatal_error("Cannot open archive file \"%s\"", archive_name);
445 if (archive_is_msdos_sfx1(archive_name))
446 seek_lha_header(afp);
448 /* extract each files */
449 while (get_header(afp, &hdr)) {
450 if (need_file(hdr.name)) {
452 read_size = extract_one(afp, &hdr);
453 if (read_size != hdr.packed_size) {
454 /* when error occurred in extract_one(), should adjust
455 point of file stream */
456 if (pos != -1 && afp != stdin)
457 fseeko(afp, pos + hdr.packed_size - read_size, SEEK_SET);
459 size_t i = hdr.packed_size - read_size;
460 while (i--) fgetc(afp);
465 fseeko(afp, hdr.packed_size, SEEK_CUR);
467 size_t i = hdr.packed_size;
468 while (i--) fgetc(afp);
473 /* close archive file */
476 /* adjust directory information */
483 * restore directory information (time stamp).
484 * added by A.Iriyama 2003.12.12
487 typedef struct lhdDirectoryInfo_t {
488 struct lhdDirectoryInfo_t *next;
492 static LzHeaderList *dirinfo;
494 static void add_dirinfo(char *name, LzHeader *hdr)
498 if (memcmp(hdr->method, LZHDIRS_METHOD, 5) != 0)
501 p = xmalloc(sizeof(LzHeaderList));
503 memcpy(&p->hdr, hdr, sizeof(LzHeader));
504 strncpy(p->hdr.name, name, sizeof(p->hdr.name));
505 p->hdr.name[sizeof(p->hdr.name)-1] = 0;
508 LzHeaderList *tmp = dirinfo;
514 static void adjust_dirinfo()
517 adjust_info(dirinfo->hdr.name, &dirinfo->hdr);
520 LzHeaderList *tmp = dirinfo;
521 dirinfo = dirinfo->next;