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 **) xrealloc(xfilev,
409 sizeof(char *) * xfilec);
412 if (strlen(inpbuf) < 1)
414 xfilev[cmd_filec++] = xstrdup(inpbuf);
416 xfilev[cmd_filec] = NULL;
451 /* ------------------------------------------------------------------------ */
453 /* ------------------------------------------------------------------------ */
455 /* ------------------------------------------------------------------------ */
459 fprintf(stderr, "%s\n", LHA_VERSION);
462 /* ------------------------------------------------------------------------ */
464 message_1(title, subject, name)
465 char *title, *subject, *name;
467 fprintf(stderr, "LHa: %s%s ", title, subject);
471 fprintf(stderr, "%s\n", name);
476 /* ------------------------------------------------------------------------ */
478 message(subject, name)
479 char *subject, *name;
481 message_1("", subject, name);
484 /* ------------------------------------------------------------------------ */
486 warning(subject, name)
487 char *subject, *name;
489 message_1("Warning: ", subject, name);
492 /* ------------------------------------------------------------------------ */
497 message_1("Error: ", subject, msg);
500 /* ------------------------------------------------------------------------ */
505 message_1("Fatal error:", "", msg);
507 if (remove_temporary_at_error)
508 unlink(temporary_name);
513 /* ------------------------------------------------------------------------ */
517 fatal_error(writting_filename);
520 /* ------------------------------------------------------------------------ */
524 fatal_error(reading_filename);
527 /* ------------------------------------------------------------------------ */
533 message("Interrupted\n", "");
536 fclose(temporary_fp);
537 unlink(temporary_name);
538 if (recover_archive_when_interrupt)
539 rename(backup_archive_name, archive_name);
540 if (remove_extracting_file_when_interrupt) {
542 message("Removing", writting_filename);
543 unlink(writting_filename);
545 signal(SIGINT, SIG_DFL);
547 signal(SIGHUP, SIG_DFL);
549 kill(getpid(), signo);
552 /* ------------------------------------------------------------------------ */
554 /* ------------------------------------------------------------------------ */
559 register char *p, *q;
563 if (generic_format) {
565 c1 = *(unsigned char *) p++;
566 c2 = *(unsigned char *) q++;
578 while (*p == *q && *p != '\0')
580 return *(unsigned char *) p - *(unsigned char *) q;
584 /* ------------------------------------------------------------------------ */
589 qsort(cmd_filev, cmd_filec, sizeof(char *), sort_by_ascii);
592 /* ------------------------------------------------------------------------ */
597 char *p = (char *) malloc(size);
599 fatal_error("Not enough memory");
603 /* ------------------------------------------------------------------------ */
609 char *p = (char *) realloc(old, size);
611 fatal_error("Not enough memory");
619 int len = strlen(str);
620 char *p = (char *)xmalloc(str + 1);
622 fatal_error("Not enough memory");
627 /* ------------------------------------------------------------------------ */
629 /* ------------------------------------------------------------------------ */
632 +-------------+-------------+------+-------------+----------+
633 | N A M E 1 \0| N A M E 2 \0| .... | N A M E n \0| |
634 +-------------+-------------+------+-------------+----------+
635 ^ ^ ^ buffer+0 buffer+used buffer+size
638 +---------------+---------------+------------- -----------------+
639 | pointer to | pointer to | pointer to ... pointer to |
640 | stringpool | N A M E 1 | N A M E 2 ... N A M E n |
641 +---------------+---------------+------------- -------------+
642 ^ malloc base returned
645 /* ------------------------------------------------------------------------ */
648 struct string_pool *sp;
650 sp->size = 1024 - 8; /* any ( >=0 ) */
653 sp->buffer = (char *) xmalloc(sp->size * sizeof(char));
656 /* ------------------------------------------------------------------------ */
658 add_sp(sp, name, len)
659 struct string_pool *sp;
660 char *name; /* stored '\0' at tail */
661 int len; /* include '\0' */
663 while (sp->used + len > sp->size) {
665 sp->buffer = (char *) xrealloc(sp->buffer, sp->size * sizeof(char));
667 bcopy(name, sp->buffer + sp->used, len);
672 /* ------------------------------------------------------------------------ */
674 finish_sp(sp, v_count, v_vector)
675 register struct string_pool *sp;
683 v = (char **) xmalloc((sp->n + 1) * sizeof(char *));
688 for (i = sp->n; i; i--) {
695 /* ------------------------------------------------------------------------ */
701 free(*vector); /* free string pool */
706 /* ------------------------------------------------------------------------ */
707 /* READ DIRECTORY FILES */
708 /* ------------------------------------------------------------------------ */
710 include_path_p(path, name)
716 return (path[-1] == '/' && *n == '\0');
717 return (*n == '/' || (n != name && path[-1] == '/' && n[-1] == '/'));
720 /* ------------------------------------------------------------------------ */
722 cleaning_files(v_filec, v_filev)
729 register char **filev = *v_filev;
730 register int filec = *v_filec;
737 flags = xmalloc(filec * sizeof(char));
739 /* flags & 0x01 : 1: ignore */
740 /* flags & 0x02 : 1: directory, 0 : regular file */
741 /* flags & 0x04 : 1: need delete */
744 for (i = 0; i < filec; i++)
745 if (GETSTAT(filev[i], &stbuf) < 0) {
748 "LHa: Cannot access \"%s\", ignored.\n", filev[i]);
751 if (is_regularfile(&stbuf))
753 else if (is_directory(&stbuf))
756 else if (is_symlink(&stbuf)) /* t.okamoto */
762 "LHa: Cannot archive \"%s\", ignored.\n", filev[i]);
767 for (i = 0; i < filec; i++) {
769 if ((flags[i] & 0x07) == 0x00) { /* regular file, not
771 for (j = i + 1; j < filec; j++) {
772 if ((flags[j] & 0x07) == 0x00) { /* regular file, not
774 if (STREQU(p, filev[j]))
775 flags[j] = 0x04; /* delete */
779 else if ((flags[i] & 0x07) == 0x02) { /* directory, not
781 for (j = i + 1; j < filec; j++) {
782 if ((flags[j] & 0x07) == 0x00) { /* regular file, not
784 if (include_path_p(p, filev[j]))
785 flags[j] = 0x04; /* delete */
787 else if ((flags[j] & 0x07) == 0x02) { /* directory, not
789 if (include_path_p(p, filev[j]))
790 flags[j] = 0x04; /* delete */
796 for (i = j = 0; i < filec; i++) {
797 if ((flags[i] & 0x04) == 0) {
808 /* ------------------------------------------------------------------------ */
810 /* please need your imprementation */
812 find_files(name, v_filec, v_filev)
817 return FALSE; /* DUMMY */
820 /* ------------------------------------------------------------------------ */
822 free_files(filec, filev)
828 /* ------------------------------------------------------------------------ */
831 find_files(name, v_filec, v_filev)
836 struct string_pool sp;
837 char newname[FILENAME_LENGTH];
841 struct stat tmp_stbuf, arc_stbuf, fil_stbuf;
843 strcpy(newname, name);
845 if (len > 0 && newname[len - 1] != '/')
846 newname[len++] = '/';
848 dirp = opendir(name);
854 GETSTAT(temporary_name, &tmp_stbuf);
855 GETSTAT(archive_name, &arc_stbuf);
857 for (dp = readdir(dirp); dp != NULL; dp = readdir(dirp)) {
859 strncpy(newname + len, dp->d_name, n);
860 newname[len + n] = '\0';
861 if (GETSTAT(newname, &fil_stbuf) < 0)
864 if ( dp->d_name[0] != '.' ||
866 (dp->d_name[1] != '.' ||
868 add_sp(&sp, newname, len+n+1);
871 if ((dp->d_ino != 0) &&
872 /* exclude '.' and '..' */
873 ((dp->d_name[0] != '.') ||
875 ((dp->d_name[1] != '.') ||
877 ((tmp_stbuf.st_dev != fil_stbuf.st_dev ||
878 tmp_stbuf.st_ino != fil_stbuf.st_ino) &&
879 (arc_stbuf.st_dev != fil_stbuf.st_dev ||
880 arc_stbuf.st_ino != fil_stbuf.st_ino))) {
881 add_sp(&sp, newname, len + n + 1);
886 finish_sp(&sp, v_filec, v_filev);
888 qsort(*v_filev, *v_filec, sizeof(char *), sort_by_ascii);
889 cleaning_files(v_filec, v_filev);
894 /* ------------------------------------------------------------------------ */
896 free_files(filec, filev)
903 /* ------------------------------------------------------------------------ */
905 /* ------------------------------------------------------------------------ */
906 /* Build temporary file name and store to TEMPORARY_NAME */
908 build_temporary_name()
910 #ifdef TMP_FILENAME_TEMPLATE
911 /* "/tmp/lhXXXXXX" etc. */
912 if (extract_directory == NULL) {
913 strcpy(temporary_name, TMP_FILENAME_TEMPLATE);
916 sprintf(temporary_name, "%s/lhXXXXXX", extract_directory);
919 mkstemp(temporary_name);
921 mktemp(temporary_name);
926 strcpy(temporary_name, archive_name);
927 for (p = temporary_name, s = (char *) 0; *p; p++)
930 strcpy((s ? s + 1 : temporary_name), "lhXXXXXX");
932 mkstemp(temporary_name);
934 mktemp(temporary_name);
939 /* ------------------------------------------------------------------------ */
941 modify_filename_extention(buffer, ext)
945 register char *p, *dot;
947 for (p = buffer, dot = (char *) 0; *p; p++) {
960 /* ------------------------------------------------------------------------ */
961 /* build backup file name */
963 build_backup_name(buffer, original)
967 strcpy(buffer, original);
968 modify_filename_extention(buffer, BACKUPNAME_EXTENTION); /* ".bak" */
971 /* ------------------------------------------------------------------------ */
973 build_standard_archive_name(buffer, orginal)
977 strcpy(buffer, orginal);
978 modify_filename_extention(buffer, ARCHIVENAME_EXTENTION); /* ".lzh" */
981 /* ------------------------------------------------------------------------ */
983 /* ------------------------------------------------------------------------ */
993 for (i = 0; i < cmd_filec; i++) {
994 if (patmatch(cmd_filev[i], name, 0))
1007 if ((fp = fopen(name, mode)) == NULL)
1013 /* ------------------------------------------------------------------------ */
1015 /* ------------------------------------------------------------------------ */
1017 open_old_archive_1(name, v_fp)
1024 if (stat(name, &stbuf) >= 0 &&
1025 is_regularfile(&stbuf) &&
1026 (fp = fopen(name, READ_BINARY)) != NULL) {
1028 archive_file_gid = stbuf.st_gid;
1029 archive_file_mode = stbuf.st_mode;
1034 archive_file_gid = -1;
1038 /* ------------------------------------------------------------------------ */
1045 if (!strcmp(archive_name, "-")) {
1046 if (cmd == CMD_EXTRACT || cmd == CMD_LIST)
1051 if (p = (char *) strrchr(archive_name, '.')) {
1052 if (strucmp(".LZH", p) == 0
1053 || strucmp(".LZS", p) == 0
1054 || strucmp(".COM", p) == 0 /* DOS SFX */
1055 || strucmp(".EXE", p) == 0
1056 || strucmp(".X", p) == 0 /* HUMAN SFX */
1057 || strucmp(".BAK", p) == 0) { /* for BackUp */
1058 open_old_archive_1(archive_name, &fp);
1063 if (open_old_archive_1(archive_name, &fp))
1065 sprintf(expanded_archive_name, "%s.lzh", archive_name);
1066 if (open_old_archive_1(expanded_archive_name, &fp)) {
1067 archive_name = expanded_archive_name;
1071 * if ( (errno&0xffff)!=E_PNNF ) { archive_name =
1072 * expanded_archive_name; return NULL; }
1074 sprintf(expanded_archive_name, "%s.lzs", archive_name);
1075 if (open_old_archive_1(expanded_archive_name, &fp)) {
1076 archive_name = expanded_archive_name;
1080 * if ( (errno&0xffff)!=E_PNNF ) { archive_name =
1081 * expanded_archive_name; return NULL; }
1084 * sprintf( expanded_archive_name , "%s.lzh",archive_name);
1085 * archive_name = expanded_archive_name;
1090 /* ------------------------------------------------------------------------ */
1092 inquire(msg, name, selective)
1093 char *msg, *name, *selective;
1099 fprintf(stderr, "%s %s ", name, msg);
1102 fgets(buffer, 1024, stdin);
1104 for (p = selective; *p; p++)
1105 if (buffer[0] == *p)
1106 return p - selective;
1111 /* ------------------------------------------------------------------------ */
1113 write_archive_tail(nafp)
1119 /* ------------------------------------------------------------------------ */
1121 copy_old_one(oafp, nafp, hdr)
1126 fseek(oafp, (long) (hdr->header_size + 2) + hdr->packed_size, SEEK_CUR);
1129 reading_filename = archive_name;
1130 writting_filename = temporary_name;
1131 if (hdr->header_level != 2) {
1132 copyfile(oafp, nafp,
1133 (long) (hdr->header_size + 2) + hdr->packed_size, 0);
1135 copyfile(oafp, nafp,
1136 (long) (hdr->header_size) + hdr->packed_size, 0);
1141 /* Local Variables: */
1144 /* compile-command:"gcc -c lharc.c" */