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;
113 /* ------------------------------------------------------------------------ */
114 /* NOTES : Text File Format */
115 /* GENERATOR NewLine */
116 /* [generic] 0D 0A */
118 /* [OS9][MacOS] 0D */
120 /* ------------------------------------------------------------------------ */
125 usage: lha [-]<commands>[<options>] [-<options> ...] archive_file [file...]\n\
126 commands: [axelvudmcpt]\n\
127 options: [q[012]vnfto[567]dizg012%s%s[w=<dir>|x=<pattern>]]\n\
128 long options: --system-kanji-code={euc,sjis,utf8,cap}\n\
129 --archive-kanji-code={euc,sjis,utf8,cap}\n\
130 --extract-broken-archive\n\
131 --convert-filename-case\n\
132 --ignore-mac-files\n\
141 #if HAVE_LIBAPPLEFILE
142 ,"b" /* decode_macbinary_contents */
153 LHarc for UNIX V 1.02 Copyright(C) 1989 Y.Tagawa\n\
154 LHx for MSDOS V C2.01 Copyright(C) 1990 H.Yoshizaki\n\
155 LHx(arc) for OSK V 2.01 Modified 1990 Momozou\n\
156 LHa for UNIX V 1.00 Copyright(C) 1992 Masaru Oki\n\
157 LHa for UNIX V 1.14 Modified 1995 Nobutaka Watazaki\n\
158 LHa for UNIX V 1.14i Modified 2000 Tsugio Okamoto\n\
159 Autoconfiscated 2001-2008 Koji Arai\n\
165 commands: options:\n\
166 a Add(or replace) to archive q{num} quiet (num:quiet mode)\n\
167 x,e EXtract from archive v verbose\n\
168 l,v List / Verbose List n not execute\n\
169 u Update newer files to archive f force (over write at extract)\n\
170 d Delete from archive t FILES are TEXT file\n");
173 m Move to archive (means 'ad') o[567] compression method (a/u/c)\n\
178 m Move to archive (means 'ad') o use LHarc compatible method (a/u/c)\n\
182 c re-Construct new archive d delete FILES after (a/u/c)\n\
183 p Print to STDOUT from archive i ignore directory path (x/e)\n\
184 t Test file CRC in archive z files not compress (a/u/c)\n\
185 g Generic format (for compatibility)\n\
186 0/1/2 header level (a/u/c)\n\
190 e TEXT code convert from/to EUC\n\
193 #if HAVE_LIBAPPLEFILE
195 b decode MacBinary (x/e)\n\
199 w=<dir> specify extract directory (x/e)\n\
200 x=<pattern> eXclude files (a/u/c)\n\
204 #include "getopt_long.h"
210 parse_suboption(int argc, char **argv)
215 SYSTEM_KANJI_CODE_OPTION,
216 ARCHIVE_KANJI_CODE_OPTION,
217 TRADITIONAL_BEHAVIOR,
222 struct option long_options[] = {
223 /* These options set a flag. */
224 {"help", no_argument, 0, HELP_OPTION},
225 {"version", no_argument, 0, VERSION_OPTION},
227 {"system-kanji-code", required_argument, 0, SYSTEM_KANJI_CODE_OPTION},
228 {"archive-kanji-code", required_argument, 0, ARCHIVE_KANJI_CODE_OPTION},
229 {"extract-broken-archive", no_argument, &extract_broken_archive, 1},
230 {"convert-filename-case", no_argument, &convertcase, TRUE},
231 {"traditional", no_argument, 0, TRADITIONAL_BEHAVIOR},
232 {"ignore-mac-files", no_argument, 0, IGNORE_MAC_FILES},
233 {"debug", required_argument, 0, DEBUG_OPTION},
238 char short_options[256] = "q[012]vnfto[567]dizg012ew:x:";
239 /* "[...]" means optional 1 byte argument (original extention) */
241 #if HAVE_LIBAPPLEFILE
242 strncat(short_options, "b", sizeof(short_options)-strlen(short_options)-1);
247 int option_index = 0;
248 int c = getopt_long(argc, argv,
249 short_options, long_options,
252 if (c == -1) break; /* end of options */
256 /* Already set a flag variable by the definition of the
271 /* In quiet mode, no confirm to overwrite */
278 case '0': /* no quiet */
279 case '1': /* no use the incremental indicator */
280 quiet_mode = *optarg - '0';
282 case '2': /* no output */
305 #if HAVE_LIBAPPLEFILE
307 decode_macbinary_contents = TRUE;
314 generic_format = TRUE;
318 delete_after_append = TRUE;
322 compress_method = LZHUFF1_METHOD_NUM;
328 compress_method = LZHUFF5_METHOD_NUM;
332 compress_method = LZHUFF6_METHOD_NUM;
335 compress_method = LZHUFF7_METHOD_NUM;
339 error("invalid compression method 'o%c'", *optarg);
344 compress_method = LZHUFF0_METHOD_NUM; /* Changed N.Watazaki */
347 ignore_directory = TRUE;
351 error("exclude files does not specified for `-x'");
355 for (i = 0; exclude_files && exclude_files[i]; i++)
357 exclude_files = (char**)xrealloc(exclude_files,
358 sizeof(char*) * (i+2));
362 exclude_files[i] = optarg;
363 exclude_files[i+1] = 0;
368 error("working directory does not specified for `-w'");
374 extract_directory = optarg;
385 case SYSTEM_KANJI_CODE_OPTION:
387 error("kanji code not specified for --%s",
388 long_options[option_index].name);
391 if (strcmp(optarg, "euc") == 0) {
392 optional_system_kanji_code = CODE_EUC;
394 else if (strcmp(optarg, "sjis") == 0) {
395 optional_system_kanji_code = CODE_SJIS;
397 else if (strcmp(optarg, "utf8") == 0) {
398 optional_system_kanji_code = CODE_UTF8;
400 else if (strcmp(optarg, "cap") == 0) {
401 optional_system_kanji_code = CODE_CAP;
404 error("unknown kanji code \"%s\"", optarg);
409 case ARCHIVE_KANJI_CODE_OPTION:
411 error("kanji code not specified for --%s",
412 long_options[option_index].name);
415 if (strcmp(optarg, "euc") == 0) {
416 optional_archive_kanji_code = CODE_EUC;
418 else if (strcmp(optarg, "sjis") == 0) {
419 optional_archive_kanji_code = CODE_SJIS;
421 else if (strcmp(optarg, "utf8") == 0) {
422 optional_archive_kanji_code = CODE_UTF8;
424 else if (strcmp(optarg, "cap") == 0) {
425 optional_archive_kanji_code = CODE_CAP;
428 error("unknown kanji code \"%s\"", optarg);
433 case TRADITIONAL_BEHAVIOR:
437 case IGNORE_MAC_FILES:
438 /* ignore Mac specific files (._*, .DS_Store and Icon\r)
440 for (i = 0; exclude_files && exclude_files[i]; i++)
442 exclude_files = (char**)xrealloc(exclude_files,
443 sizeof(char*) * (i+4));
445 exclude_files[i] = xstrdup("._*");
446 exclude_files[i+1] = xstrdup(".DS_Store");
447 exclude_files[i+2] = xstrdup("Icon\r");
448 exclude_files[i+3] = 0;
453 error("debugging item is not specified for --%s=item",
454 long_options[option_index].name);
457 if (strcmp(optarg, "nosort") == 0) {
458 sort_contents = FALSE;
460 else if (strcmp(optarg, "norecursion") == 0) {
461 recursive_archiving = FALSE;
464 error("unknown debugging item \"%s\" for --%s=item",
465 optarg, long_options[option_index].name);
471 error("unknown option `-%c'.", c);
480 archive_name = *argv++;
491 Parse LHA command and options.
494 parse_option(int argc, char **argv)
498 if (argv[1] == NULL || strcmp(argv[1], "--help") == 0) {
503 if (strcmp(argv[1], "--version") == 0) {
508 if (argc == 2 && *argv[1] != '-') {
509 archive_name = argv[1];
518 if (cmd_char[0] == '-')
529 output_to_stdout = TRUE;
547 update_if_newer = TRUE;
552 delete_after_append = TRUE;
557 verbose_listing = TRUE;
571 error("unknown command `-%c'", *cmd_char);
575 if (cmd_char[1] == '\0') {
576 /* argv[1] is command name */
582 /* Eliminate command character
583 e.g.) lha cv foo.lzh -> lha -v foo.lzh
584 lha -cv foo.lzh -> lha -v foo.lzh
590 return parse_suboption(argc, argv);
593 /* ------------------------------------------------------------------------ */
603 init_variable(); /* Added N.Watazaki */
605 if (parse_option(argc, argv) == -1) {
612 error("archive file does not specified");
618 if (!strcmp(archive_name, "-")) {
619 if (!isatty(1) && cmd == CMD_ADD)
622 #if 0 /* Comment out; IMHO, this feature is useless. by Koji Arai */
624 if (argc == 3 && !isatty(0)) { /* 1999.7.18 */
625 /* Bug(?) on MinGW, isatty() return 0 on Cygwin console.
626 mingw-runtime-1.3-2 and Cygwin 1.3.10(0.51/3/2) on Win2000 */
627 get_filename_from_stdin = TRUE;
632 /* target file name */
633 if (get_filename_from_stdin) {
639 xfilev = (char **)xmalloc(sizeof(char *) * xfilec);
640 while (fgets(inpbuf, sizeof(inpbuf), stdin)) {
641 /* delete \n if it exist */
643 while (i < sizeof(inpbuf) && p != 0) {
651 if (cmd_filec >= xfilec) {
653 xfilev = (char **) xrealloc(xfilev,
654 sizeof(char *) * xfilec);
656 if (strlen(inpbuf) < 1)
658 xfilev[cmd_filec++] = xstrdup(inpbuf);
660 xfilev[cmd_filec] = NULL;
690 /* ------------------------------------------------------------------------ */
694 /* macro PACKAGE_NAME, PACKAGE_VERSION and PLATFORM are
695 defined in config.h by configure script */
696 fprintf(stderr, "%s version %s (%s)\n",
697 PACKAGE_NAME, PACKAGE_VERSION, PLATFORM);
698 fprintf(stderr, " configure options: %s\n", LHA_CONFIGURE_OPTIONS);
703 message(char *fmt, ...)
705 message(fmt, va_alist)
710 int errno_sv = errno;
713 fprintf(stderr, "LHa: ");
716 vfprintf(stderr, fmt, v);
724 /* ------------------------------------------------------------------------ */
727 warning(char *fmt, ...)
729 warning(fmt, va_alist)
734 int errno_sv = errno;
737 fprintf(stderr, "LHa: Warning: ");
740 vfprintf(stderr, fmt, v);
748 /* ------------------------------------------------------------------------ */
751 error(char *fmt, ...)
758 int errno_sv = errno;
761 fprintf(stderr, "LHa: Error: ");
764 vfprintf(stderr, fmt, v);
776 fatal_error(char *fmt, ...)
778 fatal_error(fmt, va_alist)
783 int errno_sv = errno;
786 fprintf(stderr, "LHa: Fatal error: ");
789 vfprintf(stderr, fmt, v);
793 fprintf(stderr, ": %s\n", strerror(errno_sv));
803 if (temporary_fd != -1) {
806 unlink(temporary_name);
809 if (recover_archive_when_interrupt) {
810 rename(backup_archive_name, archive_name);
811 recover_archive_when_interrupt = FALSE;
813 if (remove_extracting_file_when_interrupt) {
814 message("Removing: %s", writing_filename);
815 unlink(writing_filename);
816 remove_extracting_file_when_interrupt = FALSE;
824 message("Interrupted");
828 signal(SIGINT, SIG_DFL);
830 signal(SIGHUP, SIG_DFL);
832 kill(getpid(), signo);
835 /* ------------------------------------------------------------------------ */
837 /* ------------------------------------------------------------------------ */
842 register char *p, *q;
846 if (generic_format) {
848 c1 = *(unsigned char *) p++;
849 c2 = *(unsigned char *) q++;
861 while (*p == *q && *p != '\0')
863 return *(unsigned char *) p - *(unsigned char *) q;
867 /* ------------------------------------------------------------------------ */
871 if (cmd_filec > 1 && sort_contents)
872 qsort(cmd_filev, cmd_filec, sizeof(char *), sort_by_ascii);
875 /* ------------------------------------------------------------------------ */
880 void *p = malloc(size);
882 fatal_error("Not enough memory");
886 /* ------------------------------------------------------------------------ */
892 void *p = (char *) realloc(old, size);
894 fatal_error("Not enough memory");
902 int len = strlen(str);
903 char *p = (char *)xmalloc(len + 1);
904 strcpy(p, str); /* ok */
908 /* ------------------------------------------------------------------------ */
910 /* ------------------------------------------------------------------------ */
913 +-------------+-------------+------+-------------+----------+
914 | N A M E 1 \0| N A M E 2 \0| .... | N A M E n \0| |
915 +-------------+-------------+------+-------------+----------+
916 ^ ^ ^ buffer+0 buffer+used buffer+size
919 +---------------+---------------+------------- -----------------+
920 | pointer to | pointer to | pointer to ... pointer to |
921 | stringpool | N A M E 1 | N A M E 2 ... N A M E n |
922 +---------------+---------------+------------- -------------+
923 ^ malloc base returned
926 /* ------------------------------------------------------------------------ */
929 struct string_pool *sp;
931 sp->size = 1024 - 8; /* any ( >=0 ) */
934 sp->buffer = (char *) xmalloc(sp->size * sizeof(char));
937 /* ------------------------------------------------------------------------ */
939 add_sp(sp, name, len)
940 struct string_pool *sp;
941 char *name; /* stored '\0' at tail */
942 int len; /* include '\0' */
944 while (sp->used + len > sp->size) {
946 sp->buffer = (char *) xrealloc(sp->buffer, sp->size * sizeof(char));
948 memmove(sp->buffer + sp->used, name, len);
953 /* ------------------------------------------------------------------------ */
955 finish_sp(sp, v_count, v_vector)
956 register struct string_pool *sp;
964 v = (char **) xmalloc((sp->n + 1) * sizeof(char *));
969 for (i = sp->n; i; i--) {
976 /* ------------------------------------------------------------------------ */
982 free(*vector); /* free string pool */
987 /* ------------------------------------------------------------------------ */
988 /* READ DIRECTORY FILES */
989 /* ------------------------------------------------------------------------ */
991 include_path_p(path, name)
997 return (path[-1] == '/' && *n == '\0');
998 return (*n == '/' || (n != name && path[-1] == '/' && n[-1] == '/'));
1001 /* ------------------------------------------------------------------------ */
1003 cleaning_files(v_filec, v_filev)
1010 register char **filev = *v_filev;
1011 register int filec = *v_filec;
1018 flags = xmalloc(filec * sizeof(char));
1020 /* flags & 0x01 : 1: ignore */
1021 /* flags & 0x02 : 1: directory, 0 : regular file */
1022 /* flags & 0x04 : 1: need delete */
1024 for (i = 0; i < filec; i++)
1025 if (GETSTAT(filev[i], &stbuf) < 0) {
1027 warning("Cannot access \"%s\" : %s; ignored.", filev[i],
1031 if (is_regularfile(&stbuf))
1033 else if (is_directory(&stbuf))
1036 else if (is_symlink(&stbuf)) /* t.okamoto */
1041 warning("Cannot archive \"%s\", ignored.", filev[i]);
1045 for (i = 0; i < filec; i++) {
1047 if ((flags[i] & 0x07) == 0x00) { /* regular file, not
1048 * deleted/ignored */
1049 for (j = i + 1; j < filec; j++) {
1050 if ((flags[j] & 0x07) == 0x00) { /* regular file, not
1051 * deleted/ignored */
1052 if (STREQU(p, filev[j]))
1053 flags[j] = 0x04; /* delete */
1057 else if ((flags[i] & 0x07) == 0x02) { /* directory, not
1058 * deleted/ignored */
1059 for (j = i + 1; j < filec; j++) {
1060 if ((flags[j] & 0x07) == 0x00) { /* regular file, not
1061 * deleted/ignored */
1062 if (include_path_p(p, filev[j]))
1063 flags[j] = 0x04; /* delete */
1065 else if ((flags[j] & 0x07) == 0x02) { /* directory, not
1066 * deleted/ignored */
1067 if (include_path_p(p, filev[j]))
1068 flags[j] = 0x04; /* delete */
1074 for (i = j = 0; i < filec; i++) {
1075 if ((flags[i] & 0x04) == 0) {
1077 filev[j] = filev[i];
1086 /* ------------------------------------------------------------------------ */
1088 find_files(name, v_filec, v_filev)
1093 struct string_pool sp;
1094 char newname[FILENAME_LENGTH];
1098 struct stat tmp_stbuf, arc_stbuf, fil_stbuf;
1099 int exist_tmp = 1, exist_arc = 1;
1101 len = str_safe_copy(newname, name, sizeof(newname));
1102 if (len > 0 && newname[len - 1] != '/') {
1103 if (len < sizeof(newname)-1)
1104 strcpy(&newname[len++], "/"); /* ok */
1106 warning("the length of pathname \"%s\" is too long.", name);
1109 dirp = opendir(name);
1115 if (GETSTAT(temporary_name, &tmp_stbuf) == -1)
1117 if (GETSTAT(archive_name, &arc_stbuf) == -1)
1120 while ((dp = readdir(dirp)) != NULL) {
1123 /* exclude '.' and '..' */
1124 if (strncmp(dp->d_name, ".", n) == 0
1125 || strncmp(dp->d_name, "..", n) == 0)
1128 /* exclude exclude_files supplied by user */
1129 for (i = 0; exclude_files && exclude_files[i]; i++) {
1130 if (fnmatch(exclude_files[i], dp->d_name,
1131 FNM_PATHNAME|FNM_NOESCAPE|FNM_PERIOD) == 0)
1135 if (len + n >= sizeof(newname)) {
1136 warning("filename is too long");
1140 strncpy(newname + len, dp->d_name, n);
1141 newname[len + n] = '\0';
1142 if (GETSTAT(newname, &fil_stbuf) < 0)
1145 #if defined(HAVE_STRUCT_STAT_ST_INO) && !__MINGW32__
1146 /* MinGW has meaningless st_ino */
1148 /* exclude temporary file, archive file and these links */
1150 tmp_stbuf.st_dev == fil_stbuf.st_dev &&
1151 tmp_stbuf.st_ino == fil_stbuf.st_ino)
1155 arc_stbuf.st_dev == fil_stbuf.st_dev &&
1156 arc_stbuf.st_ino == fil_stbuf.st_ino)
1159 add_sp(&sp, newname, len+n+1);
1164 finish_sp(&sp, v_filec, v_filev);
1165 if (*v_filec > 1 && sort_contents)
1166 qsort(*v_filev, *v_filec, sizeof(char *), sort_by_ascii);
1167 cleaning_files(v_filec, v_filev);
1172 /* ------------------------------------------------------------------------ */
1174 free_files(filec, filev)
1181 /* ------------------------------------------------------------------------ */
1183 /* ------------------------------------------------------------------------ */
1184 /* Build temporary file name and store to TEMPORARY_NAME */
1186 build_temporary_name()
1188 #ifdef TMP_FILENAME_TEMPLATE
1189 /* "/tmp/lhXXXXXX" etc. */
1190 if (extract_directory == NULL) {
1191 str_safe_copy(temporary_name, TMP_FILENAME_TEMPLATE,
1192 sizeof(temporary_name));
1195 xsnprintf(temporary_name, sizeof(temporary_name),
1196 "%s/lhXXXXXX", extract_directory);
1201 str_safe_copy(temporary_name, archive_name, sizeof(temporary_name));
1202 s = strrchr(temporary_name, '/');
1205 len = s - temporary_name;
1206 if (len + strlen("lhXXXXXX") < sizeof(temporary_name))
1207 /* use directory at archive file */
1208 strcpy(s, "lhXXXXXX"); /* ok */
1210 /* use current directory */
1211 str_safe_copy(temporary_name, "lhXXXXXX", sizeof(temporary_name));
1214 /* use current directory */
1215 str_safe_copy(temporary_name, "lhXXXXXX", sizeof(temporary_name));
1221 old_umask = umask(077);
1222 fd = mkstemp(temporary_name);
1230 mktemp(temporary_name);
1231 flags = O_CREAT|O_EXCL|O_RDWR;
1235 return open(temporary_name, flags, 0600);
1240 /* ------------------------------------------------------------------------ */
1242 modify_filename_extention(buffer, ext, size)
1247 register char *p, *dot;
1249 for (p = buffer, dot = (char *) 0; *p; p++) {
1259 str_safe_copy(p, ext, size - (p - buffer));
1262 /* ------------------------------------------------------------------------ */
1263 /* build backup file name */
1265 build_backup_name(buffer, original, size)
1270 str_safe_copy(buffer, original, size);
1271 modify_filename_extention(buffer, BACKUPNAME_EXTENTION, size); /* ".bak" */
1274 /* ------------------------------------------------------------------------ */
1276 build_standard_archive_name(buffer, original, size)
1281 str_safe_copy(buffer, original, size);
1282 modify_filename_extention(buffer, ARCHIVENAME_EXTENTION, size); /* ".lzh" */
1285 /* ------------------------------------------------------------------------ */
1287 /* ------------------------------------------------------------------------ */
1297 for (i = 0; i < cmd_filec; i++) {
1298 if (patmatch(cmd_filev[i], name, 0))
1311 if ((fp = fopen(name, mode)) == NULL)
1312 fatal_error("Cannot open file \"%s\"", name);
1317 /* ------------------------------------------------------------------------ */
1319 /* ------------------------------------------------------------------------ */
1321 open_old_archive_1(name, v_fp)
1328 if (stat(name, &stbuf) >= 0 &&
1329 is_regularfile(&stbuf) &&
1330 (fp = fopen(name, READ_BINARY)) != NULL) {
1332 archive_file_gid = stbuf.st_gid;
1333 archive_file_mode = stbuf.st_mode;
1338 archive_file_gid = -1;
1342 /* ------------------------------------------------------------------------ */
1347 char *p = NULL, *ext, *ext2;
1348 static char expanded_archive_name[FILENAME_LENGTH];
1350 if (!strcmp(archive_name, "-")) {
1351 if (cmd == CMD_EXTRACT || cmd == CMD_LIST) {
1352 #if defined(__MINGW32__) || defined(__DJGPP__)
1353 setmode(fileno(stdin), O_BINARY);
1361 ext2 = strrchr(archive_name, '.');
1370 p = xstrdup("lzh," ADDITIONAL_SUFFIXES);
1371 for (ext = strtok(p, ",");
1373 ext = strtok(NULL, ",")) {
1375 if (*ext == 0) continue;
1377 if (strcasecmp(ext, ext2)) {
1378 /* Try to open file just specified filename
1379 with usual suffixes.
1380 Return NULL if the file is not exist. */
1382 open_old_archive_1(archive_name, &fp);
1383 goto ret; /* found or not */
1390 /* Try to open file just specified filename */
1391 if (open_old_archive_1(archive_name, &fp))
1392 goto ret; /* found */
1394 /* Try to open file with implicit suffixes */
1395 p = xstrdup("lzh," ADDITIONAL_SUFFIXES);
1396 for (ext = strtok(p, ",");
1398 ext = strtok(NULL, ",")) {
1400 if (*ext == 0) continue;
1402 xsnprintf(expanded_archive_name, sizeof(expanded_archive_name),
1403 "%s.%s", archive_name, ext);
1405 if (open_old_archive_1(expanded_archive_name, &fp)) {
1406 archive_name = expanded_archive_name;
1407 goto ret; /* found */
1416 /* ------------------------------------------------------------------------ */
1418 inquire(msg, name, selective)
1419 char *msg, *name, *selective;
1425 fprintf(stderr, "%s %s ", name, msg);
1428 fgets(buffer, 1024, stdin);
1430 for (p = selective; *p; p++)
1431 if (buffer[0] == *p)
1432 return p - selective;
1437 /* ------------------------------------------------------------------------ */
1439 write_archive_tail(nafp)
1445 /* ------------------------------------------------------------------------ */
1447 copy_old_one(oafp, nafp, hdr)
1452 fseeko(oafp, hdr->header_size + hdr->packed_size, SEEK_CUR);
1455 reading_filename = archive_name;
1456 writing_filename = temporary_name;
1457 copyfile(oafp, nafp, hdr->header_size + hdr->packed_size, 0, 0);