OSDN Git Service

* src/lhext.c (make_parent_path): missing braces.
[lha/lha.git] / src / lhext.c
1 /* ------------------------------------------------------------------------ */
2 /* LHa for UNIX                                                                                                                         */
3 /*                              lhext.c -- LHarc extract                                                                        */
4 /*                                                                                                                                                      */
5 /*              Copyright (C) MCMLXXXIX Yooichi.Tagawa                                                          */
6 /*              Modified                        Nobutaka Watazaki                                                       */
7 /*                                                                                                                                                      */
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 /* ------------------------------------------------------------------------ */
16 #include "lha.h"
17 /* ------------------------------------------------------------------------ */
18 static int      skip_flg = FALSE;       /* FALSE..No Skip , TRUE..Skip */
19 static char        *methods[] =
20 {
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,
24         LZHDIRS_METHOD,
25         NULL
26 };
27
28 /* ------------------------------------------------------------------------ */
29 static          boolean
30 inquire_extract(name)
31         char           *name;
32 {
33         struct stat     stbuf;
34
35         skip_flg = FALSE;
36         if (stat(name, &stbuf) >= 0) {
37                 if (!is_regularfile(&stbuf)) {
38                         error("\"%s\" already exists (not a file)", name);
39                         return FALSE;
40                 }
41
42                 if (noexec) {
43                         printf("EXTRACT %s but file is exist.\n", name);
44                         return FALSE;
45                 }
46                 else if (!force) {
47                         if (!isatty(0))
48                                 return FALSE;
49
50                         switch (inquire("OverWrite ?(Yes/[No]/All/Skip)", name, "YyNnAaSs\n")) {
51                         case 0:
52                         case 1:/* Y/y */
53                                 break;
54                         case 2:
55                         case 3:/* N/n */
56                         case 8:/* Return */
57                                 return FALSE;
58                         case 4:
59                         case 5:/* A/a */
60                                 force = TRUE;
61                                 break;
62                         case 6:
63                         case 7:/* S/s */
64                                 skip_flg = TRUE;
65                                 break;
66                         }
67                 }
68         }
69         if (noexec)
70                 printf("EXTRACT %s\n", name);
71
72         return TRUE;
73 }
74
75 /* ------------------------------------------------------------------------ */
76 static          boolean
77 make_parent_path(name)
78         char           *name;
79 {
80         char            path[FILENAME_LENGTH];
81         struct stat     stbuf;
82         register char  *p;
83
84         /* make parent directory name into PATH for recursive call */
85         strcpy(path, name);
86         for (p = path + strlen(path); p > path; p--)
87                 if (p[-1] == '/') {
88                         *--p = '\0';
89                         break;
90                 }
91
92         if (p == path) {
93                 message("invalid path name \"%s\"", name);
94                 return FALSE;   /* no more parent. */
95         }
96
97         if (GETSTAT(path, &stbuf) >= 0) {
98                 if (is_directory(&stbuf))
99                         return TRUE;
100                 error("Not a directory \"%s\"", path);
101                 return FALSE;
102         }
103
104         if (verbose)
105                 message("Making directory \"%s\".", path);
106
107 #if defined __MINGW32__
108     if (mkdir(path) >= 0)
109         return TRUE;
110 #else
111         if (mkdir(path, 0777) >= 0)     /* try */
112                 return TRUE;    /* successful done. */
113 #endif
114
115         if (!make_parent_path(path))
116                 return FALSE;
117
118 #if defined __MINGW32__
119     if (mkdir(path) < 0) {      /* try again */
120                 error("Cannot make directory", path);
121         return FALSE;
122     }
123 #else
124         if (mkdir(path, 0777) < 0) {    /* try again */
125                 error("Cannot make directory", path);
126                 return FALSE;
127         }
128 #endif
129
130         return TRUE;
131 }
132
133 /* ------------------------------------------------------------------------ */
134 static FILE    *
135 open_with_make_path(name)
136         char           *name;
137 {
138         FILE           *fp;
139
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);
144         }
145         return fp;
146 }
147
148 /* ------------------------------------------------------------------------ */
149 static void
150 adjust_info(name, hdr)
151         char           *name;
152         LzHeader       *hdr;
153 {
154     struct utimbuf utimebuf;
155
156         /* adjust file stamp */
157         utimebuf.actime = utimebuf.modtime = hdr->unix_last_modified_stamp;
158
159         if ((hdr->unix_mode & UNIX_FILE_TYPEMASK) != UNIX_FILE_SYMLINK)
160                 utime(name, &utimebuf);
161
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.
167 #else
168                 if ((hdr->unix_mode & UNIX_FILE_TYPEMASK) != UNIX_FILE_SYMLINK)
169                         chmod(name, hdr->unix_mode);
170 #endif
171                 if (!getuid()){
172             uid_t uid = hdr->unix_uid;
173             gid_t gid = hdr->unix_gid;
174
175 #if HAVE_GETPWNAM && HAVE_GETGRNAM
176             if (hdr->user[0]) {
177                 struct passwd *ent = getpwnam(hdr->user);
178                 if (ent) uid = ent->pw_uid;
179             }
180             if (hdr->group[0]) {
181                 struct group *ent = getgrnam(hdr->group);
182                 if (ent) gid = ent->gr_gid;
183             }
184 #endif
185
186 #if HAVE_LCHOWN
187                         if ((hdr->unix_mode & UNIX_FILE_TYPEMASK) == UNIX_FILE_SYMLINK)
188                                 lchown(name, uid, gid);
189                         else
190 #endif /* HAVE_LCHWON */
191                                 chown(name, uid, gid);
192                 }
193         }
194 }
195
196 /* ------------------------------------------------------------------------ */
197 static void
198 extract_one(afp, hdr)
199         FILE           *afp;    /* archive file */
200         LzHeader       *hdr;
201 {
202         FILE           *fp;     /* output file */
203         struct stat     stbuf;
204         char            name[FILENAME_LENGTH];
205         int             crc;
206         int             method;
207         boolean         save_quiet, save_verbose, up_flag;
208         char           *q = hdr->name, c;
209
210         if (ignore_directory && strrchr(hdr->name, '/')) {
211                 q = (char *) strrchr(hdr->name, '/') + 1;
212         }
213         else {
214                 if (*q == '/') {
215                         q++;
216                         /*
217                          * if OSK then strip device name
218                          */
219                         if (hdr->extend_type == EXTEND_OS68K
220                             || hdr->extend_type == EXTEND_XOSK) {
221                                 do
222                                         c = (*q++);
223                                 while (c && c != '/');
224                                 if (!c || !*q)
225                                         q = ".";        /* if device name only */
226                         }
227                 }
228         }
229
230         if (extract_directory)
231                 xsnprintf(name, sizeof(name), "%s/%s", extract_directory, q);
232         else
233                 strcpy(name, q);
234
235
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);
242                         return;
243                 }
244                 if (memcmp(hdr->method, methods[method], 5) == 0)
245                         break;
246         }
247
248         if ((hdr->unix_mode & UNIX_FILE_TYPEMASK) == UNIX_FILE_REGULAR
249                 && method != LZHDIRS_METHOD_NUM) {
250 #if 0
251                 for (method = 0;; method++) {
252                         if (methods[method] == NULL) {
253                 error("Unknown method \"%.*s\"; \"%s\" will be skiped ...",
254                       5, hdr->method, name);
255                                 return;
256                         }
257                         if (memcmp(hdr->method, methods[method], 5) == 0)
258                                 break;
259                 }
260 #endif
261
262                 reading_filename = archive_name;
263                 writing_filename = name;
264                 if (output_to_stdout || verify_mode) {
265                         if (noexec) {
266                                 printf("%s %s\n", verify_mode ? "VERIFY" : "EXTRACT", name);
267                                 if (afp == stdin) {
268                                         int             i = hdr->packed_size;
269                                         while (i--)
270                                                 fgetc(afp);
271                                 }
272                                 return;
273                         }
274
275                         save_quiet = quiet;
276                         save_verbose = verbose;
277                         if (!quiet && output_to_stdout) {
278                                 printf("::::::::\n%s\n::::::::\n", name);
279                                 quiet = TRUE;
280                                 verbose = FALSE;
281                         }
282                         else if (verify_mode) {
283                                 quiet = FALSE;
284                                 verbose = TRUE;
285                         }
286
287 #if __MINGW32__
288             {
289                 int old_mode;
290                 fflush(stdout);
291                 old_mode = setmode(fileno(stdout), O_BINARY);
292 #endif
293
294                         crc = decode_lzhuf
295                                 (afp, stdout, hdr->original_size, hdr->packed_size, name, method);
296 #if __MINGW32__
297                 fflush(stdout);
298                 setmode(fileno(stdout), old_mode);
299             }
300 #endif
301                         quiet = save_quiet;
302                         verbose = save_verbose;
303                 }
304                 else {
305                         if (skip_flg == FALSE)  {
306                                 up_flag = inquire_extract(name);
307                                 if (up_flag == FALSE && force == FALSE) {
308                                         return;
309                                 }
310                         }
311
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) {
315                                                 if (quiet != TRUE)
316                                                         printf("%s : Skipped...\n", name);
317                                                 return;
318                                         }
319                                 }
320                         }
321                         if (noexec) {
322                                 if (afp == stdin) {
323                                         int i = hdr->packed_size;
324                                         while (i--)
325                                                 fgetc(afp);
326                                 }
327                                 return;
328                         }
329
330                         signal(SIGINT, interrupt);
331 #ifdef SIGHUP
332                         signal(SIGHUP, interrupt);
333 #endif
334
335                         unlink(name);
336                         remove_extracting_file_when_interrupt = TRUE;
337
338                         if ((fp = open_with_make_path(name)) != NULL) {
339                                 crc = decode_lzhuf
340                                         (afp, fp, hdr->original_size, hdr->packed_size, name, method);
341                                 fclose(fp);
342                         }
343                         remove_extracting_file_when_interrupt = FALSE;
344                         signal(SIGINT, SIG_DFL);
345 #ifdef SIGHUP
346                         signal(SIGHUP, SIG_DFL);
347 #endif
348                         if (!fp)
349                                 return;
350                 }
351
352                 if (hdr->has_crc && crc != hdr->crc)
353                         error("CRC error: \"%s\"", name);
354         }
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) {
360                         if (noexec) {
361                                 if (quiet != TRUE)
362                                         printf("EXTRACT %s (directory)\n", name);
363                                 return;
364                         }
365                         /* NAME has trailing SLASH '/', (^_^) */
366                         if ((hdr->unix_mode & UNIX_FILE_TYPEMASK) == UNIX_FILE_SYMLINK) {
367                                 char            buf[256], *bb1, *bb2;
368                                 int             l_code;
369                                 strcpy(buf, name);
370                                 bb1 = strtok(buf, "|");
371                                 bb2 = strtok(NULL, "|");
372
373 #ifdef S_IFLNK
374                                 if (skip_flg == FALSE)  {
375                                         up_flag = inquire_extract(name);
376                                         if (up_flag == FALSE && force == FALSE) {
377                                                 return;
378                                         }
379                                 } else {
380                                         if (GETSTAT(bb1, &stbuf) == 0 && force != TRUE) {
381                                                 if (stbuf.st_mtime >= hdr->unix_last_modified_stamp) {
382                                                         if (quiet != TRUE)
383                                                                 printf("%s : Skipped...\n", bb1);
384                                                         return;
385                                                 }
386                                         }
387                                 }
388
389                                 unlink(bb1);
390                                 l_code = symlink(bb2, bb1);
391                                 if (l_code < 0) {
392                                         if (quiet != TRUE)
393                                                 warning("Can't make Symbolic Link \"%s\" -> \"%s\"",
394                                 bb1, bb2);
395                                 }
396                                 if (quiet != TRUE) {
397                                         message("Symbolic Link %s -> %s", bb1, bb2);
398                                 }
399                                 strcpy(name, bb1);      /* Symbolic's name set */
400 #else
401                                 warning("Can't make Symbolic Link %s -> %s", bb1, bb2);
402                                 return;
403 #endif
404                         } else { /* make directory */
405                                 if (!output_to_stdout && !make_parent_path(name))
406                                         return;
407                         }
408                 }
409         }
410         else {
411                 error("Unknown information: \"%s\"", name);
412         }
413
414         if (!output_to_stdout)
415                 adjust_info(name, hdr);
416 }
417
418 /* ------------------------------------------------------------------------ */
419 /* EXTRACT COMMAND MAIN                                                                                                         */
420 /* ------------------------------------------------------------------------ */
421 void
422 cmd_extract()
423 {
424         LzHeader        hdr;
425         long            pos;
426         FILE           *afp;
427
428         /* open archive file */
429         if ((afp = open_old_archive()) == NULL)
430                 fatal_error("Cannot open archive file \"%s\"", archive_name);
431
432         if (archive_is_msdos_sfx1(archive_name))
433                 skip_msdos_sfx1_code(afp);
434
435         /* extract each files */
436         while (get_header(afp, &hdr)) {
437                 if (need_file(hdr.name)) {
438                         pos = ftell(afp);
439                         extract_one(afp, &hdr);
440             /* when error occurred in extract_one(), should adjust
441                point of file stream */
442                         if (afp != stdin)
443                 fseek(afp, pos + hdr.packed_size, SEEK_SET);
444             else {
445                 /* FIXME: assume that the extract_one() has read
446                    packed_size bytes from stdin. */
447                 long i = 0;
448                                 while (i--) fgetc(afp);
449             }
450                 } else {
451                         if (afp != stdin)
452                                 fseek(afp, hdr.packed_size, SEEK_CUR);
453                         else {
454                                 int             i = hdr.packed_size;
455                                 while (i--)
456                                         fgetc(afp);
457                         }
458                 }
459         }
460
461         /* close archive file */
462         fclose(afp);
463
464         return;
465 }
466
467 /* Local Variables: */
468 /* mode:c */
469 /* tab-width:4 */
470 /* End: */
471 /* vi: set tabstop=4: */