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__
120 error("Cannot make directory", path);
123 if (mkdir(path, 0777) < 0) { /* try again */
124 error("Cannot make directory", path);
132 /* ------------------------------------------------------------------------ */
134 open_with_make_path(name)
139 if ((fp = fopen(name, WRITE_BINARY)) == NULL) {
140 if (!make_parent_path(name) ||
141 (fp = fopen(name, WRITE_BINARY)) == NULL)
142 error("Cannot extract a file \"%s\"", name);
147 /* ------------------------------------------------------------------------ */
149 adjust_info(name, hdr)
155 /* adjust file stamp */
156 utimebuf[0] = utimebuf[1] = hdr->unix_last_modified_stamp;
158 if ((hdr->unix_mode & UNIX_FILE_TYPEMASK) != UNIX_FILE_SYMLINK)
159 utime(name, utimebuf);
161 if (hdr->extend_type == EXTEND_UNIX
162 || hdr->extend_type == EXTEND_OS68K
163 || hdr->extend_type == EXTEND_XOSK) {
164 #ifdef NOT_COMPATIBLE_MODE
165 Please need your modification in this space.
167 if ((hdr->unix_mode & UNIX_FILE_TYPEMASK) != UNIX_FILE_SYMLINK)
168 chmod(name, hdr->unix_mode);
171 uid_t uid = hdr->unix_uid;
172 gid_t gid = hdr->unix_gid;
174 #if HAVE_GETPWNAM && HAVE_GETGRNAM
176 struct passwd *ent = getpwnam(hdr->user);
177 if (ent) uid = ent->pw_uid;
180 struct group *ent = getgrnam(hdr->group);
181 if (ent) gid = ent->gr_gid;
186 if ((hdr->unix_mode & UNIX_FILE_TYPEMASK) == UNIX_FILE_SYMLINK)
187 lchown(name, uid, gid);
189 #endif /* HAVE_LCHWON */
190 chown(name, uid, gid);
195 /* ------------------------------------------------------------------------ */
197 extract_one(afp, hdr)
198 FILE *afp; /* archive file */
201 FILE *fp; /* output file */
206 boolean save_quiet, save_verbose, up_flag;
207 char *q = hdr->name, c;
209 if (ignore_directory && strrchr(hdr->name, '/')) {
210 q = (char *) strrchr(hdr->name, '/') + 1;
216 * if OSK then strip device name
218 if (hdr->extend_type == EXTEND_OS68K
219 || hdr->extend_type == EXTEND_XOSK) {
222 while (c && c != '/');
224 q = "."; /* if device name only */
229 if (extract_directory)
230 xsnprintf(name, sizeof(name), "%s/%s", extract_directory, q);
235 /* LZHDIRS_METHOD¤ò»ý¤Ä¥Ø¥Ã¥À¤ò¥Á¥§¥Ã¥¯¤¹¤ë */
236 /* 1999.4.30 t.okamoto */
237 for (method = 0;; method++) {
238 if (methods[method] == NULL) {
239 error("Unknown method \"%.*s\"; \"%s\" will be skiped ...",
240 5, hdr->method, name);
243 if (bcmp(hdr->method, methods[method], 5) == 0)
247 if ((hdr->unix_mode & UNIX_FILE_TYPEMASK) == UNIX_FILE_REGULAR
248 && method != LZHDIRS_METHOD_NUM) {
250 for (method = 0;; method++) {
251 if (methods[method] == NULL) {
252 error("Unknown method \"%.*s\"; \"%s\" will be skiped ...",
253 5, hdr->method, name);
255 if (bcmp(hdr->method, methods[method], 5) == 0)
260 reading_filename = archive_name;
261 writting_filename = name;
262 if (output_to_stdout || verify_mode) {
264 printf("%s %s\n", verify_mode ? "VERIFY" : "EXTRACT", name);
266 int i = hdr->packed_size;
274 save_verbose = verbose;
275 if (!quiet && output_to_stdout) {
276 printf("::::::::\n%s\n::::::::\n", name);
280 else if (verify_mode) {
289 old_mode = setmode(fileno(stdout), O_BINARY);
293 (afp, stdout, hdr->original_size, hdr->packed_size, name, method);
296 setmode(fileno(stdout), old_mode);
300 verbose = save_verbose;
303 if (skip_flg == FALSE) {
304 up_flag = inquire_extract(name);
305 if (up_flag == FALSE && force == FALSE) {
310 if (skip_flg == TRUE) { /* if skip_flg */
311 if (stat(name, &stbuf) == 0 && force != TRUE) {
312 if (stbuf.st_mtime >= hdr->unix_last_modified_stamp) {
314 printf("%s : Skipped...\n", name);
321 int i = hdr->packed_size;
328 signal(SIGINT, interrupt);
330 signal(SIGHUP, interrupt);
334 remove_extracting_file_when_interrupt = TRUE;
336 if ((fp = open_with_make_path(name)) != NULL) {
338 (afp, fp, hdr->original_size, hdr->packed_size, name, method);
341 remove_extracting_file_when_interrupt = FALSE;
342 signal(SIGINT, SIG_DFL);
344 signal(SIGHUP, SIG_DFL);
350 if (hdr->has_crc && crc != hdr->crc)
351 error("CRC error: \"%s\"", name);
353 else if ((hdr->unix_mode & UNIX_FILE_TYPEMASK) == UNIX_FILE_DIRECTORY
354 || (hdr->unix_mode & UNIX_FILE_TYPEMASK) == UNIX_FILE_SYMLINK
355 || method == LZHDIRS_METHOD_NUM) {
356 /* ¢¬¤³¤ì¤Ç¡¢Symblic Link ¤Ï¡¢Âç¾æÉפ«¡© */
357 if (!ignore_directory && !verify_mode) {
360 printf("EXTRACT %s (directory)\n", name);
363 /* NAME has trailing SLASH '/', (^_^) */
364 if ((hdr->unix_mode & UNIX_FILE_TYPEMASK) == UNIX_FILE_SYMLINK) {
365 char buf[256], *bb1, *bb2;
368 bb1 = strtok(buf, "|");
369 bb2 = strtok(NULL, "|");
372 if (skip_flg == FALSE) {
373 up_flag = inquire_extract(name);
374 if (up_flag == FALSE && force == FALSE) {
378 if (GETSTAT(bb1, &stbuf) == 0 && force != TRUE) {
379 if (stbuf.st_mtime >= hdr->unix_last_modified_stamp) {
381 printf("%s : Skipped...\n", bb1);
388 l_code = symlink(bb2, bb1);
391 warning("Can't make Symbolic Link \"%s\" -> \"%s\"",
395 message("Symbolic Link %s -> %s", bb1, bb2);
397 strcpy(name, bb1); /* Symbolic's name set */
399 warning("Can't make Symbolic Link %s -> %s", bb1, bb2);
402 } else { /* make directory */
403 if (!output_to_stdout && !make_parent_path(name))
409 error("Unknown information: \"%s\"", name);
412 if (!output_to_stdout)
413 adjust_info(name, hdr);
416 /* ------------------------------------------------------------------------ */
417 /* EXTRACT COMMAND MAIN */
418 /* ------------------------------------------------------------------------ */
426 /* open archive file */
427 if ((afp = open_old_archive()) == NULL)
428 fatal_error("Cannot open archive file \"%s\"", archive_name);
430 if (archive_is_msdos_sfx1(archive_name))
431 skip_msdos_sfx1_code(afp);
433 /* extract each files */
434 while (get_header(afp, &hdr)) {
435 if (need_file(hdr.name)) {
437 extract_one(afp, &hdr);
438 /* when error occurred in extract_one(), should adjust
439 point of file stream */
441 fseek(afp, pos + hdr.packed_size, SEEK_SET);
443 /* FIXME: assume that the extract_one() has read
444 packed_size bytes from stdin. */
446 while (i--) fgetc(afp);
450 fseek(afp, hdr.packed_size, SEEK_CUR);
452 int i = hdr.packed_size;
459 /* close archive file */
465 /* Local Variables: */
469 /* vi: set tabstop=4: */