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_LEVEL1;
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 fprintf(stderr, "%s\n", LHA_VERSION);
458 message(char *fmt, ...)
460 message(fmt, va_alist)
465 int errno_sv = errno;
468 fprintf(stderr, "LHa: ");
471 vfprintf(stderr, fmt, v);
479 /* ------------------------------------------------------------------------ */
482 warning(char *fmt, ...)
484 warning(fmt, va_alist)
489 int errno_sv = errno;
492 fprintf(stderr, "LHa: Warning: ");
495 vfprintf(stderr, fmt, v);
503 /* ------------------------------------------------------------------------ */
506 error(char *fmt, ...)
513 int errno_sv = errno;
516 fprintf(stderr, "LHa: Error: ");
519 vfprintf(stderr, fmt, v);
531 fatal_error(char *fmt, ...)
533 fatal_error(fmt, va_alist)
538 int errno_sv = errno;
541 fprintf(stderr, "LHa: Fatal error: ");
544 vfprintf(stderr, fmt, v);
548 fprintf(stderr, ": %s\n", strerror(errno_sv));
552 if (remove_temporary_at_error) {
553 if (temporary_fd != -1)
555 unlink(temporary_name);
561 /* ------------------------------------------------------------------------ */
566 message("Interrupted");
568 if (temporary_fd != -1)
570 unlink(temporary_name);
571 if (recover_archive_when_interrupt)
572 rename(backup_archive_name, archive_name);
573 if (remove_extracting_file_when_interrupt) {
574 message("Removing: %s", writing_filename);
575 unlink(writing_filename);
577 signal(SIGINT, SIG_DFL);
579 signal(SIGHUP, SIG_DFL);
581 kill(getpid(), signo);
584 /* ------------------------------------------------------------------------ */
586 /* ------------------------------------------------------------------------ */
591 register char *p, *q;
595 if (generic_format) {
597 c1 = *(unsigned char *) p++;
598 c2 = *(unsigned char *) q++;
610 while (*p == *q && *p != '\0')
612 return *(unsigned char *) p - *(unsigned char *) q;
616 /* ------------------------------------------------------------------------ */
621 qsort(cmd_filev, cmd_filec, sizeof(char *), sort_by_ascii);
624 /* ------------------------------------------------------------------------ */
629 void *p = malloc(size);
631 fatal_error("Not enough memory");
635 /* ------------------------------------------------------------------------ */
641 void *p = (char *) realloc(old, size);
643 fatal_error("Not enough memory");
651 int len = strlen(str);
652 char *p = (char *)xmalloc(len + 1);
657 /* ------------------------------------------------------------------------ */
659 /* ------------------------------------------------------------------------ */
662 +-------------+-------------+------+-------------+----------+
663 | N A M E 1 \0| N A M E 2 \0| .... | N A M E n \0| |
664 +-------------+-------------+------+-------------+----------+
665 ^ ^ ^ buffer+0 buffer+used buffer+size
668 +---------------+---------------+------------- -----------------+
669 | pointer to | pointer to | pointer to ... pointer to |
670 | stringpool | N A M E 1 | N A M E 2 ... N A M E n |
671 +---------------+---------------+------------- -------------+
672 ^ malloc base returned
675 /* ------------------------------------------------------------------------ */
678 struct string_pool *sp;
680 sp->size = 1024 - 8; /* any ( >=0 ) */
683 sp->buffer = (char *) xmalloc(sp->size * sizeof(char));
686 /* ------------------------------------------------------------------------ */
688 add_sp(sp, name, len)
689 struct string_pool *sp;
690 char *name; /* stored '\0' at tail */
691 int len; /* include '\0' */
693 while (sp->used + len > sp->size) {
695 sp->buffer = (char *) xrealloc(sp->buffer, sp->size * sizeof(char));
697 memmove(sp->buffer + sp->used, name, len);
702 /* ------------------------------------------------------------------------ */
704 finish_sp(sp, v_count, v_vector)
705 register struct string_pool *sp;
713 v = (char **) xmalloc((sp->n + 1) * sizeof(char *));
718 for (i = sp->n; i; i--) {
725 /* ------------------------------------------------------------------------ */
731 free(*vector); /* free string pool */
736 /* ------------------------------------------------------------------------ */
737 /* READ DIRECTORY FILES */
738 /* ------------------------------------------------------------------------ */
740 include_path_p(path, name)
746 return (path[-1] == '/' && *n == '\0');
747 return (*n == '/' || (n != name && path[-1] == '/' && n[-1] == '/'));
750 /* ------------------------------------------------------------------------ */
752 cleaning_files(v_filec, v_filev)
759 register char **filev = *v_filev;
760 register int filec = *v_filec;
767 flags = xmalloc(filec * sizeof(char));
769 /* flags & 0x01 : 1: ignore */
770 /* flags & 0x02 : 1: directory, 0 : regular file */
771 /* flags & 0x04 : 1: need delete */
774 for (i = 0; i < filec; i++)
775 if (GETSTAT(filev[i], &stbuf) < 0) {
777 warning("Cannot access \"%s\", ignored.", filev[i]);
780 if (is_regularfile(&stbuf))
782 else if (is_directory(&stbuf))
785 else if (is_symlink(&stbuf)) /* t.okamoto */
790 warning("Cannot archive \"%s\", ignored.", filev[i]);
794 for (i = 0; i < filec; i++) {
796 if ((flags[i] & 0x07) == 0x00) { /* regular file, not
798 for (j = i + 1; j < filec; j++) {
799 if ((flags[j] & 0x07) == 0x00) { /* regular file, not
801 if (STREQU(p, filev[j]))
802 flags[j] = 0x04; /* delete */
806 else if ((flags[i] & 0x07) == 0x02) { /* directory, not
808 for (j = i + 1; j < filec; j++) {
809 if ((flags[j] & 0x07) == 0x00) { /* regular file, not
811 if (include_path_p(p, filev[j]))
812 flags[j] = 0x04; /* delete */
814 else if ((flags[j] & 0x07) == 0x02) { /* directory, not
816 if (include_path_p(p, filev[j]))
817 flags[j] = 0x04; /* delete */
823 for (i = j = 0; i < filec; i++) {
824 if ((flags[i] & 0x04) == 0) {
835 /* ------------------------------------------------------------------------ */
837 find_files(name, v_filec, v_filev)
842 struct string_pool sp;
843 char newname[FILENAME_LENGTH];
847 struct stat tmp_stbuf, arc_stbuf, fil_stbuf;
849 strcpy(newname, name);
851 if (len > 0 && newname[len - 1] != '/')
852 newname[len++] = '/';
854 dirp = opendir(name);
860 GETSTAT(temporary_name, &tmp_stbuf);
861 GETSTAT(archive_name, &arc_stbuf);
863 for (dp = readdir(dirp); dp != NULL; dp = readdir(dirp)) {
865 strncpy(newname + len, dp->d_name, n);
866 newname[len + n] = '\0';
867 if (GETSTAT(newname, &fil_stbuf) < 0)
869 #if !defined(HAVE_STRUCT_STAT_ST_INO) || __MINGW32__
870 if ( dp->d_name[0] != '.' ||
872 (dp->d_name[1] != '.' ||
874 add_sp(&sp, newname, len+n+1);
877 if ((dp->d_ino != 0) &&
878 /* exclude '.' and '..' */
879 ((dp->d_name[0] != '.') ||
881 ((dp->d_name[1] != '.') ||
883 ((tmp_stbuf.st_dev != fil_stbuf.st_dev ||
884 tmp_stbuf.st_ino != fil_stbuf.st_ino) &&
885 (arc_stbuf.st_dev != fil_stbuf.st_dev ||
886 arc_stbuf.st_ino != fil_stbuf.st_ino))) {
887 add_sp(&sp, newname, len + n + 1);
892 finish_sp(&sp, v_filec, v_filev);
894 qsort(*v_filev, *v_filec, sizeof(char *), sort_by_ascii);
895 cleaning_files(v_filec, v_filev);
900 /* ------------------------------------------------------------------------ */
902 free_files(filec, filev)
909 /* ------------------------------------------------------------------------ */
911 /* ------------------------------------------------------------------------ */
912 /* Build temporary file name and store to TEMPORARY_NAME */
914 build_temporary_name()
916 #ifdef TMP_FILENAME_TEMPLATE
917 /* "/tmp/lhXXXXXX" etc. */
918 if (extract_directory == NULL) {
919 strcpy(temporary_name, TMP_FILENAME_TEMPLATE);
922 xsnprintf(temporary_name, sizeof(temporary_name),
923 "%s/lhXXXXXX", extract_directory);
928 strcpy(temporary_name, archive_name);
929 for (p = temporary_name, s = (char *) 0; *p; p++)
932 strcpy((s ? s + 1 : temporary_name), "lhXXXXXX");
938 old_umask = umask(077);
939 fd = mkstemp(temporary_name);
947 mktemp(temporary_name);
948 flags = O_CREAT|O_EXCL|O_RDWR;
952 return open(temporary_name, flags, 0600);
957 /* ------------------------------------------------------------------------ */
959 modify_filename_extention(buffer, ext)
963 register char *p, *dot;
965 for (p = buffer, dot = (char *) 0; *p; p++) {
978 /* ------------------------------------------------------------------------ */
979 /* build backup file name */
981 build_backup_name(buffer, original)
985 strcpy(buffer, original);
986 modify_filename_extention(buffer, BACKUPNAME_EXTENTION); /* ".bak" */
989 /* ------------------------------------------------------------------------ */
991 build_standard_archive_name(buffer, orginal)
995 strcpy(buffer, orginal);
996 modify_filename_extention(buffer, ARCHIVENAME_EXTENTION); /* ".lzh" */
999 /* ------------------------------------------------------------------------ */
1001 /* ------------------------------------------------------------------------ */
1011 for (i = 0; i < cmd_filec; i++) {
1012 if (patmatch(cmd_filev[i], name, 0))
1025 if ((fp = fopen(name, mode)) == NULL)
1026 fatal_error("Cannot open file \"%s\"", name);
1031 /* ------------------------------------------------------------------------ */
1033 /* ------------------------------------------------------------------------ */
1035 open_old_archive_1(name, v_fp)
1042 if (stat(name, &stbuf) >= 0 &&
1043 is_regularfile(&stbuf) &&
1044 (fp = fopen(name, READ_BINARY)) != NULL) {
1046 archive_file_gid = stbuf.st_gid;
1047 archive_file_mode = stbuf.st_mode;
1052 archive_file_gid = -1;
1056 /* ------------------------------------------------------------------------ */
1062 static char expanded_archive_name[FILENAME_LENGTH];
1064 if (!strcmp(archive_name, "-")) {
1065 if (cmd == CMD_EXTRACT || cmd == CMD_LIST) {
1067 setmode(fileno(stdin), O_BINARY);
1074 p = strrchr(archive_name, '.');
1076 if (strucmp(".LZH", p) == 0
1077 || strucmp(".LZS", p) == 0
1078 || strucmp(".COM", p) == 0 /* DOS SFX */
1079 || strucmp(".EXE", p) == 0
1080 || strucmp(".X", p) == 0 /* HUMAN SFX */
1081 || strucmp(".BAK", p) == 0) { /* for BackUp */
1082 open_old_archive_1(archive_name, &fp);
1087 if (open_old_archive_1(archive_name, &fp))
1089 xsnprintf(expanded_archive_name, sizeof(expanded_archive_name),
1090 "%s.lzh", archive_name);
1091 if (open_old_archive_1(expanded_archive_name, &fp)) {
1092 archive_name = expanded_archive_name;
1096 * if ( (errno&0xffff)!=E_PNNF ) { archive_name =
1097 * expanded_archive_name; return NULL; }
1099 xsnprintf(expanded_archive_name, sizeof(expanded_archive_name),
1100 "%s.lzs", archive_name);
1101 if (open_old_archive_1(expanded_archive_name, &fp)) {
1102 archive_name = expanded_archive_name;
1106 * if ( (errno&0xffff)!=E_PNNF ) { archive_name =
1107 * expanded_archive_name; return NULL; }
1110 * sprintf( expanded_archive_name , "%s.lzh",archive_name);
1111 * archive_name = expanded_archive_name;
1116 /* ------------------------------------------------------------------------ */
1118 inquire(msg, name, selective)
1119 char *msg, *name, *selective;
1125 fprintf(stderr, "%s %s ", name, msg);
1128 fgets(buffer, 1024, stdin);
1130 for (p = selective; *p; p++)
1131 if (buffer[0] == *p)
1132 return p - selective;
1137 /* ------------------------------------------------------------------------ */
1139 write_archive_tail(nafp)
1145 /* ------------------------------------------------------------------------ */
1147 copy_old_one(oafp, nafp, hdr)
1152 fseek(oafp, (long) (hdr->header_size + 2) + hdr->packed_size, SEEK_CUR);
1155 reading_filename = archive_name;
1156 writing_filename = temporary_name;
1157 if (hdr->header_level != 2) {
1158 copyfile(oafp, nafp,
1159 (long) (hdr->header_size + 2) + hdr->packed_size, 0);
1161 copyfile(oafp, nafp,
1162 (long) (hdr->header_size) + hdr->packed_size, 0);
1167 /* Local Variables: */
1170 /* compile-command:"gcc -c lharc.c" */
1172 /* vi: set tabstop=4: */