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);
50 switch (inquire("OverWrite ?(Yes/[No]/All/Skip)", name, "YyNnAaSs\n")) {
70 printf("EXTRACT %s\n", name);
75 /* ------------------------------------------------------------------------ */
77 make_parent_path(name)
80 char path[FILENAME_LENGTH];
84 /* make parent directory name into PATH for recursive call */
86 for (p = path + strlen(path); p > path; p--)
93 message("invalid path name \"%s\"", name);
94 return FALSE; /* no more parent. */
97 if (GETSTAT(path, &stbuf) >= 0) {
98 if (is_directory(&stbuf))
100 error("Not a directory \"%s\"", path);
105 message("Making directory \"%s\".", path);
107 #if defined __MINGW32__
108 if (mkdir(path) >= 0)
111 if (mkdir(path, 0777) >= 0) /* try */
112 return TRUE; /* successful done. */
115 if (!make_parent_path(path))
118 #if defined __MINGW32__
119 if (mkdir(path) < 0) { /* try again */
120 error("Cannot make directory \"%s\"", path);
124 if (mkdir(path, 0777) < 0) { /* try again */
125 error("Cannot make directory \"%s\"", path);
133 /* ------------------------------------------------------------------------ */
135 open_with_make_path(name)
140 if ((fp = fopen(name, WRITE_BINARY)) == NULL) {
141 if (!make_parent_path(name) ||
142 (fp = fopen(name, WRITE_BINARY)) == NULL)
143 error("Cannot extract a file \"%s\"", name);
148 /* ------------------------------------------------------------------------ */
150 adjust_info(name, hdr)
154 struct utimbuf utimebuf;
156 /* adjust file stamp */
157 utimebuf.actime = utimebuf.modtime = hdr->unix_last_modified_stamp;
159 if ((hdr->unix_mode & UNIX_FILE_TYPEMASK) != UNIX_FILE_SYMLINK)
160 utime(name, &utimebuf);
162 if (hdr->extend_type == EXTEND_UNIX
163 || hdr->extend_type == EXTEND_OS68K
164 || hdr->extend_type == EXTEND_XOSK) {
165 #ifdef NOT_COMPATIBLE_MODE
166 Please need your modification in this space.
168 if ((hdr->unix_mode & UNIX_FILE_TYPEMASK) != UNIX_FILE_SYMLINK)
169 chmod(name, hdr->unix_mode);
172 uid_t uid = hdr->unix_uid;
173 gid_t gid = hdr->unix_gid;
175 #if HAVE_GETPWNAM && HAVE_GETGRNAM
177 struct passwd *ent = getpwnam(hdr->user);
178 if (ent) uid = ent->pw_uid;
181 struct group *ent = getgrnam(hdr->group);
182 if (ent) gid = ent->gr_gid;
187 if ((hdr->unix_mode & UNIX_FILE_TYPEMASK) == UNIX_FILE_SYMLINK)
188 lchown(name, uid, gid);
190 #endif /* HAVE_LCHWON */
191 chown(name, uid, gid);
196 /* ------------------------------------------------------------------------ */
198 extract_one(afp, hdr)
199 FILE *afp; /* archive file */
202 FILE *fp; /* output file */
204 char name[FILENAME_LENGTH];
207 boolean save_quiet, save_verbose, up_flag;
208 char *q = hdr->name, c;
210 if (ignore_directory && strrchr(hdr->name, '/')) {
211 q = (char *) strrchr(hdr->name, '/') + 1;
217 * if OSK then strip device name
219 if (hdr->extend_type == EXTEND_OS68K
220 || hdr->extend_type == EXTEND_XOSK) {
223 while (c && c != '/');
225 q = "."; /* if device name only */
230 if (extract_directory)
231 xsnprintf(name, sizeof(name), "%s/%s", extract_directory, q);
236 /* LZHDIRS_METHOD¤ò»ý¤Ä¥Ø¥Ã¥À¤ò¥Á¥§¥Ã¥¯¤¹¤ë */
237 /* 1999.4.30 t.okamoto */
238 for (method = 0;; method++) {
239 if (methods[method] == NULL) {
240 error("Unknown method \"%.*s\"; \"%s\" will be skiped ...",
241 5, hdr->method, name);
244 if (memcmp(hdr->method, methods[method], 5) == 0)
248 if ((hdr->unix_mode & UNIX_FILE_TYPEMASK) == UNIX_FILE_REGULAR
249 && method != LZHDIRS_METHOD_NUM) {
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)
263 reading_filename = archive_name;
264 writing_filename = name;
265 if (output_to_stdout || verify_mode) {
267 printf("%s %s\n", verify_mode ? "VERIFY" : "EXTRACT", name);
269 int i = hdr->packed_size;
277 save_verbose = verbose;
278 if (!quiet && output_to_stdout) {
279 printf("::::::::\n%s\n::::::::\n", name);
283 else if (verify_mode) {
292 old_mode = setmode(fileno(stdout), O_BINARY);
296 (afp, stdout, hdr->original_size, hdr->packed_size, name, method);
299 setmode(fileno(stdout), old_mode);
303 verbose = save_verbose;
306 if (skip_flg == FALSE) {
307 up_flag = inquire_extract(name);
308 if (up_flag == FALSE && force == FALSE) {
313 if (skip_flg == TRUE) { /* if skip_flg */
314 if (stat(name, &stbuf) == 0 && force != TRUE) {
315 if (stbuf.st_mtime >= hdr->unix_last_modified_stamp) {
317 printf("%s : Skipped...\n", name);
324 int i = hdr->packed_size;
331 signal(SIGINT, interrupt);
333 signal(SIGHUP, interrupt);
337 remove_extracting_file_when_interrupt = TRUE;
339 if ((fp = open_with_make_path(name)) != NULL) {
341 (afp, fp, hdr->original_size, hdr->packed_size, name, method);
344 remove_extracting_file_when_interrupt = FALSE;
345 signal(SIGINT, SIG_DFL);
347 signal(SIGHUP, SIG_DFL);
353 if (hdr->has_crc && crc != hdr->crc)
354 error("CRC error: \"%s\"", name);
356 else if ((hdr->unix_mode & UNIX_FILE_TYPEMASK) == UNIX_FILE_DIRECTORY
357 || (hdr->unix_mode & UNIX_FILE_TYPEMASK) == UNIX_FILE_SYMLINK
358 || method == LZHDIRS_METHOD_NUM) {
359 /* ¢¬¤³¤ì¤Ç¡¢Symbolic Link ¤Ï¡¢Âç¾æÉפ«¡© */
360 if (!ignore_directory && !verify_mode) {
363 printf("EXTRACT %s (directory)\n", name);
366 /* NAME has trailing SLASH '/', (^_^) */
367 if ((hdr->unix_mode & UNIX_FILE_TYPEMASK) == UNIX_FILE_SYMLINK) {
368 char buf[256], *bb1, *bb2;
371 bb1 = strtok(buf, "|");
372 bb2 = strtok(NULL, "|");
375 if (skip_flg == FALSE) {
376 up_flag = inquire_extract(name);
377 if (up_flag == FALSE && force == FALSE) {
381 if (GETSTAT(bb1, &stbuf) == 0 && force != TRUE) {
382 if (stbuf.st_mtime >= hdr->unix_last_modified_stamp) {
384 printf("%s : Skipped...\n", bb1);
391 l_code = symlink(bb2, bb1);
394 warning("Can't make Symbolic Link \"%s\" -> \"%s\"",
398 message("Symbolic Link %s -> %s", bb1, bb2);
400 strcpy(name, bb1); /* Symbolic's name set */
402 warning("Can't make Symbolic Link %s -> %s", bb1, bb2);
405 } else { /* make directory */
406 if (!output_to_stdout && !make_parent_path(name))
412 if (force) /* force extract */
413 goto extract_regular;
415 error("Unknown file type: \"%s\". use `f' option to force extract.", name);
418 if (!output_to_stdout)
419 adjust_info(name, hdr);
422 /* ------------------------------------------------------------------------ */
423 /* EXTRACT COMMAND MAIN */
424 /* ------------------------------------------------------------------------ */
432 /* open archive file */
433 if ((afp = open_old_archive()) == NULL)
434 fatal_error("Cannot open archive file \"%s\"", archive_name);
436 if (archive_is_msdos_sfx1(archive_name))
437 skip_msdos_sfx1_code(afp);
439 /* extract each files */
440 while (get_header(afp, &hdr)) {
441 if (need_file(hdr.name)) {
443 extract_one(afp, &hdr);
444 /* when error occurred in extract_one(), should adjust
445 point of file stream */
447 fseek(afp, pos + hdr.packed_size, SEEK_SET);
449 /* FIXME: assume that the extract_one() has read
450 packed_size bytes from stdin. */
452 while (i--) fgetc(afp);
456 fseek(afp, hdr.packed_size, SEEK_CUR);
458 int i = hdr.packed_size;
465 /* close archive file */
471 /* Local Variables: */
475 /* vi: set tabstop=4: */