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", path);
124 if (mkdir(path, 0777) < 0) { /* try again */
125 error("Cannot make directory", 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) {
251 for (method = 0;; method++) {
252 if (methods[method] == NULL) {
253 error("Unknown method \"%.*s\"; \"%s\" will be skiped ...",
254 5, hdr->method, name);
257 if (memcmp(hdr->method, methods[method], 5) == 0)
262 reading_filename = archive_name;
263 writing_filename = name;
264 if (output_to_stdout || verify_mode) {
266 printf("%s %s\n", verify_mode ? "VERIFY" : "EXTRACT", name);
268 int i = hdr->packed_size;
276 save_verbose = verbose;
277 if (!quiet && output_to_stdout) {
278 printf("::::::::\n%s\n::::::::\n", name);
282 else if (verify_mode) {
291 old_mode = setmode(fileno(stdout), O_BINARY);
295 (afp, stdout, hdr->original_size, hdr->packed_size, name, method);
298 setmode(fileno(stdout), old_mode);
302 verbose = save_verbose;
305 if (skip_flg == FALSE) {
306 up_flag = inquire_extract(name);
307 if (up_flag == FALSE && force == FALSE) {
312 if (skip_flg == TRUE) { /* if skip_flg */
313 if (stat(name, &stbuf) == 0 && force != TRUE) {
314 if (stbuf.st_mtime >= hdr->unix_last_modified_stamp) {
316 printf("%s : Skipped...\n", name);
323 int i = hdr->packed_size;
330 signal(SIGINT, interrupt);
332 signal(SIGHUP, interrupt);
336 remove_extracting_file_when_interrupt = TRUE;
338 if ((fp = open_with_make_path(name)) != NULL) {
340 (afp, fp, hdr->original_size, hdr->packed_size, name, method);
343 remove_extracting_file_when_interrupt = FALSE;
344 signal(SIGINT, SIG_DFL);
346 signal(SIGHUP, SIG_DFL);
352 if (hdr->has_crc && crc != hdr->crc)
353 error("CRC error: \"%s\"", name);
355 else if ((hdr->unix_mode & UNIX_FILE_TYPEMASK) == UNIX_FILE_DIRECTORY
356 || (hdr->unix_mode & UNIX_FILE_TYPEMASK) == UNIX_FILE_SYMLINK
357 || method == LZHDIRS_METHOD_NUM) {
358 /* ¢¬¤³¤ì¤Ç¡¢Symbolic Link ¤Ï¡¢Âç¾æÉפ«¡© */
359 if (!ignore_directory && !verify_mode) {
362 printf("EXTRACT %s (directory)\n", name);
365 /* NAME has trailing SLASH '/', (^_^) */
366 if ((hdr->unix_mode & UNIX_FILE_TYPEMASK) == UNIX_FILE_SYMLINK) {
367 char buf[256], *bb1, *bb2;
370 bb1 = strtok(buf, "|");
371 bb2 = strtok(NULL, "|");
374 if (skip_flg == FALSE) {
375 up_flag = inquire_extract(name);
376 if (up_flag == FALSE && force == FALSE) {
380 if (GETSTAT(bb1, &stbuf) == 0 && force != TRUE) {
381 if (stbuf.st_mtime >= hdr->unix_last_modified_stamp) {
383 printf("%s : Skipped...\n", bb1);
390 l_code = symlink(bb2, bb1);
393 warning("Can't make Symbolic Link \"%s\" -> \"%s\"",
397 message("Symbolic Link %s -> %s", bb1, bb2);
399 strcpy(name, bb1); /* Symbolic's name set */
401 warning("Can't make Symbolic Link %s -> %s", bb1, bb2);
404 } else { /* make directory */
405 if (!output_to_stdout && !make_parent_path(name))
411 error("Unknown information: \"%s\"", name);
414 if (!output_to_stdout)
415 adjust_info(name, hdr);
418 /* ------------------------------------------------------------------------ */
419 /* EXTRACT COMMAND MAIN */
420 /* ------------------------------------------------------------------------ */
428 /* open archive file */
429 if ((afp = open_old_archive()) == NULL)
430 fatal_error("Cannot open archive file \"%s\"", archive_name);
432 if (archive_is_msdos_sfx1(archive_name))
433 skip_msdos_sfx1_code(afp);
435 /* extract each files */
436 while (get_header(afp, &hdr)) {
437 if (need_file(hdr.name)) {
439 extract_one(afp, &hdr);
440 /* when error occurred in extract_one(), should adjust
441 point of file stream */
443 fseek(afp, pos + hdr.packed_size, SEEK_SET);
445 /* FIXME: assume that the extract_one() has read
446 packed_size bytes from stdin. */
448 while (i--) fgetc(afp);
452 fseek(afp, hdr.packed_size, SEEK_CUR);
454 int i = hdr.packed_size;
461 /* close archive file */
467 /* Local Variables: */
471 /* vi: set tabstop=4: */