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 /* ------------------------------------------------------------------------ */
50 /* ------------------------------------------------------------------------ */
51 static int cmd = CMD_UNKNOWN;
53 /* 1996.8.13 t.okamoto */
59 char temporary_name[FILENAME_LENGTH];
60 char backup_archive_name[FILENAME_LENGTH];
63 /* static functions */
64 static void sort_files();
65 static void print_version();
67 char *extract_directory = NULL;
71 /* 1996.8.13 t.okamoto */
73 char *writting_filename;
74 char *reading_filename;
76 int archive_file_mode;
79 /* ------------------------------------------------------------------------ */
81 init_variable() /* Added N.Watazaki */
87 noexec = FALSE; /* debugging option */
91 compress_method = DEFAULT_LZHUFF_METHOD; /* defined in config.h */
93 header_level = HEADER_LEVEL1;
100 /* view command flags */
101 verbose_listing = FALSE;
103 /* extract command flags */
104 output_to_stdout = FALSE;
106 /* append command flags */
108 update_if_newer = FALSE;
109 delete_after_append = FALSE;
110 generic_format = FALSE;
112 remove_temporary_at_error = FALSE;
113 recover_archive_when_interrupt = FALSE;
114 remove_extracting_file_when_interrupt = FALSE;
115 get_filename_from_stdin = FALSE;
116 ignore_directory = FALSE;
119 noconvertcase = FALSE;
121 extract_directory = NULL;
126 /* ------------------------------------------------------------------------ */
127 /* NOTES : Text File Format */
128 /* GENERATOR NewLine */
129 /* [generic] 0D 0A */
131 /* [OS9][MacOS] 0D */
133 /* ------------------------------------------------------------------------ */
135 print_tiny_usage_and_exit()
138 LHarc for UNIX V 1.02 Copyright(C) 1989 Y.Tagawa\n\
139 LHx for MSDOS V C2.01 Copyright(C) 1990 H.Yoshizaki\n\
140 LHx(arc) for OSK V 2.01 Modified 1990 Momozou\n\
141 LHa for UNIX V 1.00 Copyright(C) 1992 Masaru Oki\n\
142 LHa for UNIX V 1.14 Modified 1995 Nobutaka Watazaki\n\
143 LHa for UNIX V 1.14i Modified 2000 Tsugio Okamoto\n\
144 Modified 2002 Hiroto Sakai\n\
145 Autoconfiscated 2001,2002 Koji Arai\n\
148 usage: lha [-]{axelvudmcp[q[num]][vnfodizg012]}[w=<dir>] archive_file [file...]\n\
149 commands: options:\n\
150 a Add(or replace) to archive q{num} quiet (num:quiet mode)\n\
151 x,e EXtract from archive v verbose\n\
152 l,v List / Verbose List n not execute\n\
153 u Update newer files to archive f force (over write at extract)\n\
154 d Delete from archive t FILES are TEXT file\n");
157 m Move to archive (means 'ad') o[567] compression method (a/u)\n\
162 m Move to archive (means 'ad') o use LHarc compatible method (a/u)\n\
166 c re-Construct new archive w=<dir> specify extract directory (a/u/m/x/e)\n\
167 p Print to STDOUT from archive d delete FILES after (a/u/c)\n\
168 t Test file CRC in archive i ignore directory path (x/e)\n\
169 z files not compress (a/u)\n\
170 g Generic format (for compatibility)\n\
171 or not convert case when extracting\n\
172 0/1/2 header level (a/u)\n\
176 e TEXT code convert from/to EUC\n\
182 /* ------------------------------------------------------------------------ */
188 char *p, inpbuf[256];
194 init_variable(); /* Added N.Watazaki */
197 av = (char **)xmalloc( sizeof(char*)*argc );
198 for (i=0; i<argc; i++) {
199 av[i] = xstrdup( argv[i] );
202 if (ac < 2 || strcmp(av[1], "--help") == 0)
203 print_tiny_usage_and_exit();
205 if (strcmp(av[1], "--version") == 0) {
212 av--; /* argv--; */ /* 1999.7.18 */
229 output_to_stdout = TRUE;
247 update_if_newer = TRUE;
252 delete_after_append = TRUE;
257 verbose_listing = TRUE;
271 print_tiny_usage_and_exit();
276 /* p = &argv[1][1]; */
282 case '0': /* no quiet */
283 case '1': /* no use the incremental indicator */
284 quiet_mode = *p - '0';
287 case '2': /* no output */
291 /* In quiet mode, no confirm to overwrite */
319 generic_format = TRUE;
320 noconvertcase = TRUE;
324 delete_after_append = TRUE;
329 compress_method = LZHUFF1_METHOD_NUM;
333 compress_method = LZHUFF5_METHOD_NUM;
338 compress_method = LZHUFF6_METHOD_NUM;
342 compress_method = LZHUFF7_METHOD_NUM;
347 error("invalid compression method 'o%c'", *p);
352 compress_method = LZHUFF0_METHOD_NUM; /* Changed N.Watazaki */
355 ignore_directory = TRUE;
360 extract_directory = p;
365 header_level = HEADER_LEVEL0;
368 header_level = HEADER_LEVEL1;
371 header_level = HEADER_LEVEL2;
374 error("Unknown option '%c'.", p[-1]);
380 /* archive file name */
381 archive_name = av[2];
383 if (!strcmp(archive_name, "-")) {
384 if (!isatty(1) && cmd == CMD_ADD)
387 #if 0 /* Comment out; IMHO, this feature is useless. by Koji Arai */
389 if (ac == 3 && !isatty(0)) { /* 1999.7.18 */
390 /* Bug(?) on MinGW, isatty() return 0 on Cygwin console.
391 mingw-runtime-1.3-2 and Cygwin 1.3.10(0.51/3/2) on
393 get_filename_from_stdin = TRUE;
398 /* target file name */
399 if (get_filename_from_stdin) {
401 xfilev = (char **)xmalloc(sizeof(char *) * xfilec);
402 while (fgets(inpbuf, sizeof(inpbuf), stdin)) {
403 /* delete \n if it exist */
405 while (i < sizeof(inpbuf) && p != 0) {
413 if (cmd_filec >= xfilec) {
415 xfilev = (char **) xrealloc(xfilev,
416 sizeof(char *) * xfilec);
418 if (strlen(inpbuf) < 1)
420 xfilev[cmd_filec++] = xstrdup(inpbuf);
422 xfilev[cmd_filec] = NULL;
457 /* ------------------------------------------------------------------------ */
459 /* ------------------------------------------------------------------------ */
461 /* ------------------------------------------------------------------------ */
465 fprintf(stderr, "%s\n", LHA_VERSION);
470 message(char *fmt, ...)
472 message(fmt, va_alist)
477 int errno_sv = errno;
480 fprintf(stderr, "LHa: ");
483 vfprintf(stderr, fmt, v);
491 /* ------------------------------------------------------------------------ */
494 warning(char *fmt, ...)
496 warning(fmt, va_alist)
501 int errno_sv = errno;
504 fprintf(stderr, "LHa: Warning: ");
507 vfprintf(stderr, fmt, v);
515 /* ------------------------------------------------------------------------ */
518 error(char *fmt, ...)
525 int errno_sv = errno;
528 fprintf(stderr, "LHa: Error: ");
531 vfprintf(stderr, fmt, v);
541 fatal_error(char *fmt, ...)
543 fatal_error(fmt, va_alist)
548 int errno_sv = errno;
551 fprintf(stderr, "LHa: Fatal error: ");
554 vfprintf(stderr, fmt, v);
558 fprintf(stderr, ": %s\n", strerror(errno_sv));
562 if (remove_temporary_at_error) {
563 if (temporary_fd != -1)
565 unlink(temporary_name);
571 /* ------------------------------------------------------------------------ */
576 message("Interrupted");
578 if (temporary_fd != -1)
580 unlink(temporary_name);
581 if (recover_archive_when_interrupt)
582 rename(backup_archive_name, archive_name);
583 if (remove_extracting_file_when_interrupt) {
584 message("Removing: %s", writting_filename);
585 unlink(writting_filename);
587 signal(SIGINT, SIG_DFL);
589 signal(SIGHUP, SIG_DFL);
591 kill(getpid(), signo);
594 /* ------------------------------------------------------------------------ */
596 /* ------------------------------------------------------------------------ */
601 register char *p, *q;
605 if (generic_format) {
607 c1 = *(unsigned char *) p++;
608 c2 = *(unsigned char *) q++;
620 while (*p == *q && *p != '\0')
622 return *(unsigned char *) p - *(unsigned char *) q;
626 /* ------------------------------------------------------------------------ */
631 qsort(cmd_filev, cmd_filec, sizeof(char *), sort_by_ascii);
634 /* ------------------------------------------------------------------------ */
639 char *p = (char *) malloc(size);
641 fatal_error("Not enough memory");
645 /* ------------------------------------------------------------------------ */
651 char *p = (char *) realloc(old, size);
653 fatal_error("Not enough memory");
661 int len = strlen(str);
662 char *p = (char *)xmalloc(len + 1);
667 /* ------------------------------------------------------------------------ */
669 /* ------------------------------------------------------------------------ */
672 +-------------+-------------+------+-------------+----------+
673 | N A M E 1 \0| N A M E 2 \0| .... | N A M E n \0| |
674 +-------------+-------------+------+-------------+----------+
675 ^ ^ ^ buffer+0 buffer+used buffer+size
678 +---------------+---------------+------------- -----------------+
679 | pointer to | pointer to | pointer to ... pointer to |
680 | stringpool | N A M E 1 | N A M E 2 ... N A M E n |
681 +---------------+---------------+------------- -------------+
682 ^ malloc base returned
685 /* ------------------------------------------------------------------------ */
688 struct string_pool *sp;
690 sp->size = 1024 - 8; /* any ( >=0 ) */
693 sp->buffer = (char *) xmalloc(sp->size * sizeof(char));
696 /* ------------------------------------------------------------------------ */
698 add_sp(sp, name, len)
699 struct string_pool *sp;
700 char *name; /* stored '\0' at tail */
701 int len; /* include '\0' */
703 while (sp->used + len > sp->size) {
705 sp->buffer = (char *) xrealloc(sp->buffer, sp->size * sizeof(char));
707 memmove(sp->buffer + sp->used, name, len);
712 /* ------------------------------------------------------------------------ */
714 finish_sp(sp, v_count, v_vector)
715 register struct string_pool *sp;
723 v = (char **) xmalloc((sp->n + 1) * sizeof(char *));
728 for (i = sp->n; i; i--) {
735 /* ------------------------------------------------------------------------ */
741 free(*vector); /* free string pool */
746 /* ------------------------------------------------------------------------ */
747 /* READ DIRECTORY FILES */
748 /* ------------------------------------------------------------------------ */
750 include_path_p(path, name)
756 return (path[-1] == '/' && *n == '\0');
757 return (*n == '/' || (n != name && path[-1] == '/' && n[-1] == '/'));
760 /* ------------------------------------------------------------------------ */
762 cleaning_files(v_filec, v_filev)
769 register char **filev = *v_filev;
770 register int filec = *v_filec;
777 flags = xmalloc(filec * sizeof(char));
779 /* flags & 0x01 : 1: ignore */
780 /* flags & 0x02 : 1: directory, 0 : regular file */
781 /* flags & 0x04 : 1: need delete */
784 for (i = 0; i < filec; i++)
785 if (GETSTAT(filev[i], &stbuf) < 0) {
787 warning("Cannot access \"%s\", ignored.", filev[i]);
790 if (is_regularfile(&stbuf))
792 else if (is_directory(&stbuf))
795 else if (is_symlink(&stbuf)) /* t.okamoto */
800 warning("Cannot archive \"%s\", ignored.", filev[i]);
804 for (i = 0; i < filec; i++) {
806 if ((flags[i] & 0x07) == 0x00) { /* regular file, not
808 for (j = i + 1; j < filec; j++) {
809 if ((flags[j] & 0x07) == 0x00) { /* regular file, not
811 if (STREQU(p, filev[j]))
812 flags[j] = 0x04; /* delete */
816 else if ((flags[i] & 0x07) == 0x02) { /* directory, not
818 for (j = i + 1; j < filec; j++) {
819 if ((flags[j] & 0x07) == 0x00) { /* regular file, not
821 if (include_path_p(p, filev[j]))
822 flags[j] = 0x04; /* delete */
824 else if ((flags[j] & 0x07) == 0x02) { /* directory, not
826 if (include_path_p(p, filev[j]))
827 flags[j] = 0x04; /* delete */
833 for (i = j = 0; i < filec; i++) {
834 if ((flags[i] & 0x04) == 0) {
845 /* ------------------------------------------------------------------------ */
847 /* please need your imprementation */
849 find_files(name, v_filec, v_filev)
854 return FALSE; /* DUMMY */
857 /* ------------------------------------------------------------------------ */
859 free_files(filec, filev)
865 /* ------------------------------------------------------------------------ */
868 find_files(name, v_filec, v_filev)
873 struct string_pool sp;
874 char newname[FILENAME_LENGTH];
878 struct stat tmp_stbuf, arc_stbuf, fil_stbuf;
880 strcpy(newname, name);
882 if (len > 0 && newname[len - 1] != '/')
883 newname[len++] = '/';
885 dirp = opendir(name);
891 GETSTAT(temporary_name, &tmp_stbuf);
892 GETSTAT(archive_name, &arc_stbuf);
894 for (dp = readdir(dirp); dp != NULL; dp = readdir(dirp)) {
896 strncpy(newname + len, dp->d_name, n);
897 newname[len + n] = '\0';
898 if (GETSTAT(newname, &fil_stbuf) < 0)
900 #if !defined(HAVE_STRUCT_STAT_ST_INO) || __MINGW32__
901 if ( dp->d_name[0] != '.' ||
903 (dp->d_name[1] != '.' ||
905 add_sp(&sp, newname, len+n+1);
908 if ((dp->d_ino != 0) &&
909 /* exclude '.' and '..' */
910 ((dp->d_name[0] != '.') ||
912 ((dp->d_name[1] != '.') ||
914 ((tmp_stbuf.st_dev != fil_stbuf.st_dev ||
915 tmp_stbuf.st_ino != fil_stbuf.st_ino) &&
916 (arc_stbuf.st_dev != fil_stbuf.st_dev ||
917 arc_stbuf.st_ino != fil_stbuf.st_ino))) {
918 add_sp(&sp, newname, len + n + 1);
923 finish_sp(&sp, v_filec, v_filev);
925 qsort(*v_filev, *v_filec, sizeof(char *), sort_by_ascii);
926 cleaning_files(v_filec, v_filev);
931 /* ------------------------------------------------------------------------ */
933 free_files(filec, filev)
940 /* ------------------------------------------------------------------------ */
942 /* ------------------------------------------------------------------------ */
943 /* Build temporary file name and store to TEMPORARY_NAME */
945 build_temporary_name()
947 #ifdef TMP_FILENAME_TEMPLATE
948 /* "/tmp/lhXXXXXX" etc. */
949 if (extract_directory == NULL) {
950 strcpy(temporary_name, TMP_FILENAME_TEMPLATE);
953 xsnprintf(temporary_name, sizeof(temporary_name),
954 "%s/lhXXXXXX", extract_directory);
959 strcpy(temporary_name, archive_name);
960 for (p = temporary_name, s = (char *) 0; *p; p++)
963 strcpy((s ? s + 1 : temporary_name), "lhXXXXXX");
969 old_umask = umask(077);
970 fd = mkstemp(temporary_name);
978 mktemp(temporary_name);
979 flags = O_CREAT|O_EXCL|O_RDWR;
983 return open(temporary_name, flags, 0600);
988 /* ------------------------------------------------------------------------ */
990 modify_filename_extention(buffer, ext)
994 register char *p, *dot;
996 for (p = buffer, dot = (char *) 0; *p; p++) {
1009 /* ------------------------------------------------------------------------ */
1010 /* build backup file name */
1012 build_backup_name(buffer, original)
1016 strcpy(buffer, original);
1017 modify_filename_extention(buffer, BACKUPNAME_EXTENTION); /* ".bak" */
1020 /* ------------------------------------------------------------------------ */
1022 build_standard_archive_name(buffer, orginal)
1026 strcpy(buffer, orginal);
1027 modify_filename_extention(buffer, ARCHIVENAME_EXTENTION); /* ".lzh" */
1030 /* ------------------------------------------------------------------------ */
1032 /* ------------------------------------------------------------------------ */
1042 for (i = 0; i < cmd_filec; i++) {
1043 if (patmatch(cmd_filev[i], name, 0))
1056 if ((fp = fopen(name, mode)) == NULL)
1057 fatal_error("Cannot open file \"%s\"", name);
1062 /* ------------------------------------------------------------------------ */
1064 /* ------------------------------------------------------------------------ */
1066 open_old_archive_1(name, v_fp)
1073 if (stat(name, &stbuf) >= 0 &&
1074 is_regularfile(&stbuf) &&
1075 (fp = fopen(name, READ_BINARY)) != NULL) {
1077 archive_file_gid = stbuf.st_gid;
1078 archive_file_mode = stbuf.st_mode;
1083 archive_file_gid = -1;
1087 /* ------------------------------------------------------------------------ */
1093 static char expanded_archive_name[FILENAME_LENGTH];
1095 if (!strcmp(archive_name, "-")) {
1096 if (cmd == CMD_EXTRACT || cmd == CMD_LIST) {
1098 setmode(fileno(stdin), O_BINARY);
1105 if (p = (char *) strrchr(archive_name, '.')) {
1106 if (strucmp(".LZH", p) == 0
1107 || strucmp(".LZS", p) == 0
1108 || strucmp(".COM", p) == 0 /* DOS SFX */
1109 || strucmp(".EXE", p) == 0
1110 || strucmp(".X", p) == 0 /* HUMAN SFX */
1111 || strucmp(".BAK", p) == 0) { /* for BackUp */
1112 open_old_archive_1(archive_name, &fp);
1117 if (open_old_archive_1(archive_name, &fp))
1119 xsnprintf(expanded_archive_name, sizeof(expanded_archive_name),
1120 "%s.lzh", archive_name);
1121 if (open_old_archive_1(expanded_archive_name, &fp)) {
1122 archive_name = expanded_archive_name;
1126 * if ( (errno&0xffff)!=E_PNNF ) { archive_name =
1127 * expanded_archive_name; return NULL; }
1129 xsnprintf(expanded_archive_name, sizeof(expanded_archive_name),
1130 "%s.lzs", archive_name);
1131 if (open_old_archive_1(expanded_archive_name, &fp)) {
1132 archive_name = expanded_archive_name;
1136 * if ( (errno&0xffff)!=E_PNNF ) { archive_name =
1137 * expanded_archive_name; return NULL; }
1140 * sprintf( expanded_archive_name , "%s.lzh",archive_name);
1141 * archive_name = expanded_archive_name;
1146 /* ------------------------------------------------------------------------ */
1148 inquire(msg, name, selective)
1149 char *msg, *name, *selective;
1155 fprintf(stderr, "%s %s ", name, msg);
1158 fgets(buffer, 1024, stdin);
1160 for (p = selective; *p; p++)
1161 if (buffer[0] == *p)
1162 return p - selective;
1167 /* ------------------------------------------------------------------------ */
1169 write_archive_tail(nafp)
1175 /* ------------------------------------------------------------------------ */
1177 copy_old_one(oafp, nafp, hdr)
1182 fseek(oafp, (long) (hdr->header_size + 2) + hdr->packed_size, SEEK_CUR);
1185 reading_filename = archive_name;
1186 writting_filename = temporary_name;
1187 if (hdr->header_level != 2) {
1188 copyfile(oafp, nafp,
1189 (long) (hdr->header_size + 2) + hdr->packed_size, 0);
1191 copyfile(oafp, nafp,
1192 (long) (hdr->header_size) + hdr->packed_size, 0);
1197 /* Local Variables: */
1200 /* compile-command:"gcc -c lharc.c" */
1202 /* vi: set tabstop=4: */