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 /* ------------------------------------------------------------------------ */
56 init_variable() /* Added N.Watazaki */
62 noexec = FALSE; /* debugging option */
66 compress_method = DEFAULT_LZHUFF_METHOD; /* defined in config.h */
68 header_level = 2; /* level 2 */
75 /* view command flags */
76 verbose_listing = FALSE;
78 /* extract command flags */
79 output_to_stdout = FALSE;
81 /* append command flags */
83 update_if_newer = FALSE;
84 delete_after_append = FALSE;
85 generic_format = FALSE;
87 remove_temporary_at_error = FALSE;
88 recover_archive_when_interrupt = FALSE;
89 remove_extracting_file_when_interrupt = FALSE;
90 get_filename_from_stdin = FALSE;
91 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 Autoconfiscated 2001-2003 Koji Arai\n\
128 usage: lha [-]<commands>[<options>] [-<options> ...] archive_file [file...]\n\
129 commands: [axelvudmcpt]\n\
130 options: [q[012]vnfto[567]dizg012e[w=<dir>|x=<pattern>]]\n\
131 commands: options:\n\
132 a Add(or replace) to archive q{num} quiet (num:quiet mode)\n\
133 x,e EXtract from archive v verbose\n\
134 l,v List / Verbose List n not execute\n\
135 u Update newer files to archive f force (over write at extract)\n\
136 d Delete from archive t FILES are TEXT file\n");
139 m Move to archive (means 'ad') o[567] compression method (a/u/c)\n\
144 m Move to archive (means 'ad') o use LHarc compatible method (a/u/c)\n\
148 c re-Construct new archive d delete FILES after (a/u/c)\n\
149 p Print to STDOUT from archive i ignore directory path (x/e)\n\
150 t Test file CRC in archive z files not compress (a/u/c)\n\
151 g Generic format (for compatibility)\n\
152 or not convert case when extracting\n\
153 0/1/2 header level (a/u/c)\n\
157 e TEXT code convert from/to EUC\n\
161 w=<dir> specify extract directory (x/e)\n\
162 x=<pattern> eXclude files (a/u/c)\n\
164 #if IGNORE_DOT_FILES /* experimental feature */
166 X ignore dot files (a/u/c)\n\
172 parse_option(int argc, char **argv)
177 argv++; argc--; /* exclude command name */
184 if (strcmp(*argv, "--help") == 0) {
188 if (strcmp(*argv, "--version") == 0) {
194 archive_name = *argv++; argc--;
201 opt = *argv++; argc--;
214 output_to_stdout = TRUE;
232 update_if_newer = TRUE;
237 delete_after_append = TRUE;
242 verbose_listing = TRUE;
268 case '0': /* no quiet */
269 case '1': /* no use the incremental indicator */
270 quiet_mode = *p - '0';
273 case '2': /* no output */
277 /* In quiet mode, no confirm to overwrite */
305 generic_format = TRUE;
306 noconvertcase = TRUE;
310 delete_after_append = TRUE;
315 compress_method = LZHUFF1_METHOD_NUM;
319 compress_method = LZHUFF5_METHOD_NUM;
324 compress_method = LZHUFF6_METHOD_NUM;
328 compress_method = LZHUFF7_METHOD_NUM;
333 error("invalid compression method 'o%c'", *p);
338 compress_method = LZHUFF0_METHOD_NUM; /* Changed N.Watazaki */
341 ignore_directory = TRUE;
347 for (i = 0; exclude_files && exclude_files[i]; i++)
349 exclude_files = (char**)xrealloc(exclude_files,
350 sizeof(char*) * (i+2));
357 exclude_files[i] = *argv++; argc--;
358 exclude_files[i+1] = 0;
362 exclude_files[i] = p;
363 exclude_files[i+1] = 0;
367 #if IGNORE_DOT_FILES /* experimental feature */
369 for (i = 0; exclude_files && exclude_files[i]; i++)
371 exclude_files = (char**)xrealloc(exclude_files,
372 sizeof(char*) * (i+2));
374 exclude_files[i] = xstrdup(".*");
375 exclude_files[i+1] = 0;
386 extract_directory = *argv++; argc--;
390 extract_directory = p;
404 error("Unknown option '%c'.", p[-1]);
411 if (!opt || opt[0] != '-')
414 /* special archive name */
415 if (strcmp(opt, "-") == 0)
421 archive_name = *argv++; argc--;
427 /* ------------------------------------------------------------------------ */
437 init_variable(); /* Added N.Watazaki */
439 parse_option(argc, argv);
446 if (!strcmp(archive_name, "-")) {
447 if (!isatty(1) && cmd == CMD_ADD)
450 #if 0 /* Comment out; IMHO, this feature is useless. by Koji Arai */
452 if (argc == 3 && !isatty(0)) { /* 1999.7.18 */
453 /* Bug(?) on MinGW, isatty() return 0 on Cygwin console.
454 mingw-runtime-1.3-2 and Cygwin 1.3.10(0.51/3/2) on Win2000 */
455 get_filename_from_stdin = TRUE;
460 /* target file name */
461 if (get_filename_from_stdin) {
467 xfilev = (char **)xmalloc(sizeof(char *) * xfilec);
468 while (fgets(inpbuf, sizeof(inpbuf), stdin)) {
469 /* delete \n if it exist */
471 while (i < sizeof(inpbuf) && p != 0) {
479 if (cmd_filec >= xfilec) {
481 xfilev = (char **) xrealloc(xfilev,
482 sizeof(char *) * xfilec);
484 if (strlen(inpbuf) < 1)
486 xfilev[cmd_filec++] = xstrdup(inpbuf);
488 xfilev[cmd_filec] = NULL;
523 /* ------------------------------------------------------------------------ */
525 /* ------------------------------------------------------------------------ */
527 /* ------------------------------------------------------------------------ */
531 /* macro PACKAGE_NAME, PACKAGE_VERSION and PLATFORM are
532 defined in config.h by configure script */
533 fprintf(stderr, "%s version %s (%s)\n",
534 PACKAGE_NAME, PACKAGE_VERSION, PLATFORM);
539 message(char *fmt, ...)
541 message(fmt, va_alist)
546 int errno_sv = errno;
549 fprintf(stderr, "LHa: ");
552 vfprintf(stderr, fmt, v);
560 /* ------------------------------------------------------------------------ */
563 warning(char *fmt, ...)
565 warning(fmt, va_alist)
570 int errno_sv = errno;
573 fprintf(stderr, "LHa: Warning: ");
576 vfprintf(stderr, fmt, v);
584 /* ------------------------------------------------------------------------ */
587 error(char *fmt, ...)
594 int errno_sv = errno;
597 fprintf(stderr, "LHa: Error: ");
600 vfprintf(stderr, fmt, v);
612 fatal_error(char *fmt, ...)
614 fatal_error(fmt, va_alist)
619 int errno_sv = errno;
622 fprintf(stderr, "LHa: Fatal error: ");
625 vfprintf(stderr, fmt, v);
629 fprintf(stderr, ": %s\n", strerror(errno_sv));
633 if (remove_temporary_at_error) {
634 if (temporary_fd != -1)
636 unlink(temporary_name);
642 /* ------------------------------------------------------------------------ */
647 message("Interrupted");
649 if (temporary_fd != -1)
651 unlink(temporary_name);
652 if (recover_archive_when_interrupt)
653 rename(backup_archive_name, archive_name);
654 if (remove_extracting_file_when_interrupt) {
655 message("Removing: %s", writing_filename);
656 unlink(writing_filename);
658 signal(SIGINT, SIG_DFL);
660 signal(SIGHUP, SIG_DFL);
662 kill(getpid(), signo);
665 /* ------------------------------------------------------------------------ */
667 /* ------------------------------------------------------------------------ */
672 register char *p, *q;
676 if (generic_format) {
678 c1 = *(unsigned char *) p++;
679 c2 = *(unsigned char *) q++;
691 while (*p == *q && *p != '\0')
693 return *(unsigned char *) p - *(unsigned char *) q;
697 /* ------------------------------------------------------------------------ */
702 qsort(cmd_filev, cmd_filec, sizeof(char *), sort_by_ascii);
705 /* ------------------------------------------------------------------------ */
710 void *p = malloc(size);
712 fatal_error("Not enough memory");
716 /* ------------------------------------------------------------------------ */
722 void *p = (char *) realloc(old, size);
724 fatal_error("Not enough memory");
732 int len = strlen(str);
733 char *p = (char *)xmalloc(len + 1);
738 /* ------------------------------------------------------------------------ */
740 /* ------------------------------------------------------------------------ */
743 +-------------+-------------+------+-------------+----------+
744 | N A M E 1 \0| N A M E 2 \0| .... | N A M E n \0| |
745 +-------------+-------------+------+-------------+----------+
746 ^ ^ ^ buffer+0 buffer+used buffer+size
749 +---------------+---------------+------------- -----------------+
750 | pointer to | pointer to | pointer to ... pointer to |
751 | stringpool | N A M E 1 | N A M E 2 ... N A M E n |
752 +---------------+---------------+------------- -------------+
753 ^ malloc base returned
756 /* ------------------------------------------------------------------------ */
759 struct string_pool *sp;
761 sp->size = 1024 - 8; /* any ( >=0 ) */
764 sp->buffer = (char *) xmalloc(sp->size * sizeof(char));
767 /* ------------------------------------------------------------------------ */
769 add_sp(sp, name, len)
770 struct string_pool *sp;
771 char *name; /* stored '\0' at tail */
772 int len; /* include '\0' */
774 while (sp->used + len > sp->size) {
776 sp->buffer = (char *) xrealloc(sp->buffer, sp->size * sizeof(char));
778 memmove(sp->buffer + sp->used, name, len);
783 /* ------------------------------------------------------------------------ */
785 finish_sp(sp, v_count, v_vector)
786 register struct string_pool *sp;
794 v = (char **) xmalloc((sp->n + 1) * sizeof(char *));
799 for (i = sp->n; i; i--) {
806 /* ------------------------------------------------------------------------ */
812 free(*vector); /* free string pool */
817 /* ------------------------------------------------------------------------ */
818 /* READ DIRECTORY FILES */
819 /* ------------------------------------------------------------------------ */
821 include_path_p(path, name)
827 return (path[-1] == '/' && *n == '\0');
828 return (*n == '/' || (n != name && path[-1] == '/' && n[-1] == '/'));
831 /* ------------------------------------------------------------------------ */
833 cleaning_files(v_filec, v_filev)
840 register char **filev = *v_filev;
841 register int filec = *v_filec;
848 flags = xmalloc(filec * sizeof(char));
850 /* flags & 0x01 : 1: ignore */
851 /* flags & 0x02 : 1: directory, 0 : regular file */
852 /* flags & 0x04 : 1: need delete */
855 for (i = 0; i < filec; i++)
856 if (GETSTAT(filev[i], &stbuf) < 0) {
858 warning("Cannot access \"%s\", ignored.", filev[i]);
861 if (is_regularfile(&stbuf))
863 else if (is_directory(&stbuf))
866 else if (is_symlink(&stbuf)) /* t.okamoto */
871 warning("Cannot archive \"%s\", ignored.", filev[i]);
875 for (i = 0; i < filec; i++) {
877 if ((flags[i] & 0x07) == 0x00) { /* regular file, not
879 for (j = i + 1; j < filec; j++) {
880 if ((flags[j] & 0x07) == 0x00) { /* regular file, not
882 if (STREQU(p, filev[j]))
883 flags[j] = 0x04; /* delete */
887 else if ((flags[i] & 0x07) == 0x02) { /* directory, not
889 for (j = i + 1; j < filec; j++) {
890 if ((flags[j] & 0x07) == 0x00) { /* regular file, not
892 if (include_path_p(p, filev[j]))
893 flags[j] = 0x04; /* delete */
895 else if ((flags[j] & 0x07) == 0x02) { /* directory, not
897 if (include_path_p(p, filev[j]))
898 flags[j] = 0x04; /* delete */
904 for (i = j = 0; i < filec; i++) {
905 if ((flags[i] & 0x04) == 0) {
916 /* ------------------------------------------------------------------------ */
918 find_files(name, v_filec, v_filev)
923 struct string_pool sp;
924 char newname[FILENAME_LENGTH];
928 struct stat tmp_stbuf, arc_stbuf, fil_stbuf;
930 strcpy(newname, name);
932 if (len > 0 && newname[len - 1] != '/')
933 newname[len++] = '/';
935 dirp = opendir(name);
941 GETSTAT(temporary_name, &tmp_stbuf);
942 GETSTAT(archive_name, &arc_stbuf);
944 for (dp = readdir(dirp); dp != NULL; dp = readdir(dirp)) {
945 for (i = 0; exclude_files && exclude_files[i]; i++) {
946 if (fnmatch(exclude_files[i], dp->d_name,
947 FNM_PATHNAME|FNM_NOESCAPE|FNM_PERIOD) == 0)
952 strncpy(newname + len, dp->d_name, n);
953 newname[len + n] = '\0';
954 if (GETSTAT(newname, &fil_stbuf) < 0)
956 #if !defined(HAVE_STRUCT_STAT_ST_INO) || __MINGW32__
957 if ( dp->d_name[0] != '.' ||
959 (dp->d_name[1] != '.' ||
961 add_sp(&sp, newname, len+n+1);
964 if ((dp->d_ino != 0) &&
965 /* exclude '.' and '..' */
966 ((dp->d_name[0] != '.') ||
968 ((dp->d_name[1] != '.') ||
970 ((tmp_stbuf.st_dev != fil_stbuf.st_dev ||
971 tmp_stbuf.st_ino != fil_stbuf.st_ino) &&
972 (arc_stbuf.st_dev != fil_stbuf.st_dev ||
973 arc_stbuf.st_ino != fil_stbuf.st_ino))) {
974 add_sp(&sp, newname, len + n + 1);
981 finish_sp(&sp, v_filec, v_filev);
983 qsort(*v_filev, *v_filec, sizeof(char *), sort_by_ascii);
984 cleaning_files(v_filec, v_filev);
989 /* ------------------------------------------------------------------------ */
991 free_files(filec, filev)
998 /* ------------------------------------------------------------------------ */
1000 /* ------------------------------------------------------------------------ */
1001 /* Build temporary file name and store to TEMPORARY_NAME */
1003 build_temporary_name()
1005 #ifdef TMP_FILENAME_TEMPLATE
1006 /* "/tmp/lhXXXXXX" etc. */
1007 if (extract_directory == NULL) {
1008 strcpy(temporary_name, TMP_FILENAME_TEMPLATE);
1011 xsnprintf(temporary_name, sizeof(temporary_name),
1012 "%s/lhXXXXXX", extract_directory);
1017 strcpy(temporary_name, archive_name);
1018 for (p = temporary_name, s = (char *) 0; *p; p++)
1021 strcpy((s ? s + 1 : temporary_name), "lhXXXXXX");
1027 old_umask = umask(077);
1028 fd = mkstemp(temporary_name);
1036 mktemp(temporary_name);
1037 flags = O_CREAT|O_EXCL|O_RDWR;
1041 return open(temporary_name, flags, 0600);
1046 /* ------------------------------------------------------------------------ */
1048 modify_filename_extention(buffer, ext)
1052 register char *p, *dot;
1054 for (p = buffer, dot = (char *) 0; *p; p++) {
1067 /* ------------------------------------------------------------------------ */
1068 /* build backup file name */
1070 build_backup_name(buffer, original)
1074 strcpy(buffer, original);
1075 modify_filename_extention(buffer, BACKUPNAME_EXTENTION); /* ".bak" */
1078 /* ------------------------------------------------------------------------ */
1080 build_standard_archive_name(buffer, orginal)
1084 strcpy(buffer, orginal);
1085 modify_filename_extention(buffer, ARCHIVENAME_EXTENTION); /* ".lzh" */
1088 /* ------------------------------------------------------------------------ */
1090 /* ------------------------------------------------------------------------ */
1100 for (i = 0; i < cmd_filec; i++) {
1101 if (patmatch(cmd_filev[i], name, 0))
1114 if ((fp = fopen(name, mode)) == NULL)
1115 fatal_error("Cannot open file \"%s\"", name);
1120 /* ------------------------------------------------------------------------ */
1122 /* ------------------------------------------------------------------------ */
1124 open_old_archive_1(name, v_fp)
1131 if (stat(name, &stbuf) >= 0 &&
1132 is_regularfile(&stbuf) &&
1133 (fp = fopen(name, READ_BINARY)) != NULL) {
1135 archive_file_gid = stbuf.st_gid;
1136 archive_file_mode = stbuf.st_mode;
1141 archive_file_gid = -1;
1145 /* ------------------------------------------------------------------------ */
1151 static char expanded_archive_name[FILENAME_LENGTH];
1153 if (!strcmp(archive_name, "-")) {
1154 if (cmd == CMD_EXTRACT || cmd == CMD_LIST) {
1156 setmode(fileno(stdin), O_BINARY);
1163 p = strrchr(archive_name, '.');
1165 if (strcasecmp(".LZH", p) == 0
1166 || strcasecmp(".LZS", p) == 0
1167 || strcasecmp(".COM", p) == 0 /* DOS SFX */
1168 || strcasecmp(".EXE", p) == 0
1169 || strcasecmp(".X", p) == 0 /* HUMAN SFX */
1170 || strcasecmp(".BAK", p) == 0) { /* for BackUp */
1171 open_old_archive_1(archive_name, &fp);
1176 if (open_old_archive_1(archive_name, &fp))
1178 xsnprintf(expanded_archive_name, sizeof(expanded_archive_name),
1179 "%s.lzh", archive_name);
1180 if (open_old_archive_1(expanded_archive_name, &fp)) {
1181 archive_name = expanded_archive_name;
1185 * if ( (errno&0xffff)!=E_PNNF ) { archive_name =
1186 * expanded_archive_name; return NULL; }
1188 xsnprintf(expanded_archive_name, sizeof(expanded_archive_name),
1189 "%s.lzs", archive_name);
1190 if (open_old_archive_1(expanded_archive_name, &fp)) {
1191 archive_name = expanded_archive_name;
1195 * if ( (errno&0xffff)!=E_PNNF ) { archive_name =
1196 * expanded_archive_name; return NULL; }
1199 * sprintf( expanded_archive_name , "%s.lzh",archive_name);
1200 * archive_name = expanded_archive_name;
1205 /* ------------------------------------------------------------------------ */
1207 inquire(msg, name, selective)
1208 char *msg, *name, *selective;
1214 fprintf(stderr, "%s %s ", name, msg);
1217 fgets(buffer, 1024, stdin);
1219 for (p = selective; *p; p++)
1220 if (buffer[0] == *p)
1221 return p - selective;
1226 /* ------------------------------------------------------------------------ */
1228 write_archive_tail(nafp)
1234 /* ------------------------------------------------------------------------ */
1236 copy_old_one(oafp, nafp, hdr)
1241 fseek(oafp, hdr->header_size + hdr->packed_size, SEEK_CUR);
1244 reading_filename = archive_name;
1245 writing_filename = temporary_name;
1246 copyfile(oafp, nafp, hdr->header_size + hdr->packed_size, 0, 0);