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 if ((av[i] = strdup( argv[i] )) == NULL)
203 fatal_error("not enough memory\n");
207 print_tiny_usage_and_exit();
209 if (strcmp(av[1], "--version") == 0) {
216 av--; /* argv--; */ /* 1999.7.18 */
233 output_to_stdout = TRUE;
251 update_if_newer = TRUE;
256 delete_after_append = TRUE;
261 verbose_listing = TRUE;
275 print_tiny_usage_and_exit();
280 /* p = &argv[1][1]; */
288 quiet_mode = *p - '0';
318 generic_format = TRUE;
319 noconvertcase = TRUE;
323 delete_after_append = TRUE;
328 compress_method = LZHUFF1_METHOD_NUM;
332 compress_method = LZHUFF5_METHOD_NUM;
337 compress_method = LZHUFF6_METHOD_NUM;
341 compress_method = LZHUFF7_METHOD_NUM;
346 fprintf(stderr, "LHa: error option o%c\n", p[-1]);
351 compress_method = LZHUFF0_METHOD_NUM; /* Changed N.Watazaki */
354 ignore_directory = TRUE;
359 extract_directory = p;
364 header_level = HEADER_LEVEL0;
367 header_level = HEADER_LEVEL1;
370 header_level = HEADER_LEVEL2;
373 fprintf(stderr, "LHa: Unknown option '%c'.\n", p[-1]);
379 /* archive file name */
380 archive_name = av[2];
382 if (!strcmp(archive_name, "-")) {
383 if (!isatty(1) && cmd == CMD_ADD)
387 if (ac == 3 && !isatty(0)) { /* 1999.7.18 */
388 get_filename_from_stdin = TRUE;
392 /* target file name */
393 if (get_filename_from_stdin) {
395 xfilev = (char **)xmalloc(sizeof(char *) * xfilec);
396 while (fgets(inpbuf, sizeof(inpbuf), stdin)) {
397 /* delete \n if it exist */
399 while (i < sizeof(inpbuf) && p != 0) {
407 if (cmd_filec >= xfilec) {
409 cmd_filev = (char **) realloc(xfilev,
410 sizeof(char *) * xfilec);
411 if (cmd_filev == NULL)
412 fatal_error("Virtual memory exhausted\n");
415 if (strlen(inpbuf) < 1)
417 if ((xfilev[cmd_filec++] = (char *) strdup(inpbuf)) == NULL)
418 fatal_error("Virtual memory exhausted\n");
420 xfilev[cmd_filec] = NULL;
455 /* ------------------------------------------------------------------------ */
457 /* ------------------------------------------------------------------------ */
459 /* ------------------------------------------------------------------------ */
463 fprintf(stderr, "%s\n", LHA_VERSION);
466 /* ------------------------------------------------------------------------ */
468 message_1(title, subject, name)
469 char *title, *subject, *name;
471 fprintf(stderr, "LHa: %s%s ", title, subject);
475 fprintf(stderr, "%s\n", name);
480 /* ------------------------------------------------------------------------ */
482 message(subject, name)
483 char *subject, *name;
485 message_1("", subject, name);
488 /* ------------------------------------------------------------------------ */
490 warning(subject, name)
491 char *subject, *name;
493 message_1("Warning: ", subject, name);
496 /* ------------------------------------------------------------------------ */
501 message_1("Error: ", subject, msg);
504 /* ------------------------------------------------------------------------ */
509 message_1("Fatal error:", "", msg);
511 if (remove_temporary_at_error)
512 unlink(temporary_name);
517 /* ------------------------------------------------------------------------ */
521 fatal_error(writting_filename);
524 /* ------------------------------------------------------------------------ */
528 fatal_error(reading_filename);
531 /* ------------------------------------------------------------------------ */
537 message("Interrupted\n", "");
540 fclose(temporary_fp);
541 unlink(temporary_name);
542 if (recover_archive_when_interrupt)
543 rename(backup_archive_name, archive_name);
544 if (remove_extracting_file_when_interrupt) {
546 message("Removing", writting_filename);
547 unlink(writting_filename);
549 signal(SIGINT, SIG_DFL);
551 signal(SIGHUP, SIG_DFL);
553 kill(getpid(), signo);
556 /* ------------------------------------------------------------------------ */
558 /* ------------------------------------------------------------------------ */
563 register char *p, *q;
567 if (generic_format) {
569 c1 = *(unsigned char *) p++;
570 c2 = *(unsigned char *) q++;
582 while (*p == *q && *p != '\0')
584 return *(unsigned char *) p - *(unsigned char *) q;
588 /* ------------------------------------------------------------------------ */
593 qsort(cmd_filev, cmd_filec, sizeof(char *), sort_by_ascii);
596 /* ------------------------------------------------------------------------ */
601 char *p = (char *) malloc(size);
603 fatal_error("Not enough memory");
607 /* ------------------------------------------------------------------------ */
613 char *p = (char *) realloc(old, size);
615 fatal_error("Not enough memory");
619 /* ------------------------------------------------------------------------ */
621 /* ------------------------------------------------------------------------ */
624 +-------------+-------------+------+-------------+----------+
625 | N A M E 1 \0| N A M E 2 \0| .... | N A M E n \0| |
626 +-------------+-------------+------+-------------+----------+
627 ^ ^ ^ buffer+0 buffer+used buffer+size
630 +---------------+---------------+------------- -----------------+
631 | pointer to | pointer to | pointer to ... pointer to |
632 | stringpool | N A M E 1 | N A M E 2 ... N A M E n |
633 +---------------+---------------+------------- -------------+
634 ^ malloc base returned
637 /* ------------------------------------------------------------------------ */
640 struct string_pool *sp;
642 sp->size = 1024 - 8; /* any ( >=0 ) */
645 sp->buffer = (char *) xmalloc(sp->size * sizeof(char));
648 /* ------------------------------------------------------------------------ */
650 add_sp(sp, name, len)
651 struct string_pool *sp;
652 char *name; /* stored '\0' at tail */
653 int len; /* include '\0' */
655 while (sp->used + len > sp->size) {
657 sp->buffer = (char *) xrealloc(sp->buffer, sp->size * sizeof(char));
659 bcopy(name, sp->buffer + sp->used, len);
664 /* ------------------------------------------------------------------------ */
666 finish_sp(sp, v_count, v_vector)
667 register struct string_pool *sp;
675 v = (char **) xmalloc((sp->n + 1) * sizeof(char *));
680 for (i = sp->n; i; i--) {
687 /* ------------------------------------------------------------------------ */
693 free(*vector); /* free string pool */
698 /* ------------------------------------------------------------------------ */
699 /* READ DIRECTORY FILES */
700 /* ------------------------------------------------------------------------ */
702 include_path_p(path, name)
708 return (path[-1] == '/' && *n == '\0');
709 return (*n == '/' || (n != name && path[-1] == '/' && n[-1] == '/'));
712 /* ------------------------------------------------------------------------ */
714 cleaning_files(v_filec, v_filev)
721 register char **filev = *v_filev;
722 register int filec = *v_filec;
729 flags = xmalloc(filec * sizeof(char));
731 /* flags & 0x01 : 1: ignore */
732 /* flags & 0x02 : 1: directory, 0 : regular file */
733 /* flags & 0x04 : 1: need delete */
736 for (i = 0; i < filec; i++)
737 if (GETSTAT(filev[i], &stbuf) < 0) {
740 "LHa: Cannot access \"%s\", ignored.\n", filev[i]);
743 if (is_regularfile(&stbuf))
745 else if (is_directory(&stbuf))
748 else if (is_symlink(&stbuf)) /* t.okamoto */
754 "LHa: Cannot archive \"%s\", ignored.\n", filev[i]);
759 for (i = 0; i < filec; i++) {
761 if ((flags[i] & 0x07) == 0x00) { /* regular file, not
763 for (j = i + 1; j < filec; j++) {
764 if ((flags[j] & 0x07) == 0x00) { /* regular file, not
766 if (STREQU(p, filev[j]))
767 flags[j] = 0x04; /* delete */
771 else if ((flags[i] & 0x07) == 0x02) { /* directory, not
773 for (j = i + 1; j < filec; j++) {
774 if ((flags[j] & 0x07) == 0x00) { /* regular file, not
776 if (include_path_p(p, filev[j]))
777 flags[j] = 0x04; /* delete */
779 else if ((flags[j] & 0x07) == 0x02) { /* directory, not
781 if (include_path_p(p, filev[j]))
782 flags[j] = 0x04; /* delete */
788 for (i = j = 0; i < filec; i++) {
789 if ((flags[i] & 0x04) == 0) {
800 /* ------------------------------------------------------------------------ */
802 /* please need your imprementation */
804 find_files(name, v_filec, v_filev)
809 return FALSE; /* DUMMY */
812 /* ------------------------------------------------------------------------ */
814 free_files(filec, filev)
820 /* ------------------------------------------------------------------------ */
823 find_files(name, v_filec, v_filev)
828 struct string_pool sp;
829 char newname[FILENAME_LENGTH];
833 struct stat tmp_stbuf, arc_stbuf, fil_stbuf;
835 strcpy(newname, name);
837 if (len > 0 && newname[len - 1] != '/')
838 newname[len++] = '/';
840 dirp = opendir(name);
846 GETSTAT(temporary_name, &tmp_stbuf);
847 GETSTAT(archive_name, &arc_stbuf);
849 for (dp = readdir(dirp); dp != NULL; dp = readdir(dirp)) {
851 strncpy(newname + len, dp->d_name, n);
852 newname[len + n] = '\0';
853 if (GETSTAT(newname, &fil_stbuf) < 0)
856 if ( dp->d_name[0] != '.' ||
858 (dp->d_name[1] != '.' ||
860 add_sp(&sp, newname, len+n+1);
863 if ((dp->d_ino != 0) &&
864 /* exclude '.' and '..' */
865 ((dp->d_name[0] != '.') ||
867 ((dp->d_name[1] != '.') ||
869 ((tmp_stbuf.st_dev != fil_stbuf.st_dev ||
870 tmp_stbuf.st_ino != fil_stbuf.st_ino) &&
871 (arc_stbuf.st_dev != fil_stbuf.st_dev ||
872 arc_stbuf.st_ino != fil_stbuf.st_ino))) {
873 add_sp(&sp, newname, len + n + 1);
878 finish_sp(&sp, v_filec, v_filev);
880 qsort(*v_filev, *v_filec, sizeof(char *), sort_by_ascii);
881 cleaning_files(v_filec, v_filev);
886 /* ------------------------------------------------------------------------ */
888 free_files(filec, filev)
895 /* ------------------------------------------------------------------------ */
897 /* ------------------------------------------------------------------------ */
898 /* Build temporary file name and store to TEMPORARY_NAME */
900 build_temporary_name()
902 #ifdef TMP_FILENAME_TEMPLATE
903 /* "/tmp/lhXXXXXX" etc. */
904 if (extract_directory == NULL) {
905 strcpy(temporary_name, TMP_FILENAME_TEMPLATE);
908 sprintf(temporary_name, "%s/lhXXXXXX", extract_directory);
911 mkstemp(temporary_name);
913 mktemp(temporary_name);
918 strcpy(temporary_name, archive_name);
919 for (p = temporary_name, s = (char *) 0; *p; p++)
922 strcpy((s ? s + 1 : temporary_name), "lhXXXXXX");
924 mkstemp(temporary_name);
926 mktemp(temporary_name);
931 /* ------------------------------------------------------------------------ */
933 modify_filename_extention(buffer, ext)
937 register char *p, *dot;
939 for (p = buffer, dot = (char *) 0; *p; p++) {
952 /* ------------------------------------------------------------------------ */
953 /* build backup file name */
955 build_backup_name(buffer, original)
959 strcpy(buffer, original);
960 modify_filename_extention(buffer, BACKUPNAME_EXTENTION); /* ".bak" */
963 /* ------------------------------------------------------------------------ */
965 build_standard_archive_name(buffer, orginal)
969 strcpy(buffer, orginal);
970 modify_filename_extention(buffer, ARCHIVENAME_EXTENTION); /* ".lzh" */
973 /* ------------------------------------------------------------------------ */
975 /* ------------------------------------------------------------------------ */
985 for (i = 0; i < cmd_filec; i++) {
986 if (patmatch(cmd_filev[i], name, 0))
999 if ((fp = fopen(name, mode)) == NULL)
1005 /* ------------------------------------------------------------------------ */
1007 /* ------------------------------------------------------------------------ */
1009 open_old_archive_1(name, v_fp)
1016 if (stat(name, &stbuf) >= 0 &&
1017 is_regularfile(&stbuf) &&
1018 (fp = fopen(name, READ_BINARY)) != NULL) {
1020 archive_file_gid = stbuf.st_gid;
1021 archive_file_mode = stbuf.st_mode;
1026 archive_file_gid = -1;
1030 /* ------------------------------------------------------------------------ */
1037 if (!strcmp(archive_name, "-")) {
1038 if (cmd == CMD_EXTRACT || cmd == CMD_LIST)
1043 if (p = (char *) strrchr(archive_name, '.')) {
1044 if (strucmp(".LZH", p) == 0
1045 || strucmp(".LZS", p) == 0
1046 || strucmp(".COM", p) == 0 /* DOS SFX */
1047 || strucmp(".EXE", p) == 0
1048 || strucmp(".X", p) == 0 /* HUMAN SFX */
1049 || strucmp(".BAK", p) == 0) { /* for BackUp */
1050 open_old_archive_1(archive_name, &fp);
1055 if (open_old_archive_1(archive_name, &fp))
1057 sprintf(expanded_archive_name, "%s.lzh", archive_name);
1058 if (open_old_archive_1(expanded_archive_name, &fp)) {
1059 archive_name = expanded_archive_name;
1063 * if ( (errno&0xffff)!=E_PNNF ) { archive_name =
1064 * expanded_archive_name; return NULL; }
1066 sprintf(expanded_archive_name, "%s.lzs", archive_name);
1067 if (open_old_archive_1(expanded_archive_name, &fp)) {
1068 archive_name = expanded_archive_name;
1072 * if ( (errno&0xffff)!=E_PNNF ) { archive_name =
1073 * expanded_archive_name; return NULL; }
1076 * sprintf( expanded_archive_name , "%s.lzh",archive_name);
1077 * archive_name = expanded_archive_name;
1082 /* ------------------------------------------------------------------------ */
1084 inquire(msg, name, selective)
1085 char *msg, *name, *selective;
1091 fprintf(stderr, "%s %s ", name, msg);
1094 fgets(buffer, 1024, stdin);
1096 for (p = selective; *p; p++)
1097 if (buffer[0] == *p)
1098 return p - selective;
1103 /* ------------------------------------------------------------------------ */
1105 write_archive_tail(nafp)
1111 /* ------------------------------------------------------------------------ */
1113 copy_old_one(oafp, nafp, hdr)
1118 fseek(oafp, (long) (hdr->header_size + 2) + hdr->packed_size, SEEK_CUR);
1121 reading_filename = archive_name;
1122 writting_filename = temporary_name;
1123 if (hdr->header_level != 2) {
1124 copyfile(oafp, nafp,
1125 (long) (hdr->header_size + 2) + hdr->packed_size, 0);
1127 copyfile(oafp, nafp,
1128 (long) (hdr->header_size) + hdr->packed_size, 0);
1133 /* Local Variables: */
1136 /* compile-command:"gcc -c lharc.c" */