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;
96 noconvertcase = FALSE;
98 extract_directory = NULL;
101 #if BACKUP_OLD_ARCHIVE
102 backup_old_archive = TRUE;
104 backup_old_archive = FALSE;
108 /* ------------------------------------------------------------------------ */
109 /* NOTES : Text File Format */
110 /* GENERATOR NewLine */
111 /* [generic] 0D 0A */
113 /* [OS9][MacOS] 0D */
115 /* ------------------------------------------------------------------------ */
120 LHarc for UNIX V 1.02 Copyright(C) 1989 Y.Tagawa\n\
121 LHx for MSDOS V C2.01 Copyright(C) 1990 H.Yoshizaki\n\
122 LHx(arc) for OSK V 2.01 Modified 1990 Momozou\n\
123 LHa for UNIX V 1.00 Copyright(C) 1992 Masaru Oki\n\
124 LHa for UNIX V 1.14 Modified 1995 Nobutaka Watazaki\n\
125 LHa for UNIX V 1.14i Modified 2000 Tsugio Okamoto\n\
126 Modified 2002 Hiroto Sakai\n\
127 Autoconfiscated 2001,2002 Koji Arai\n\
130 usage: lha [-]<commands><options> [-<options> ...] archive_file [file...]\n\
131 commands: [axelvudmcpt]\n\
132 options: [q[num]vnfto[num]dizge012[w=<dir>][x=<pattern>]]\n\
133 commands: options:\n\
134 a Add(or replace) to archive q{num} quiet (num:quiet mode)\n\
135 x,e EXtract from archive v verbose\n\
136 l,v List / Verbose List n not execute\n\
137 u Update newer files to archive f force (over write at extract)\n\
138 d Delete from archive t FILES are TEXT file\n");
141 m Move to archive (means 'ad') o[567] compression method (a/u)\n\
146 m Move to archive (means 'ad') o use LHarc compatible method (a/u)\n\
150 c re-Construct new archive w=<dir> specify extract directory (a/u/m/x/e)\n\
151 p Print to STDOUT from archive d delete FILES after (a/u/c)\n\
152 t Test file CRC in archive i ignore directory path (x/e)\n\
153 x=<pattern> eXclude files (a/u)\n\
154 z files not compress (a/u)\n\
155 g Generic format (for compatibility)\n\
156 or not convert case when extracting\n\
157 0/1/2 header level (a/u)\n\
161 e TEXT code convert from/to EUC\n\
167 parse_option(int argc, char **argv)
172 argv++; argc--; /* exclude command name */
179 if (strcmp(*argv, "--help") == 0) {
183 if (strcmp(*argv, "--version") == 0) {
189 archive_name = *argv++; argc--;
196 opt = *argv++; argc--;
209 output_to_stdout = TRUE;
227 update_if_newer = TRUE;
232 delete_after_append = TRUE;
237 verbose_listing = TRUE;
263 case '0': /* no quiet */
264 case '1': /* no use the incremental indicator */
265 quiet_mode = *p - '0';
268 case '2': /* no output */
272 /* In quiet mode, no confirm to overwrite */
300 generic_format = TRUE;
301 noconvertcase = TRUE;
305 delete_after_append = TRUE;
310 compress_method = LZHUFF1_METHOD_NUM;
314 compress_method = LZHUFF5_METHOD_NUM;
319 compress_method = LZHUFF6_METHOD_NUM;
323 compress_method = LZHUFF7_METHOD_NUM;
328 error("invalid compression method 'o%c'", *p);
333 compress_method = LZHUFF0_METHOD_NUM; /* Changed N.Watazaki */
336 ignore_directory = TRUE;
342 for (i = 0; exclude_files && exclude_files[i]; i++)
344 exclude_files = (char**)xrealloc(exclude_files,
345 sizeof(char*) * (i+2));
348 exclude_files[i] = *argv++; argc--;
349 exclude_files[i+1] = 0;
353 exclude_files[i] = p;
354 exclude_files[i+1] = 0;
362 extract_directory = *argv++; argc--;
366 extract_directory = p;
371 header_level = HEADER_LEVEL0;
374 header_level = HEADER_LEVEL1;
377 header_level = HEADER_LEVEL2;
380 error("Unknown option '%c'.", p[-1]);
387 if (!opt || opt[0] != '-')
390 /* special archive name */
391 if (strcmp(opt, "-") == 0)
397 archive_name = *argv++; argc--;
403 /* ------------------------------------------------------------------------ */
413 init_variable(); /* Added N.Watazaki */
415 parse_option(argc, argv);
422 if (!strcmp(archive_name, "-")) {
423 if (!isatty(1) && cmd == CMD_ADD)
426 #if 0 /* Comment out; IMHO, this feature is useless. by Koji Arai */
428 if (argc == 3 && !isatty(0)) { /* 1999.7.18 */
429 /* Bug(?) on MinGW, isatty() return 0 on Cygwin console.
430 mingw-runtime-1.3-2 and Cygwin 1.3.10(0.51/3/2) on
432 get_filename_from_stdin = TRUE;
437 /* target file name */
438 if (get_filename_from_stdin) {
444 xfilev = (char **)xmalloc(sizeof(char *) * xfilec);
445 while (fgets(inpbuf, sizeof(inpbuf), stdin)) {
446 /* delete \n if it exist */
448 while (i < sizeof(inpbuf) && p != 0) {
456 if (cmd_filec >= xfilec) {
458 xfilev = (char **) xrealloc(xfilev,
459 sizeof(char *) * xfilec);
461 if (strlen(inpbuf) < 1)
463 xfilev[cmd_filec++] = xstrdup(inpbuf);
465 xfilev[cmd_filec] = NULL;
500 /* ------------------------------------------------------------------------ */
502 /* ------------------------------------------------------------------------ */
504 /* ------------------------------------------------------------------------ */
508 /* macro PACKAGE_NAME, PACKAGE_VERSION and PLATFORM are
509 defined in config.h by configure script */
510 fprintf(stderr, "%s version %s (%s)\n",
511 PACKAGE_NAME, PACKAGE_VERSION, PLATFORM);
516 message(char *fmt, ...)
518 message(fmt, va_alist)
523 int errno_sv = errno;
526 fprintf(stderr, "LHa: ");
529 vfprintf(stderr, fmt, v);
537 /* ------------------------------------------------------------------------ */
540 warning(char *fmt, ...)
542 warning(fmt, va_alist)
547 int errno_sv = errno;
550 fprintf(stderr, "LHa: Warning: ");
553 vfprintf(stderr, fmt, v);
561 /* ------------------------------------------------------------------------ */
564 error(char *fmt, ...)
571 int errno_sv = errno;
574 fprintf(stderr, "LHa: Error: ");
577 vfprintf(stderr, fmt, v);
589 fatal_error(char *fmt, ...)
591 fatal_error(fmt, va_alist)
596 int errno_sv = errno;
599 fprintf(stderr, "LHa: Fatal error: ");
602 vfprintf(stderr, fmt, v);
606 fprintf(stderr, ": %s\n", strerror(errno_sv));
610 if (remove_temporary_at_error) {
611 if (temporary_fd != -1)
613 unlink(temporary_name);
619 /* ------------------------------------------------------------------------ */
624 message("Interrupted");
626 if (temporary_fd != -1)
628 unlink(temporary_name);
629 if (recover_archive_when_interrupt)
630 rename(backup_archive_name, archive_name);
631 if (remove_extracting_file_when_interrupt) {
632 message("Removing: %s", writing_filename);
633 unlink(writing_filename);
635 signal(SIGINT, SIG_DFL);
637 signal(SIGHUP, SIG_DFL);
639 kill(getpid(), signo);
642 /* ------------------------------------------------------------------------ */
644 /* ------------------------------------------------------------------------ */
649 register char *p, *q;
653 if (generic_format) {
655 c1 = *(unsigned char *) p++;
656 c2 = *(unsigned char *) q++;
668 while (*p == *q && *p != '\0')
670 return *(unsigned char *) p - *(unsigned char *) q;
674 /* ------------------------------------------------------------------------ */
679 qsort(cmd_filev, cmd_filec, sizeof(char *), sort_by_ascii);
682 /* ------------------------------------------------------------------------ */
687 void *p = malloc(size);
689 fatal_error("Not enough memory");
693 /* ------------------------------------------------------------------------ */
699 void *p = (char *) realloc(old, size);
701 fatal_error("Not enough memory");
709 int len = strlen(str);
710 char *p = (char *)xmalloc(len + 1);
715 /* ------------------------------------------------------------------------ */
717 /* ------------------------------------------------------------------------ */
720 +-------------+-------------+------+-------------+----------+
721 | N A M E 1 \0| N A M E 2 \0| .... | N A M E n \0| |
722 +-------------+-------------+------+-------------+----------+
723 ^ ^ ^ buffer+0 buffer+used buffer+size
726 +---------------+---------------+------------- -----------------+
727 | pointer to | pointer to | pointer to ... pointer to |
728 | stringpool | N A M E 1 | N A M E 2 ... N A M E n |
729 +---------------+---------------+------------- -------------+
730 ^ malloc base returned
733 /* ------------------------------------------------------------------------ */
736 struct string_pool *sp;
738 sp->size = 1024 - 8; /* any ( >=0 ) */
741 sp->buffer = (char *) xmalloc(sp->size * sizeof(char));
744 /* ------------------------------------------------------------------------ */
746 add_sp(sp, name, len)
747 struct string_pool *sp;
748 char *name; /* stored '\0' at tail */
749 int len; /* include '\0' */
751 while (sp->used + len > sp->size) {
753 sp->buffer = (char *) xrealloc(sp->buffer, sp->size * sizeof(char));
755 memmove(sp->buffer + sp->used, name, len);
760 /* ------------------------------------------------------------------------ */
762 finish_sp(sp, v_count, v_vector)
763 register struct string_pool *sp;
771 v = (char **) xmalloc((sp->n + 1) * sizeof(char *));
776 for (i = sp->n; i; i--) {
783 /* ------------------------------------------------------------------------ */
789 free(*vector); /* free string pool */
794 /* ------------------------------------------------------------------------ */
795 /* READ DIRECTORY FILES */
796 /* ------------------------------------------------------------------------ */
798 include_path_p(path, name)
804 return (path[-1] == '/' && *n == '\0');
805 return (*n == '/' || (n != name && path[-1] == '/' && n[-1] == '/'));
808 /* ------------------------------------------------------------------------ */
810 cleaning_files(v_filec, v_filev)
817 register char **filev = *v_filev;
818 register int filec = *v_filec;
825 flags = xmalloc(filec * sizeof(char));
827 /* flags & 0x01 : 1: ignore */
828 /* flags & 0x02 : 1: directory, 0 : regular file */
829 /* flags & 0x04 : 1: need delete */
832 for (i = 0; i < filec; i++)
833 if (GETSTAT(filev[i], &stbuf) < 0) {
835 warning("Cannot access \"%s\", ignored.", filev[i]);
838 if (is_regularfile(&stbuf))
840 else if (is_directory(&stbuf))
843 else if (is_symlink(&stbuf)) /* t.okamoto */
848 warning("Cannot archive \"%s\", ignored.", filev[i]);
852 for (i = 0; i < filec; i++) {
854 if ((flags[i] & 0x07) == 0x00) { /* regular file, not
856 for (j = i + 1; j < filec; j++) {
857 if ((flags[j] & 0x07) == 0x00) { /* regular file, not
859 if (STREQU(p, filev[j]))
860 flags[j] = 0x04; /* delete */
864 else if ((flags[i] & 0x07) == 0x02) { /* directory, not
866 for (j = i + 1; j < filec; j++) {
867 if ((flags[j] & 0x07) == 0x00) { /* regular file, not
869 if (include_path_p(p, filev[j]))
870 flags[j] = 0x04; /* delete */
872 else if ((flags[j] & 0x07) == 0x02) { /* directory, not
874 if (include_path_p(p, filev[j]))
875 flags[j] = 0x04; /* delete */
881 for (i = j = 0; i < filec; i++) {
882 if ((flags[i] & 0x04) == 0) {
893 /* ------------------------------------------------------------------------ */
895 find_files(name, v_filec, v_filev)
900 struct string_pool sp;
901 char newname[FILENAME_LENGTH];
905 struct stat tmp_stbuf, arc_stbuf, fil_stbuf;
907 strcpy(newname, name);
909 if (len > 0 && newname[len - 1] != '/')
910 newname[len++] = '/';
912 dirp = opendir(name);
918 GETSTAT(temporary_name, &tmp_stbuf);
919 GETSTAT(archive_name, &arc_stbuf);
921 for (dp = readdir(dirp); dp != NULL; dp = readdir(dirp)) {
922 for (i = 0; exclude_files && exclude_files[i]; i++) {
923 if (fnmatch(exclude_files[i], dp->d_name,
924 FNM_PATHNAME|FNM_NOESCAPE|FNM_PERIOD) == 0)
929 strncpy(newname + len, dp->d_name, n);
930 newname[len + n] = '\0';
931 if (GETSTAT(newname, &fil_stbuf) < 0)
933 #if !defined(HAVE_STRUCT_STAT_ST_INO) || __MINGW32__
934 if ( dp->d_name[0] != '.' ||
936 (dp->d_name[1] != '.' ||
938 add_sp(&sp, newname, len+n+1);
941 if ((dp->d_ino != 0) &&
942 /* exclude '.' and '..' */
943 ((dp->d_name[0] != '.') ||
945 ((dp->d_name[1] != '.') ||
947 ((tmp_stbuf.st_dev != fil_stbuf.st_dev ||
948 tmp_stbuf.st_ino != fil_stbuf.st_ino) &&
949 (arc_stbuf.st_dev != fil_stbuf.st_dev ||
950 arc_stbuf.st_ino != fil_stbuf.st_ino))) {
951 add_sp(&sp, newname, len + n + 1);
957 finish_sp(&sp, v_filec, v_filev);
959 qsort(*v_filev, *v_filec, sizeof(char *), sort_by_ascii);
960 cleaning_files(v_filec, v_filev);
965 /* ------------------------------------------------------------------------ */
967 free_files(filec, filev)
974 /* ------------------------------------------------------------------------ */
976 /* ------------------------------------------------------------------------ */
977 /* Build temporary file name and store to TEMPORARY_NAME */
979 build_temporary_name()
981 #ifdef TMP_FILENAME_TEMPLATE
982 /* "/tmp/lhXXXXXX" etc. */
983 if (extract_directory == NULL) {
984 strcpy(temporary_name, TMP_FILENAME_TEMPLATE);
987 xsnprintf(temporary_name, sizeof(temporary_name),
988 "%s/lhXXXXXX", extract_directory);
993 strcpy(temporary_name, archive_name);
994 for (p = temporary_name, s = (char *) 0; *p; p++)
997 strcpy((s ? s + 1 : temporary_name), "lhXXXXXX");
1003 old_umask = umask(077);
1004 fd = mkstemp(temporary_name);
1012 mktemp(temporary_name);
1013 flags = O_CREAT|O_EXCL|O_RDWR;
1017 return open(temporary_name, flags, 0600);
1022 /* ------------------------------------------------------------------------ */
1024 modify_filename_extention(buffer, ext)
1028 register char *p, *dot;
1030 for (p = buffer, dot = (char *) 0; *p; p++) {
1043 /* ------------------------------------------------------------------------ */
1044 /* build backup file name */
1046 build_backup_name(buffer, original)
1050 strcpy(buffer, original);
1051 modify_filename_extention(buffer, BACKUPNAME_EXTENTION); /* ".bak" */
1054 /* ------------------------------------------------------------------------ */
1056 build_standard_archive_name(buffer, orginal)
1060 strcpy(buffer, orginal);
1061 modify_filename_extention(buffer, ARCHIVENAME_EXTENTION); /* ".lzh" */
1064 /* ------------------------------------------------------------------------ */
1066 /* ------------------------------------------------------------------------ */
1076 for (i = 0; i < cmd_filec; i++) {
1077 if (patmatch(cmd_filev[i], name, 0))
1090 if ((fp = fopen(name, mode)) == NULL)
1091 fatal_error("Cannot open file \"%s\"", name);
1096 /* ------------------------------------------------------------------------ */
1098 /* ------------------------------------------------------------------------ */
1100 open_old_archive_1(name, v_fp)
1107 if (stat(name, &stbuf) >= 0 &&
1108 is_regularfile(&stbuf) &&
1109 (fp = fopen(name, READ_BINARY)) != NULL) {
1111 archive_file_gid = stbuf.st_gid;
1112 archive_file_mode = stbuf.st_mode;
1117 archive_file_gid = -1;
1121 /* ------------------------------------------------------------------------ */
1127 static char expanded_archive_name[FILENAME_LENGTH];
1129 if (!strcmp(archive_name, "-")) {
1130 if (cmd == CMD_EXTRACT || cmd == CMD_LIST) {
1132 setmode(fileno(stdin), O_BINARY);
1139 p = strrchr(archive_name, '.');
1141 if (strucmp(".LZH", p) == 0
1142 || strucmp(".LZS", p) == 0
1143 || strucmp(".COM", p) == 0 /* DOS SFX */
1144 || strucmp(".EXE", p) == 0
1145 || strucmp(".X", p) == 0 /* HUMAN SFX */
1146 || strucmp(".BAK", p) == 0) { /* for BackUp */
1147 open_old_archive_1(archive_name, &fp);
1152 if (open_old_archive_1(archive_name, &fp))
1154 xsnprintf(expanded_archive_name, sizeof(expanded_archive_name),
1155 "%s.lzh", archive_name);
1156 if (open_old_archive_1(expanded_archive_name, &fp)) {
1157 archive_name = expanded_archive_name;
1161 * if ( (errno&0xffff)!=E_PNNF ) { archive_name =
1162 * expanded_archive_name; return NULL; }
1164 xsnprintf(expanded_archive_name, sizeof(expanded_archive_name),
1165 "%s.lzs", archive_name);
1166 if (open_old_archive_1(expanded_archive_name, &fp)) {
1167 archive_name = expanded_archive_name;
1171 * if ( (errno&0xffff)!=E_PNNF ) { archive_name =
1172 * expanded_archive_name; return NULL; }
1175 * sprintf( expanded_archive_name , "%s.lzh",archive_name);
1176 * archive_name = expanded_archive_name;
1181 /* ------------------------------------------------------------------------ */
1183 inquire(msg, name, selective)
1184 char *msg, *name, *selective;
1190 fprintf(stderr, "%s %s ", name, msg);
1193 fgets(buffer, 1024, stdin);
1195 for (p = selective; *p; p++)
1196 if (buffer[0] == *p)
1197 return p - selective;
1202 /* ------------------------------------------------------------------------ */
1204 write_archive_tail(nafp)
1210 /* ------------------------------------------------------------------------ */
1212 copy_old_one(oafp, nafp, hdr)
1217 fseek(oafp, hdr->header_size + hdr->packed_size, SEEK_CUR);
1220 reading_filename = archive_name;
1221 writing_filename = temporary_name;
1222 copyfile(oafp, nafp, hdr->header_size + hdr->packed_size, 0, 0);