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);
469 message(char *fmt, ...)
471 int errno_sv = errno;
474 fprintf(stderr, "LHa: ");
477 vfprintf(stderr, fmt, v);
485 /* ------------------------------------------------------------------------ */
487 warning(char *fmt, ...)
489 int errno_sv = errno;
492 fprintf(stderr, "LHa: Warning: ");
495 vfprintf(stderr, fmt, v);
503 /* ------------------------------------------------------------------------ */
505 error(char *fmt, ...)
507 int errno_sv = errno;
510 fprintf(stderr, "LHa: Error: ");
513 vfprintf(stderr, fmt, v);
522 fatal_error(char *fmt, ...)
524 int errno_sv = errno;
527 fprintf(stderr, "LHa: Fatal error: ");
530 vfprintf(stderr, fmt, v);
534 fprintf(stderr, ": %s\n", strerror(errno_sv));
538 if (remove_temporary_at_error) {
539 if (temporary_fd != -1)
541 unlink(temporary_name);
547 /* ------------------------------------------------------------------------ */
552 message("Interrupted");
554 if (temporary_fd != -1)
556 unlink(temporary_name);
557 if (recover_archive_when_interrupt)
558 rename(backup_archive_name, archive_name);
559 if (remove_extracting_file_when_interrupt) {
560 message("Removing: %s", writting_filename);
561 unlink(writting_filename);
563 signal(SIGINT, SIG_DFL);
565 signal(SIGHUP, SIG_DFL);
567 kill(getpid(), signo);
570 /* ------------------------------------------------------------------------ */
572 /* ------------------------------------------------------------------------ */
577 register char *p, *q;
581 if (generic_format) {
583 c1 = *(unsigned char *) p++;
584 c2 = *(unsigned char *) q++;
596 while (*p == *q && *p != '\0')
598 return *(unsigned char *) p - *(unsigned char *) q;
602 /* ------------------------------------------------------------------------ */
607 qsort(cmd_filev, cmd_filec, sizeof(char *), sort_by_ascii);
610 /* ------------------------------------------------------------------------ */
615 char *p = (char *) malloc(size);
617 fatal_error("Not enough memory");
621 /* ------------------------------------------------------------------------ */
627 char *p = (char *) realloc(old, size);
629 fatal_error("Not enough memory");
637 int len = strlen(str);
638 char *p = (char *)xmalloc(len + 1);
643 /* ------------------------------------------------------------------------ */
645 /* ------------------------------------------------------------------------ */
648 +-------------+-------------+------+-------------+----------+
649 | N A M E 1 \0| N A M E 2 \0| .... | N A M E n \0| |
650 +-------------+-------------+------+-------------+----------+
651 ^ ^ ^ buffer+0 buffer+used buffer+size
654 +---------------+---------------+------------- -----------------+
655 | pointer to | pointer to | pointer to ... pointer to |
656 | stringpool | N A M E 1 | N A M E 2 ... N A M E n |
657 +---------------+---------------+------------- -------------+
658 ^ malloc base returned
661 /* ------------------------------------------------------------------------ */
664 struct string_pool *sp;
666 sp->size = 1024 - 8; /* any ( >=0 ) */
669 sp->buffer = (char *) xmalloc(sp->size * sizeof(char));
672 /* ------------------------------------------------------------------------ */
674 add_sp(sp, name, len)
675 struct string_pool *sp;
676 char *name; /* stored '\0' at tail */
677 int len; /* include '\0' */
679 while (sp->used + len > sp->size) {
681 sp->buffer = (char *) xrealloc(sp->buffer, sp->size * sizeof(char));
683 memmove(sp->buffer + sp->used, name, len);
688 /* ------------------------------------------------------------------------ */
690 finish_sp(sp, v_count, v_vector)
691 register struct string_pool *sp;
699 v = (char **) xmalloc((sp->n + 1) * sizeof(char *));
704 for (i = sp->n; i; i--) {
711 /* ------------------------------------------------------------------------ */
717 free(*vector); /* free string pool */
722 /* ------------------------------------------------------------------------ */
723 /* READ DIRECTORY FILES */
724 /* ------------------------------------------------------------------------ */
726 include_path_p(path, name)
732 return (path[-1] == '/' && *n == '\0');
733 return (*n == '/' || (n != name && path[-1] == '/' && n[-1] == '/'));
736 /* ------------------------------------------------------------------------ */
738 cleaning_files(v_filec, v_filev)
745 register char **filev = *v_filev;
746 register int filec = *v_filec;
753 flags = xmalloc(filec * sizeof(char));
755 /* flags & 0x01 : 1: ignore */
756 /* flags & 0x02 : 1: directory, 0 : regular file */
757 /* flags & 0x04 : 1: need delete */
760 for (i = 0; i < filec; i++)
761 if (GETSTAT(filev[i], &stbuf) < 0) {
763 warning("Cannot access \"%s\", ignored.", filev[i]);
766 if (is_regularfile(&stbuf))
768 else if (is_directory(&stbuf))
771 else if (is_symlink(&stbuf)) /* t.okamoto */
776 warning("Cannot archive \"%s\", ignored.", filev[i]);
780 for (i = 0; i < filec; i++) {
782 if ((flags[i] & 0x07) == 0x00) { /* regular file, not
784 for (j = i + 1; j < filec; j++) {
785 if ((flags[j] & 0x07) == 0x00) { /* regular file, not
787 if (STREQU(p, filev[j]))
788 flags[j] = 0x04; /* delete */
792 else if ((flags[i] & 0x07) == 0x02) { /* directory, not
794 for (j = i + 1; j < filec; j++) {
795 if ((flags[j] & 0x07) == 0x00) { /* regular file, not
797 if (include_path_p(p, filev[j]))
798 flags[j] = 0x04; /* delete */
800 else if ((flags[j] & 0x07) == 0x02) { /* directory, not
802 if (include_path_p(p, filev[j]))
803 flags[j] = 0x04; /* delete */
809 for (i = j = 0; i < filec; i++) {
810 if ((flags[i] & 0x04) == 0) {
821 /* ------------------------------------------------------------------------ */
823 /* please need your imprementation */
825 find_files(name, v_filec, v_filev)
830 return FALSE; /* DUMMY */
833 /* ------------------------------------------------------------------------ */
835 free_files(filec, filev)
841 /* ------------------------------------------------------------------------ */
844 find_files(name, v_filec, v_filev)
849 struct string_pool sp;
850 char newname[FILENAME_LENGTH];
854 struct stat tmp_stbuf, arc_stbuf, fil_stbuf;
856 strcpy(newname, name);
858 if (len > 0 && newname[len - 1] != '/')
859 newname[len++] = '/';
861 dirp = opendir(name);
867 GETSTAT(temporary_name, &tmp_stbuf);
868 GETSTAT(archive_name, &arc_stbuf);
870 for (dp = readdir(dirp); dp != NULL; dp = readdir(dirp)) {
872 strncpy(newname + len, dp->d_name, n);
873 newname[len + n] = '\0';
874 if (GETSTAT(newname, &fil_stbuf) < 0)
876 #if !defined(HAVE_STRUCT_STAT_ST_INO) || __MINGW32__
877 if ( dp->d_name[0] != '.' ||
879 (dp->d_name[1] != '.' ||
881 add_sp(&sp, newname, len+n+1);
884 if ((dp->d_ino != 0) &&
885 /* exclude '.' and '..' */
886 ((dp->d_name[0] != '.') ||
888 ((dp->d_name[1] != '.') ||
890 ((tmp_stbuf.st_dev != fil_stbuf.st_dev ||
891 tmp_stbuf.st_ino != fil_stbuf.st_ino) &&
892 (arc_stbuf.st_dev != fil_stbuf.st_dev ||
893 arc_stbuf.st_ino != fil_stbuf.st_ino))) {
894 add_sp(&sp, newname, len + n + 1);
899 finish_sp(&sp, v_filec, v_filev);
901 qsort(*v_filev, *v_filec, sizeof(char *), sort_by_ascii);
902 cleaning_files(v_filec, v_filev);
907 /* ------------------------------------------------------------------------ */
909 free_files(filec, filev)
916 /* ------------------------------------------------------------------------ */
918 /* ------------------------------------------------------------------------ */
919 /* Build temporary file name and store to TEMPORARY_NAME */
921 build_temporary_name()
923 #ifdef TMP_FILENAME_TEMPLATE
924 /* "/tmp/lhXXXXXX" etc. */
925 if (extract_directory == NULL) {
926 strcpy(temporary_name, TMP_FILENAME_TEMPLATE);
929 xsnprintf(temporary_name, sizeof(temporary_name),
930 "%s/lhXXXXXX", extract_directory);
935 strcpy(temporary_name, archive_name);
936 for (p = temporary_name, s = (char *) 0; *p; p++)
939 strcpy((s ? s + 1 : temporary_name), "lhXXXXXX");
945 old_umask = umask(077);
946 fd = mkstemp(temporary_name);
954 mktemp(temporary_name);
955 flags = O_CREAT|O_EXCL|O_RDWR;
959 return open(temporary_name, flags, 0600);
964 /* ------------------------------------------------------------------------ */
966 modify_filename_extention(buffer, ext)
970 register char *p, *dot;
972 for (p = buffer, dot = (char *) 0; *p; p++) {
985 /* ------------------------------------------------------------------------ */
986 /* build backup file name */
988 build_backup_name(buffer, original)
992 strcpy(buffer, original);
993 modify_filename_extention(buffer, BACKUPNAME_EXTENTION); /* ".bak" */
996 /* ------------------------------------------------------------------------ */
998 build_standard_archive_name(buffer, orginal)
1002 strcpy(buffer, orginal);
1003 modify_filename_extention(buffer, ARCHIVENAME_EXTENTION); /* ".lzh" */
1006 /* ------------------------------------------------------------------------ */
1008 /* ------------------------------------------------------------------------ */
1018 for (i = 0; i < cmd_filec; i++) {
1019 if (patmatch(cmd_filev[i], name, 0))
1032 if ((fp = fopen(name, mode)) == NULL)
1033 fatal_error("Cannot open file \"%s\"", name);
1038 /* ------------------------------------------------------------------------ */
1040 /* ------------------------------------------------------------------------ */
1042 open_old_archive_1(name, v_fp)
1049 if (stat(name, &stbuf) >= 0 &&
1050 is_regularfile(&stbuf) &&
1051 (fp = fopen(name, READ_BINARY)) != NULL) {
1053 archive_file_gid = stbuf.st_gid;
1054 archive_file_mode = stbuf.st_mode;
1059 archive_file_gid = -1;
1063 /* ------------------------------------------------------------------------ */
1069 static char expanded_archive_name[FILENAME_LENGTH];
1071 if (!strcmp(archive_name, "-")) {
1072 if (cmd == CMD_EXTRACT || cmd == CMD_LIST) {
1074 setmode(fileno(stdin), O_BINARY);
1081 if (p = (char *) strrchr(archive_name, '.')) {
1082 if (strucmp(".LZH", p) == 0
1083 || strucmp(".LZS", p) == 0
1084 || strucmp(".COM", p) == 0 /* DOS SFX */
1085 || strucmp(".EXE", p) == 0
1086 || strucmp(".X", p) == 0 /* HUMAN SFX */
1087 || strucmp(".BAK", p) == 0) { /* for BackUp */
1088 open_old_archive_1(archive_name, &fp);
1093 if (open_old_archive_1(archive_name, &fp))
1095 xsnprintf(expanded_archive_name, sizeof(expanded_archive_name),
1096 "%s.lzh", archive_name);
1097 if (open_old_archive_1(expanded_archive_name, &fp)) {
1098 archive_name = expanded_archive_name;
1102 * if ( (errno&0xffff)!=E_PNNF ) { archive_name =
1103 * expanded_archive_name; return NULL; }
1105 xsnprintf(expanded_archive_name, sizeof(expanded_archive_name),
1106 "%s.lzs", archive_name);
1107 if (open_old_archive_1(expanded_archive_name, &fp)) {
1108 archive_name = expanded_archive_name;
1112 * if ( (errno&0xffff)!=E_PNNF ) { archive_name =
1113 * expanded_archive_name; return NULL; }
1116 * sprintf( expanded_archive_name , "%s.lzh",archive_name);
1117 * archive_name = expanded_archive_name;
1122 /* ------------------------------------------------------------------------ */
1124 inquire(msg, name, selective)
1125 char *msg, *name, *selective;
1131 fprintf(stderr, "%s %s ", name, msg);
1134 fgets(buffer, 1024, stdin);
1136 for (p = selective; *p; p++)
1137 if (buffer[0] == *p)
1138 return p - selective;
1143 /* ------------------------------------------------------------------------ */
1145 write_archive_tail(nafp)
1151 /* ------------------------------------------------------------------------ */
1153 copy_old_one(oafp, nafp, hdr)
1158 fseek(oafp, (long) (hdr->header_size + 2) + hdr->packed_size, SEEK_CUR);
1161 reading_filename = archive_name;
1162 writting_filename = temporary_name;
1163 if (hdr->header_level != 2) {
1164 copyfile(oafp, nafp,
1165 (long) (hdr->header_size + 2) + hdr->packed_size, 0);
1167 copyfile(oafp, nafp,
1168 (long) (hdr->header_size) + hdr->packed_size, 0);
1173 /* Local Variables: */
1176 /* compile-command:"gcc -c lharc.c" */
1178 /* vi: set tabstop=4: */