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 /* ------------------------------------------------------------------------ */
47 static int cmd = CMD_UNKNOWN;
48 static int error_occurred;
50 /* static functions */
51 static void sort_files();
52 static void print_version();
54 extern int optional_archive_kanji_code;
55 extern int optional_system_kanji_code;
57 /* ------------------------------------------------------------------------ */
59 init_variable() /* Added N.Watazaki */
65 noexec = FALSE; /* debugging option */
68 compress_method = DEFAULT_LZHUFF_METHOD; /* defined in config.h */
70 header_level = 2; /* level 2 */
77 /* view command flags */
78 verbose_listing = FALSE;
80 /* extract command flags */
81 output_to_stdout = FALSE;
83 /* append command flags */
85 update_if_newer = FALSE;
86 delete_after_append = FALSE;
87 generic_format = FALSE;
89 recover_archive_when_interrupt = FALSE;
90 remove_extracting_file_when_interrupt = FALSE;
91 get_filename_from_stdin = FALSE;
92 ignore_directory = FALSE;
98 extract_directory = NULL;
101 #if BACKUP_OLD_ARCHIVE
102 backup_old_archive = TRUE;
104 backup_old_archive = FALSE;
107 extract_broken_archive = FALSE;
108 decode_macbinary_contents = FALSE;
109 sort_contents = TRUE;
110 recursive_archiving = TRUE;
114 /* ------------------------------------------------------------------------ */
115 /* NOTES : Text File Format */
116 /* GENERATOR NewLine */
117 /* [generic] 0D 0A */
119 /* [OS9][MacOS] 0D */
121 /* ------------------------------------------------------------------------ */
126 usage: lha [-]<commands>[<options>] [-<options> ...] archive_file [file...]\n\
127 commands: [axelvudmcpt]\n\
128 options: [q[012]vnfto[567]dizg012%s%s[w=<dir>|x=<pattern>]]\n\
129 long options: --system-kanji-code={euc,sjis,utf8,cap}\n\
130 --archive-kanji-code={euc,sjis,utf8,cap}\n\
131 --extract-broken-archive\n\
132 --convert-filename-case\n\
133 --ignore-mac-files\n\
142 #if HAVE_LIBAPPLEFILE
143 ,"b" /* decode_macbinary_contents */
154 LHarc for UNIX V 1.02 Copyright(C) 1989 Y.Tagawa\n\
155 LHx for MSDOS V C2.01 Copyright(C) 1990 H.Yoshizaki\n\
156 LHx(arc) for OSK V 2.01 Modified 1990 Momozou\n\
157 LHa for UNIX V 1.00 Copyright(C) 1992 Masaru Oki\n\
158 LHa for UNIX V 1.14 Modified 1995 Nobutaka Watazaki\n\
159 LHa for UNIX V 1.14i Modified 2000 Tsugio Okamoto\n\
160 Autoconfiscated 2001-2008 Koji Arai\n\
166 commands: options:\n\
167 a Add(or replace) to archive q{num} quiet (num:quiet mode)\n\
168 x,e EXtract from archive v verbose\n\
169 l,v List / Verbose List n not execute\n\
170 u Update newer files to archive f force (over write at extract)\n\
171 d Delete from archive t FILES are TEXT file\n");
174 m Move to archive (means 'ad') o[567] compression method (a/u/c)\n\
179 m Move to archive (means 'ad') o use LHarc compatible method (a/u/c)\n\
183 c re-Construct new archive d delete FILES after (a/u/c)\n\
184 p Print to STDOUT from archive i ignore directory path (x/e)\n\
185 t Test file CRC in archive z files not compress (a/u/c)\n\
186 g Generic format (for compatibility)\n\
187 0/1/2 header level (a/u/c)\n\
191 e TEXT code convert from/to EUC\n\
194 #if HAVE_LIBAPPLEFILE
196 b decode MacBinary (x/e)\n\
200 w=<dir> specify extract directory (x/e)\n\
201 x=<pattern> eXclude files (a/u/c)\n\
205 #include "getopt_long.h"
211 parse_suboption(int argc, char **argv)
216 SYSTEM_KANJI_CODE_OPTION,
217 ARCHIVE_KANJI_CODE_OPTION,
218 TRADITIONAL_BEHAVIOR,
223 struct option long_options[] = {
224 /* These options set a flag. */
225 {"help", no_argument, 0, HELP_OPTION},
226 {"version", no_argument, 0, VERSION_OPTION},
228 {"system-kanji-code", required_argument, 0, SYSTEM_KANJI_CODE_OPTION},
229 {"archive-kanji-code", required_argument, 0, ARCHIVE_KANJI_CODE_OPTION},
230 {"extract-broken-archive", no_argument, &extract_broken_archive, 1},
231 {"convert-filename-case", no_argument, &convertcase, TRUE},
232 {"traditional", no_argument, 0, TRADITIONAL_BEHAVIOR},
233 {"ignore-mac-files", no_argument, 0, IGNORE_MAC_FILES},
234 {"debug", required_argument, 0, DEBUG_OPTION},
239 char short_options[256] = "q[012]vnfto[567]dizg012ew:x:";
240 /* "[...]" means optional 1 byte argument (original extention) */
242 #if HAVE_LIBAPPLEFILE
243 strncat(short_options, "b", sizeof(short_options)-strlen(short_options)-1);
248 int option_index = 0;
249 int c = getopt_long(argc, argv,
250 short_options, long_options,
253 if (c == -1) break; /* end of options */
257 /* Already set a flag variable by the definition of the
272 /* In quiet mode, no confirm to overwrite */
279 case '0': /* no quiet */
280 case '1': /* no use the incremental indicator */
281 quiet_mode = *optarg - '0';
283 case '2': /* no output */
306 #if HAVE_LIBAPPLEFILE
308 decode_macbinary_contents = TRUE;
315 generic_format = TRUE;
319 delete_after_append = TRUE;
323 compress_method = LZHUFF1_METHOD_NUM;
329 compress_method = LZHUFF5_METHOD_NUM;
333 compress_method = LZHUFF6_METHOD_NUM;
336 compress_method = LZHUFF7_METHOD_NUM;
340 error("invalid compression method 'o%c'", *optarg);
345 compress_method = LZHUFF0_METHOD_NUM; /* Changed N.Watazaki */
348 ignore_directory = TRUE;
352 error("exclude files does not specified for `-x'");
356 for (i = 0; exclude_files && exclude_files[i]; i++)
358 exclude_files = (char**)xrealloc(exclude_files,
359 sizeof(char*) * (i+2));
363 exclude_files[i] = optarg;
364 exclude_files[i+1] = 0;
369 error("working directory does not specified for `-w'");
375 extract_directory = optarg;
386 case SYSTEM_KANJI_CODE_OPTION:
388 error("kanji code not specified for --%s",
389 long_options[option_index].name);
392 if (strcmp(optarg, "euc") == 0) {
393 optional_system_kanji_code = CODE_EUC;
395 else if (strcmp(optarg, "sjis") == 0) {
396 optional_system_kanji_code = CODE_SJIS;
398 else if (strcmp(optarg, "utf8") == 0) {
399 optional_system_kanji_code = CODE_UTF8;
401 else if (strcmp(optarg, "cap") == 0) {
402 optional_system_kanji_code = CODE_CAP;
405 error("unknown kanji code \"%s\"", optarg);
410 case ARCHIVE_KANJI_CODE_OPTION:
412 error("kanji code not specified for --%s",
413 long_options[option_index].name);
416 if (strcmp(optarg, "euc") == 0) {
417 optional_archive_kanji_code = CODE_EUC;
419 else if (strcmp(optarg, "sjis") == 0) {
420 optional_archive_kanji_code = CODE_SJIS;
422 else if (strcmp(optarg, "utf8") == 0) {
423 optional_archive_kanji_code = CODE_UTF8;
425 else if (strcmp(optarg, "cap") == 0) {
426 optional_archive_kanji_code = CODE_CAP;
429 error("unknown kanji code \"%s\"", optarg);
434 case TRADITIONAL_BEHAVIOR:
438 case IGNORE_MAC_FILES:
439 /* ignore Mac specific files (._*, .DS_Store and Icon\r)
441 for (i = 0; exclude_files && exclude_files[i]; i++)
443 exclude_files = (char**)xrealloc(exclude_files,
444 sizeof(char*) * (i+4));
446 exclude_files[i] = xstrdup("._*");
447 exclude_files[i+1] = xstrdup(".DS_Store");
448 exclude_files[i+2] = xstrdup("Icon\r");
449 exclude_files[i+3] = 0;
454 error("debugging item is not specified for --%s",
455 long_options[option_index].name);
458 if (strcmp(optarg, "nosort") == 0) {
459 sort_contents = FALSE;
461 else if (strcmp(optarg, "norecursion") == 0) {
462 recursive_archiving = FALSE;
464 else if (strcmp(optarg, "dumplzss") == 0) {
469 error("unknown debugging item \"%s\" for --%s",
470 optarg, long_options[option_index].name);
476 error("unknown option `-%c'.", c);
485 archive_name = *argv++;
496 Parse LHA command and options.
499 parse_option(int argc, char **argv)
503 if (argv[1] == NULL || strcmp(argv[1], "--help") == 0) {
508 if (strcmp(argv[1], "--version") == 0) {
513 if (argc == 2 && *argv[1] != '-') {
514 archive_name = argv[1];
523 if (cmd_char[0] == '-')
534 output_to_stdout = TRUE;
552 update_if_newer = TRUE;
557 delete_after_append = TRUE;
562 verbose_listing = TRUE;
576 error("unknown command `-%c'", *cmd_char);
580 if (cmd_char[1] == '\0') {
581 /* argv[1] is command name */
587 /* Eliminate command character
588 e.g.) lha cv foo.lzh -> lha -v foo.lzh
589 lha -cv foo.lzh -> lha -v foo.lzh
595 return parse_suboption(argc, argv);
598 /* ------------------------------------------------------------------------ */
608 init_variable(); /* Added N.Watazaki */
610 if (parse_option(argc, argv) == -1) {
617 error("archive file does not specified");
623 if (!strcmp(archive_name, "-")) {
624 if (!isatty(1) && cmd == CMD_ADD)
627 #if 0 /* Comment out; IMHO, this feature is useless. by Koji Arai */
629 if (argc == 3 && !isatty(0)) { /* 1999.7.18 */
630 /* Bug(?) on MinGW, isatty() return 0 on Cygwin console.
631 mingw-runtime-1.3-2 and Cygwin 1.3.10(0.51/3/2) on Win2000 */
632 get_filename_from_stdin = TRUE;
637 /* target file name */
638 if (get_filename_from_stdin) {
644 xfilev = (char **)xmalloc(sizeof(char *) * xfilec);
645 while (fgets(inpbuf, sizeof(inpbuf), stdin)) {
646 /* delete \n if it exist */
648 while (i < sizeof(inpbuf) && p != 0) {
656 if (cmd_filec >= xfilec) {
658 xfilev = (char **) xrealloc(xfilev,
659 sizeof(char *) * xfilec);
661 if (strlen(inpbuf) < 1)
663 xfilev[cmd_filec++] = xstrdup(inpbuf);
665 xfilev[cmd_filec] = NULL;
695 /* ------------------------------------------------------------------------ */
699 /* macro PACKAGE_NAME, PACKAGE_VERSION and PLATFORM are
700 defined in config.h by configure script */
701 fprintf(stderr, "%s version %s (%s)\n",
702 PACKAGE_NAME, PACKAGE_VERSION, PLATFORM);
703 fprintf(stderr, " configure options: %s\n", LHA_CONFIGURE_OPTIONS);
708 message(char *fmt, ...)
710 message(fmt, va_alist)
715 int errno_sv = errno;
718 fprintf(stderr, "LHa: ");
721 vfprintf(stderr, fmt, v);
729 /* ------------------------------------------------------------------------ */
732 warning(char *fmt, ...)
734 warning(fmt, va_alist)
739 int errno_sv = errno;
742 fprintf(stderr, "LHa: Warning: ");
745 vfprintf(stderr, fmt, v);
753 /* ------------------------------------------------------------------------ */
756 error(char *fmt, ...)
763 int errno_sv = errno;
766 fprintf(stderr, "LHa: Error: ");
769 vfprintf(stderr, fmt, v);
781 fatal_error(char *fmt, ...)
783 fatal_error(fmt, va_alist)
788 int errno_sv = errno;
791 fprintf(stderr, "LHa: Fatal error: ");
794 vfprintf(stderr, fmt, v);
798 fprintf(stderr, ": %s\n", strerror(errno_sv));
808 if (temporary_fd != -1) {
811 unlink(temporary_name);
814 if (recover_archive_when_interrupt) {
815 rename(backup_archive_name, archive_name);
816 recover_archive_when_interrupt = FALSE;
818 if (remove_extracting_file_when_interrupt) {
819 message("Removing: %s", writing_filename);
820 unlink(writing_filename);
821 remove_extracting_file_when_interrupt = FALSE;
829 message("Interrupted");
833 signal(SIGINT, SIG_DFL);
835 signal(SIGHUP, SIG_DFL);
837 kill(getpid(), signo);
840 /* ------------------------------------------------------------------------ */
842 /* ------------------------------------------------------------------------ */
847 register char *p, *q;
851 if (generic_format) {
853 c1 = *(unsigned char *) p++;
854 c2 = *(unsigned char *) q++;
866 while (*p == *q && *p != '\0')
868 return *(unsigned char *) p - *(unsigned char *) q;
872 /* ------------------------------------------------------------------------ */
876 if (cmd_filec > 1 && sort_contents)
877 qsort(cmd_filev, cmd_filec, sizeof(char *), sort_by_ascii);
880 /* ------------------------------------------------------------------------ */
885 void *p = malloc(size);
887 fatal_error("Not enough memory");
891 /* ------------------------------------------------------------------------ */
897 void *p = (char *) realloc(old, size);
899 fatal_error("Not enough memory");
907 int len = strlen(str);
908 char *p = (char *)xmalloc(len + 1);
909 strcpy(p, str); /* ok */
913 /* ------------------------------------------------------------------------ */
915 /* ------------------------------------------------------------------------ */
918 +-------------+-------------+------+-------------+----------+
919 | N A M E 1 \0| N A M E 2 \0| .... | N A M E n \0| |
920 +-------------+-------------+------+-------------+----------+
921 ^ ^ ^ buffer+0 buffer+used buffer+size
924 +---------------+---------------+------------- -----------------+
925 | pointer to | pointer to | pointer to ... pointer to |
926 | stringpool | N A M E 1 | N A M E 2 ... N A M E n |
927 +---------------+---------------+------------- -------------+
928 ^ malloc base returned
931 /* ------------------------------------------------------------------------ */
934 struct string_pool *sp;
936 sp->size = 1024 - 8; /* any ( >=0 ) */
939 sp->buffer = (char *) xmalloc(sp->size * sizeof(char));
942 /* ------------------------------------------------------------------------ */
944 add_sp(sp, name, len)
945 struct string_pool *sp;
946 char *name; /* stored '\0' at tail */
947 int len; /* include '\0' */
949 while (sp->used + len > sp->size) {
951 sp->buffer = (char *) xrealloc(sp->buffer, sp->size * sizeof(char));
953 memmove(sp->buffer + sp->used, name, len);
958 /* ------------------------------------------------------------------------ */
960 finish_sp(sp, v_count, v_vector)
961 register struct string_pool *sp;
969 v = (char **) xmalloc((sp->n + 1) * sizeof(char *));
974 for (i = sp->n; i; i--) {
981 /* ------------------------------------------------------------------------ */
987 free(*vector); /* free string pool */
992 /* ------------------------------------------------------------------------ */
993 /* READ DIRECTORY FILES */
994 /* ------------------------------------------------------------------------ */
996 include_path_p(path, name)
1001 if (*path++ != *n++)
1002 return (path[-1] == '/' && *n == '\0');
1003 return (*n == '/' || (n != name && path[-1] == '/' && n[-1] == '/'));
1006 /* ------------------------------------------------------------------------ */
1008 cleaning_files(v_filec, v_filev)
1015 register char **filev = *v_filev;
1016 register int filec = *v_filec;
1023 flags = xmalloc(filec * sizeof(char));
1025 /* flags & 0x01 : 1: ignore */
1026 /* flags & 0x02 : 1: directory, 0 : regular file */
1027 /* flags & 0x04 : 1: need delete */
1029 for (i = 0; i < filec; i++)
1030 if (GETSTAT(filev[i], &stbuf) < 0) {
1032 warning("Cannot access \"%s\" : %s; ignored.", filev[i],
1036 if (is_regularfile(&stbuf))
1038 else if (is_directory(&stbuf))
1041 else if (is_symlink(&stbuf)) /* t.okamoto */
1046 warning("Cannot archive \"%s\", ignored.", filev[i]);
1050 for (i = 0; i < filec; i++) {
1052 if ((flags[i] & 0x07) == 0x00) { /* regular file, not
1053 * deleted/ignored */
1054 for (j = i + 1; j < filec; j++) {
1055 if ((flags[j] & 0x07) == 0x00) { /* regular file, not
1056 * deleted/ignored */
1057 if (STREQU(p, filev[j]))
1058 flags[j] = 0x04; /* delete */
1062 else if ((flags[i] & 0x07) == 0x02) { /* directory, not
1063 * deleted/ignored */
1064 for (j = i + 1; j < filec; j++) {
1065 if ((flags[j] & 0x07) == 0x00) { /* regular file, not
1066 * deleted/ignored */
1067 if (include_path_p(p, filev[j]))
1068 flags[j] = 0x04; /* delete */
1070 else if ((flags[j] & 0x07) == 0x02) { /* directory, not
1071 * deleted/ignored */
1072 if (include_path_p(p, filev[j]))
1073 flags[j] = 0x04; /* delete */
1079 for (i = j = 0; i < filec; i++) {
1080 if ((flags[i] & 0x04) == 0) {
1082 filev[j] = filev[i];
1091 /* ------------------------------------------------------------------------ */
1093 find_files(name, v_filec, v_filev)
1098 struct string_pool sp;
1099 char newname[FILENAME_LENGTH];
1103 struct stat tmp_stbuf, arc_stbuf, fil_stbuf;
1104 int exist_tmp = 1, exist_arc = 1;
1106 len = str_safe_copy(newname, name, sizeof(newname));
1107 if (len > 0 && newname[len - 1] != '/') {
1108 if (len < sizeof(newname)-1)
1109 strcpy(&newname[len++], "/"); /* ok */
1111 warning("the length of pathname \"%s\" is too long.", name);
1114 dirp = opendir(name);
1120 if (GETSTAT(temporary_name, &tmp_stbuf) == -1)
1122 if (GETSTAT(archive_name, &arc_stbuf) == -1)
1125 while ((dp = readdir(dirp)) != NULL) {
1128 /* exclude '.' and '..' */
1129 if (strncmp(dp->d_name, ".", n) == 0
1130 || strncmp(dp->d_name, "..", n) == 0)
1133 /* exclude exclude_files supplied by user */
1134 for (i = 0; exclude_files && exclude_files[i]; i++) {
1135 if (fnmatch(exclude_files[i], dp->d_name,
1136 FNM_PATHNAME|FNM_NOESCAPE|FNM_PERIOD) == 0)
1140 if (len + n >= sizeof(newname)) {
1141 warning("filename is too long");
1145 strncpy(newname + len, dp->d_name, n);
1146 newname[len + n] = '\0';
1147 if (GETSTAT(newname, &fil_stbuf) < 0)
1150 #if defined(HAVE_STRUCT_STAT_ST_INO) && !__MINGW32__
1151 /* MinGW has meaningless st_ino */
1153 /* exclude temporary file, archive file and these links */
1155 tmp_stbuf.st_dev == fil_stbuf.st_dev &&
1156 tmp_stbuf.st_ino == fil_stbuf.st_ino)
1160 arc_stbuf.st_dev == fil_stbuf.st_dev &&
1161 arc_stbuf.st_ino == fil_stbuf.st_ino)
1164 add_sp(&sp, newname, len+n+1);
1169 finish_sp(&sp, v_filec, v_filev);
1170 if (*v_filec > 1 && sort_contents)
1171 qsort(*v_filev, *v_filec, sizeof(char *), sort_by_ascii);
1172 cleaning_files(v_filec, v_filev);
1177 /* ------------------------------------------------------------------------ */
1179 free_files(filec, filev)
1186 /* ------------------------------------------------------------------------ */
1188 /* ------------------------------------------------------------------------ */
1189 /* Build temporary file name and store to TEMPORARY_NAME */
1191 build_temporary_name()
1193 #ifdef TMP_FILENAME_TEMPLATE
1194 /* "/tmp/lhXXXXXX" etc. */
1195 if (extract_directory == NULL) {
1196 str_safe_copy(temporary_name, TMP_FILENAME_TEMPLATE,
1197 sizeof(temporary_name));
1200 xsnprintf(temporary_name, sizeof(temporary_name),
1201 "%s/lhXXXXXX", extract_directory);
1206 str_safe_copy(temporary_name, archive_name, sizeof(temporary_name));
1207 s = strrchr(temporary_name, '/');
1210 len = s - temporary_name;
1211 if (len + strlen("lhXXXXXX") < sizeof(temporary_name))
1212 /* use directory at archive file */
1213 strcpy(s, "lhXXXXXX"); /* ok */
1215 /* use current directory */
1216 str_safe_copy(temporary_name, "lhXXXXXX", sizeof(temporary_name));
1219 /* use current directory */
1220 str_safe_copy(temporary_name, "lhXXXXXX", sizeof(temporary_name));
1226 old_umask = umask(077);
1227 fd = mkstemp(temporary_name);
1235 mktemp(temporary_name);
1236 flags = O_CREAT|O_EXCL|O_RDWR;
1240 return open(temporary_name, flags, 0600);
1245 /* ------------------------------------------------------------------------ */
1247 modify_filename_extention(buffer, ext, size)
1252 register char *p, *dot;
1254 for (p = buffer, dot = (char *) 0; *p; p++) {
1264 str_safe_copy(p, ext, size - (p - buffer));
1267 /* ------------------------------------------------------------------------ */
1268 /* build backup file name */
1270 build_backup_name(buffer, original, size)
1275 str_safe_copy(buffer, original, size);
1276 modify_filename_extention(buffer, BACKUPNAME_EXTENTION, size); /* ".bak" */
1279 /* ------------------------------------------------------------------------ */
1281 build_standard_archive_name(buffer, original, size)
1286 str_safe_copy(buffer, original, size);
1287 modify_filename_extention(buffer, ARCHIVENAME_EXTENTION, size); /* ".lzh" */
1290 /* ------------------------------------------------------------------------ */
1292 /* ------------------------------------------------------------------------ */
1302 for (i = 0; i < cmd_filec; i++) {
1303 if (patmatch(cmd_filev[i], name, 0))
1316 if ((fp = fopen(name, mode)) == NULL)
1317 fatal_error("Cannot open file \"%s\"", name);
1322 /* ------------------------------------------------------------------------ */
1324 /* ------------------------------------------------------------------------ */
1326 open_old_archive_1(name, v_fp)
1333 if (stat(name, &stbuf) >= 0 &&
1334 is_regularfile(&stbuf) &&
1335 (fp = fopen(name, READ_BINARY)) != NULL) {
1337 archive_file_gid = stbuf.st_gid;
1338 archive_file_mode = stbuf.st_mode;
1343 archive_file_gid = -1;
1347 /* ------------------------------------------------------------------------ */
1352 char *p = NULL, *ext, *ext2;
1353 static char expanded_archive_name[FILENAME_LENGTH];
1355 if (!strcmp(archive_name, "-")) {
1356 if (cmd == CMD_EXTRACT || cmd == CMD_LIST) {
1357 #if defined(__MINGW32__) || defined(__DJGPP__)
1358 setmode(fileno(stdin), O_BINARY);
1366 ext2 = strrchr(archive_name, '.');
1375 p = xstrdup("lzh," ADDITIONAL_SUFFIXES);
1376 for (ext = strtok(p, ",");
1378 ext = strtok(NULL, ",")) {
1380 if (*ext == 0) continue;
1382 if (strcasecmp(ext, ext2)) {
1383 /* Try to open file just specified filename
1384 with usual suffixes.
1385 Return NULL if the file is not exist. */
1387 open_old_archive_1(archive_name, &fp);
1388 goto ret; /* found or not */
1395 /* Try to open file just specified filename */
1396 if (open_old_archive_1(archive_name, &fp))
1397 goto ret; /* found */
1399 /* Try to open file with implicit suffixes */
1400 p = xstrdup("lzh," ADDITIONAL_SUFFIXES);
1401 for (ext = strtok(p, ",");
1403 ext = strtok(NULL, ",")) {
1405 if (*ext == 0) continue;
1407 xsnprintf(expanded_archive_name, sizeof(expanded_archive_name),
1408 "%s.%s", archive_name, ext);
1410 if (open_old_archive_1(expanded_archive_name, &fp)) {
1411 archive_name = expanded_archive_name;
1412 goto ret; /* found */
1421 /* ------------------------------------------------------------------------ */
1423 inquire(msg, name, selective)
1424 char *msg, *name, *selective;
1430 fprintf(stderr, "%s %s ", name, msg);
1433 fgets(buffer, 1024, stdin);
1435 for (p = selective; *p; p++)
1436 if (buffer[0] == *p)
1437 return p - selective;
1442 /* ------------------------------------------------------------------------ */
1444 write_archive_tail(nafp)
1450 /* ------------------------------------------------------------------------ */
1452 copy_old_one(oafp, nafp, hdr)
1457 fseeko(oafp, hdr->header_size + hdr->packed_size, SEEK_CUR);
1460 reading_filename = archive_name;
1461 writing_filename = temporary_name;
1462 copyfile(oafp, nafp, hdr->header_size + hdr->packed_size, 0, 0);