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;
131 /* ------------------------------------------------------------------------ */
132 /* NOTES : Text File Format */
133 /* GENERATOR NewLine */
134 /* [generic] 0D 0A */
136 /* [OS9][MacOS] 0D */
138 /* ------------------------------------------------------------------------ */
140 print_tiny_usage_and_exit()
143 LHarc for UNIX V 1.02 Copyright(C) 1989 Y.Tagawa\n\
144 LHx for MSDOS V C2.01 Copyright(C) 1990 H.Yoshizaki\n\
145 LHx(arc) for OSK V 2.01 Modified 1990 Momozou\n\
146 LHa for UNIX V 1.00 Copyright(C) 1992 Masaru Oki\n\
147 LHa for UNIX V 1.14 Modified 1995 Nobutaka Watazaki\n\
148 LHa for UNIX V 1.14i Modified 2000 Tsugio Okamoto\n\
151 usage: lha [-]{axelvudmcp[q[num]][vnfodizg012]}[w=<dir>] archive_file [file...]\n\
152 commands: options:\n\
153 a Add(or replace) to archive q{num} quiet (num:quiet mode)\n\
154 x,e EXtract from archive v verbose\n\
155 l,v List / Verbose List n not execute\n\
156 u Update newer files to archive f force (over write at extract)\n\
157 d Delete from archive t FILES are TEXT file\n");
160 m Move to archive (means 'ad') o[567] compression method (a/u)\n\
165 m Move to archive (means 'ad') o use LHarc compatible method (a/u)\n\
169 c re-Construct new archive w=<dir> specify extract directory (a/u/m/x/e)\n\
170 p Print to STDOUT from archive d delete FILES after (a/u/c)\n\
171 t Test file CRC in archive i ignore directory path (x/e)\n\
172 z files not compress (a/u)\n\
173 g Generic format (for compatibility)\n\
174 or not convert case when extracting\n\
175 0/1/2 header level (a/u)\n\
179 e TEXT code convert from/to EUC\n\
185 /* ------------------------------------------------------------------------ */
191 char *p, inpbuf[256];
197 init_variable(); /* Added N.Watazaki */
200 av = (char **)xmalloc( sizeof(char*)*argc );
201 for (i=0; i<argc; i++) {
202 av[i] = xstrdup( argv[i] );
206 print_tiny_usage_and_exit();
208 if (strcmp(av[1], "--version") == 0) {
215 av--; /* argv--; */ /* 1999.7.18 */
232 output_to_stdout = TRUE;
250 update_if_newer = TRUE;
255 delete_after_append = TRUE;
260 verbose_listing = TRUE;
274 print_tiny_usage_and_exit();
279 /* p = &argv[1][1]; */
287 quiet_mode = *p - '0';
317 generic_format = TRUE;
318 noconvertcase = TRUE;
322 delete_after_append = TRUE;
327 compress_method = LZHUFF1_METHOD_NUM;
331 compress_method = LZHUFF5_METHOD_NUM;
336 compress_method = LZHUFF6_METHOD_NUM;
340 compress_method = LZHUFF7_METHOD_NUM;
345 fprintf(stderr, "LHa: error option o%c\n", p[-1]);
350 compress_method = LZHUFF0_METHOD_NUM; /* Changed N.Watazaki */
353 ignore_directory = TRUE;
358 extract_directory = p;
363 header_level = HEADER_LEVEL0;
366 header_level = HEADER_LEVEL1;
369 header_level = HEADER_LEVEL2;
372 fprintf(stderr, "LHa: Unknown option '%c'.\n", p[-1]);
378 /* archive file name */
379 archive_name = av[2];
381 if (!strcmp(archive_name, "-")) {
382 if (!isatty(1) && cmd == CMD_ADD)
386 if (ac == 3 && !isatty(0)) { /* 1999.7.18 */
387 get_filename_from_stdin = TRUE;
391 /* target file name */
392 if (get_filename_from_stdin) {
394 xfilev = (char **)xmalloc(sizeof(char *) * xfilec);
395 while (fgets(inpbuf, sizeof(inpbuf), stdin)) {
396 /* delete \n if it exist */
398 while (i < sizeof(inpbuf) && p != 0) {
406 if (cmd_filec >= xfilec) {
408 cmd_filev = (char **) realloc(xfilev,
409 sizeof(char *) * xfilec);
410 if (cmd_filev == NULL)
411 fatal_error("Virtual memory exhausted\n");
414 if (strlen(inpbuf) < 1)
416 xfilev[cmd_filec++] = xstrdup(inpbuf);
418 xfilev[cmd_filec] = NULL;
453 /* ------------------------------------------------------------------------ */
455 /* ------------------------------------------------------------------------ */
457 /* ------------------------------------------------------------------------ */
461 fprintf(stderr, "%s\n", LHA_VERSION);
464 /* ------------------------------------------------------------------------ */
466 message_1(title, subject, name)
467 char *title, *subject, *name;
469 fprintf(stderr, "LHa: %s%s ", title, subject);
473 fprintf(stderr, "%s\n", name);
478 /* ------------------------------------------------------------------------ */
480 message(subject, name)
481 char *subject, *name;
483 message_1("", subject, name);
486 /* ------------------------------------------------------------------------ */
488 warning(subject, name)
489 char *subject, *name;
491 message_1("Warning: ", subject, name);
494 /* ------------------------------------------------------------------------ */
499 message_1("Error: ", subject, msg);
502 /* ------------------------------------------------------------------------ */
507 message_1("Fatal error:", "", msg);
509 if (remove_temporary_at_error)
510 unlink(temporary_name);
515 /* ------------------------------------------------------------------------ */
519 fatal_error(writting_filename);
522 /* ------------------------------------------------------------------------ */
526 fatal_error(reading_filename);
529 /* ------------------------------------------------------------------------ */
535 message("Interrupted\n", "");
538 fclose(temporary_fp);
539 unlink(temporary_name);
540 if (recover_archive_when_interrupt)
541 rename(backup_archive_name, archive_name);
542 if (remove_extracting_file_when_interrupt) {
544 message("Removing", writting_filename);
545 unlink(writting_filename);
547 signal(SIGINT, SIG_DFL);
549 signal(SIGHUP, SIG_DFL);
551 kill(getpid(), signo);
554 /* ------------------------------------------------------------------------ */
556 /* ------------------------------------------------------------------------ */
561 register char *p, *q;
565 if (generic_format) {
567 c1 = *(unsigned char *) p++;
568 c2 = *(unsigned char *) q++;
580 while (*p == *q && *p != '\0')
582 return *(unsigned char *) p - *(unsigned char *) q;
586 /* ------------------------------------------------------------------------ */
591 qsort(cmd_filev, cmd_filec, sizeof(char *), sort_by_ascii);
594 /* ------------------------------------------------------------------------ */
599 char *p = (char *) malloc(size);
601 fatal_error("Not enough memory");
605 /* ------------------------------------------------------------------------ */
611 char *p = (char *) realloc(old, size);
613 fatal_error("Not enough memory");
621 int len = strlen(str);
622 char *p = (char *)xmalloc(str + 1);
624 fatal_error("Not enough memory");
629 /* ------------------------------------------------------------------------ */
631 /* ------------------------------------------------------------------------ */
634 +-------------+-------------+------+-------------+----------+
635 | N A M E 1 \0| N A M E 2 \0| .... | N A M E n \0| |
636 +-------------+-------------+------+-------------+----------+
637 ^ ^ ^ buffer+0 buffer+used buffer+size
640 +---------------+---------------+------------- -----------------+
641 | pointer to | pointer to | pointer to ... pointer to |
642 | stringpool | N A M E 1 | N A M E 2 ... N A M E n |
643 +---------------+---------------+------------- -------------+
644 ^ malloc base returned
647 /* ------------------------------------------------------------------------ */
650 struct string_pool *sp;
652 sp->size = 1024 - 8; /* any ( >=0 ) */
655 sp->buffer = (char *) xmalloc(sp->size * sizeof(char));
658 /* ------------------------------------------------------------------------ */
660 add_sp(sp, name, len)
661 struct string_pool *sp;
662 char *name; /* stored '\0' at tail */
663 int len; /* include '\0' */
665 while (sp->used + len > sp->size) {
667 sp->buffer = (char *) xrealloc(sp->buffer, sp->size * sizeof(char));
669 bcopy(name, sp->buffer + sp->used, len);
674 /* ------------------------------------------------------------------------ */
676 finish_sp(sp, v_count, v_vector)
677 register struct string_pool *sp;
685 v = (char **) xmalloc((sp->n + 1) * sizeof(char *));
690 for (i = sp->n; i; i--) {
697 /* ------------------------------------------------------------------------ */
703 free(*vector); /* free string pool */
708 /* ------------------------------------------------------------------------ */
709 /* READ DIRECTORY FILES */
710 /* ------------------------------------------------------------------------ */
712 include_path_p(path, name)
718 return (path[-1] == '/' && *n == '\0');
719 return (*n == '/' || (n != name && path[-1] == '/' && n[-1] == '/'));
722 /* ------------------------------------------------------------------------ */
724 cleaning_files(v_filec, v_filev)
731 register char **filev = *v_filev;
732 register int filec = *v_filec;
739 flags = xmalloc(filec * sizeof(char));
741 /* flags & 0x01 : 1: ignore */
742 /* flags & 0x02 : 1: directory, 0 : regular file */
743 /* flags & 0x04 : 1: need delete */
746 for (i = 0; i < filec; i++)
747 if (GETSTAT(filev[i], &stbuf) < 0) {
750 "LHa: Cannot access \"%s\", ignored.\n", filev[i]);
753 if (is_regularfile(&stbuf))
755 else if (is_directory(&stbuf))
758 else if (is_symlink(&stbuf)) /* t.okamoto */
764 "LHa: Cannot archive \"%s\", ignored.\n", filev[i]);
769 for (i = 0; i < filec; i++) {
771 if ((flags[i] & 0x07) == 0x00) { /* regular file, not
773 for (j = i + 1; j < filec; j++) {
774 if ((flags[j] & 0x07) == 0x00) { /* regular file, not
776 if (STREQU(p, filev[j]))
777 flags[j] = 0x04; /* delete */
781 else if ((flags[i] & 0x07) == 0x02) { /* directory, not
783 for (j = i + 1; j < filec; j++) {
784 if ((flags[j] & 0x07) == 0x00) { /* regular file, not
786 if (include_path_p(p, filev[j]))
787 flags[j] = 0x04; /* delete */
789 else if ((flags[j] & 0x07) == 0x02) { /* directory, not
791 if (include_path_p(p, filev[j]))
792 flags[j] = 0x04; /* delete */
798 for (i = j = 0; i < filec; i++) {
799 if ((flags[i] & 0x04) == 0) {
810 /* ------------------------------------------------------------------------ */
812 /* please need your imprementation */
814 find_files(name, v_filec, v_filev)
819 return FALSE; /* DUMMY */
822 /* ------------------------------------------------------------------------ */
824 free_files(filec, filev)
830 /* ------------------------------------------------------------------------ */
833 find_files(name, v_filec, v_filev)
838 struct string_pool sp;
839 char newname[FILENAME_LENGTH];
843 struct stat tmp_stbuf, arc_stbuf, fil_stbuf;
845 strcpy(newname, name);
847 if (len > 0 && newname[len - 1] != '/')
848 newname[len++] = '/';
850 dirp = opendir(name);
856 GETSTAT(temporary_name, &tmp_stbuf);
857 GETSTAT(archive_name, &arc_stbuf);
859 for (dp = readdir(dirp); dp != NULL; dp = readdir(dirp)) {
861 strncpy(newname + len, dp->d_name, n);
862 newname[len + n] = '\0';
863 if (GETSTAT(newname, &fil_stbuf) < 0)
866 if ( dp->d_name[0] != '.' ||
868 (dp->d_name[1] != '.' ||
870 add_sp(&sp, newname, len+n+1);
873 if ((dp->d_ino != 0) &&
874 /* exclude '.' and '..' */
875 ((dp->d_name[0] != '.') ||
877 ((dp->d_name[1] != '.') ||
879 ((tmp_stbuf.st_dev != fil_stbuf.st_dev ||
880 tmp_stbuf.st_ino != fil_stbuf.st_ino) &&
881 (arc_stbuf.st_dev != fil_stbuf.st_dev ||
882 arc_stbuf.st_ino != fil_stbuf.st_ino))) {
883 add_sp(&sp, newname, len + n + 1);
888 finish_sp(&sp, v_filec, v_filev);
890 qsort(*v_filev, *v_filec, sizeof(char *), sort_by_ascii);
891 cleaning_files(v_filec, v_filev);
896 /* ------------------------------------------------------------------------ */
898 free_files(filec, filev)
905 /* ------------------------------------------------------------------------ */
907 /* ------------------------------------------------------------------------ */
908 /* Build temporary file name and store to TEMPORARY_NAME */
910 build_temporary_name()
912 #ifdef TMP_FILENAME_TEMPLATE
913 /* "/tmp/lhXXXXXX" etc. */
914 if (extract_directory == NULL) {
915 strcpy(temporary_name, TMP_FILENAME_TEMPLATE);
918 sprintf(temporary_name, "%s/lhXXXXXX", extract_directory);
921 mkstemp(temporary_name);
923 mktemp(temporary_name);
928 strcpy(temporary_name, archive_name);
929 for (p = temporary_name, s = (char *) 0; *p; p++)
932 strcpy((s ? s + 1 : temporary_name), "lhXXXXXX");
934 mkstemp(temporary_name);
936 mktemp(temporary_name);
941 /* ------------------------------------------------------------------------ */
943 modify_filename_extention(buffer, ext)
947 register char *p, *dot;
949 for (p = buffer, dot = (char *) 0; *p; p++) {
962 /* ------------------------------------------------------------------------ */
963 /* build backup file name */
965 build_backup_name(buffer, original)
969 strcpy(buffer, original);
970 modify_filename_extention(buffer, BACKUPNAME_EXTENTION); /* ".bak" */
973 /* ------------------------------------------------------------------------ */
975 build_standard_archive_name(buffer, orginal)
979 strcpy(buffer, orginal);
980 modify_filename_extention(buffer, ARCHIVENAME_EXTENTION); /* ".lzh" */
983 /* ------------------------------------------------------------------------ */
985 /* ------------------------------------------------------------------------ */
995 for (i = 0; i < cmd_filec; i++) {
996 if (patmatch(cmd_filev[i], name, 0))
1009 if ((fp = fopen(name, mode)) == NULL)
1015 /* ------------------------------------------------------------------------ */
1017 /* ------------------------------------------------------------------------ */
1019 open_old_archive_1(name, v_fp)
1026 if (stat(name, &stbuf) >= 0 &&
1027 is_regularfile(&stbuf) &&
1028 (fp = fopen(name, READ_BINARY)) != NULL) {
1030 archive_file_gid = stbuf.st_gid;
1031 archive_file_mode = stbuf.st_mode;
1036 archive_file_gid = -1;
1040 /* ------------------------------------------------------------------------ */
1047 if (!strcmp(archive_name, "-")) {
1048 if (cmd == CMD_EXTRACT || cmd == CMD_LIST)
1053 if (p = (char *) strrchr(archive_name, '.')) {
1054 if (strucmp(".LZH", p) == 0
1055 || strucmp(".LZS", p) == 0
1056 || strucmp(".COM", p) == 0 /* DOS SFX */
1057 || strucmp(".EXE", p) == 0
1058 || strucmp(".X", p) == 0 /* HUMAN SFX */
1059 || strucmp(".BAK", p) == 0) { /* for BackUp */
1060 open_old_archive_1(archive_name, &fp);
1065 if (open_old_archive_1(archive_name, &fp))
1067 sprintf(expanded_archive_name, "%s.lzh", archive_name);
1068 if (open_old_archive_1(expanded_archive_name, &fp)) {
1069 archive_name = expanded_archive_name;
1073 * if ( (errno&0xffff)!=E_PNNF ) { archive_name =
1074 * expanded_archive_name; return NULL; }
1076 sprintf(expanded_archive_name, "%s.lzs", archive_name);
1077 if (open_old_archive_1(expanded_archive_name, &fp)) {
1078 archive_name = expanded_archive_name;
1082 * if ( (errno&0xffff)!=E_PNNF ) { archive_name =
1083 * expanded_archive_name; return NULL; }
1086 * sprintf( expanded_archive_name , "%s.lzh",archive_name);
1087 * archive_name = expanded_archive_name;
1092 /* ------------------------------------------------------------------------ */
1094 inquire(msg, name, selective)
1095 char *msg, *name, *selective;
1101 fprintf(stderr, "%s %s ", name, msg);
1104 fgets(buffer, 1024, stdin);
1106 for (p = selective; *p; p++)
1107 if (buffer[0] == *p)
1108 return p - selective;
1113 /* ------------------------------------------------------------------------ */
1115 write_archive_tail(nafp)
1121 /* ------------------------------------------------------------------------ */
1123 copy_old_one(oafp, nafp, hdr)
1128 fseek(oafp, (long) (hdr->header_size + 2) + hdr->packed_size, SEEK_CUR);
1131 reading_filename = archive_name;
1132 writting_filename = temporary_name;
1133 if (hdr->header_level != 2) {
1134 copyfile(oafp, nafp,
1135 (long) (hdr->header_size + 2) + hdr->packed_size, 0);
1137 copyfile(oafp, nafp,
1138 (long) (hdr->header_size) + hdr->packed_size, 0);
1143 /* Local Variables: */
1146 /* compile-command:"gcc -c lharc.c" */