1 /* ------------------------------------------------------------------------ */
3 /* lharc.c -- append to archive */
5 /* Copyright (C) MCMLXXXIX Yooichi.Tagawa */
6 /* Modified Nobutaka Watazaki */
7 /* Thanks to H.Yoshizaki. (MS-DOS LHarc) */
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 */
21 /* DOS-Version Original LHx V C2.01 (C) H.Yohizaki */
23 /* V2.00 UNIX Lharc + DOS LHx -> OSK LHx 1990.11.01 Momozou */
24 /* V2.01 Minor Modified 1990.11.24 Momozou */
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 */
31 /* Ver. 1.10 for Symbolic Link 1993.06.25 N.Watazaki */
32 /* Ver. 1.11 for Symbolic 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 /* Modified for Mac OS X 2002.06.03 H.Sakai */
43 /* ------------------------------------------------------------------------ */
48 static int cmd = CMD_UNKNOWN;
49 static int error_occurred;
51 /* static functions */
52 static void sort_files();
53 static void print_version();
55 /* ------------------------------------------------------------------------ */
57 init_variable() /* Added N.Watazaki */
63 noexec = FALSE; /* debugging option */
67 compress_method = DEFAULT_LZHUFF_METHOD; /* defined in config.h */
69 header_level = HEADER_LEVEL2;
76 /* view command flags */
77 verbose_listing = FALSE;
79 /* extract command flags */
80 output_to_stdout = FALSE;
82 /* append command flags */
84 update_if_newer = FALSE;
85 delete_after_append = FALSE;
86 generic_format = FALSE;
88 remove_temporary_at_error = FALSE;
89 recover_archive_when_interrupt = FALSE;
90 remove_extracting_file_when_interrupt = FALSE;
91 get_filename_from_stdin = FALSE;
92 ignore_directory = FALSE;
95 noconvertcase = FALSE;
97 extract_directory = NULL;
100 #if BACKUP_OLD_ARCHIVE
101 backup_old_archive = TRUE;
103 backup_old_archive = FALSE;
107 /* ------------------------------------------------------------------------ */
108 /* NOTES : Text File Format */
109 /* GENERATOR NewLine */
110 /* [generic] 0D 0A */
112 /* [OS9][MacOS] 0D */
114 /* ------------------------------------------------------------------------ */
119 LHarc for UNIX V 1.02 Copyright(C) 1989 Y.Tagawa\n\
120 LHx for MSDOS V C2.01 Copyright(C) 1990 H.Yoshizaki\n\
121 LHx(arc) for OSK V 2.01 Modified 1990 Momozou\n\
122 LHa for UNIX V 1.00 Copyright(C) 1992 Masaru Oki\n\
123 LHa for UNIX V 1.14 Modified 1995 Nobutaka Watazaki\n\
124 LHa for UNIX V 1.14i Modified 2000 Tsugio Okamoto\n\
125 Modified 2002 Hiroto Sakai\n\
126 Autoconfiscated 2001,2002 Koji Arai\n\
129 usage: lha [-]{axelvudmcp[q[num]][vnfodizg012]}[w=<dir>] archive_file [file...]\n\
130 commands: options:\n\
131 a Add(or replace) to archive q{num} quiet (num:quiet mode)\n\
132 x,e EXtract from archive v verbose\n\
133 l,v List / Verbose List n not execute\n\
134 u Update newer files to archive f force (over write at extract)\n\
135 d Delete from archive t FILES are TEXT file\n");
138 m Move to archive (means 'ad') o[567] compression method (a/u)\n\
143 m Move to archive (means 'ad') o use LHarc compatible method (a/u)\n\
147 c re-Construct new archive w=<dir> specify extract directory (a/u/m/x/e)\n\
148 p Print to STDOUT from archive d delete FILES after (a/u/c)\n\
149 t Test file CRC in archive i ignore directory path (x/e)\n\
150 z files not compress (a/u)\n\
151 g Generic format (for compatibility)\n\
152 or not convert case when extracting\n\
153 0/1/2 header level (a/u)\n\
157 e TEXT code convert from/to EUC\n\
162 /* ------------------------------------------------------------------------ */
174 init_variable(); /* Added N.Watazaki */
177 av = (char **)xmalloc( sizeof(char*)*argc );
178 for (i=0; i<argc; i++) {
179 av[i] = xstrdup( argv[i] );
182 if (ac < 2 || strcmp(av[1], "--help") == 0) {
187 if (strcmp(av[1], "--version") == 0) {
194 av--; /* argv--; */ /* 1999.7.18 */
211 output_to_stdout = TRUE;
229 update_if_newer = TRUE;
234 delete_after_append = TRUE;
239 verbose_listing = TRUE;
258 /* p = &argv[1][1]; */
264 case '0': /* no quiet */
265 case '1': /* no use the incremental indicator */
266 quiet_mode = *p - '0';
269 case '2': /* no output */
273 /* In quiet mode, no confirm to overwrite */
301 generic_format = TRUE;
302 noconvertcase = TRUE;
306 delete_after_append = TRUE;
311 compress_method = LZHUFF1_METHOD_NUM;
315 compress_method = LZHUFF5_METHOD_NUM;
320 compress_method = LZHUFF6_METHOD_NUM;
324 compress_method = LZHUFF7_METHOD_NUM;
329 error("invalid compression method 'o%c'", *p);
334 compress_method = LZHUFF0_METHOD_NUM; /* Changed N.Watazaki */
337 ignore_directory = TRUE;
342 extract_directory = p;
347 header_level = HEADER_LEVEL0;
350 header_level = HEADER_LEVEL1;
353 header_level = HEADER_LEVEL2;
356 error("Unknown option '%c'.", p[-1]);
362 /* archive file name */
363 archive_name = av[2];
365 if (!strcmp(archive_name, "-")) {
366 if (!isatty(1) && cmd == CMD_ADD)
369 #if 0 /* Comment out; IMHO, this feature is useless. by Koji Arai */
371 if (ac == 3 && !isatty(0)) { /* 1999.7.18 */
372 /* Bug(?) on MinGW, isatty() return 0 on Cygwin console.
373 mingw-runtime-1.3-2 and Cygwin 1.3.10(0.51/3/2) on
375 get_filename_from_stdin = TRUE;
380 /* target file name */
381 if (get_filename_from_stdin) {
387 xfilev = (char **)xmalloc(sizeof(char *) * xfilec);
388 while (fgets(inpbuf, sizeof(inpbuf), stdin)) {
389 /* delete \n if it exist */
391 while (i < sizeof(inpbuf) && p != 0) {
399 if (cmd_filec >= xfilec) {
401 xfilev = (char **) xrealloc(xfilev,
402 sizeof(char *) * xfilec);
404 if (strlen(inpbuf) < 1)
406 xfilev[cmd_filec++] = xstrdup(inpbuf);
408 xfilev[cmd_filec] = NULL;
445 /* ------------------------------------------------------------------------ */
447 /* ------------------------------------------------------------------------ */
449 /* ------------------------------------------------------------------------ */
453 /* macro PACKAGE_NAME, PACKAGE_VERSION and PLATFORM are
454 defined in config.h by configure script */
455 fprintf(stderr, "%s version %s (%s)\n",
456 PACKAGE_NAME, PACKAGE_VERSION, PLATFORM);
461 message(char *fmt, ...)
463 message(fmt, va_alist)
468 int errno_sv = errno;
471 fprintf(stderr, "LHa: ");
474 vfprintf(stderr, fmt, v);
482 /* ------------------------------------------------------------------------ */
485 warning(char *fmt, ...)
487 warning(fmt, va_alist)
492 int errno_sv = errno;
495 fprintf(stderr, "LHa: Warning: ");
498 vfprintf(stderr, fmt, v);
506 /* ------------------------------------------------------------------------ */
509 error(char *fmt, ...)
516 int errno_sv = errno;
519 fprintf(stderr, "LHa: Error: ");
522 vfprintf(stderr, fmt, v);
534 fatal_error(char *fmt, ...)
536 fatal_error(fmt, va_alist)
541 int errno_sv = errno;
544 fprintf(stderr, "LHa: Fatal error: ");
547 vfprintf(stderr, fmt, v);
551 fprintf(stderr, ": %s\n", strerror(errno_sv));
555 if (remove_temporary_at_error) {
556 if (temporary_fd != -1)
558 unlink(temporary_name);
564 /* ------------------------------------------------------------------------ */
569 message("Interrupted");
571 if (temporary_fd != -1)
573 unlink(temporary_name);
574 if (recover_archive_when_interrupt)
575 rename(backup_archive_name, archive_name);
576 if (remove_extracting_file_when_interrupt) {
577 message("Removing: %s", writing_filename);
578 unlink(writing_filename);
580 signal(SIGINT, SIG_DFL);
582 signal(SIGHUP, SIG_DFL);
584 kill(getpid(), signo);
587 /* ------------------------------------------------------------------------ */
589 /* ------------------------------------------------------------------------ */
594 register char *p, *q;
598 if (generic_format) {
600 c1 = *(unsigned char *) p++;
601 c2 = *(unsigned char *) q++;
613 while (*p == *q && *p != '\0')
615 return *(unsigned char *) p - *(unsigned char *) q;
619 /* ------------------------------------------------------------------------ */
624 qsort(cmd_filev, cmd_filec, sizeof(char *), sort_by_ascii);
627 /* ------------------------------------------------------------------------ */
632 void *p = malloc(size);
634 fatal_error("Not enough memory");
638 /* ------------------------------------------------------------------------ */
644 void *p = (char *) realloc(old, size);
646 fatal_error("Not enough memory");
654 int len = strlen(str);
655 char *p = (char *)xmalloc(len + 1);
660 /* ------------------------------------------------------------------------ */
662 /* ------------------------------------------------------------------------ */
665 +-------------+-------------+------+-------------+----------+
666 | N A M E 1 \0| N A M E 2 \0| .... | N A M E n \0| |
667 +-------------+-------------+------+-------------+----------+
668 ^ ^ ^ buffer+0 buffer+used buffer+size
671 +---------------+---------------+------------- -----------------+
672 | pointer to | pointer to | pointer to ... pointer to |
673 | stringpool | N A M E 1 | N A M E 2 ... N A M E n |
674 +---------------+---------------+------------- -------------+
675 ^ malloc base returned
678 /* ------------------------------------------------------------------------ */
681 struct string_pool *sp;
683 sp->size = 1024 - 8; /* any ( >=0 ) */
686 sp->buffer = (char *) xmalloc(sp->size * sizeof(char));
689 /* ------------------------------------------------------------------------ */
691 add_sp(sp, name, len)
692 struct string_pool *sp;
693 char *name; /* stored '\0' at tail */
694 int len; /* include '\0' */
696 while (sp->used + len > sp->size) {
698 sp->buffer = (char *) xrealloc(sp->buffer, sp->size * sizeof(char));
700 memmove(sp->buffer + sp->used, name, len);
705 /* ------------------------------------------------------------------------ */
707 finish_sp(sp, v_count, v_vector)
708 register struct string_pool *sp;
716 v = (char **) xmalloc((sp->n + 1) * sizeof(char *));
721 for (i = sp->n; i; i--) {
728 /* ------------------------------------------------------------------------ */
734 free(*vector); /* free string pool */
739 /* ------------------------------------------------------------------------ */
740 /* READ DIRECTORY FILES */
741 /* ------------------------------------------------------------------------ */
743 include_path_p(path, name)
749 return (path[-1] == '/' && *n == '\0');
750 return (*n == '/' || (n != name && path[-1] == '/' && n[-1] == '/'));
753 /* ------------------------------------------------------------------------ */
755 cleaning_files(v_filec, v_filev)
762 register char **filev = *v_filev;
763 register int filec = *v_filec;
770 flags = xmalloc(filec * sizeof(char));
772 /* flags & 0x01 : 1: ignore */
773 /* flags & 0x02 : 1: directory, 0 : regular file */
774 /* flags & 0x04 : 1: need delete */
777 for (i = 0; i < filec; i++)
778 if (GETSTAT(filev[i], &stbuf) < 0) {
780 warning("Cannot access \"%s\", ignored.", filev[i]);
783 if (is_regularfile(&stbuf))
785 else if (is_directory(&stbuf))
788 else if (is_symlink(&stbuf)) /* t.okamoto */
793 warning("Cannot archive \"%s\", ignored.", filev[i]);
797 for (i = 0; i < filec; i++) {
799 if ((flags[i] & 0x07) == 0x00) { /* regular file, not
801 for (j = i + 1; j < filec; j++) {
802 if ((flags[j] & 0x07) == 0x00) { /* regular file, not
804 if (STREQU(p, filev[j]))
805 flags[j] = 0x04; /* delete */
809 else if ((flags[i] & 0x07) == 0x02) { /* directory, not
811 for (j = i + 1; j < filec; j++) {
812 if ((flags[j] & 0x07) == 0x00) { /* regular file, not
814 if (include_path_p(p, filev[j]))
815 flags[j] = 0x04; /* delete */
817 else if ((flags[j] & 0x07) == 0x02) { /* directory, not
819 if (include_path_p(p, filev[j]))
820 flags[j] = 0x04; /* delete */
826 for (i = j = 0; i < filec; i++) {
827 if ((flags[i] & 0x04) == 0) {
838 /* ------------------------------------------------------------------------ */
840 find_files(name, v_filec, v_filev)
845 struct string_pool sp;
846 char newname[FILENAME_LENGTH];
850 struct stat tmp_stbuf, arc_stbuf, fil_stbuf;
852 strcpy(newname, name);
854 if (len > 0 && newname[len - 1] != '/')
855 newname[len++] = '/';
857 dirp = opendir(name);
863 GETSTAT(temporary_name, &tmp_stbuf);
864 GETSTAT(archive_name, &arc_stbuf);
866 for (dp = readdir(dirp); dp != NULL; dp = readdir(dirp)) {
868 strncpy(newname + len, dp->d_name, n);
869 newname[len + n] = '\0';
870 if (GETSTAT(newname, &fil_stbuf) < 0)
872 #if !defined(HAVE_STRUCT_STAT_ST_INO) || __MINGW32__
873 if ( dp->d_name[0] != '.' ||
875 (dp->d_name[1] != '.' ||
877 add_sp(&sp, newname, len+n+1);
880 if ((dp->d_ino != 0) &&
881 /* exclude '.' and '..' */
882 ((dp->d_name[0] != '.') ||
884 ((dp->d_name[1] != '.') ||
886 ((tmp_stbuf.st_dev != fil_stbuf.st_dev ||
887 tmp_stbuf.st_ino != fil_stbuf.st_ino) &&
888 (arc_stbuf.st_dev != fil_stbuf.st_dev ||
889 arc_stbuf.st_ino != fil_stbuf.st_ino))) {
890 add_sp(&sp, newname, len + n + 1);
895 finish_sp(&sp, v_filec, v_filev);
897 qsort(*v_filev, *v_filec, sizeof(char *), sort_by_ascii);
898 cleaning_files(v_filec, v_filev);
903 /* ------------------------------------------------------------------------ */
905 free_files(filec, filev)
912 /* ------------------------------------------------------------------------ */
914 /* ------------------------------------------------------------------------ */
915 /* Build temporary file name and store to TEMPORARY_NAME */
917 build_temporary_name()
919 #ifdef TMP_FILENAME_TEMPLATE
920 /* "/tmp/lhXXXXXX" etc. */
921 if (extract_directory == NULL) {
922 strcpy(temporary_name, TMP_FILENAME_TEMPLATE);
925 xsnprintf(temporary_name, sizeof(temporary_name),
926 "%s/lhXXXXXX", extract_directory);
931 strcpy(temporary_name, archive_name);
932 for (p = temporary_name, s = (char *) 0; *p; p++)
935 strcpy((s ? s + 1 : temporary_name), "lhXXXXXX");
941 old_umask = umask(077);
942 fd = mkstemp(temporary_name);
950 mktemp(temporary_name);
951 flags = O_CREAT|O_EXCL|O_RDWR;
955 return open(temporary_name, flags, 0600);
960 /* ------------------------------------------------------------------------ */
962 modify_filename_extention(buffer, ext)
966 register char *p, *dot;
968 for (p = buffer, dot = (char *) 0; *p; p++) {
981 /* ------------------------------------------------------------------------ */
982 /* build backup file name */
984 build_backup_name(buffer, original)
988 strcpy(buffer, original);
989 modify_filename_extention(buffer, BACKUPNAME_EXTENTION); /* ".bak" */
992 /* ------------------------------------------------------------------------ */
994 build_standard_archive_name(buffer, orginal)
998 strcpy(buffer, orginal);
999 modify_filename_extention(buffer, ARCHIVENAME_EXTENTION); /* ".lzh" */
1002 /* ------------------------------------------------------------------------ */
1004 /* ------------------------------------------------------------------------ */
1014 for (i = 0; i < cmd_filec; i++) {
1015 if (patmatch(cmd_filev[i], name, 0))
1028 if ((fp = fopen(name, mode)) == NULL)
1029 fatal_error("Cannot open file \"%s\"", name);
1034 /* ------------------------------------------------------------------------ */
1036 /* ------------------------------------------------------------------------ */
1038 open_old_archive_1(name, v_fp)
1045 if (stat(name, &stbuf) >= 0 &&
1046 is_regularfile(&stbuf) &&
1047 (fp = fopen(name, READ_BINARY)) != NULL) {
1049 archive_file_gid = stbuf.st_gid;
1050 archive_file_mode = stbuf.st_mode;
1055 archive_file_gid = -1;
1059 /* ------------------------------------------------------------------------ */
1065 static char expanded_archive_name[FILENAME_LENGTH];
1067 if (!strcmp(archive_name, "-")) {
1068 if (cmd == CMD_EXTRACT || cmd == CMD_LIST) {
1070 setmode(fileno(stdin), O_BINARY);
1077 p = strrchr(archive_name, '.');
1079 if (strucmp(".LZH", p) == 0
1080 || strucmp(".LZS", p) == 0
1081 || strucmp(".COM", p) == 0 /* DOS SFX */
1082 || strucmp(".EXE", p) == 0
1083 || strucmp(".X", p) == 0 /* HUMAN SFX */
1084 || strucmp(".BAK", p) == 0) { /* for BackUp */
1085 open_old_archive_1(archive_name, &fp);
1090 if (open_old_archive_1(archive_name, &fp))
1092 xsnprintf(expanded_archive_name, sizeof(expanded_archive_name),
1093 "%s.lzh", archive_name);
1094 if (open_old_archive_1(expanded_archive_name, &fp)) {
1095 archive_name = expanded_archive_name;
1099 * if ( (errno&0xffff)!=E_PNNF ) { archive_name =
1100 * expanded_archive_name; return NULL; }
1102 xsnprintf(expanded_archive_name, sizeof(expanded_archive_name),
1103 "%s.lzs", archive_name);
1104 if (open_old_archive_1(expanded_archive_name, &fp)) {
1105 archive_name = expanded_archive_name;
1109 * if ( (errno&0xffff)!=E_PNNF ) { archive_name =
1110 * expanded_archive_name; return NULL; }
1113 * sprintf( expanded_archive_name , "%s.lzh",archive_name);
1114 * archive_name = expanded_archive_name;
1119 /* ------------------------------------------------------------------------ */
1121 inquire(msg, name, selective)
1122 char *msg, *name, *selective;
1128 fprintf(stderr, "%s %s ", name, msg);
1131 fgets(buffer, 1024, stdin);
1133 for (p = selective; *p; p++)
1134 if (buffer[0] == *p)
1135 return p - selective;
1140 /* ------------------------------------------------------------------------ */
1142 write_archive_tail(nafp)
1148 /* ------------------------------------------------------------------------ */
1150 copy_old_one(oafp, nafp, hdr)
1155 fseek(oafp, hdr->header_size + hdr->packed_size, SEEK_CUR);
1158 reading_filename = archive_name;
1159 writing_filename = temporary_name;
1160 copyfile(oafp, nafp, hdr->header_size + hdr->packed_size, 0, 0);