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 /* ------------------------------------------------------------------------ */
47 static int cmd = CMD_UNKNOWN;
48 static int error_occurred;
50 /* static functions */
51 static void sort_files();
52 static void print_version();
54 extern int optional_archive_kanji_code;
55 extern int optional_system_kanji_code;
57 /* ------------------------------------------------------------------------ */
59 init_variable() /* Added N.Watazaki */
65 noexec = FALSE; /* debugging option */
68 compress_method = DEFAULT_LZHUFF_METHOD; /* defined in config.h */
70 header_level = 2; /* level 2 */
77 /* view command flags */
78 verbose_listing = FALSE;
80 /* extract command flags */
81 output_to_stdout = FALSE;
83 /* append command flags */
85 update_if_newer = FALSE;
86 delete_after_append = FALSE;
87 generic_format = FALSE;
89 remove_temporary_at_error = FALSE;
90 recover_archive_when_interrupt = FALSE;
91 remove_extracting_file_when_interrupt = FALSE;
92 get_filename_from_stdin = FALSE;
93 ignore_directory = FALSE;
97 noconvertcase = FALSE;
99 extract_directory = NULL;
102 #if BACKUP_OLD_ARCHIVE
103 backup_old_archive = TRUE;
105 backup_old_archive = FALSE;
109 /* ------------------------------------------------------------------------ */
110 /* NOTES : Text File Format */
111 /* GENERATOR NewLine */
112 /* [generic] 0D 0A */
114 /* [OS9][MacOS] 0D */
116 /* ------------------------------------------------------------------------ */
121 LHarc for UNIX V 1.02 Copyright(C) 1989 Y.Tagawa\n\
122 LHx for MSDOS V C2.01 Copyright(C) 1990 H.Yoshizaki\n\
123 LHx(arc) for OSK V 2.01 Modified 1990 Momozou\n\
124 LHa for UNIX V 1.00 Copyright(C) 1992 Masaru Oki\n\
125 LHa for UNIX V 1.14 Modified 1995 Nobutaka Watazaki\n\
126 LHa for UNIX V 1.14i Modified 2000 Tsugio Okamoto\n\
127 Autoconfiscated 2001-2003 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\
166 #if IGNORE_DOT_FILES /* experimental feature */
168 X ignore dot files (a/u/c)\n\
174 parse_option(int argc, char **argv)
179 argv++; argc--; /* exclude command name */
186 if (strcmp(*argv, "--help") == 0) {
190 if (strcmp(*argv, "--version") == 0) {
196 archive_name = *argv++; argc--;
203 opt = *argv++; argc--;
216 output_to_stdout = TRUE;
234 update_if_newer = TRUE;
239 delete_after_append = TRUE;
244 verbose_listing = TRUE;
270 case '0': /* no quiet */
271 case '1': /* no use the incremental indicator */
272 quiet_mode = *p - '0';
275 case '2': /* no output */
279 /* In quiet mode, no confirm to overwrite */
304 generic_format = TRUE;
305 noconvertcase = TRUE;
309 delete_after_append = TRUE;
314 compress_method = LZHUFF1_METHOD_NUM;
318 compress_method = LZHUFF5_METHOD_NUM;
323 compress_method = LZHUFF6_METHOD_NUM;
327 compress_method = LZHUFF7_METHOD_NUM;
332 error("invalid compression method 'o%c'", *p);
337 compress_method = LZHUFF0_METHOD_NUM; /* Changed N.Watazaki */
340 ignore_directory = TRUE;
346 for (i = 0; exclude_files && exclude_files[i]; i++)
348 exclude_files = (char**)xrealloc(exclude_files,
349 sizeof(char*) * (i+2));
356 exclude_files[i] = *argv++; argc--;
357 exclude_files[i+1] = 0;
361 exclude_files[i] = p;
362 exclude_files[i+1] = 0;
366 #if IGNORE_DOT_FILES /* experimental feature */
368 for (i = 0; exclude_files && exclude_files[i]; i++)
370 exclude_files = (char**)xrealloc(exclude_files,
371 sizeof(char*) * (i+2));
373 exclude_files[i] = xstrdup(".*");
374 exclude_files[i+1] = 0;
385 extract_directory = *argv++; argc--;
389 extract_directory = p;
403 error("Unknown option '%c'.", p[-1]);
410 if (!opt || opt[0] != '-')
413 /* special archive name */
414 if (strcmp(opt, "-") == 0)
417 /* GNU style long options */
418 if (opt[0] == '-' && opt[1] == '-') {
421 if (strncmp(opt, "system-kanji-code=",
422 sizeof("system-kanji-code=")-1) == 0) {
423 opt += sizeof("system-kanji-code=")-1;
424 if (strcmp(opt, "euc") == 0) {
425 optional_system_kanji_code = CODE_EUC;
427 else if (strcmp(opt, "sjis") == 0) {
428 optional_system_kanji_code = CODE_SJIS;
430 else if (strcmp(opt, "utf8") == 0) {
431 optional_system_kanji_code = CODE_UTF8;
433 else if (strcmp(opt, "cap") == 0) {
434 optional_system_kanji_code = CODE_CAP;
441 else if (strncmp(opt, "archive-kanji-code=",
442 sizeof("archive-kanji-code=")-1) == 0) {
443 opt += sizeof("archive-kanji-code=")-1;
444 if (strcmp(opt, "euc") == 0) {
445 optional_archive_kanji_code = CODE_EUC;
447 else if (strcmp(opt, "sjis") == 0) {
448 optional_archive_kanji_code = CODE_SJIS;
450 else if (strcmp(opt, "utf8") == 0) {
451 optional_archive_kanji_code = CODE_UTF8;
453 else if (strcmp(opt, "cap") == 0) {
454 optional_archive_kanji_code = CODE_CAP;
468 archive_name = *argv++; argc--;
474 /* ------------------------------------------------------------------------ */
484 init_variable(); /* Added N.Watazaki */
486 parse_option(argc, argv);
493 if (!strcmp(archive_name, "-")) {
494 if (!isatty(1) && cmd == CMD_ADD)
497 #if 0 /* Comment out; IMHO, this feature is useless. by Koji Arai */
499 if (argc == 3 && !isatty(0)) { /* 1999.7.18 */
500 /* Bug(?) on MinGW, isatty() return 0 on Cygwin console.
501 mingw-runtime-1.3-2 and Cygwin 1.3.10(0.51/3/2) on Win2000 */
502 get_filename_from_stdin = TRUE;
507 /* target file name */
508 if (get_filename_from_stdin) {
514 xfilev = (char **)xmalloc(sizeof(char *) * xfilec);
515 while (fgets(inpbuf, sizeof(inpbuf), stdin)) {
516 /* delete \n if it exist */
518 while (i < sizeof(inpbuf) && p != 0) {
526 if (cmd_filec >= xfilec) {
528 xfilev = (char **) xrealloc(xfilev,
529 sizeof(char *) * xfilec);
531 if (strlen(inpbuf) < 1)
533 xfilev[cmd_filec++] = xstrdup(inpbuf);
535 xfilev[cmd_filec] = NULL;
565 /* ------------------------------------------------------------------------ */
569 /* macro PACKAGE_NAME, PACKAGE_VERSION and PLATFORM are
570 defined in config.h by configure script */
571 fprintf(stderr, "%s version %s (%s)\n",
572 PACKAGE_NAME, PACKAGE_VERSION, PLATFORM);
577 message(char *fmt, ...)
579 message(fmt, va_alist)
584 int errno_sv = errno;
587 fprintf(stderr, "LHa: ");
590 vfprintf(stderr, fmt, v);
598 /* ------------------------------------------------------------------------ */
601 warning(char *fmt, ...)
603 warning(fmt, va_alist)
608 int errno_sv = errno;
611 fprintf(stderr, "LHa: Warning: ");
614 vfprintf(stderr, fmt, v);
622 /* ------------------------------------------------------------------------ */
625 error(char *fmt, ...)
632 int errno_sv = errno;
635 fprintf(stderr, "LHa: Error: ");
638 vfprintf(stderr, fmt, v);
650 fatal_error(char *fmt, ...)
652 fatal_error(fmt, va_alist)
657 int errno_sv = errno;
660 fprintf(stderr, "LHa: Fatal error: ");
663 vfprintf(stderr, fmt, v);
667 fprintf(stderr, ": %s\n", strerror(errno_sv));
671 if (remove_temporary_at_error) {
672 if (temporary_fd != -1)
674 unlink(temporary_name);
680 /* ------------------------------------------------------------------------ */
685 message("Interrupted");
687 if (temporary_fd != -1)
689 unlink(temporary_name);
690 if (recover_archive_when_interrupt)
691 rename(backup_archive_name, archive_name);
692 if (remove_extracting_file_when_interrupt) {
693 message("Removing: %s", writing_filename);
694 unlink(writing_filename);
696 signal(SIGINT, SIG_DFL);
698 signal(SIGHUP, SIG_DFL);
700 kill(getpid(), signo);
703 /* ------------------------------------------------------------------------ */
705 /* ------------------------------------------------------------------------ */
710 register char *p, *q;
714 if (generic_format) {
716 c1 = *(unsigned char *) p++;
717 c2 = *(unsigned char *) q++;
729 while (*p == *q && *p != '\0')
731 return *(unsigned char *) p - *(unsigned char *) q;
735 /* ------------------------------------------------------------------------ */
740 qsort(cmd_filev, cmd_filec, sizeof(char *), sort_by_ascii);
743 /* ------------------------------------------------------------------------ */
748 void *p = malloc(size);
750 fatal_error("Not enough memory");
754 /* ------------------------------------------------------------------------ */
760 void *p = (char *) realloc(old, size);
762 fatal_error("Not enough memory");
770 int len = strlen(str);
771 char *p = (char *)xmalloc(len + 1);
776 /* ------------------------------------------------------------------------ */
778 /* ------------------------------------------------------------------------ */
781 +-------------+-------------+------+-------------+----------+
782 | N A M E 1 \0| N A M E 2 \0| .... | N A M E n \0| |
783 +-------------+-------------+------+-------------+----------+
784 ^ ^ ^ buffer+0 buffer+used buffer+size
787 +---------------+---------------+------------- -----------------+
788 | pointer to | pointer to | pointer to ... pointer to |
789 | stringpool | N A M E 1 | N A M E 2 ... N A M E n |
790 +---------------+---------------+------------- -------------+
791 ^ malloc base returned
794 /* ------------------------------------------------------------------------ */
797 struct string_pool *sp;
799 sp->size = 1024 - 8; /* any ( >=0 ) */
802 sp->buffer = (char *) xmalloc(sp->size * sizeof(char));
805 /* ------------------------------------------------------------------------ */
807 add_sp(sp, name, len)
808 struct string_pool *sp;
809 char *name; /* stored '\0' at tail */
810 int len; /* include '\0' */
812 while (sp->used + len > sp->size) {
814 sp->buffer = (char *) xrealloc(sp->buffer, sp->size * sizeof(char));
816 memmove(sp->buffer + sp->used, name, len);
821 /* ------------------------------------------------------------------------ */
823 finish_sp(sp, v_count, v_vector)
824 register struct string_pool *sp;
832 v = (char **) xmalloc((sp->n + 1) * sizeof(char *));
837 for (i = sp->n; i; i--) {
844 /* ------------------------------------------------------------------------ */
850 free(*vector); /* free string pool */
855 /* ------------------------------------------------------------------------ */
856 /* READ DIRECTORY FILES */
857 /* ------------------------------------------------------------------------ */
859 include_path_p(path, name)
865 return (path[-1] == '/' && *n == '\0');
866 return (*n == '/' || (n != name && path[-1] == '/' && n[-1] == '/'));
869 /* ------------------------------------------------------------------------ */
871 cleaning_files(v_filec, v_filev)
878 register char **filev = *v_filev;
879 register int filec = *v_filec;
886 flags = xmalloc(filec * sizeof(char));
888 /* flags & 0x01 : 1: ignore */
889 /* flags & 0x02 : 1: directory, 0 : regular file */
890 /* flags & 0x04 : 1: need delete */
892 for (i = 0; i < filec; i++)
893 if (GETSTAT(filev[i], &stbuf) < 0) {
895 warning("Cannot access \"%s\" : %s; ignored.", filev[i],
899 if (is_regularfile(&stbuf))
901 else if (is_directory(&stbuf))
904 else if (is_symlink(&stbuf)) /* t.okamoto */
909 warning("Cannot archive \"%s\", ignored.", filev[i]);
913 for (i = 0; i < filec; i++) {
915 if ((flags[i] & 0x07) == 0x00) { /* regular file, not
917 for (j = i + 1; j < filec; j++) {
918 if ((flags[j] & 0x07) == 0x00) { /* regular file, not
920 if (STREQU(p, filev[j]))
921 flags[j] = 0x04; /* delete */
925 else if ((flags[i] & 0x07) == 0x02) { /* directory, not
927 for (j = i + 1; j < filec; j++) {
928 if ((flags[j] & 0x07) == 0x00) { /* regular file, not
930 if (include_path_p(p, filev[j]))
931 flags[j] = 0x04; /* delete */
933 else if ((flags[j] & 0x07) == 0x02) { /* directory, not
935 if (include_path_p(p, filev[j]))
936 flags[j] = 0x04; /* delete */
942 for (i = j = 0; i < filec; i++) {
943 if ((flags[i] & 0x04) == 0) {
954 /* ------------------------------------------------------------------------ */
956 find_files(name, v_filec, v_filev)
961 struct string_pool sp;
962 char newname[FILENAME_LENGTH];
966 struct stat tmp_stbuf, arc_stbuf, fil_stbuf;
967 int exist_tmp = 1, exist_arc = 1;
969 strcpy(newname, name);
971 if (len > 0 && newname[len - 1] != '/') {
972 newname[len++] = '/';
976 dirp = opendir(name);
982 if (GETSTAT(temporary_name, &tmp_stbuf) == -1)
984 if (GETSTAT(archive_name, &arc_stbuf) == -1)
987 while ((dp = readdir(dirp)) != NULL) {
990 /* exclude '.' and '..' */
991 if (strncmp(dp->d_name, ".", n) == 0
992 || strncmp(dp->d_name, "..", n) == 0)
995 /* exclude exclude_files supplied by user */
996 for (i = 0; exclude_files && exclude_files[i]; i++) {
997 if (fnmatch(exclude_files[i], dp->d_name,
998 FNM_PATHNAME|FNM_NOESCAPE|FNM_PERIOD) == 0)
1002 if (len + n >= sizeof(newname)) {
1003 warning("filename is too long");
1007 strncpy(newname + len, dp->d_name, n);
1008 newname[len + n] = '\0';
1009 if (GETSTAT(newname, &fil_stbuf) < 0)
1012 #if defined(HAVE_STRUCT_STAT_ST_INO) && !__MINGW32__
1013 /* MinGW has meaningless st_ino */
1015 /* exclude temporary file, archive file and these links */
1017 tmp_stbuf.st_dev == fil_stbuf.st_dev &&
1018 tmp_stbuf.st_ino == fil_stbuf.st_ino)
1022 arc_stbuf.st_dev == fil_stbuf.st_dev &&
1023 arc_stbuf.st_ino == fil_stbuf.st_ino)
1026 add_sp(&sp, newname, len+n+1);
1031 finish_sp(&sp, v_filec, v_filev);
1033 qsort(*v_filev, *v_filec, sizeof(char *), sort_by_ascii);
1034 cleaning_files(v_filec, v_filev);
1039 /* ------------------------------------------------------------------------ */
1041 free_files(filec, filev)
1048 /* ------------------------------------------------------------------------ */
1050 /* ------------------------------------------------------------------------ */
1051 /* Build temporary file name and store to TEMPORARY_NAME */
1053 build_temporary_name()
1055 #ifdef TMP_FILENAME_TEMPLATE
1056 /* "/tmp/lhXXXXXX" etc. */
1057 if (extract_directory == NULL) {
1058 strcpy(temporary_name, TMP_FILENAME_TEMPLATE);
1061 xsnprintf(temporary_name, sizeof(temporary_name),
1062 "%s/lhXXXXXX", extract_directory);
1067 strcpy(temporary_name, archive_name);
1068 for (p = temporary_name, s = (char *) 0; *p; p++)
1071 strcpy((s ? s + 1 : temporary_name), "lhXXXXXX");
1077 old_umask = umask(077);
1078 fd = mkstemp(temporary_name);
1086 mktemp(temporary_name);
1087 flags = O_CREAT|O_EXCL|O_RDWR;
1091 return open(temporary_name, flags, 0600);
1096 /* ------------------------------------------------------------------------ */
1098 modify_filename_extention(buffer, ext)
1102 register char *p, *dot;
1104 for (p = buffer, dot = (char *) 0; *p; p++) {
1117 /* ------------------------------------------------------------------------ */
1118 /* build backup file name */
1120 build_backup_name(buffer, original)
1124 strcpy(buffer, original);
1125 modify_filename_extention(buffer, BACKUPNAME_EXTENTION); /* ".bak" */
1128 /* ------------------------------------------------------------------------ */
1130 build_standard_archive_name(buffer, orginal)
1134 strcpy(buffer, orginal);
1135 modify_filename_extention(buffer, ARCHIVENAME_EXTENTION); /* ".lzh" */
1138 /* ------------------------------------------------------------------------ */
1140 /* ------------------------------------------------------------------------ */
1150 for (i = 0; i < cmd_filec; i++) {
1151 if (patmatch(cmd_filev[i], name, 0))
1164 if ((fp = fopen(name, mode)) == NULL)
1165 fatal_error("Cannot open file \"%s\"", name);
1170 /* ------------------------------------------------------------------------ */
1172 /* ------------------------------------------------------------------------ */
1174 open_old_archive_1(name, v_fp)
1181 if (stat(name, &stbuf) >= 0 &&
1182 is_regularfile(&stbuf) &&
1183 (fp = fopen(name, READ_BINARY)) != NULL) {
1185 archive_file_gid = stbuf.st_gid;
1186 archive_file_mode = stbuf.st_mode;
1191 archive_file_gid = -1;
1195 /* ------------------------------------------------------------------------ */
1201 static char expanded_archive_name[FILENAME_LENGTH];
1203 if (!strcmp(archive_name, "-")) {
1204 if (cmd == CMD_EXTRACT || cmd == CMD_LIST) {
1206 setmode(fileno(stdin), O_BINARY);
1213 p = strrchr(archive_name, '.');
1215 if (strcasecmp(".LZH", p) == 0
1216 || strcasecmp(".LZS", p) == 0
1217 || strcasecmp(".COM", p) == 0 /* DOS SFX */
1218 || strcasecmp(".EXE", p) == 0
1219 || strcasecmp(".X", p) == 0 /* HUMAN SFX */
1220 || strcasecmp(".BAK", p) == 0) { /* for BackUp */
1221 open_old_archive_1(archive_name, &fp);
1226 if (open_old_archive_1(archive_name, &fp))
1228 xsnprintf(expanded_archive_name, sizeof(expanded_archive_name),
1229 "%s.lzh", archive_name);
1230 if (open_old_archive_1(expanded_archive_name, &fp)) {
1231 archive_name = expanded_archive_name;
1235 * if ( (errno&0xffff)!=E_PNNF ) { archive_name =
1236 * expanded_archive_name; return NULL; }
1238 xsnprintf(expanded_archive_name, sizeof(expanded_archive_name),
1239 "%s.lzs", archive_name);
1240 if (open_old_archive_1(expanded_archive_name, &fp)) {
1241 archive_name = expanded_archive_name;
1245 * if ( (errno&0xffff)!=E_PNNF ) { archive_name =
1246 * expanded_archive_name; return NULL; }
1249 * sprintf( expanded_archive_name , "%s.lzh",archive_name);
1250 * archive_name = expanded_archive_name;
1255 /* ------------------------------------------------------------------------ */
1257 inquire(msg, name, selective)
1258 char *msg, *name, *selective;
1264 fprintf(stderr, "%s %s ", name, msg);
1267 fgets(buffer, 1024, stdin);
1269 for (p = selective; *p; p++)
1270 if (buffer[0] == *p)
1271 return p - selective;
1276 /* ------------------------------------------------------------------------ */
1278 write_archive_tail(nafp)
1284 /* ------------------------------------------------------------------------ */
1286 copy_old_one(oafp, nafp, hdr)
1291 fseeko(oafp, hdr->header_size + hdr->packed_size, SEEK_CUR);
1294 reading_filename = archive_name;
1295 writing_filename = temporary_name;
1296 copyfile(oafp, nafp, hdr->header_size + hdr->packed_size, 0, 0);