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 /* ------------------------------------------------------------------------ */
36 if (stat(name, &stbuf) >= 0) {
37 if (!is_regularfile(&stbuf)) {
38 error("\"%s\" already exists (not a file)", name);
43 printf("EXTRACT %s but file is exist.\n", name);
48 warning("skip to extract %s.", name);
52 switch (inquire("OverWrite ?(Yes/[No]/All/Skip)", name, "YyNnAaSs\n")) {
73 printf("EXTRACT %s\n", name);
78 /* ------------------------------------------------------------------------ */
80 make_parent_path(name)
83 char path[FILENAME_LENGTH];
87 /* make parent directory name into PATH for recursive call */
89 for (p = path + strlen(path); p > path; p--)
96 message("invalid path name \"%s\"", name);
97 return FALSE; /* no more parent. */
100 if (GETSTAT(path, &stbuf) >= 0) {
101 if (is_directory(&stbuf))
106 message("Making directory \"%s\".", path);
108 #if defined __MINGW32__
109 if (mkdir(path) >= 0)
112 if (mkdir(path, 0777) >= 0) /* try */
113 return TRUE; /* successful done. */
116 if (!make_parent_path(path))
119 #if defined __MINGW32__
120 if (mkdir(path) < 0) { /* try again */
121 error("Cannot make directory \"%s\"", path);
125 if (mkdir(path, 0777) < 0) { /* try again */
126 error("Cannot make directory \"%s\"", path);
134 /* ------------------------------------------------------------------------ */
136 open_with_make_path(name)
141 if ((fp = fopen(name, WRITE_BINARY)) == NULL) {
142 if (!make_parent_path(name) ||
143 (fp = fopen(name, WRITE_BINARY)) == NULL)
144 error("Cannot extract a file \"%s\"", name);
149 /* ------------------------------------------------------------------------ */
151 adjust_info(name, hdr)
155 struct utimbuf utimebuf;
157 /* adjust file stamp */
158 utimebuf.actime = utimebuf.modtime = hdr->unix_last_modified_stamp;
160 if ((hdr->unix_mode & UNIX_FILE_TYPEMASK) != UNIX_FILE_SYMLINK)
161 utime(name, &utimebuf);
163 if (hdr->extend_type == EXTEND_UNIX
164 || hdr->extend_type == EXTEND_OS68K
165 || hdr->extend_type == EXTEND_XOSK) {
166 #ifdef NOT_COMPATIBLE_MODE
167 Please need your modification in this space.
169 if ((hdr->unix_mode & UNIX_FILE_TYPEMASK) != UNIX_FILE_SYMLINK)
170 chmod(name, hdr->unix_mode);
173 uid_t uid = hdr->unix_uid;
174 gid_t gid = hdr->unix_gid;
176 #if HAVE_GETPWNAM && HAVE_GETGRNAM
178 struct passwd *ent = getpwnam(hdr->user);
179 if (ent) uid = ent->pw_uid;
182 struct group *ent = getgrnam(hdr->group);
183 if (ent) gid = ent->gr_gid;
188 if ((hdr->unix_mode & UNIX_FILE_TYPEMASK) == UNIX_FILE_SYMLINK)
189 lchown(name, uid, gid);
191 #endif /* HAVE_LCHWON */
192 chown(name, uid, gid);
197 /* On Cygwin, execute permission should be set for .exe or .dll. */
200 umask(m = umask(0)); /* get current umask */
201 chmod(name, 0777 & ~m);
206 /* ------------------------------------------------------------------------ */
208 extract_one(afp, hdr)
209 FILE *afp; /* archive file */
212 FILE *fp; /* output file */
214 char name[FILENAME_LENGTH];
217 boolean save_quiet, save_verbose, up_flag;
218 char *q = hdr->name, c;
219 size_t read_size = 0;
221 if (ignore_directory && strrchr(hdr->name, '/')) {
222 q = (char *) strrchr(hdr->name, '/') + 1;
228 * if OSK then strip device name
230 if (hdr->extend_type == EXTEND_OS68K
231 || hdr->extend_type == EXTEND_XOSK) {
234 while (c && c != '/');
236 q = "."; /* if device name only */
241 if (extract_directory)
242 xsnprintf(name, sizeof(name), "%s/%s", extract_directory, q);
247 /* LZHDIRS_METHOD¤ò»ý¤Ä¥Ø¥Ã¥À¤ò¥Á¥§¥Ã¥¯¤¹¤ë */
248 /* 1999.4.30 t.okamoto */
249 for (method = 0;; method++) {
250 if (methods[method] == NULL) {
251 error("Unknown method \"%.*s\"; \"%s\" will be skiped ...",
252 5, hdr->method, name);
255 if (memcmp(hdr->method, methods[method], 5) == 0)
259 if ((hdr->unix_mode & UNIX_FILE_TYPEMASK) == UNIX_FILE_REGULAR
260 && method != LZHDIRS_METHOD_NUM) {
263 for (method = 0;; method++) {
264 if (methods[method] == NULL) {
265 error("Unknown method \"%.*s\"; \"%s\" will be skiped ...",
266 5, hdr->method, name);
269 if (memcmp(hdr->method, methods[method], 5) == 0)
274 reading_filename = archive_name;
275 writing_filename = name;
276 if (output_to_stdout || verify_mode) {
278 printf("%s %s\n", verify_mode ? "VERIFY" : "EXTRACT", name);
283 save_verbose = verbose;
284 if (!quiet && output_to_stdout) {
285 printf("::::::::\n%s\n::::::::\n", name);
289 else if (verify_mode) {
298 old_mode = setmode(fileno(stdout), O_BINARY);
301 crc = decode_lzhuf(afp, stdout,
302 hdr->original_size, hdr->packed_size,
303 name, method, &read_size);
306 setmode(fileno(stdout), old_mode);
310 verbose = save_verbose;
313 if (skip_flg == FALSE) {
314 up_flag = inquire_extract(name);
315 if (up_flag == FALSE && force == FALSE) {
320 if (skip_flg == TRUE) { /* if skip_flg */
321 if (stat(name, &stbuf) == 0 && force != TRUE) {
322 if (stbuf.st_mtime >= hdr->unix_last_modified_stamp) {
324 printf("%s : Skipped...\n", name);
333 signal(SIGINT, interrupt);
335 signal(SIGHUP, interrupt);
339 remove_extracting_file_when_interrupt = TRUE;
341 if ((fp = open_with_make_path(name)) != NULL) {
342 crc = decode_lzhuf(afp, fp,
343 hdr->original_size, hdr->packed_size,
344 name, method, &read_size);
347 remove_extracting_file_when_interrupt = FALSE;
348 signal(SIGINT, SIG_DFL);
350 signal(SIGHUP, SIG_DFL);
356 if (hdr->has_crc && crc != hdr->crc)
357 error("CRC error: \"%s\"", name);
359 else if ((hdr->unix_mode & UNIX_FILE_TYPEMASK) == UNIX_FILE_DIRECTORY
360 || (hdr->unix_mode & UNIX_FILE_TYPEMASK) == UNIX_FILE_SYMLINK
361 || method == LZHDIRS_METHOD_NUM) {
362 /* ¢¬¤³¤ì¤Ç¡¢Symbolic Link ¤Ï¡¢Âç¾æÉפ«¡© */
363 if (!ignore_directory && !verify_mode) {
366 printf("EXTRACT %s (directory)\n", name);
369 /* NAME has trailing SLASH '/', (^_^) */
370 if ((hdr->unix_mode & UNIX_FILE_TYPEMASK) == UNIX_FILE_SYMLINK) {
374 if (skip_flg == FALSE) {
375 up_flag = inquire_extract(name);
376 if (up_flag == FALSE && force == FALSE) {
380 if (GETSTAT(name, &stbuf) == 0 && force != TRUE) {
381 if (stbuf.st_mtime >= hdr->unix_last_modified_stamp) {
383 printf("%s : Skipped...\n", name);
390 make_parent_path(name);
391 l_code = symlink(hdr->realname, name);
394 warning("Can't make Symbolic Link \"%s\" -> \"%s\"",
395 hdr->realname, name);
398 message("Symbolic Link %s -> %s",
399 hdr->realname, name);
402 warning("Can't make Symbolic Link %s -> %s",
403 hdr->realname, name);
406 } else { /* make directory */
407 if (!output_to_stdout && !make_parent_path(name))
413 if (force) /* force extract */
414 goto extract_regular;
416 error("Unknown file type: \"%s\". use `f' option to force extract.", name);
419 if (!output_to_stdout)
420 adjust_info(name, hdr);
425 /* ------------------------------------------------------------------------ */
426 /* EXTRACT COMMAND MAIN */
427 /* ------------------------------------------------------------------------ */
436 /* open archive file */
437 if ((afp = open_old_archive()) == NULL)
438 fatal_error("Cannot open archive file \"%s\"", archive_name);
440 if (archive_is_msdos_sfx1(archive_name))
441 seek_lha_header(afp);
443 /* extract each files */
444 while (get_header(afp, &hdr)) {
445 if (need_file(hdr.name)) {
447 read_size = extract_one(afp, &hdr);
448 if (read_size != hdr.packed_size) {
449 /* when error occurred in extract_one(), should adjust
450 point of file stream */
451 if (pos != -1 && afp != stdin)
452 fseeko(afp, pos + hdr.packed_size - read_size, SEEK_SET);
454 size_t i = hdr.packed_size - read_size;
455 while (i--) fgetc(afp);
460 fseeko(afp, hdr.packed_size, SEEK_CUR);
462 size_t i = hdr.packed_size;
463 while (i--) fgetc(afp);
468 /* close archive file */