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 Symblic Link 1993.06.25 N.Watazaki */
32 /* Ver. 1.11 for Symblic 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 /* ------------------------------------------------------------------------ */
49 /* ------------------------------------------------------------------------ */
50 static int cmd = CMD_UNKNOWN;
52 /* 1996.8.13 t.okamoto */
58 char expanded_archive_name[FILENAME_LENGTH];
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 = LZHUFF5_METHOD_NUM;
94 compress_method = LZHUFF7_METHOD_NUM;
97 compress_method = DEFAULT_LZHUFF_METHOD;
99 header_level = HEADER_LEVEL1;
106 /* view command flags */
107 verbose_listing = FALSE;
109 /* extract command flags */
110 output_to_stdout = FALSE;
112 /* append command flags */
114 update_if_newer = FALSE;
115 delete_after_append = FALSE;
116 generic_format = FALSE;
118 remove_temporary_at_error = FALSE;
119 recover_archive_when_interrupt = FALSE;
120 remove_extracting_file_when_interrupt = FALSE;
121 get_filename_from_stdin = FALSE;
122 ignore_directory = FALSE;
125 noconvertcase = FALSE;
127 extract_directory = NULL;
132 /* ------------------------------------------------------------------------ */
133 /* NOTES : Text File Format */
134 /* GENERATOR NewLine */
135 /* [generic] 0D 0A */
137 /* [OS9][MacOS] 0D */
139 /* ------------------------------------------------------------------------ */
141 print_tiny_usage_and_exit()
144 LHarc for UNIX V 1.02 Copyright(C) 1989 Y.Tagawa\n\
145 LHx for MSDOS V C2.01 Copyright(C) 1990 H.Yoshizaki\n\
146 LHx(arc) for OSK V 2.01 Modified 1990 Momozou\n\
147 LHa for UNIX V 1.00 Copyright(C) 1992 Masaru Oki\n\
148 LHa for UNIX V 1.14 Modified 1995 Nobutaka Watazaki\n\
149 LHa for UNIX V 1.14i Modified 2000 Tsugio Okamoto\n\
150 Autoconfiscated 2001,2002 Koji Arai\n\
153 usage: lha [-]{axelvudmcp[q[num]][vnfodizg012]}[w=<dir>] archive_file [file...]\n\
154 commands: options:\n\
155 a Add(or replace) to archive q{num} quiet (num:quiet mode)\n\
156 x,e EXtract from archive v verbose\n\
157 l,v List / Verbose List n not execute\n\
158 u Update newer files to archive f force (over write at extract)\n\
159 d Delete from archive t FILES are TEXT file\n");
162 m Move to archive (means 'ad') o[567] compression method (a/u)\n\
167 m Move to archive (means 'ad') o use LHarc compatible method (a/u)\n\
171 c re-Construct new archive w=<dir> specify extract directory (a/u/m/x/e)\n\
172 p Print to STDOUT from archive d delete FILES after (a/u/c)\n\
173 t Test file CRC in archive i ignore directory path (x/e)\n\
174 z files not compress (a/u)\n\
175 g Generic format (for compatibility)\n\
176 or not convert case when extracting\n\
177 0/1/2 header level (a/u)\n\
181 e TEXT code convert from/to EUC\n\
187 /* ------------------------------------------------------------------------ */
193 char *p, inpbuf[256];
199 init_variable(); /* Added N.Watazaki */
202 av = (char **)xmalloc( sizeof(char*)*argc );
203 for (i=0; i<argc; i++) {
204 av[i] = xstrdup( argv[i] );
208 print_tiny_usage_and_exit();
210 if (strcmp(av[1], "--version") == 0) {
217 av--; /* argv--; */ /* 1999.7.18 */
234 output_to_stdout = TRUE;
252 update_if_newer = TRUE;
257 delete_after_append = TRUE;
262 verbose_listing = TRUE;
276 print_tiny_usage_and_exit();
281 /* p = &argv[1][1]; */
289 quiet_mode = *p - '0';
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 fprintf(stderr, "LHa: error option o%c\n", p[-1]);
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 fprintf(stderr, "LHa: Unknown option '%c'.\n", 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);
468 /* ------------------------------------------------------------------------ */
470 message_1(title, subject, name)
471 char *title, *subject, *name;
473 fprintf(stderr, "LHa: %s%s ", title, subject);
477 fprintf(stderr, "%s\n", name);
482 /* ------------------------------------------------------------------------ */
484 message(subject, name)
485 char *subject, *name;
487 message_1("", subject, name);
490 /* ------------------------------------------------------------------------ */
492 warning(subject, name)
493 char *subject, *name;
495 message_1("Warning: ", subject, name);
498 /* ------------------------------------------------------------------------ */
503 message_1("Error: ", subject, msg);
506 /* ------------------------------------------------------------------------ */
511 message_1("Fatal error:", "", msg);
513 if (remove_temporary_at_error) {
514 if (temporary_fd != -1)
516 unlink(temporary_name);
522 /* ------------------------------------------------------------------------ */
526 fatal_error(writting_filename);
529 /* ------------------------------------------------------------------------ */
533 fatal_error(reading_filename);
536 /* ------------------------------------------------------------------------ */
542 message("Interrupted\n", "");
544 if (temporary_fd != -1)
546 unlink(temporary_name);
547 if (recover_archive_when_interrupt)
548 rename(backup_archive_name, archive_name);
549 if (remove_extracting_file_when_interrupt) {
551 message("Removing", writting_filename);
552 unlink(writting_filename);
554 signal(SIGINT, SIG_DFL);
556 signal(SIGHUP, SIG_DFL);
558 kill(getpid(), signo);
561 /* ------------------------------------------------------------------------ */
563 /* ------------------------------------------------------------------------ */
568 register char *p, *q;
572 if (generic_format) {
574 c1 = *(unsigned char *) p++;
575 c2 = *(unsigned char *) q++;
587 while (*p == *q && *p != '\0')
589 return *(unsigned char *) p - *(unsigned char *) q;
593 /* ------------------------------------------------------------------------ */
598 qsort(cmd_filev, cmd_filec, sizeof(char *), sort_by_ascii);
601 /* ------------------------------------------------------------------------ */
606 char *p = (char *) malloc(size);
608 fatal_error("Not enough memory");
612 /* ------------------------------------------------------------------------ */
618 char *p = (char *) realloc(old, size);
620 fatal_error("Not enough memory");
628 int len = strlen(str);
629 char *p = (char *)xmalloc(len + 1);
634 /* ------------------------------------------------------------------------ */
636 /* ------------------------------------------------------------------------ */
639 +-------------+-------------+------+-------------+----------+
640 | N A M E 1 \0| N A M E 2 \0| .... | N A M E n \0| |
641 +-------------+-------------+------+-------------+----------+
642 ^ ^ ^ buffer+0 buffer+used buffer+size
645 +---------------+---------------+------------- -----------------+
646 | pointer to | pointer to | pointer to ... pointer to |
647 | stringpool | N A M E 1 | N A M E 2 ... N A M E n |
648 +---------------+---------------+------------- -------------+
649 ^ malloc base returned
652 /* ------------------------------------------------------------------------ */
655 struct string_pool *sp;
657 sp->size = 1024 - 8; /* any ( >=0 ) */
660 sp->buffer = (char *) xmalloc(sp->size * sizeof(char));
663 /* ------------------------------------------------------------------------ */
665 add_sp(sp, name, len)
666 struct string_pool *sp;
667 char *name; /* stored '\0' at tail */
668 int len; /* include '\0' */
670 while (sp->used + len > sp->size) {
672 sp->buffer = (char *) xrealloc(sp->buffer, sp->size * sizeof(char));
674 bcopy(name, sp->buffer + sp->used, len);
679 /* ------------------------------------------------------------------------ */
681 finish_sp(sp, v_count, v_vector)
682 register struct string_pool *sp;
690 v = (char **) xmalloc((sp->n + 1) * sizeof(char *));
695 for (i = sp->n; i; i--) {
702 /* ------------------------------------------------------------------------ */
708 free(*vector); /* free string pool */
713 /* ------------------------------------------------------------------------ */
714 /* READ DIRECTORY FILES */
715 /* ------------------------------------------------------------------------ */
717 include_path_p(path, name)
723 return (path[-1] == '/' && *n == '\0');
724 return (*n == '/' || (n != name && path[-1] == '/' && n[-1] == '/'));
727 /* ------------------------------------------------------------------------ */
729 cleaning_files(v_filec, v_filev)
736 register char **filev = *v_filev;
737 register int filec = *v_filec;
744 flags = xmalloc(filec * sizeof(char));
746 /* flags & 0x01 : 1: ignore */
747 /* flags & 0x02 : 1: directory, 0 : regular file */
748 /* flags & 0x04 : 1: need delete */
751 for (i = 0; i < filec; i++)
752 if (GETSTAT(filev[i], &stbuf) < 0) {
755 "LHa: Cannot access \"%s\", ignored.\n", filev[i]);
758 if (is_regularfile(&stbuf))
760 else if (is_directory(&stbuf))
763 else if (is_symlink(&stbuf)) /* t.okamoto */
769 "LHa: Cannot archive \"%s\", ignored.\n", filev[i]);
774 for (i = 0; i < filec; i++) {
776 if ((flags[i] & 0x07) == 0x00) { /* regular file, not
778 for (j = i + 1; j < filec; j++) {
779 if ((flags[j] & 0x07) == 0x00) { /* regular file, not
781 if (STREQU(p, filev[j]))
782 flags[j] = 0x04; /* delete */
786 else if ((flags[i] & 0x07) == 0x02) { /* directory, not
788 for (j = i + 1; j < filec; j++) {
789 if ((flags[j] & 0x07) == 0x00) { /* regular file, not
791 if (include_path_p(p, filev[j]))
792 flags[j] = 0x04; /* delete */
794 else if ((flags[j] & 0x07) == 0x02) { /* directory, not
796 if (include_path_p(p, filev[j]))
797 flags[j] = 0x04; /* delete */
803 for (i = j = 0; i < filec; i++) {
804 if ((flags[i] & 0x04) == 0) {
815 /* ------------------------------------------------------------------------ */
817 /* please need your imprementation */
819 find_files(name, v_filec, v_filev)
824 return FALSE; /* DUMMY */
827 /* ------------------------------------------------------------------------ */
829 free_files(filec, filev)
835 /* ------------------------------------------------------------------------ */
838 find_files(name, v_filec, v_filev)
843 struct string_pool sp;
844 char newname[FILENAME_LENGTH];
848 struct stat tmp_stbuf, arc_stbuf, fil_stbuf;
850 strcpy(newname, name);
852 if (len > 0 && newname[len - 1] != '/')
853 newname[len++] = '/';
855 dirp = opendir(name);
861 GETSTAT(temporary_name, &tmp_stbuf);
862 GETSTAT(archive_name, &arc_stbuf);
864 for (dp = readdir(dirp); dp != NULL; dp = readdir(dirp)) {
866 strncpy(newname + len, dp->d_name, n);
867 newname[len + n] = '\0';
868 if (GETSTAT(newname, &fil_stbuf) < 0)
870 #if !defined(HAVE_STRUCT_STAT_ST_INO) || __MINGW32__
871 if ( dp->d_name[0] != '.' ||
873 (dp->d_name[1] != '.' ||
875 add_sp(&sp, newname, len+n+1);
878 if ((dp->d_ino != 0) &&
879 /* exclude '.' and '..' */
880 ((dp->d_name[0] != '.') ||
882 ((dp->d_name[1] != '.') ||
884 ((tmp_stbuf.st_dev != fil_stbuf.st_dev ||
885 tmp_stbuf.st_ino != fil_stbuf.st_ino) &&
886 (arc_stbuf.st_dev != fil_stbuf.st_dev ||
887 arc_stbuf.st_ino != fil_stbuf.st_ino))) {
888 add_sp(&sp, newname, len + n + 1);
893 finish_sp(&sp, v_filec, v_filev);
895 qsort(*v_filev, *v_filec, sizeof(char *), sort_by_ascii);
896 cleaning_files(v_filec, v_filev);
901 /* ------------------------------------------------------------------------ */
903 free_files(filec, filev)
910 /* ------------------------------------------------------------------------ */
912 /* ------------------------------------------------------------------------ */
913 /* Build temporary file name and store to TEMPORARY_NAME */
915 build_temporary_name()
917 #ifdef TMP_FILENAME_TEMPLATE
918 /* "/tmp/lhXXXXXX" etc. */
919 if (extract_directory == NULL) {
920 strcpy(temporary_name, TMP_FILENAME_TEMPLATE);
923 sprintf(temporary_name, "%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)
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 /* ------------------------------------------------------------------------ */
1063 if (!strcmp(archive_name, "-")) {
1064 if (cmd == CMD_EXTRACT || cmd == CMD_LIST) {
1066 setmode(fileno(stdin), O_BINARY);
1073 if (p = (char *) strrchr(archive_name, '.')) {
1074 if (strucmp(".LZH", p) == 0
1075 || strucmp(".LZS", p) == 0
1076 || strucmp(".COM", p) == 0 /* DOS SFX */
1077 || strucmp(".EXE", p) == 0
1078 || strucmp(".X", p) == 0 /* HUMAN SFX */
1079 || strucmp(".BAK", p) == 0) { /* for BackUp */
1080 open_old_archive_1(archive_name, &fp);
1085 if (open_old_archive_1(archive_name, &fp))
1087 sprintf(expanded_archive_name, "%s.lzh", archive_name);
1088 if (open_old_archive_1(expanded_archive_name, &fp)) {
1089 archive_name = expanded_archive_name;
1093 * if ( (errno&0xffff)!=E_PNNF ) { archive_name =
1094 * expanded_archive_name; return NULL; }
1096 sprintf(expanded_archive_name, "%s.lzs", 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; }
1106 * sprintf( expanded_archive_name , "%s.lzh",archive_name);
1107 * archive_name = expanded_archive_name;
1112 /* ------------------------------------------------------------------------ */
1114 inquire(msg, name, selective)
1115 char *msg, *name, *selective;
1121 fprintf(stderr, "%s %s ", name, msg);
1124 fgets(buffer, 1024, stdin);
1126 for (p = selective; *p; p++)
1127 if (buffer[0] == *p)
1128 return p - selective;
1133 /* ------------------------------------------------------------------------ */
1135 write_archive_tail(nafp)
1141 /* ------------------------------------------------------------------------ */
1143 copy_old_one(oafp, nafp, hdr)
1148 fseek(oafp, (long) (hdr->header_size + 2) + hdr->packed_size, SEEK_CUR);
1151 reading_filename = archive_name;
1152 writting_filename = temporary_name;
1153 if (hdr->header_level != 2) {
1154 copyfile(oafp, nafp,
1155 (long) (hdr->header_size + 2) + hdr->packed_size, 0);
1157 copyfile(oafp, nafp,
1158 (long) (hdr->header_size) + hdr->packed_size, 0);
1163 /* Local Variables: */
1166 /* compile-command:"gcc -c lharc.c" */
1168 /* vi: set tabstop=4: */