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;
111 /* ------------------------------------------------------------------------ */
112 /* NOTES : Text File Format */
113 /* GENERATOR NewLine */
114 /* [generic] 0D 0A */
116 /* [OS9][MacOS] 0D */
118 /* ------------------------------------------------------------------------ */
123 usage: lha [-]<commands>[<options>] [-<options> ...] archive_file [file...]\n\
124 commands: [axelvudmcpt]\n\
125 options: [q[012]vnfto[567]dizg012%s%s[w=<dir>|x=<pattern>]]\n\
126 long options: --system-kanji-code={euc,sjis,utf8,cap}\n\
127 --archive-kanji-code={euc,sjis,utf8,cap}\n\
128 --extract-broken-archive\n\
129 --convert-filename-case\n\
130 --ignore-mac-files\n\
139 #if HAVE_LIBAPPLEFILE
140 ,"b" /* decode_macbinary_contents */
151 LHarc for UNIX V 1.02 Copyright(C) 1989 Y.Tagawa\n\
152 LHx for MSDOS V C2.01 Copyright(C) 1990 H.Yoshizaki\n\
153 LHx(arc) for OSK V 2.01 Modified 1990 Momozou\n\
154 LHa for UNIX V 1.00 Copyright(C) 1992 Masaru Oki\n\
155 LHa for UNIX V 1.14 Modified 1995 Nobutaka Watazaki\n\
156 LHa for UNIX V 1.14i Modified 2000 Tsugio Okamoto\n\
157 Autoconfiscated 2001-2008 Koji Arai\n\
163 commands: options:\n\
164 a Add(or replace) to archive q{num} quiet (num:quiet mode)\n\
165 x,e EXtract from archive v verbose\n\
166 l,v List / Verbose List n not execute\n\
167 u Update newer files to archive f force (over write at extract)\n\
168 d Delete from archive t FILES are TEXT file\n");
171 m Move to archive (means 'ad') o[567] compression method (a/u/c)\n\
176 m Move to archive (means 'ad') o use LHarc compatible method (a/u/c)\n\
180 c re-Construct new archive d delete FILES after (a/u/c)\n\
181 p Print to STDOUT from archive i ignore directory path (x/e)\n\
182 t Test file CRC in archive z files not compress (a/u/c)\n\
183 g Generic format (for compatibility)\n\
184 0/1/2 header level (a/u/c)\n\
188 e TEXT code convert from/to EUC\n\
191 #if HAVE_LIBAPPLEFILE
193 b decode MacBinary (x/e)\n\
197 w=<dir> specify extract directory (x/e)\n\
198 x=<pattern> eXclude files (a/u/c)\n\
202 #include "getopt_long.h"
208 parse_suboption(int argc, char **argv)
213 SYSTEM_KANJI_CODE_OPTION,
214 ARCHIVE_KANJI_CODE_OPTION,
215 TRADITIONAL_BEHAVIOR,
219 struct option long_options[] = {
220 /* These options set a flag. */
221 {"help", no_argument, 0, HELP_OPTION},
222 {"version", no_argument, 0, VERSION_OPTION},
224 {"system-kanji-code", required_argument, 0, SYSTEM_KANJI_CODE_OPTION},
225 {"archive-kanji-code", required_argument, 0, ARCHIVE_KANJI_CODE_OPTION},
226 {"extract-broken-archive", no_argument, &extract_broken_archive, 1},
227 {"convert-filename-case", no_argument, &convertcase, TRUE},
228 {"traditional", no_argument, 0, TRADITIONAL_BEHAVIOR},
229 {"ignore-mac-files", no_argument, 0, IGNORE_MAC_FILES},
234 char short_options[256] = "q[012]vnfto[567]dizg012ew:x:";
235 /* "[...]" means optional 1 byte argument (original extention) */
237 #if HAVE_LIBAPPLEFILE
238 strncat(short_options, "b", sizeof(short_options)-strlen(short_options)-1);
243 int option_index = 0;
244 int c = getopt_long(argc, argv,
245 short_options, long_options,
248 if (c == -1) break; /* end of options */
252 /* Already set a flag variable by the definition of the
267 /* In quiet mode, no confirm to overwrite */
274 case '0': /* no quiet */
275 case '1': /* no use the incremental indicator */
276 quiet_mode = *optarg - '0';
278 case '2': /* no output */
301 #if HAVE_LIBAPPLEFILE
303 decode_macbinary_contents = TRUE;
310 generic_format = TRUE;
314 delete_after_append = TRUE;
318 compress_method = LZHUFF1_METHOD_NUM;
324 compress_method = LZHUFF5_METHOD_NUM;
328 compress_method = LZHUFF6_METHOD_NUM;
331 compress_method = LZHUFF7_METHOD_NUM;
335 error("invalid compression method 'o%c'", *optarg);
340 compress_method = LZHUFF0_METHOD_NUM; /* Changed N.Watazaki */
343 ignore_directory = TRUE;
347 error("exclude files does not specified for `-x'");
351 for (i = 0; exclude_files && exclude_files[i]; i++)
353 exclude_files = (char**)xrealloc(exclude_files,
354 sizeof(char*) * (i+2));
358 exclude_files[i] = optarg;
359 exclude_files[i+1] = 0;
364 error("working directory does not specified for `-w'");
370 extract_directory = optarg;
381 case SYSTEM_KANJI_CODE_OPTION:
383 error("kanji code not specified for --%s",
384 long_options[option_index].name);
387 if (strcmp(optarg, "euc") == 0) {
388 optional_system_kanji_code = CODE_EUC;
390 else if (strcmp(optarg, "sjis") == 0) {
391 optional_system_kanji_code = CODE_SJIS;
393 else if (strcmp(optarg, "utf8") == 0) {
394 optional_system_kanji_code = CODE_UTF8;
396 else if (strcmp(optarg, "cap") == 0) {
397 optional_system_kanji_code = CODE_CAP;
400 error("unknown kanji code \"%s\"", optarg);
405 case ARCHIVE_KANJI_CODE_OPTION:
407 error("kanji code not specified for --%s",
408 long_options[option_index].name);
411 if (strcmp(optarg, "euc") == 0) {
412 optional_archive_kanji_code = CODE_EUC;
414 else if (strcmp(optarg, "sjis") == 0) {
415 optional_archive_kanji_code = CODE_SJIS;
417 else if (strcmp(optarg, "utf8") == 0) {
418 optional_archive_kanji_code = CODE_UTF8;
420 else if (strcmp(optarg, "cap") == 0) {
421 optional_archive_kanji_code = CODE_CAP;
424 error("unknown kanji code \"%s\"", optarg);
429 case TRADITIONAL_BEHAVIOR:
433 case IGNORE_MAC_FILES:
434 /* ignore Mac specific files (._*, .DS_Store and Icon\r)
436 for (i = 0; exclude_files && exclude_files[i]; i++)
438 exclude_files = (char**)xrealloc(exclude_files,
439 sizeof(char*) * (i+4));
441 exclude_files[i] = xstrdup("._*");
442 exclude_files[i+1] = xstrdup(".DS_Store");
443 exclude_files[i+2] = xstrdup("Icon\r");
444 exclude_files[i+3] = 0;
448 error("unknown option `-%c'.", c);
457 archive_name = *argv++;
468 Parse LHA command and options.
471 parse_option(int argc, char **argv)
475 if (argv[1] == NULL || strcmp(argv[1], "--help") == 0) {
480 if (strcmp(argv[1], "--version") == 0) {
485 if (argc == 2 && *argv[1] != '-') {
486 archive_name = argv[1];
495 if (cmd_char[0] == '-')
506 output_to_stdout = TRUE;
524 update_if_newer = TRUE;
529 delete_after_append = TRUE;
534 verbose_listing = TRUE;
548 error("unknown command `-%c'", *cmd_char);
552 if (cmd_char[1] == '\0') {
553 /* argv[1] is command name */
559 /* Eliminate command character
560 e.g.) lha cv foo.lzh -> lha -v foo.lzh
561 lha -cv foo.lzh -> lha -v foo.lzh
567 return parse_suboption(argc, argv);
570 /* ------------------------------------------------------------------------ */
580 init_variable(); /* Added N.Watazaki */
582 if (parse_option(argc, argv) == -1) {
589 error("archive file does not specified");
595 if (!strcmp(archive_name, "-")) {
596 if (!isatty(1) && cmd == CMD_ADD)
599 #if 0 /* Comment out; IMHO, this feature is useless. by Koji Arai */
601 if (argc == 3 && !isatty(0)) { /* 1999.7.18 */
602 /* Bug(?) on MinGW, isatty() return 0 on Cygwin console.
603 mingw-runtime-1.3-2 and Cygwin 1.3.10(0.51/3/2) on Win2000 */
604 get_filename_from_stdin = TRUE;
609 /* target file name */
610 if (get_filename_from_stdin) {
616 xfilev = (char **)xmalloc(sizeof(char *) * xfilec);
617 while (fgets(inpbuf, sizeof(inpbuf), stdin)) {
618 /* delete \n if it exist */
620 while (i < sizeof(inpbuf) && p != 0) {
628 if (cmd_filec >= xfilec) {
630 xfilev = (char **) xrealloc(xfilev,
631 sizeof(char *) * xfilec);
633 if (strlen(inpbuf) < 1)
635 xfilev[cmd_filec++] = xstrdup(inpbuf);
637 xfilev[cmd_filec] = NULL;
667 /* ------------------------------------------------------------------------ */
671 /* macro PACKAGE_NAME, PACKAGE_VERSION and PLATFORM are
672 defined in config.h by configure script */
673 fprintf(stderr, "%s version %s (%s)\n",
674 PACKAGE_NAME, PACKAGE_VERSION, PLATFORM);
675 fprintf(stderr, " configure options: %s\n", LHA_CONFIGURE_OPTIONS);
680 message(char *fmt, ...)
682 message(fmt, va_alist)
687 int errno_sv = errno;
690 fprintf(stderr, "LHa: ");
693 vfprintf(stderr, fmt, v);
701 /* ------------------------------------------------------------------------ */
704 warning(char *fmt, ...)
706 warning(fmt, va_alist)
711 int errno_sv = errno;
714 fprintf(stderr, "LHa: Warning: ");
717 vfprintf(stderr, fmt, v);
725 /* ------------------------------------------------------------------------ */
728 error(char *fmt, ...)
735 int errno_sv = errno;
738 fprintf(stderr, "LHa: Error: ");
741 vfprintf(stderr, fmt, v);
753 fatal_error(char *fmt, ...)
755 fatal_error(fmt, va_alist)
760 int errno_sv = errno;
763 fprintf(stderr, "LHa: Fatal error: ");
766 vfprintf(stderr, fmt, v);
770 fprintf(stderr, ": %s\n", strerror(errno_sv));
780 if (temporary_fd != -1) {
783 unlink(temporary_name);
786 if (recover_archive_when_interrupt) {
787 rename(backup_archive_name, archive_name);
788 recover_archive_when_interrupt = FALSE;
790 if (remove_extracting_file_when_interrupt) {
791 message("Removing: %s", writing_filename);
792 unlink(writing_filename);
793 remove_extracting_file_when_interrupt = FALSE;
801 message("Interrupted");
805 signal(SIGINT, SIG_DFL);
807 signal(SIGHUP, SIG_DFL);
809 kill(getpid(), signo);
812 /* ------------------------------------------------------------------------ */
814 /* ------------------------------------------------------------------------ */
819 register char *p, *q;
823 if (generic_format) {
825 c1 = *(unsigned char *) p++;
826 c2 = *(unsigned char *) q++;
838 while (*p == *q && *p != '\0')
840 return *(unsigned char *) p - *(unsigned char *) q;
844 /* ------------------------------------------------------------------------ */
849 qsort(cmd_filev, cmd_filec, sizeof(char *), sort_by_ascii);
852 /* ------------------------------------------------------------------------ */
857 void *p = malloc(size);
859 fatal_error("Not enough memory");
863 /* ------------------------------------------------------------------------ */
869 void *p = (char *) realloc(old, size);
871 fatal_error("Not enough memory");
879 int len = strlen(str);
880 char *p = (char *)xmalloc(len + 1);
881 strcpy(p, str); /* ok */
885 /* ------------------------------------------------------------------------ */
887 /* ------------------------------------------------------------------------ */
890 +-------------+-------------+------+-------------+----------+
891 | N A M E 1 \0| N A M E 2 \0| .... | N A M E n \0| |
892 +-------------+-------------+------+-------------+----------+
893 ^ ^ ^ buffer+0 buffer+used buffer+size
896 +---------------+---------------+------------- -----------------+
897 | pointer to | pointer to | pointer to ... pointer to |
898 | stringpool | N A M E 1 | N A M E 2 ... N A M E n |
899 +---------------+---------------+------------- -------------+
900 ^ malloc base returned
903 /* ------------------------------------------------------------------------ */
906 struct string_pool *sp;
908 sp->size = 1024 - 8; /* any ( >=0 ) */
911 sp->buffer = (char *) xmalloc(sp->size * sizeof(char));
914 /* ------------------------------------------------------------------------ */
916 add_sp(sp, name, len)
917 struct string_pool *sp;
918 char *name; /* stored '\0' at tail */
919 int len; /* include '\0' */
921 while (sp->used + len > sp->size) {
923 sp->buffer = (char *) xrealloc(sp->buffer, sp->size * sizeof(char));
925 memmove(sp->buffer + sp->used, name, len);
930 /* ------------------------------------------------------------------------ */
932 finish_sp(sp, v_count, v_vector)
933 register struct string_pool *sp;
941 v = (char **) xmalloc((sp->n + 1) * sizeof(char *));
946 for (i = sp->n; i; i--) {
953 /* ------------------------------------------------------------------------ */
959 free(*vector); /* free string pool */
964 /* ------------------------------------------------------------------------ */
965 /* READ DIRECTORY FILES */
966 /* ------------------------------------------------------------------------ */
968 include_path_p(path, name)
974 return (path[-1] == '/' && *n == '\0');
975 return (*n == '/' || (n != name && path[-1] == '/' && n[-1] == '/'));
978 /* ------------------------------------------------------------------------ */
980 cleaning_files(v_filec, v_filev)
987 register char **filev = *v_filev;
988 register int filec = *v_filec;
995 flags = xmalloc(filec * sizeof(char));
997 /* flags & 0x01 : 1: ignore */
998 /* flags & 0x02 : 1: directory, 0 : regular file */
999 /* flags & 0x04 : 1: need delete */
1001 for (i = 0; i < filec; i++)
1002 if (GETSTAT(filev[i], &stbuf) < 0) {
1004 warning("Cannot access \"%s\" : %s; ignored.", filev[i],
1008 if (is_regularfile(&stbuf))
1010 else if (is_directory(&stbuf))
1013 else if (is_symlink(&stbuf)) /* t.okamoto */
1018 warning("Cannot archive \"%s\", ignored.", filev[i]);
1022 for (i = 0; i < filec; i++) {
1024 if ((flags[i] & 0x07) == 0x00) { /* regular file, not
1025 * deleted/ignored */
1026 for (j = i + 1; j < filec; j++) {
1027 if ((flags[j] & 0x07) == 0x00) { /* regular file, not
1028 * deleted/ignored */
1029 if (STREQU(p, filev[j]))
1030 flags[j] = 0x04; /* delete */
1034 else if ((flags[i] & 0x07) == 0x02) { /* directory, not
1035 * deleted/ignored */
1036 for (j = i + 1; j < filec; j++) {
1037 if ((flags[j] & 0x07) == 0x00) { /* regular file, not
1038 * deleted/ignored */
1039 if (include_path_p(p, filev[j]))
1040 flags[j] = 0x04; /* delete */
1042 else if ((flags[j] & 0x07) == 0x02) { /* directory, not
1043 * deleted/ignored */
1044 if (include_path_p(p, filev[j]))
1045 flags[j] = 0x04; /* delete */
1051 for (i = j = 0; i < filec; i++) {
1052 if ((flags[i] & 0x04) == 0) {
1054 filev[j] = filev[i];
1063 /* ------------------------------------------------------------------------ */
1065 find_files(name, v_filec, v_filev)
1070 struct string_pool sp;
1071 char newname[FILENAME_LENGTH];
1075 struct stat tmp_stbuf, arc_stbuf, fil_stbuf;
1076 int exist_tmp = 1, exist_arc = 1;
1078 len = str_safe_copy(newname, name, sizeof(newname));
1079 if (len > 0 && newname[len - 1] != '/') {
1080 if (len < sizeof(newname)-1)
1081 strcpy(&newname[len++], "/"); /* ok */
1083 warning("the length of pathname \"%s\" is too long.", name);
1086 dirp = opendir(name);
1092 if (GETSTAT(temporary_name, &tmp_stbuf) == -1)
1094 if (GETSTAT(archive_name, &arc_stbuf) == -1)
1097 while ((dp = readdir(dirp)) != NULL) {
1100 /* exclude '.' and '..' */
1101 if (strncmp(dp->d_name, ".", n) == 0
1102 || strncmp(dp->d_name, "..", n) == 0)
1105 /* exclude exclude_files supplied by user */
1106 for (i = 0; exclude_files && exclude_files[i]; i++) {
1107 if (fnmatch(exclude_files[i], dp->d_name,
1108 FNM_PATHNAME|FNM_NOESCAPE|FNM_PERIOD) == 0)
1112 if (len + n >= sizeof(newname)) {
1113 warning("filename is too long");
1117 strncpy(newname + len, dp->d_name, n);
1118 newname[len + n] = '\0';
1119 if (GETSTAT(newname, &fil_stbuf) < 0)
1122 #if defined(HAVE_STRUCT_STAT_ST_INO) && !__MINGW32__
1123 /* MinGW has meaningless st_ino */
1125 /* exclude temporary file, archive file and these links */
1127 tmp_stbuf.st_dev == fil_stbuf.st_dev &&
1128 tmp_stbuf.st_ino == fil_stbuf.st_ino)
1132 arc_stbuf.st_dev == fil_stbuf.st_dev &&
1133 arc_stbuf.st_ino == fil_stbuf.st_ino)
1136 add_sp(&sp, newname, len+n+1);
1141 finish_sp(&sp, v_filec, v_filev);
1143 qsort(*v_filev, *v_filec, sizeof(char *), sort_by_ascii);
1144 cleaning_files(v_filec, v_filev);
1149 /* ------------------------------------------------------------------------ */
1151 free_files(filec, filev)
1158 /* ------------------------------------------------------------------------ */
1160 /* ------------------------------------------------------------------------ */
1161 /* Build temporary file name and store to TEMPORARY_NAME */
1163 build_temporary_name()
1165 #ifdef TMP_FILENAME_TEMPLATE
1166 /* "/tmp/lhXXXXXX" etc. */
1167 if (extract_directory == NULL) {
1168 str_safe_copy(temporary_name, TMP_FILENAME_TEMPLATE,
1169 sizeof(temporary_name));
1172 xsnprintf(temporary_name, sizeof(temporary_name),
1173 "%s/lhXXXXXX", extract_directory);
1178 str_safe_copy(temporary_name, archive_name, sizeof(temporary_name));
1179 s = strrchr(temporary_name, '/');
1182 len = s - temporary_name;
1183 if (len + strlen("lhXXXXXX") < sizeof(temporary_name))
1184 /* use directory at archive file */
1185 strcpy(s, "lhXXXXXX"); /* ok */
1187 /* use current directory */
1188 str_safe_copy(temporary_name, "lhXXXXXX", sizeof(temporary_name));
1191 /* use current directory */
1192 str_safe_copy(temporary_name, "lhXXXXXX", sizeof(temporary_name));
1198 old_umask = umask(077);
1199 fd = mkstemp(temporary_name);
1207 mktemp(temporary_name);
1208 flags = O_CREAT|O_EXCL|O_RDWR;
1212 return open(temporary_name, flags, 0600);
1217 /* ------------------------------------------------------------------------ */
1219 modify_filename_extention(buffer, ext, size)
1224 register char *p, *dot;
1226 for (p = buffer, dot = (char *) 0; *p; p++) {
1236 str_safe_copy(p, ext, size - (p - buffer));
1239 /* ------------------------------------------------------------------------ */
1240 /* build backup file name */
1242 build_backup_name(buffer, original, size)
1247 str_safe_copy(buffer, original, size);
1248 modify_filename_extention(buffer, BACKUPNAME_EXTENTION, size); /* ".bak" */
1251 /* ------------------------------------------------------------------------ */
1253 build_standard_archive_name(buffer, original, size)
1258 str_safe_copy(buffer, original, size);
1259 modify_filename_extention(buffer, ARCHIVENAME_EXTENTION, size); /* ".lzh" */
1262 /* ------------------------------------------------------------------------ */
1264 /* ------------------------------------------------------------------------ */
1274 for (i = 0; i < cmd_filec; i++) {
1275 if (patmatch(cmd_filev[i], name, 0))
1288 if ((fp = fopen(name, mode)) == NULL)
1289 fatal_error("Cannot open file \"%s\"", name);
1294 /* ------------------------------------------------------------------------ */
1296 /* ------------------------------------------------------------------------ */
1298 open_old_archive_1(name, v_fp)
1305 if (stat(name, &stbuf) >= 0 &&
1306 is_regularfile(&stbuf) &&
1307 (fp = fopen(name, READ_BINARY)) != NULL) {
1309 archive_file_gid = stbuf.st_gid;
1310 archive_file_mode = stbuf.st_mode;
1315 archive_file_gid = -1;
1319 /* ------------------------------------------------------------------------ */
1324 char *p = NULL, *ext, *ext2;
1325 static char expanded_archive_name[FILENAME_LENGTH];
1327 if (!strcmp(archive_name, "-")) {
1328 if (cmd == CMD_EXTRACT || cmd == CMD_LIST) {
1329 #if defined(__MINGW32__) || defined(__DJGPP__)
1330 setmode(fileno(stdin), O_BINARY);
1338 ext2 = strrchr(archive_name, '.');
1347 p = xstrdup("lzh," ADDITIONAL_SUFFIXES);
1348 for (ext = strtok(p, ",");
1350 ext = strtok(NULL, ",")) {
1352 if (*ext == 0) continue;
1354 if (strcasecmp(ext, ext2)) {
1355 /* Try to open file just specified filename
1356 with usual suffixes.
1357 Return NULL if the file is not exist. */
1359 open_old_archive_1(archive_name, &fp);
1360 goto ret; /* found or not */
1367 /* Try to open file just specified filename */
1368 if (open_old_archive_1(archive_name, &fp))
1369 goto ret; /* found */
1371 /* Try to open file with implicit suffixes */
1372 p = xstrdup("lzh," ADDITIONAL_SUFFIXES);
1373 for (ext = strtok(p, ",");
1375 ext = strtok(NULL, ",")) {
1377 if (*ext == 0) continue;
1379 xsnprintf(expanded_archive_name, sizeof(expanded_archive_name),
1380 "%s.%s", archive_name, ext);
1382 if (open_old_archive_1(expanded_archive_name, &fp)) {
1383 archive_name = expanded_archive_name;
1384 goto ret; /* found */
1393 /* ------------------------------------------------------------------------ */
1395 inquire(msg, name, selective)
1396 char *msg, *name, *selective;
1402 fprintf(stderr, "%s %s ", name, msg);
1405 fgets(buffer, 1024, stdin);
1407 for (p = selective; *p; p++)
1408 if (buffer[0] == *p)
1409 return p - selective;
1414 /* ------------------------------------------------------------------------ */
1416 write_archive_tail(nafp)
1422 /* ------------------------------------------------------------------------ */
1424 copy_old_one(oafp, nafp, hdr)
1429 fseeko(oafp, hdr->header_size + hdr->packed_size, SEEK_CUR);
1432 reading_filename = archive_name;
1433 writing_filename = temporary_name;
1434 copyfile(oafp, nafp, hdr->header_size + hdr->packed_size, 0, 0);