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);
175 uid_t uid = hdr->unix_uid;
176 gid_t gid = hdr->unix_gid;
178 #if HAVE_GETPWNAM && HAVE_GETGRNAM
180 struct passwd *ent = getpwnam(hdr->user);
181 if (ent) uid = ent->pw_uid;
184 struct group *ent = getgrnam(hdr->group);
185 if (ent) gid = ent->gr_gid;
190 if ((hdr->unix_mode & UNIX_FILE_TYPEMASK) == UNIX_FILE_SYMLINK)
191 lchown(name, uid, gid);
193 #endif /* HAVE_LCHWON */
194 chown(name, uid, gid);
200 /* ------------------------------------------------------------------------ */
202 extract_one(afp, hdr)
203 FILE *afp; /* archive file */
206 FILE *fp; /* output file */
211 boolean save_quiet, save_verbose, up_flag;
212 char *q = hdr->name, c;
214 if (ignore_directory && strrchr(hdr->name, '/')) {
215 q = (char *) strrchr(hdr->name, '/') + 1;
221 * if OSK then strip device name
223 if (hdr->extend_type == EXTEND_OS68K
224 || hdr->extend_type == EXTEND_XOSK) {
227 while (c && c != '/');
229 q = "."; /* if device name only */
234 if (extract_directory)
235 sprintf(name, "%s/%s", extract_directory, q);
240 /* LZHDIRS_METHOD¤ò»ý¤Ä¥Ø¥Ã¥À¤ò¥Á¥§¥Ã¥¯¤¹¤ë */
241 /* 1999.4.30 t.okamoto */
242 for (method = 0;; method++) {
243 if (methods[method] == NULL) {
244 error("Unknown method skiped ...", name);
247 if (bcmp(hdr->method, methods[method], 5) == 0)
251 if ((hdr->unix_mode & UNIX_FILE_TYPEMASK) == UNIX_FILE_REGULAR
252 && method != LZHDIRS_METHOD_NUM) {
254 for (method = 0;; method++) {
255 if (methods[method] == NULL) {
256 error("Unknown method skiped ...", name);
259 if (bcmp(hdr->method, methods[method], 5) == 0)
264 reading_filename = archive_name;
265 writting_filename = name;
266 if (output_to_stdout || verify_mode) {
268 printf("%s %s\n", verify_mode ? "VERIFY" : "EXTRACT", name);
270 int i = hdr->packed_size;
278 save_verbose = verbose;
279 if (!quiet && output_to_stdout) {
280 printf("::::::::\n%s\n::::::::\n", name);
284 else if (verify_mode) {
293 old_mode = setmode(fileno(stdout), O_BINARY);
297 (afp, stdout, hdr->original_size, hdr->packed_size, name, method);
300 setmode(fileno(stdout), old_mode);
304 verbose = save_verbose;
307 if (skip_flg == FALSE) {
308 up_flag = inquire_extract(name);
309 if (up_flag == FALSE && force == FALSE) {
314 if (skip_flg == TRUE) { /* if skip_flg */
315 if (stat(name, &stbuf) == 0 && force != TRUE) {
316 if (stbuf.st_mtime >= hdr->unix_last_modified_stamp) {
318 printf("%s : Skipped...\n", name);
325 int i = hdr->packed_size;
332 signal(SIGINT, interrupt);
334 signal(SIGHUP, interrupt);
339 remove_extracting_file_when_interrupt = TRUE;
341 if ((fp = open_with_make_path(name)) != NULL) {
343 (afp, fp, hdr->original_size, hdr->packed_size, name, method);
346 remove_extracting_file_when_interrupt = FALSE;
347 signal(SIGINT, SIG_DFL);
349 signal(SIGHUP, SIG_DFL);
356 if (hdr->has_crc && crc != hdr->crc)
357 error("CRC error", 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 /* ¢¬¤³¤ì¤Ç¡¢Symblic 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) {
371 char buf[256], *bb1, *bb2;
374 bb1 = strtok(buf, "|");
375 bb2 = strtok(NULL, "|");
378 if (skip_flg == FALSE) {
379 up_flag = inquire_extract(name);
380 if (up_flag == FALSE && force == FALSE) {
384 if (GETSTAT(bb1, &stbuf) == 0 && force != TRUE) {
385 if (stbuf.st_mtime >= hdr->unix_last_modified_stamp) {
387 printf("%s : Skipped...\n", bb1);
394 l_code = symlink(bb2, bb1);
397 warning("Can't make Symbolic Link : ");
400 printf("Symbolic Link %s -> %s\n", bb1, bb2);
402 strcpy(name, bb1); /* Symbolic's name set */
404 sprintf(buf, "%s -> %s", bb1, bb2);
405 warning("Can't make Symbolic Link", buf);
408 } else { /* make directory */
409 if (!output_to_stdout && !make_parent_path(name))
415 error("Unknown information", 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(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 #if 0 /* On MinGW, if afp == stdin, fseek() will succeed.
445 but next getc(afp) return EOF */
446 fseek(afp, pos + hdr.packed_size, SEEK_SET);
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: */