OSDN Git Service

5ad289ffa121f4f5938e85d7bb2c1206aab3c6e7
[lha/lha.git] / src / lharc.c
1 /* ------------------------------------------------------------------------ */
2 /* LHa for UNIX                                                                                                                         */
3 /*                              lharc.c -- append to archive                                                            */
4 /*                                                                                                                                                      */
5 /*              Copyright (C) MCMLXXXIX Yooichi.Tagawa                                                          */
6 /*              Modified                        Nobutaka Watazaki                                                       */
7 /*                                                      Thanks to H.Yoshizaki. (MS-DOS LHarc)                   */
8 /*                                                                                                                                                      */
9 /*  Ver. 0.00  Original                                                 1988.05.23  Y.Tagawa            */
10 /*  Ver. 0.01  Alpha Version (for 4.2BSD)               1989.05.28  Y.Tagawa            */
11 /*  Ver. 0.02  Alpha Version Rel.2                              1989.05.29  Y.Tagawa            */
12 /*  Ver. 0.03  Release #3  Beta Version                 1989.07.02  Y.Tagawa            */
13 /*  Ver. 0.03a Debug                                                    1989.07.03  Y.Tagawa            */
14 /*  Ver. 0.03b Modified                                                 1989.07.13  Y.Tagawa            */
15 /*  Ver. 0.03c Debug (Thanks to void@rena.dit.junet)                                            */
16 /*                                                                                              1989.08.09  Y.Tagawa            */
17 /*  Ver. 0.03d Modified (quiet and verbose)             1989.09.14  Y.Tagawa            */
18 /*  V1.00  Fixed                                                                1989.09.22  Y.Tagawa            */
19 /*  V1.01  Bug Fixed                                                    1989.12.25  Y.Tagawa            */
20 /*                                                                                                                                                      */
21 /*  DOS-Version Original LHx V C2.01            (C) H.Yohizaki                                  */
22 /*                                                                                                                                                      */
23 /*  V2.00  UNIX Lharc + DOS LHx -> OSK LHx              1990.11.01  Momozou                     */
24 /*  V2.01  Minor Modified                                               1990.11.24  Momozou                     */
25 /*                                                                                                                                                      */
26 /*  Ver. 0.02  LHx for UNIX                                             1991.11.18  M.Oki                       */
27 /*  Ver. 0.03  LHa for UNIX                                             1991.12.17  M.Oki                       */
28 /*  Ver. 0.04  LHa for UNIX     beta version            1992.01.20  M.Oki                       */
29 /*  Ver. 1.00  LHa for UNIX     Fixed                           1992.03.19  M.Oki                       */
30 /*                                                                                                                                                      */
31 /*  Ver. 1.10  for Symblic Link                                 1993.06.25  N.Watazaki          */
32 /*  Ver. 1.11  for Symblic Link Bug Fixed               1993.08.18  N.Watazaki          */
33 /*  Ver. 1.12  for File Date Check                              1993.10.28  N.Watazaki          */
34 /*  Ver. 1.13  Bug Fixed (Idicator calcurate)   1994.02.21  N.Watazaki          */
35 /*  Ver. 1.13a Bug Fixed (Sym. Link delete)             1994.03.11  N.Watazaki          */
36 /*      Ver. 1.13b Bug Fixed (Sym. Link delete)         1994.07.29  N.Watazaki          */
37 /*      Ver. 1.14  Source All chagned                           1995.01.14      N.Watazaki              */
38 /*      Ver. 1.14b,c  Bug Fixed                     1996.03.07  t.okamoto               */
39 /*  Ver. 1.14d Version up                       1997.01.12  t.okamoto       */
40 /*  Ver. 1.14g Bug Fixed                        2000.05.06  t.okamoto       */
41 /*  Ver. 1.14i Modified                         2000.10.06  t.okamoto       */
42 /* ------------------------------------------------------------------------ */
43 #define LHA_MAIN_SRC
44
45 #include "lha.h"
46
47 /* ------------------------------------------------------------------------ */
48 /*                                                              PROGRAM                                                                         */
49 /* ------------------------------------------------------------------------ */
50 static int      cmd = CMD_UNKNOWN;
51
52 /* 1996.8.13 t.okamoto */
53 #if 0
54 char          **cmd_filev;
55 int             cmd_filec;
56
57 char           *archive_name;
58 char            expanded_archive_name[FILENAME_LENGTH];
59 char            temporary_name[FILENAME_LENGTH];
60 char            backup_archive_name[FILENAME_LENGTH];
61 #endif
62
63 /* static functions */
64 static void     sort_files();
65 static void             print_version();
66
67 char                *extract_directory = NULL;
68 char              **xfilev;
69 int             xfilec = 257;
70
71 /* 1996.8.13 t.okamoto */
72 #if 0
73 char           *writting_filename;
74 char           *reading_filename;
75
76 int             archive_file_mode;
77 int             archive_file_gid;
78 #endif
79 /* ------------------------------------------------------------------------ */
80 static void
81 init_variable()         /* Added N.Watazaki */
82 {
83 /* options */
84         quiet                   = FALSE;
85         text_mode               = FALSE;
86         verbose                 = FALSE;
87         noexec                  = FALSE;        /* debugging option */
88         force                   = FALSE;
89         prof                    = FALSE;
90 #ifndef SUPPORT_LH7
91         compress_method = LZHUFF5_METHOD_NUM;
92 #endif
93 #ifdef SUPPORT_LH7
94         compress_method = LZHUFF7_METHOD_NUM;
95 #endif
96
97         compress_method = DEFAULT_LZHUFF_METHOD;
98
99         header_level    = HEADER_LEVEL1;
100         quiet_mode              = 0;
101
102 #ifdef EUC
103         euc_mode                = FALSE;
104 #endif
105
106 /* view command flags */
107         verbose_listing = FALSE;
108
109 /* extract command flags */
110         output_to_stdout = FALSE;
111
112 /* append command flags */
113         new_archive                     = FALSE;
114         update_if_newer         = FALSE;
115         delete_after_append = FALSE;
116         generic_format          = FALSE;
117
118         remove_temporary_at_error                               = FALSE;
119         recover_archive_when_interrupt                  = FALSE;
120         remove_extracting_file_when_interrupt   = FALSE;
121         get_filename_from_stdin                                 = FALSE;
122         ignore_directory                                                = FALSE;
123         verify_mode                                                             = FALSE;
124
125         noconvertcase                                                   = FALSE;
126
127         extract_directory = NULL;
128         xfilec = 257;
129     temporary_fd = -1;
130 }
131
132 /* ------------------------------------------------------------------------ */
133 /* NOTES :                      Text File Format                                                                                */
134 /* GENERATOR            NewLine                                                                                                 */
135 /* [generic]            0D 0A                                                                                                   */
136 /* [MS-DOS]                     0D 0A                                                                                                   */
137 /* [OS9][MacOS]         0D                                                                                                              */
138 /* [UNIX]                       0A                                                                                                              */
139 /* ------------------------------------------------------------------------ */
140 static void
141 print_tiny_usage_and_exit()
142 {
143         fprintf(stderr, "\
144 LHarc    for UNIX  V 1.02  Copyright(C) 1989  Y.Tagawa\n\
145 LHx      for MSDOS V C2.01 Copyright(C) 1990  H.Yoshizaki\n\
146 LHx(arc) for OSK   V 2.01  Modified     1990  Momozou\n\
147 LHa      for UNIX  V 1.00  Copyright(C) 1992  Masaru Oki\n\
148 LHa      for UNIX  V 1.14  Modified     1995  Nobutaka Watazaki\n\
149 LHa      for UNIX  V 1.14i Modified     2000  Tsugio Okamoto\n\
150                            Autoconfiscated 2001,2002 Koji Arai\n\
151 ");
152         fprintf(stderr, "\
153 usage: lha [-]{axelvudmcp[q[num]][vnfodizg012]}[w=<dir>] archive_file [file...]\n\
154 commands:                           options:\n\
155  a   Add(or replace) to archive      q{num} quiet (num:quiet mode)\n\
156  x,e EXtract from archive            v  verbose\n\
157  l,v List / Verbose List             n  not execute\n\
158  u   Update newer files to archive   f  force (over write at extract)\n\
159  d   Delete from archive             t  FILES are TEXT file\n");
160 #ifdef SUPPORT_LH7
161         fprintf(stderr, "\
162  m   Move to archive (means 'ad')    o[567] compression method (a/u)\n\
163 ");
164 #endif
165 #ifndef SUPPORT_LH7
166         fprintf(stderr, "\
167  m   Move to archive (means 'ad')    o  use LHarc compatible method (a/u)\n\
168 ");
169 #endif
170         fprintf(stderr, "\
171  c   re-Construct new archive        w=<dir> specify extract directory (a/u/m/x/e)\n\
172  p   Print to STDOUT from archive    d  delete FILES after (a/u/c)\n\
173  t   Test file CRC in archive        i  ignore directory path (x/e)\n\
174                                      z  files not compress (a/u)\n\
175                                      g  Generic format (for compatibility)\n\
176                                         or not convert case when extracting\n\
177                                      0/1/2 header level (a/u)\n\
178 ");
179 #ifdef EUC
180         fprintf(stderr, "\
181                                      e  TEXT code convert from/to EUC\n\
182 ");
183 #endif
184         exit(1);
185 }
186
187 /* ------------------------------------------------------------------------ */
188 int
189 main(argc, argv)
190         int             argc;
191         char           *argv[];
192 {
193         char           *p, inpbuf[256];
194
195         int i;
196         int  ac;
197         char **av, *m;
198
199         init_variable();                /* Added N.Watazaki */
200
201         ac = argc;
202         av = (char **)xmalloc( sizeof(char*)*argc );
203         for (i=0; i<argc; i++) {
204         av[i] = xstrdup( argv[i] );
205         }
206
207         if (ac < 2)
208                 print_tiny_usage_and_exit();
209
210         if (strcmp(av[1], "--version") == 0) {
211                 print_version();
212                 exit(1);
213         }
214
215         if (ac < 3) {
216                 cmd = CMD_LIST;
217                 av--; /* argv--; */ /* 1999.7.18 */
218                 ac++; /* argc++; */
219                 goto work;
220         }
221
222         m = av[1];
223
224         if (m[0] == '-')
225                 m++;
226         /* commands */
227         switch (*m) {
228         case 'x':
229         case 'e':
230                 cmd = CMD_EXTRACT;
231                 break;
232
233         case 'p':
234                 output_to_stdout = TRUE;
235                 cmd = CMD_EXTRACT;
236                 break;
237
238         case 'c':
239                 new_archive = TRUE;
240                 cmd = CMD_ADD;
241                 break;
242
243         case 'a':
244                 cmd = CMD_ADD;
245                 break;
246
247         case 'd':
248                 cmd = CMD_DELETE;
249                 break;
250
251         case 'u':
252                 update_if_newer = TRUE;
253                 cmd = CMD_ADD;
254                 break;
255
256         case 'm':
257                 delete_after_append = TRUE;
258                 cmd = CMD_ADD;
259                 break;
260
261         case 'v':
262                 verbose_listing = TRUE;
263                 cmd = CMD_LIST;
264                 break;
265
266         case 'l':
267                 cmd = CMD_LIST;
268                 break;
269
270         case 't':
271                 cmd = CMD_EXTRACT;
272                 verify_mode = TRUE;
273                 break;
274
275         default:
276                 print_tiny_usage_and_exit();
277
278         }
279
280         /* options */
281         /* p = &argv[1][1]; */
282         p = m+1;
283         while ( *p != 0 ) {
284                 switch ((*p++)) {
285                 case 'q':
286                         switch (*p) {
287                         case '0':
288                         case '1':
289                                 quiet_mode = *p - '0';
290                                 ++p;
291                                 break;
292                         case '2':
293                         default:
294                                 quiet = TRUE;
295                                 break;
296                         }
297                 case 'f':
298                         force = TRUE;
299                         break;
300                 case 'p':
301                         prof = TRUE;
302                         break;
303                 case 'v':
304                         verbose = TRUE;
305                         break;
306                 case 't':
307                         text_mode = TRUE;
308                         break;
309 #ifdef EUC
310                 case 'e':
311                         text_mode = TRUE;
312                         euc_mode = TRUE;
313                         break;
314 #endif
315                 case 'n':
316                         noexec = TRUE;
317                         break;
318                 case 'g':
319                         generic_format = TRUE;
320                         noconvertcase = TRUE;
321                         header_level = 0;
322                         break;
323                 case 'd':
324                         delete_after_append = TRUE;
325                         break;
326                 case 'o':
327                         switch (*p) {
328                         case 0:
329                                 compress_method = LZHUFF1_METHOD_NUM;
330                                 header_level = 0;
331                                 break;
332                         case '5':
333                                 compress_method = LZHUFF5_METHOD_NUM;
334                                 p++;
335                                 break;
336 #ifdef SUPPORT_LH7
337                         case '6':
338                                 compress_method = LZHUFF6_METHOD_NUM;
339                                 p++;
340                                 break;
341                         case '7':
342                                 compress_method = LZHUFF7_METHOD_NUM;
343                                 p++;
344                                 break;
345 #endif
346                         default:
347                                 fprintf(stderr, "LHa: error option o%c\n", p[-1]);
348                                 exit(1);
349                         }
350                         break;
351                 case 'z':
352                         compress_method = LZHUFF0_METHOD_NUM;   /* Changed N.Watazaki */
353                         break;
354                 case 'i':
355                         ignore_directory = TRUE;
356                         break;
357                 case 'w':
358                         if (*p == '=')
359                                 p++;
360                         extract_directory = p;
361                         while (*p)
362                                 p++;
363                         break;
364                 case '0':
365                         header_level = HEADER_LEVEL0;
366                         break;
367                 case '1':
368                         header_level = HEADER_LEVEL1;
369                         break;
370                 case '2':
371                         header_level = HEADER_LEVEL2;
372                         break;
373                 default:
374                         fprintf(stderr, "LHa: Unknown option '%c'.\n", p[-1]);
375                         exit(1);
376                 }
377         }
378
379 work:
380         /* archive file name */
381         archive_name = av[2];
382
383         if (!strcmp(archive_name, "-")) {
384                 if (!isatty(1) && cmd == CMD_ADD)
385                         quiet = TRUE;
386         }
387         else {
388                 if (ac == 3 && !isatty(0)) { /* 1999.7.18 */
389 #if !__MINGW32__ /* FIXME: Bug(?) on MinGW, isatty() return 0 on
390                     Cygwin console.  Cygwin 1.3.10(0.51/3/2) on Win2000 */
391                         get_filename_from_stdin = TRUE;
392 #endif
393                 }
394         }
395
396         /* target file name */
397         if (get_filename_from_stdin) {
398                 cmd_filec = 0;
399                 xfilev = (char **)xmalloc(sizeof(char *) * xfilec);
400                 while (fgets(inpbuf, sizeof(inpbuf), stdin)) {
401                     /* delete \n if it exist */
402                         i=0; p=inpbuf;
403                         while (i < sizeof(inpbuf) && p != 0) {
404                             if (*p == '\n') {
405                                     *p = 0;
406                                         break;
407                                 }
408                                 p++; i++;
409                         }
410
411                         if (cmd_filec >= xfilec) {
412                                 xfilec += 256;
413                                 xfilev = (char **) xrealloc(xfilev,
414                                                    sizeof(char *) * xfilec);
415                         }
416                         if (strlen(inpbuf) < 1)
417                                 continue;
418                         xfilev[cmd_filec++] = xstrdup(inpbuf);
419                 }
420                 xfilev[cmd_filec] = NULL;
421                 cmd_filev = xfilev;
422         } else {
423                 cmd_filec = ac - 3;
424                 cmd_filev = av + 3;
425         }
426         sort_files();
427
428         /* make crc table */
429         make_crctable();
430
431         switch (cmd) {
432         case CMD_EXTRACT:
433                 cmd_extract();
434                 break;
435         case CMD_ADD:
436                 cmd_add();
437                 break;
438         case CMD_LIST:
439                 cmd_list();
440                 break;
441         case CMD_DELETE:
442                 cmd_delete();
443                 break;
444         }
445
446 #ifdef USE_PROF
447         if (!prof)
448                 exit(0);
449 #endif
450
451         return 0;
452 }
453
454
455 /* ------------------------------------------------------------------------ */
456 /* */
457 /* ------------------------------------------------------------------------ */
458
459 /* ------------------------------------------------------------------------ */
460 static void
461 print_version()
462 {
463         fprintf(stderr, "%s\n", LHA_VERSION);
464 }
465
466 /* ------------------------------------------------------------------------ */
467 static void
468 message_1(title, subject, name)
469         char           *title, *subject, *name;
470 {
471         fprintf(stderr, "LHa: %s%s ", title, subject);
472         fflush(stderr);
473
474         if (errno == 0)
475                 fprintf(stderr, "%s\n", name);
476         else
477                 perror(name);
478 }
479
480 /* ------------------------------------------------------------------------ */
481 void
482 message(subject, name)
483         char           *subject, *name;
484 {
485         message_1("", subject, name);
486 }
487
488 /* ------------------------------------------------------------------------ */
489 void
490 warning(subject, name)
491         char           *subject, *name;
492 {
493         message_1("Warning: ", subject, name);
494 }
495
496 /* ------------------------------------------------------------------------ */
497 void
498 error(subject, msg)
499         char           *subject, *msg;
500 {
501         message_1("Error: ", subject, msg);
502 }
503
504 /* ------------------------------------------------------------------------ */
505 void
506 fatal_error(msg)
507         char           *msg;
508 {
509     message_1("Fatal error:", "", msg);
510
511     if (remove_temporary_at_error) {
512         if (temporary_fd != -1)
513             close(temporary_fd);
514         unlink(temporary_name);
515     }
516
517     exit(1);
518 }
519
520 /* ------------------------------------------------------------------------ */
521 void
522 write_error()
523 {
524         fatal_error(writting_filename);
525 }
526
527 /* ------------------------------------------------------------------------ */
528 void
529 read_error()
530 {
531         fatal_error(reading_filename);
532 }
533
534 /* ------------------------------------------------------------------------ */
535 void
536 interrupt(signo)
537         int             signo;
538 {
539         errno = 0;
540         message("Interrupted\n", "");
541
542         if (temporary_fd != -1)
543                 close(temporary_fd);
544         unlink(temporary_name);
545         if (recover_archive_when_interrupt)
546                 rename(backup_archive_name, archive_name);
547         if (remove_extracting_file_when_interrupt) {
548                 errno = 0;
549                 message("Removing", writting_filename);
550                 unlink(writting_filename);
551         }
552         signal(SIGINT, SIG_DFL);
553 #ifdef SIGHUP
554         signal(SIGHUP, SIG_DFL);
555 #endif
556         kill(getpid(), signo);
557 }
558
559 /* ------------------------------------------------------------------------ */
560 /*                                                                                                                                                      */
561 /* ------------------------------------------------------------------------ */
562 static int
563 sort_by_ascii(a, b)
564         char          **a, **b;
565 {
566         register char  *p, *q;
567         register int    c1, c2;
568
569         p = *a, q = *b;
570         if (generic_format) {
571                 do {
572                         c1 = *(unsigned char *) p++;
573                         c2 = *(unsigned char *) q++;
574                         if (!c1 || !c2)
575                                 break;
576                         if (islower(c1))
577                                 c1 = toupper(c1);
578                         if (islower(c2))
579                                 c2 = toupper(c2);
580                 }
581                 while (c1 == c2);
582                 return c1 - c2;
583         }
584         else {
585                 while (*p == *q && *p != '\0')
586                         p++, q++;
587                 return *(unsigned char *) p - *(unsigned char *) q;
588         }
589 }
590
591 /* ------------------------------------------------------------------------ */
592 static void
593 sort_files()
594 {
595         if (cmd_filec > 1)
596                 qsort(cmd_filev, cmd_filec, sizeof(char *), sort_by_ascii);
597 }
598
599 /* ------------------------------------------------------------------------ */
600 char           *
601 xmalloc(size)
602         int             size;
603 {
604         char           *p = (char *) malloc(size);
605         if (!p)
606                 fatal_error("Not enough memory");
607         return p;
608 }
609
610 /* ------------------------------------------------------------------------ */
611 char           *
612 xrealloc(old, size)
613         char           *old;
614         int             size;
615 {
616         char           *p = (char *) realloc(old, size);
617         if (!p)
618                 fatal_error("Not enough memory");
619         return p;
620 }
621
622 char *
623 xstrdup(str)
624         char *str;
625 {
626     int len = strlen(str);
627         char *p = (char *)xmalloc(len + 1);
628         if (!p)
629                 fatal_error("Not enough memory");
630     strcpy(p, str);
631         return p;
632 }
633
634 /* ------------------------------------------------------------------------ */
635 /*                                                              STRING POOL                                                                     */
636 /* ------------------------------------------------------------------------ */
637 /*
638   string pool :
639         +-------------+-------------+------+-------------+----------+
640         | N A M E 1 \0| N A M E 2 \0| .... | N A M E n \0|                      |
641         +-------------+-------------+------+-------------+----------+
642           ^ ^            ^ buffer+0 buffer+used buffer+size
643
644   vector :
645         +---------------+---------------+------------- -----------------+
646         | pointer to    | pointer to    | pointer to   ...  pointer to  |
647         |  stringpool   |  N A M E 1    |  N A M E 2   ...   N A M E n  |
648         +---------------+---------------+-------------     -------------+
649         ^ malloc base      returned
650 */
651
652 /* ------------------------------------------------------------------------ */
653 void
654 init_sp(sp)
655         struct string_pool *sp;
656 {
657         sp->size = 1024 - 8;    /* any ( >=0 ) */
658         sp->used = 0;
659         sp->n = 0;
660         sp->buffer = (char *) xmalloc(sp->size * sizeof(char));
661 }
662
663 /* ------------------------------------------------------------------------ */
664 void
665 add_sp(sp, name, len)
666         struct string_pool *sp;
667         char           *name;   /* stored '\0' at tail */
668         int             len;    /* include '\0' */
669 {
670         while (sp->used + len > sp->size) {
671                 sp->size *= 2;
672                 sp->buffer = (char *) xrealloc(sp->buffer, sp->size * sizeof(char));
673         }
674         bcopy(name, sp->buffer + sp->used, len);
675         sp->used += len;
676         sp->n++;
677 }
678
679 /* ------------------------------------------------------------------------ */
680 void
681 finish_sp(sp, v_count, v_vector)
682         register struct string_pool *sp;
683         int            *v_count;
684         char         ***v_vector;
685 {
686         int             i;
687         register char  *p;
688         char          **v;
689
690         v = (char **) xmalloc((sp->n + 1) * sizeof(char *));
691         *v++ = sp->buffer;
692         *v_vector = v;
693         *v_count = sp->n;
694         p = sp->buffer;
695         for (i = sp->n; i; i--) {
696                 *v++ = p;
697                 if (i - 1)
698                         p += strlen(p) + 1;
699         }
700 }
701
702 /* ------------------------------------------------------------------------ */
703 void
704 free_sp(vector)
705         char          **vector;
706 {
707         vector--;
708         free(*vector);          /* free string pool */
709         free(vector);
710 }
711
712
713 /* ------------------------------------------------------------------------ */
714 /*                                                      READ DIRECTORY FILES                                                    */
715 /* ------------------------------------------------------------------------ */
716 static          boolean
717 include_path_p(path, name)
718         char           *path, *name;
719 {
720         char           *n = name;
721         while (*path)
722                 if (*path++ != *n++)
723                         return (path[-1] == '/' && *n == '\0');
724         return (*n == '/' || (n != name && path[-1] == '/' && n[-1] == '/'));
725 }
726
727 /* ------------------------------------------------------------------------ */
728 void
729 cleaning_files(v_filec, v_filev)
730         int            *v_filec;
731         char         ***v_filev;
732 {
733         char           *flags;
734         struct stat     stbuf;
735
736         register char **filev = *v_filev;
737         register int    filec = *v_filec;
738         register char  *p;
739         register int    i, j;
740
741         if (filec == 0)
742                 return;
743
744         flags = xmalloc(filec * sizeof(char));
745
746         /* flags & 0x01 :       1: ignore */
747         /* flags & 0x02 :       1: directory, 0 : regular file */
748         /* flags & 0x04 :       1: need delete */
749
750         
751         for (i = 0; i < filec; i++)
752                 if (GETSTAT(filev[i], &stbuf) < 0) {
753                         flags[i] = 0x04;
754                         fprintf(stderr,
755                          "LHa: Cannot access \"%s\", ignored.\n", filev[i]);
756                 }
757                 else {
758                         if (is_regularfile(&stbuf))
759                                 flags[i] = 0x00;
760                         else if (is_directory(&stbuf))
761                                 flags[i] = 0x02;
762 #ifdef S_IFLNK
763                         else if (is_symlink(&stbuf)) /* t.okamoto */
764                                 flags[i] = 0x00;
765 #endif                  
766                         else {
767                                 flags[i] = 0x04;
768                                 fprintf(stderr,
769                                         "LHa: Cannot archive \"%s\", ignored.\n", filev[i]);
770                         }
771                 }
772         errno = 0;
773
774         for (i = 0; i < filec; i++) {
775                 p = filev[i];
776                 if ((flags[i] & 0x07) == 0x00) {        /* regular file, not
777                                                          * deleted/ignored */
778                         for (j = i + 1; j < filec; j++) {
779                                 if ((flags[j] & 0x07) == 0x00) {        /* regular file, not
780                                                                          * deleted/ignored */
781                                         if (STREQU(p, filev[j]))
782                                                 flags[j] = 0x04;        /* delete */
783                                 }
784                         }
785                 }
786                 else if ((flags[i] & 0x07) == 0x02) {   /* directory, not
787                                                          * deleted/ignored */
788                         for (j = i + 1; j < filec; j++) {
789                                 if ((flags[j] & 0x07) == 0x00) {        /* regular file, not
790                                                                          * deleted/ignored */
791                                         if (include_path_p(p, filev[j]))
792                                                 flags[j] = 0x04;        /* delete */
793                                 }
794                                 else if ((flags[j] & 0x07) == 0x02) {   /* directory, not
795                                                                          * deleted/ignored */
796                                         if (include_path_p(p, filev[j]))
797                                                 flags[j] = 0x04;        /* delete */
798                                 }
799                         }
800                 }
801         }
802
803         for (i = j = 0; i < filec; i++) {
804                 if ((flags[i] & 0x04) == 0) {
805                         if (i != j)
806                                 filev[j] = filev[i];
807                         j++;
808                 }
809         }
810         *v_filec = j;
811
812         free(flags);
813 }
814
815 /* ------------------------------------------------------------------------ */
816 #ifdef NODIRECTORY
817 /* please need your imprementation */
818 boolean
819 find_files(name, v_filec, v_filev)
820         char           *name;
821         int            *v_filec;
822         char         ***v_filev;
823 {
824         return FALSE;           /* DUMMY */
825 }
826
827 /* ------------------------------------------------------------------------ */
828 void
829 free_files(filec, filev)
830         int             filec;
831         char          **filev;
832 {
833         /* do nothing */
834 }
835 /* ------------------------------------------------------------------------ */
836 #else
837 boolean
838 find_files(name, v_filec, v_filev)
839         char           *name;
840         int            *v_filec;
841         char         ***v_filev;
842 {
843         struct string_pool sp;
844         char            newname[FILENAME_LENGTH];
845         int             len, n;
846         DIR            *dirp;
847         DIRENTRY       *dp;
848         struct stat     tmp_stbuf, arc_stbuf, fil_stbuf;
849
850         strcpy(newname, name);
851         len = strlen(name);
852         if (len > 0 && newname[len - 1] != '/')
853                 newname[len++] = '/';
854
855         dirp = opendir(name);
856         if (!dirp)
857                 return FALSE;
858
859         init_sp(&sp);
860
861         GETSTAT(temporary_name, &tmp_stbuf);
862         GETSTAT(archive_name, &arc_stbuf);
863
864         for (dp = readdir(dirp); dp != NULL; dp = readdir(dirp)) {
865                 n = NAMLEN(dp);
866                 strncpy(newname + len, dp->d_name, n);
867                 newname[len + n] = '\0';
868                 if (GETSTAT(newname, &fil_stbuf) < 0)
869                         continue;
870 #if !defined(HAVE_ST_INO) || __MINGW32__
871                 if ( dp->d_name[0] != '.' ||
872                         (n != 1 &&
873                          (dp->d_name[1] != '.' ||
874                           n != 2))  ) {
875                         add_sp(&sp, newname, len+n+1);
876                 }
877 #else           
878                 if ((dp->d_ino != 0) &&
879                 /* exclude '.' and '..' */
880                     ((dp->d_name[0] != '.') ||
881                      ((n != 1) &&
882                       ((dp->d_name[1] != '.') ||
883                        (n != 2)))) &&
884                     ((tmp_stbuf.st_dev != fil_stbuf.st_dev ||
885                       tmp_stbuf.st_ino != fil_stbuf.st_ino) &&
886                      (arc_stbuf.st_dev != fil_stbuf.st_dev ||
887                       arc_stbuf.st_ino != fil_stbuf.st_ino))) {
888                         add_sp(&sp, newname, len + n + 1);
889                 }
890 #endif
891         }
892         closedir(dirp);
893         finish_sp(&sp, v_filec, v_filev);
894         if (*v_filec > 1)
895                 qsort(*v_filev, *v_filec, sizeof(char *), sort_by_ascii);
896         cleaning_files(v_filec, v_filev);
897
898         return TRUE;
899 }
900
901 /* ------------------------------------------------------------------------ */
902 void
903 free_files(filec, filev)
904         int             filec;
905         char          **filev;
906 {
907         free_sp(filev);
908 }
909 #endif
910 /* ------------------------------------------------------------------------ */
911 /*                                                                                                                                                      */
912 /* ------------------------------------------------------------------------ */
913 /* Build temporary file name and store to TEMPORARY_NAME */
914 int
915 build_temporary_name()
916 {
917 #ifdef TMP_FILENAME_TEMPLATE
918         /* "/tmp/lhXXXXXX" etc. */
919         if (extract_directory == NULL) {
920                 strcpy(temporary_name, TMP_FILENAME_TEMPLATE);
921         }
922         else {
923                 sprintf(temporary_name, "%s/lhXXXXXX", extract_directory);
924         }
925 #else
926         char           *p, *s;
927
928         strcpy(temporary_name, archive_name);
929         for (p = temporary_name, s = (char *) 0; *p; p++)
930                 if (*p == '/')
931                         s = p;
932         strcpy((s ? s + 1 : temporary_name), "lhXXXXXX");
933 #endif
934 #ifdef HAVE_MKSTEMP
935     {
936         int old_umask, fd;
937
938         old_umask = umask(077);
939         fd = mkstemp(temporary_name);
940         umask(old_umask);
941         return fd;
942     }
943 #else
944     {
945         int flags;
946
947         mktemp(temporary_name);
948         flags = O_CREAT|O_EXCL|O_RDWR;
949 #ifdef O_BINARY
950         flags |= O_BINARY;
951 #endif
952         return open(temporary_name, flags, 0600);
953     }
954 #endif
955 }
956
957 /* ------------------------------------------------------------------------ */
958 static void
959 modify_filename_extention(buffer, ext)
960         char           *buffer;
961         char           *ext;
962 {
963         register char  *p, *dot;
964
965         for (p = buffer, dot = (char *) 0; *p; p++) {
966                 if (*p == '.')
967                         dot = p;
968                 else if (*p == '/')
969                         dot = (char *) 0;
970         }
971
972         if (dot)
973                 p = dot;
974
975         strcpy(p, ext);
976 }
977
978 /* ------------------------------------------------------------------------ */
979 /* build backup file name */
980 void
981 build_backup_name(buffer, original)
982         char           *buffer;
983         char           *original;
984 {
985         strcpy(buffer, original);
986         modify_filename_extention(buffer, BACKUPNAME_EXTENTION);        /* ".bak" */
987 }
988
989 /* ------------------------------------------------------------------------ */
990 void
991 build_standard_archive_name(buffer, orginal)
992         char           *buffer;
993         char           *orginal;
994 {
995         strcpy(buffer, orginal);
996         modify_filename_extention(buffer, ARCHIVENAME_EXTENTION);       /* ".lzh" */
997 }
998
999 /* ------------------------------------------------------------------------ */
1000 /*                                                                                                                                                      */
1001 /* ------------------------------------------------------------------------ */
1002 boolean
1003 need_file(name)
1004         char           *name;
1005 {
1006         int             i;
1007
1008         if (cmd_filec == 0)
1009                 return TRUE;
1010
1011         for (i = 0; i < cmd_filec; i++) {
1012                 if (patmatch(cmd_filev[i], name, 0))
1013                         return TRUE;
1014         }
1015
1016         return FALSE;
1017 }
1018
1019 FILE           *
1020 xfopen(name, mode)
1021         char           *name, *mode;
1022 {
1023         FILE           *fp;
1024
1025         if ((fp = fopen(name, mode)) == NULL)
1026                 fatal_error(name);
1027
1028         return fp;
1029 }
1030
1031 /* ------------------------------------------------------------------------ */
1032 /*                                                                                                                                                      */
1033 /* ------------------------------------------------------------------------ */
1034 static          boolean
1035 open_old_archive_1(name, v_fp)
1036         char           *name;
1037         FILE          **v_fp;
1038 {
1039         FILE           *fp;
1040         struct stat     stbuf;
1041
1042         if (stat(name, &stbuf) >= 0 &&
1043             is_regularfile(&stbuf) &&
1044             (fp = fopen(name, READ_BINARY)) != NULL) {
1045                 *v_fp = fp;
1046                 archive_file_gid = stbuf.st_gid;
1047                 archive_file_mode = stbuf.st_mode;
1048                 return TRUE;
1049         }
1050
1051         *v_fp = NULL;
1052         archive_file_gid = -1;
1053         return FALSE;
1054 }
1055
1056 /* ------------------------------------------------------------------------ */
1057 FILE           *
1058 open_old_archive()
1059 {
1060         FILE           *fp;
1061         char           *p;
1062
1063         if (!strcmp(archive_name, "-")) {
1064                 if (cmd == CMD_EXTRACT || cmd == CMD_LIST) {
1065 #if __MINGW32__
1066             setmode(fileno(stdin), O_BINARY);
1067 #endif
1068                         return stdin;
1069         }
1070                 else
1071                         return NULL;
1072         }
1073         if (p = (char *) strrchr(archive_name, '.')) {
1074                 if (strucmp(".LZH", p) == 0
1075                     || strucmp(".LZS", p) == 0
1076                     || strucmp(".COM", p) == 0  /* DOS SFX */
1077                     || strucmp(".EXE", p) == 0
1078                     || strucmp(".X", p) == 0    /* HUMAN SFX */
1079                     || strucmp(".BAK", p) == 0) {       /* for BackUp */
1080                         open_old_archive_1(archive_name, &fp);
1081                         return fp;
1082                 }
1083         }
1084
1085         if (open_old_archive_1(archive_name, &fp))
1086                 return fp;
1087         sprintf(expanded_archive_name, "%s.lzh", archive_name);
1088         if (open_old_archive_1(expanded_archive_name, &fp)) {
1089                 archive_name = expanded_archive_name;
1090                 return fp;
1091         }
1092         /*
1093          * if ( (errno&0xffff)!=E_PNNF ) { archive_name =
1094          * expanded_archive_name; return NULL; }
1095          */
1096         sprintf(expanded_archive_name, "%s.lzs", archive_name);
1097         if (open_old_archive_1(expanded_archive_name, &fp)) {
1098                 archive_name = expanded_archive_name;
1099                 return fp;
1100         }
1101         /*
1102          * if ( (errno&0xffff)!=E_PNNF ) { archive_name =
1103          * expanded_archive_name; return NULL; }
1104          */
1105         /*
1106          * sprintf( expanded_archive_name , "%s.lzh",archive_name);
1107          * archive_name = expanded_archive_name;
1108          */
1109         return NULL;
1110 }
1111
1112 /* ------------------------------------------------------------------------ */
1113 int
1114 inquire(msg, name, selective)
1115         char           *msg, *name, *selective;
1116 {
1117         char            buffer[1024];
1118         char           *p;
1119
1120         for (;;) {
1121                 fprintf(stderr, "%s %s ", name, msg);
1122                 fflush(stderr);
1123
1124                 fgets(buffer, 1024, stdin);
1125
1126                 for (p = selective; *p; p++)
1127                         if (buffer[0] == *p)
1128                                 return p - selective;
1129         }
1130         /* NOTREACHED */
1131 }
1132
1133 /* ------------------------------------------------------------------------ */
1134 void
1135 write_archive_tail(nafp)
1136         FILE           *nafp;
1137 {
1138         putc(0x00, nafp);
1139 }
1140
1141 /* ------------------------------------------------------------------------ */
1142 void
1143 copy_old_one(oafp, nafp, hdr)
1144         FILE           *oafp, *nafp;
1145         LzHeader       *hdr;
1146 {
1147         if (noexec) {
1148                 fseek(oafp, (long) (hdr->header_size + 2) + hdr->packed_size, SEEK_CUR);
1149         }
1150         else {
1151                 reading_filename = archive_name;
1152                 writting_filename = temporary_name;
1153                 if (hdr->header_level != 2) {
1154                         copyfile(oafp, nafp,
1155                                          (long) (hdr->header_size + 2) + hdr->packed_size, 0);
1156                 } else {
1157                         copyfile(oafp, nafp,
1158                                          (long) (hdr->header_size) + hdr->packed_size, 0);
1159                 }
1160         }
1161 }
1162
1163 /* Local Variables: */
1164 /* mode:c */
1165 /* tab-width:4 */
1166 /* compile-command:"gcc -c lharc.c" */
1167 /* End: */