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[012]vnfto[567]dizg012e[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/c)\n\
146 m Move to archive (means 'ad') o use LHarc compatible method (a/u/c)\n\
150 c re-Construct new archive d delete FILES after (a/u/c)\n\
151 p Print to STDOUT from archive i ignore directory path (x/e)\n\
152 t Test file CRC in archive z files not compress (a/u/c)\n\
153 g Generic format (for compatibility)\n\
154 or not convert case when extracting\n\
155 0/1/2 header level (a/u/c)\n\
159 e TEXT code convert from/to EUC\n\
163 w=<dir> specify extract directory (x/e)\n\
164 x=<pattern> eXclude files (a/u/c)\n\
169 parse_option(int argc, char **argv)
174 argv++; argc--; /* exclude command name */
181 if (strcmp(*argv, "--help") == 0) {
185 if (strcmp(*argv, "--version") == 0) {
191 archive_name = *argv++; argc--;
198 opt = *argv++; argc--;
211 output_to_stdout = TRUE;
229 update_if_newer = TRUE;
234 delete_after_append = TRUE;
239 verbose_listing = TRUE;
265 case '0': /* no quiet */
266 case '1': /* no use the incremental indicator */
267 quiet_mode = *p - '0';
270 case '2': /* no output */
274 /* In quiet mode, no confirm to overwrite */
302 generic_format = TRUE;
303 noconvertcase = TRUE;
307 delete_after_append = TRUE;
312 compress_method = LZHUFF1_METHOD_NUM;
316 compress_method = LZHUFF5_METHOD_NUM;
321 compress_method = LZHUFF6_METHOD_NUM;
325 compress_method = LZHUFF7_METHOD_NUM;
330 error("invalid compression method 'o%c'", *p);
335 compress_method = LZHUFF0_METHOD_NUM; /* Changed N.Watazaki */
338 ignore_directory = TRUE;
344 for (i = 0; exclude_files && exclude_files[i]; i++)
346 exclude_files = (char**)xrealloc(exclude_files,
347 sizeof(char*) * (i+2));
350 exclude_files[i] = *argv++; argc--;
351 exclude_files[i+1] = 0;
355 exclude_files[i] = p;
356 exclude_files[i+1] = 0;
364 extract_directory = *argv++; argc--;
368 extract_directory = p;
373 header_level = HEADER_LEVEL0;
376 header_level = HEADER_LEVEL1;
379 header_level = HEADER_LEVEL2;
382 error("Unknown option '%c'.", p[-1]);
389 if (!opt || opt[0] != '-')
392 /* special archive name */
393 if (strcmp(opt, "-") == 0)
399 archive_name = *argv++; argc--;
405 /* ------------------------------------------------------------------------ */
415 init_variable(); /* Added N.Watazaki */
417 parse_option(argc, argv);
424 if (!strcmp(archive_name, "-")) {
425 if (!isatty(1) && cmd == CMD_ADD)
428 #if 0 /* Comment out; IMHO, this feature is useless. by Koji Arai */
430 if (argc == 3 && !isatty(0)) { /* 1999.7.18 */
431 /* Bug(?) on MinGW, isatty() return 0 on Cygwin console.
432 mingw-runtime-1.3-2 and Cygwin 1.3.10(0.51/3/2) on Win2000 */
433 get_filename_from_stdin = TRUE;
438 /* target file name */
439 if (get_filename_from_stdin) {
445 xfilev = (char **)xmalloc(sizeof(char *) * xfilec);
446 while (fgets(inpbuf, sizeof(inpbuf), stdin)) {
447 /* delete \n if it exist */
449 while (i < sizeof(inpbuf) && p != 0) {
457 if (cmd_filec >= xfilec) {
459 xfilev = (char **) xrealloc(xfilev,
460 sizeof(char *) * xfilec);
462 if (strlen(inpbuf) < 1)
464 xfilev[cmd_filec++] = xstrdup(inpbuf);
466 xfilev[cmd_filec] = NULL;
501 /* ------------------------------------------------------------------------ */
503 /* ------------------------------------------------------------------------ */
505 /* ------------------------------------------------------------------------ */
509 /* macro PACKAGE_NAME, PACKAGE_VERSION and PLATFORM are
510 defined in config.h by configure script */
511 fprintf(stderr, "%s version %s (%s)\n",
512 PACKAGE_NAME, PACKAGE_VERSION, PLATFORM);
517 message(char *fmt, ...)
519 message(fmt, va_alist)
524 int errno_sv = errno;
527 fprintf(stderr, "LHa: ");
530 vfprintf(stderr, fmt, v);
538 /* ------------------------------------------------------------------------ */
541 warning(char *fmt, ...)
543 warning(fmt, va_alist)
548 int errno_sv = errno;
551 fprintf(stderr, "LHa: Warning: ");
554 vfprintf(stderr, fmt, v);
562 /* ------------------------------------------------------------------------ */
565 error(char *fmt, ...)
572 int errno_sv = errno;
575 fprintf(stderr, "LHa: Error: ");
578 vfprintf(stderr, fmt, v);
590 fatal_error(char *fmt, ...)
592 fatal_error(fmt, va_alist)
597 int errno_sv = errno;
600 fprintf(stderr, "LHa: Fatal error: ");
603 vfprintf(stderr, fmt, v);
607 fprintf(stderr, ": %s\n", strerror(errno_sv));
611 if (remove_temporary_at_error) {
612 if (temporary_fd != -1)
614 unlink(temporary_name);
620 /* ------------------------------------------------------------------------ */
625 message("Interrupted");
627 if (temporary_fd != -1)
629 unlink(temporary_name);
630 if (recover_archive_when_interrupt)
631 rename(backup_archive_name, archive_name);
632 if (remove_extracting_file_when_interrupt) {
633 message("Removing: %s", writing_filename);
634 unlink(writing_filename);
636 signal(SIGINT, SIG_DFL);
638 signal(SIGHUP, SIG_DFL);
640 kill(getpid(), signo);
643 /* ------------------------------------------------------------------------ */
645 /* ------------------------------------------------------------------------ */
650 register char *p, *q;
654 if (generic_format) {
656 c1 = *(unsigned char *) p++;
657 c2 = *(unsigned char *) q++;
669 while (*p == *q && *p != '\0')
671 return *(unsigned char *) p - *(unsigned char *) q;
675 /* ------------------------------------------------------------------------ */
680 qsort(cmd_filev, cmd_filec, sizeof(char *), sort_by_ascii);
683 /* ------------------------------------------------------------------------ */
688 void *p = malloc(size);
690 fatal_error("Not enough memory");
694 /* ------------------------------------------------------------------------ */
700 void *p = (char *) realloc(old, size);
702 fatal_error("Not enough memory");
710 int len = strlen(str);
711 char *p = (char *)xmalloc(len + 1);
716 /* ------------------------------------------------------------------------ */
718 /* ------------------------------------------------------------------------ */
721 +-------------+-------------+------+-------------+----------+
722 | N A M E 1 \0| N A M E 2 \0| .... | N A M E n \0| |
723 +-------------+-------------+------+-------------+----------+
724 ^ ^ ^ buffer+0 buffer+used buffer+size
727 +---------------+---------------+------------- -----------------+
728 | pointer to | pointer to | pointer to ... pointer to |
729 | stringpool | N A M E 1 | N A M E 2 ... N A M E n |
730 +---------------+---------------+------------- -------------+
731 ^ malloc base returned
734 /* ------------------------------------------------------------------------ */
737 struct string_pool *sp;
739 sp->size = 1024 - 8; /* any ( >=0 ) */
742 sp->buffer = (char *) xmalloc(sp->size * sizeof(char));
745 /* ------------------------------------------------------------------------ */
747 add_sp(sp, name, len)
748 struct string_pool *sp;
749 char *name; /* stored '\0' at tail */
750 int len; /* include '\0' */
752 while (sp->used + len > sp->size) {
754 sp->buffer = (char *) xrealloc(sp->buffer, sp->size * sizeof(char));
756 memmove(sp->buffer + sp->used, name, len);
761 /* ------------------------------------------------------------------------ */
763 finish_sp(sp, v_count, v_vector)
764 register struct string_pool *sp;
772 v = (char **) xmalloc((sp->n + 1) * sizeof(char *));
777 for (i = sp->n; i; i--) {
784 /* ------------------------------------------------------------------------ */
790 free(*vector); /* free string pool */
795 /* ------------------------------------------------------------------------ */
796 /* READ DIRECTORY FILES */
797 /* ------------------------------------------------------------------------ */
799 include_path_p(path, name)
805 return (path[-1] == '/' && *n == '\0');
806 return (*n == '/' || (n != name && path[-1] == '/' && n[-1] == '/'));
809 /* ------------------------------------------------------------------------ */
811 cleaning_files(v_filec, v_filev)
818 register char **filev = *v_filev;
819 register int filec = *v_filec;
826 flags = xmalloc(filec * sizeof(char));
828 /* flags & 0x01 : 1: ignore */
829 /* flags & 0x02 : 1: directory, 0 : regular file */
830 /* flags & 0x04 : 1: need delete */
833 for (i = 0; i < filec; i++)
834 if (GETSTAT(filev[i], &stbuf) < 0) {
836 warning("Cannot access \"%s\", ignored.", filev[i]);
839 if (is_regularfile(&stbuf))
841 else if (is_directory(&stbuf))
844 else if (is_symlink(&stbuf)) /* t.okamoto */
849 warning("Cannot archive \"%s\", ignored.", filev[i]);
853 for (i = 0; i < filec; i++) {
855 if ((flags[i] & 0x07) == 0x00) { /* regular file, not
857 for (j = i + 1; j < filec; j++) {
858 if ((flags[j] & 0x07) == 0x00) { /* regular file, not
860 if (STREQU(p, filev[j]))
861 flags[j] = 0x04; /* delete */
865 else if ((flags[i] & 0x07) == 0x02) { /* directory, not
867 for (j = i + 1; j < filec; j++) {
868 if ((flags[j] & 0x07) == 0x00) { /* regular file, not
870 if (include_path_p(p, filev[j]))
871 flags[j] = 0x04; /* delete */
873 else if ((flags[j] & 0x07) == 0x02) { /* directory, not
875 if (include_path_p(p, filev[j]))
876 flags[j] = 0x04; /* delete */
882 for (i = j = 0; i < filec; i++) {
883 if ((flags[i] & 0x04) == 0) {
894 /* ------------------------------------------------------------------------ */
896 find_files(name, v_filec, v_filev)
901 struct string_pool sp;
902 char newname[FILENAME_LENGTH];
906 struct stat tmp_stbuf, arc_stbuf, fil_stbuf;
908 strcpy(newname, name);
910 if (len > 0 && newname[len - 1] != '/')
911 newname[len++] = '/';
913 dirp = opendir(name);
919 GETSTAT(temporary_name, &tmp_stbuf);
920 GETSTAT(archive_name, &arc_stbuf);
922 for (dp = readdir(dirp); dp != NULL; dp = readdir(dirp)) {
923 for (i = 0; exclude_files && exclude_files[i]; i++) {
924 if (fnmatch(exclude_files[i], dp->d_name,
925 FNM_PATHNAME|FNM_NOESCAPE|FNM_PERIOD) == 0)
930 strncpy(newname + len, dp->d_name, n);
931 newname[len + n] = '\0';
932 if (GETSTAT(newname, &fil_stbuf) < 0)
934 #if !defined(HAVE_STRUCT_STAT_ST_INO) || __MINGW32__
935 if ( dp->d_name[0] != '.' ||
937 (dp->d_name[1] != '.' ||
939 add_sp(&sp, newname, len+n+1);
942 if ((dp->d_ino != 0) &&
943 /* exclude '.' and '..' */
944 ((dp->d_name[0] != '.') ||
946 ((dp->d_name[1] != '.') ||
948 ((tmp_stbuf.st_dev != fil_stbuf.st_dev ||
949 tmp_stbuf.st_ino != fil_stbuf.st_ino) &&
950 (arc_stbuf.st_dev != fil_stbuf.st_dev ||
951 arc_stbuf.st_ino != fil_stbuf.st_ino))) {
952 add_sp(&sp, newname, len + n + 1);
958 finish_sp(&sp, v_filec, v_filev);
960 qsort(*v_filev, *v_filec, sizeof(char *), sort_by_ascii);
961 cleaning_files(v_filec, v_filev);
966 /* ------------------------------------------------------------------------ */
968 free_files(filec, filev)
975 /* ------------------------------------------------------------------------ */
977 /* ------------------------------------------------------------------------ */
978 /* Build temporary file name and store to TEMPORARY_NAME */
980 build_temporary_name()
982 #ifdef TMP_FILENAME_TEMPLATE
983 /* "/tmp/lhXXXXXX" etc. */
984 if (extract_directory == NULL) {
985 strcpy(temporary_name, TMP_FILENAME_TEMPLATE);
988 xsnprintf(temporary_name, sizeof(temporary_name),
989 "%s/lhXXXXXX", extract_directory);
994 strcpy(temporary_name, archive_name);
995 for (p = temporary_name, s = (char *) 0; *p; p++)
998 strcpy((s ? s + 1 : temporary_name), "lhXXXXXX");
1004 old_umask = umask(077);
1005 fd = mkstemp(temporary_name);
1013 mktemp(temporary_name);
1014 flags = O_CREAT|O_EXCL|O_RDWR;
1018 return open(temporary_name, flags, 0600);
1023 /* ------------------------------------------------------------------------ */
1025 modify_filename_extention(buffer, ext)
1029 register char *p, *dot;
1031 for (p = buffer, dot = (char *) 0; *p; p++) {
1044 /* ------------------------------------------------------------------------ */
1045 /* build backup file name */
1047 build_backup_name(buffer, original)
1051 strcpy(buffer, original);
1052 modify_filename_extention(buffer, BACKUPNAME_EXTENTION); /* ".bak" */
1055 /* ------------------------------------------------------------------------ */
1057 build_standard_archive_name(buffer, orginal)
1061 strcpy(buffer, orginal);
1062 modify_filename_extention(buffer, ARCHIVENAME_EXTENTION); /* ".lzh" */
1065 /* ------------------------------------------------------------------------ */
1067 /* ------------------------------------------------------------------------ */
1077 for (i = 0; i < cmd_filec; i++) {
1078 if (patmatch(cmd_filev[i], name, 0))
1091 if ((fp = fopen(name, mode)) == NULL)
1092 fatal_error("Cannot open file \"%s\"", name);
1097 /* ------------------------------------------------------------------------ */
1099 /* ------------------------------------------------------------------------ */
1101 open_old_archive_1(name, v_fp)
1108 if (stat(name, &stbuf) >= 0 &&
1109 is_regularfile(&stbuf) &&
1110 (fp = fopen(name, READ_BINARY)) != NULL) {
1112 archive_file_gid = stbuf.st_gid;
1113 archive_file_mode = stbuf.st_mode;
1118 archive_file_gid = -1;
1122 /* ------------------------------------------------------------------------ */
1128 static char expanded_archive_name[FILENAME_LENGTH];
1130 if (!strcmp(archive_name, "-")) {
1131 if (cmd == CMD_EXTRACT || cmd == CMD_LIST) {
1133 setmode(fileno(stdin), O_BINARY);
1140 p = strrchr(archive_name, '.');
1142 if (strucmp(".LZH", p) == 0
1143 || strucmp(".LZS", p) == 0
1144 || strucmp(".COM", p) == 0 /* DOS SFX */
1145 || strucmp(".EXE", p) == 0
1146 || strucmp(".X", p) == 0 /* HUMAN SFX */
1147 || strucmp(".BAK", p) == 0) { /* for BackUp */
1148 open_old_archive_1(archive_name, &fp);
1153 if (open_old_archive_1(archive_name, &fp))
1155 xsnprintf(expanded_archive_name, sizeof(expanded_archive_name),
1156 "%s.lzh", archive_name);
1157 if (open_old_archive_1(expanded_archive_name, &fp)) {
1158 archive_name = expanded_archive_name;
1162 * if ( (errno&0xffff)!=E_PNNF ) { archive_name =
1163 * expanded_archive_name; return NULL; }
1165 xsnprintf(expanded_archive_name, sizeof(expanded_archive_name),
1166 "%s.lzs", archive_name);
1167 if (open_old_archive_1(expanded_archive_name, &fp)) {
1168 archive_name = expanded_archive_name;
1172 * if ( (errno&0xffff)!=E_PNNF ) { archive_name =
1173 * expanded_archive_name; return NULL; }
1176 * sprintf( expanded_archive_name , "%s.lzh",archive_name);
1177 * archive_name = expanded_archive_name;
1182 /* ------------------------------------------------------------------------ */
1184 inquire(msg, name, selective)
1185 char *msg, *name, *selective;
1191 fprintf(stderr, "%s %s ", name, msg);
1194 fgets(buffer, 1024, stdin);
1196 for (p = selective; *p; p++)
1197 if (buffer[0] == *p)
1198 return p - selective;
1203 /* ------------------------------------------------------------------------ */
1205 write_archive_tail(nafp)
1211 /* ------------------------------------------------------------------------ */
1213 copy_old_one(oafp, nafp, hdr)
1218 fseek(oafp, hdr->header_size + hdr->packed_size, SEEK_CUR);
1221 reading_filename = archive_name;
1222 writing_filename = temporary_name;
1223 copyfile(oafp, nafp, hdr->header_size + hdr->packed_size, 0, 0);