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("Already exist (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("Why?", "ROOT");
94 return FALSE; /* no more parent. */
97 if (GETSTAT(path, &stbuf) >= 0) {
98 if (is_directory(&stbuf))
100 error("Not a directory", path);
106 printf("Making directory \"%s\".\n", path);
108 #if defined __MINGW32__
109 if (mkdir(path) >= 0)
112 if (mkdir(path, 0777) >= 0) /* try */
113 return TRUE; /* successful done. */
117 if (!make_parent_path(path))
120 #if defined __MINGW32__
122 message("Cannot make directory", path);
125 if (mkdir(path, 0777) < 0) { /* try again */
126 message("Cannot make directory", path);
134 /* ------------------------------------------------------------------------ */
136 open_with_make_path(name)
141 if ((fp = fopen(name, WRITE_BINARY)) == NULL) {
143 if (!make_parent_path(name) ||
144 (fp = fopen(name, WRITE_BINARY)) == NULL)
145 error("Cannot extract", name);
151 /* ------------------------------------------------------------------------ */
153 adjust_info(name, hdr)
159 /* adjust file stamp */
160 utimebuf[0] = utimebuf[1] = hdr->unix_last_modified_stamp;
162 if ((hdr->unix_mode & UNIX_FILE_TYPEMASK) != UNIX_FILE_SYMLINK)
163 utime(name, utimebuf);
165 if (hdr->extend_type == EXTEND_UNIX
166 || hdr->extend_type == EXTEND_OS68K
167 || hdr->extend_type == EXTEND_XOSK) {
168 #ifdef NOT_COMPATIBLE_MODE
169 Please need your modification in this space.
171 if ((hdr->unix_mode & UNIX_FILE_TYPEMASK) != UNIX_FILE_SYMLINK)
172 chmod(name, hdr->unix_mode);
176 if ((hdr->unix_mode & UNIX_FILE_TYPEMASK) == UNIX_FILE_SYMLINK)
177 lchown(name, hdr->unix_uid, hdr->unix_gid);
179 #endif /* HAVE_LCHWON */
180 chown(name, hdr->unix_uid, hdr->unix_gid);
186 /* ------------------------------------------------------------------------ */
188 extract_one(afp, hdr)
189 FILE *afp; /* archive file */
192 FILE *fp; /* output file */
197 boolean save_quiet, save_verbose, up_flag;
198 char *q = hdr->name, c;
200 if (ignore_directory && strrchr(hdr->name, '/')) {
201 q = (char *) strrchr(hdr->name, '/') + 1;
207 * if OSK then strip device name
209 if (hdr->extend_type == EXTEND_OS68K
210 || hdr->extend_type == EXTEND_XOSK) {
213 while (c && c != '/');
215 q = "."; /* if device name only */
220 if (extract_directory)
221 sprintf(name, "%s/%s", extract_directory, q);
226 /* LZHDIRS_METHOD¤ò»ý¤Ä¥Ø¥Ã¥À¤ò¥Á¥§¥Ã¥¯¤¹¤ë */
227 /* 1999.4.30 t.okamoto */
228 for (method = 0;; method++) {
229 if (methods[method] == NULL) {
230 error("Unknown method skiped ...", name);
233 if (bcmp(hdr->method, methods[method], 5) == 0)
237 if ((hdr->unix_mode & UNIX_FILE_TYPEMASK) == UNIX_FILE_REGULAR
238 && method != LZHDIRS_METHOD_NUM) {
240 for (method = 0;; method++) {
241 if (methods[method] == NULL) {
242 error("Unknown method skiped ...", name);
245 if (bcmp(hdr->method, methods[method], 5) == 0)
250 reading_filename = archive_name;
251 writting_filename = name;
252 if (output_to_stdout || verify_mode) {
254 printf("%s %s\n", verify_mode ? "VERIFY" : "EXTRACT", name);
256 int i = hdr->packed_size;
264 save_verbose = verbose;
265 if (!quiet && output_to_stdout) {
266 printf("::::::::\n%s\n::::::::\n", name);
270 else if (verify_mode) {
276 (afp, stdout, hdr->original_size, hdr->packed_size, name, method);
278 verbose = save_verbose;
281 if (skip_flg == FALSE) {
282 up_flag = inquire_extract(name);
283 if (up_flag == FALSE && force == FALSE) {
288 if (skip_flg == TRUE) { /* if skip_flg */
289 if (stat(name, &stbuf) == 0 && force != TRUE) {
290 if (stbuf.st_mtime >= hdr->unix_last_modified_stamp) {
292 printf("%s : Skipped...\n", name);
299 int i = hdr->packed_size;
306 signal(SIGINT, interrupt);
308 signal(SIGHUP, interrupt);
313 remove_extracting_file_when_interrupt = TRUE;
315 if ((fp = open_with_make_path(name)) != NULL) {
317 (afp, fp, hdr->original_size, hdr->packed_size, name, method);
320 remove_extracting_file_when_interrupt = FALSE;
321 signal(SIGINT, SIG_DFL);
323 signal(SIGHUP, SIG_DFL);
330 if (hdr->has_crc && crc != hdr->crc)
331 error("CRC error", name);
333 else if ((hdr->unix_mode & UNIX_FILE_TYPEMASK) == UNIX_FILE_DIRECTORY
334 || (hdr->unix_mode & UNIX_FILE_TYPEMASK) == UNIX_FILE_SYMLINK
335 || method == LZHDIRS_METHOD_NUM) {
336 /* ¢¬¤³¤ì¤Ç¡¢Symblic Link ¤Ï¡¢Âç¾æÉפ«¡© */
337 if (!ignore_directory && !verify_mode) {
340 printf("EXTRACT %s (directory)\n", name);
343 /* NAME has trailing SLASH '/', (^_^) */
344 if ((hdr->unix_mode & UNIX_FILE_TYPEMASK) == UNIX_FILE_SYMLINK) {
345 char buf[256], *bb1, *bb2;
348 bb1 = strtok(buf, "|");
349 bb2 = strtok(NULL, "|");
352 if (skip_flg == FALSE) {
353 up_flag = inquire_extract(name);
354 if (up_flag == FALSE && force == FALSE) {
358 if (GETSTAT(bb1, &stbuf) == 0 && force != TRUE) {
359 if (stbuf.st_mtime >= hdr->unix_last_modified_stamp) {
361 printf("%s : Skipped...\n", bb1);
368 l_code = symlink(bb2, bb1);
371 warning("Can't make Symbolic Link : ");
374 printf("Symbolic Link %s -> %s\n", bb1, bb2);
376 strcpy(name, bb1); /* Symbolic's name set */
378 sprintf(buf, "%s -> %s", bb1, bb2);
379 warning("Can't make Symbolic Link", buf);
382 } else { /* make directory */
383 if (!output_to_stdout && !make_parent_path(name))
389 error("Unknown information", name);
392 if (!output_to_stdout)
393 adjust_info(name, hdr);
396 /* ------------------------------------------------------------------------ */
397 /* EXTRACT COMMAND MAIN */
398 /* ------------------------------------------------------------------------ */
406 /* open archive file */
407 if ((afp = open_old_archive()) == NULL)
408 fatal_error(archive_name);
410 if (archive_is_msdos_sfx1(archive_name))
411 skip_msdos_sfx1_code(afp);
413 /* extract each files */
414 while (get_header(afp, &hdr)) {
415 if (need_file(hdr.name)) {
417 extract_one(afp, &hdr);
418 fseek(afp, pos + hdr.packed_size, SEEK_SET);
421 fseek(afp, hdr.packed_size, SEEK_CUR);
423 int i = hdr.packed_size;
430 /* close archive file */
436 /* Local Variables: */