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 */
67 timestamp_archive = FALSE;
69 compress_method = DEFAULT_LZHUFF_METHOD; /* defined in config.h */
71 header_level = 2; /* level 2 */
78 /* view command flags */
79 verbose_listing = FALSE;
81 /* extract command flags */
82 output_to_stdout = FALSE;
84 /* append command flags */
86 update_if_newer = FALSE;
87 delete_after_append = FALSE;
88 generic_format = FALSE;
90 recover_archive_when_interrupt = FALSE;
91 remove_extracting_file_when_interrupt = FALSE;
92 get_filename_from_stdin = FALSE;
93 ignore_directory = FALSE;
99 extract_directory = NULL;
102 #if BACKUP_OLD_ARCHIVE
103 backup_old_archive = TRUE;
105 backup_old_archive = FALSE;
108 extract_broken_archive = FALSE;
109 decode_macbinary_contents = FALSE;
110 sort_contents = TRUE;
111 recursive_archiving = TRUE;
115 /* ------------------------------------------------------------------------ */
116 /* NOTES : Text File Format */
117 /* GENERATOR NewLine */
118 /* [generic] 0D 0A */
120 /* [OS9][MacOS] 0D */
122 /* ------------------------------------------------------------------------ */
127 usage: lha [-]<commands>[<options>] [-<options> ...] archive_file [file...]\n\
128 commands: [axelvudmcpt]\n\
129 options: [q[012]vnfto[567]dizg012%s%s[w=<dir>|x=<pattern>]]\n\
130 long options: --system-kanji-code={euc,sjis,utf8,cap}\n\
131 --archive-kanji-code={euc,sjis,utf8,cap}\n\
132 --extract-broken-archive\n\
133 --convert-filename-case\n\
134 --ignore-mac-files\n\
135 --timestamp-archive\n\
144 #if HAVE_LIBAPPLEFILE
145 ,"b" /* decode_macbinary_contents */
156 LHarc for UNIX V 1.02 Copyright(C) 1989 Y.Tagawa\n\
157 LHx for MSDOS V C2.01 Copyright(C) 1990 H.Yoshizaki\n\
158 LHx(arc) for OSK V 2.01 Modified 1990 Momozou\n\
159 LHa for UNIX V 1.00 Copyright(C) 1992 Masaru Oki\n\
160 LHa for UNIX V 1.14 Modified 1995 Nobutaka Watazaki\n\
161 LHa for UNIX V 1.14i Modified 2000 Tsugio Okamoto\n\
162 Autoconfiscated 2001-2008 Koji Arai\n\
168 commands: options:\n\
169 a Add(or replace) to archive q{num} quiet (num:quiet mode)\n\
170 x,e EXtract from archive v verbose\n\
171 l,v List / Verbose List n not execute\n\
172 u Update newer files to archive f force (over write at extract)\n\
173 d Delete from archive t FILES are TEXT file\n");
176 m Move to archive (means 'ad') o[567] compression method (a/u/c)\n\
181 m Move to archive (means 'ad') o use LHarc compatible method (a/u/c)\n\
185 c re-Construct new archive d delete FILES after (a/u/c)\n\
186 p Print to STDOUT from archive i ignore directory path (x/e)\n\
187 t Test file CRC in archive z files not compress (a/u/c)\n\
188 g Generic format (for compatibility)\n\
189 0/1/2 header level (a/u/c)\n\
193 e TEXT code convert from/to EUC\n\
196 #if HAVE_LIBAPPLEFILE
198 b decode MacBinary (x/e)\n\
202 w=<dir> specify extract directory (x/e)\n\
203 x=<pattern> eXclude files (a/u/c)\n\
207 #include "getopt_long.h"
213 parse_suboption(int argc, char **argv)
218 SYSTEM_KANJI_CODE_OPTION,
219 ARCHIVE_KANJI_CODE_OPTION,
220 TRADITIONAL_BEHAVIOR,
225 struct option long_options[] = {
226 /* These options set a flag. */
227 {"help", no_argument, 0, HELP_OPTION},
228 {"version", no_argument, 0, VERSION_OPTION},
230 {"system-kanji-code", required_argument, 0, SYSTEM_KANJI_CODE_OPTION},
231 {"archive-kanji-code", required_argument, 0, ARCHIVE_KANJI_CODE_OPTION},
232 {"extract-broken-archive", no_argument, &extract_broken_archive, 1},
233 {"convert-filename-case", no_argument, &convertcase, TRUE},
234 {"traditional", no_argument, 0, TRADITIONAL_BEHAVIOR},
235 {"ignore-mac-files", no_argument, 0, IGNORE_MAC_FILES},
236 {"timestamp-archive", no_argument, ×tamp_archive, 1},
237 {"debug", required_argument, 0, DEBUG_OPTION},
242 char short_options[256] = "q[012]vnfto[567]dizg012ew:x:";
243 /* "[...]" means optional 1 byte argument (original extention) */
245 #if HAVE_LIBAPPLEFILE
246 strncat(short_options, "b", sizeof(short_options)-strlen(short_options)-1);
251 int option_index = 0;
252 int c = getopt_long(argc, argv,
253 short_options, long_options,
256 if (c == -1) break; /* end of options */
260 /* Already set a flag variable by the definition of the
275 /* In quiet mode, no confirm to overwrite */
282 case '0': /* no quiet */
283 case '1': /* no use the incremental indicator */
284 quiet_mode = *optarg - '0';
286 case '2': /* no output */
309 #if HAVE_LIBAPPLEFILE
311 decode_macbinary_contents = TRUE;
318 generic_format = TRUE;
322 delete_after_append = TRUE;
326 compress_method = LZHUFF1_METHOD_NUM;
332 compress_method = LZHUFF5_METHOD_NUM;
336 compress_method = LZHUFF6_METHOD_NUM;
339 compress_method = LZHUFF7_METHOD_NUM;
343 error("invalid compression method 'o%c'", *optarg);
348 compress_method = LZHUFF0_METHOD_NUM; /* Changed N.Watazaki */
351 ignore_directory = TRUE;
355 error("exclude files does not specified for `-x'");
359 for (i = 0; exclude_files && exclude_files[i]; i++)
361 exclude_files = (char**)xrealloc(exclude_files,
362 sizeof(char*) * (i+2));
366 exclude_files[i] = optarg;
367 exclude_files[i+1] = 0;
372 error("working directory does not specified for `-w'");
378 extract_directory = optarg;
389 case SYSTEM_KANJI_CODE_OPTION:
391 error("kanji code not specified for --%s",
392 long_options[option_index].name);
395 if (strcmp(optarg, "euc") == 0) {
396 optional_system_kanji_code = CODE_EUC;
398 else if (strcmp(optarg, "sjis") == 0) {
399 optional_system_kanji_code = CODE_SJIS;
401 else if (strcmp(optarg, "utf8") == 0) {
402 optional_system_kanji_code = CODE_UTF8;
404 else if (strcmp(optarg, "cap") == 0) {
405 optional_system_kanji_code = CODE_CAP;
408 error("unknown kanji code \"%s\"", optarg);
413 case ARCHIVE_KANJI_CODE_OPTION:
415 error("kanji code not specified for --%s",
416 long_options[option_index].name);
419 if (strcmp(optarg, "euc") == 0) {
420 optional_archive_kanji_code = CODE_EUC;
422 else if (strcmp(optarg, "sjis") == 0) {
423 optional_archive_kanji_code = CODE_SJIS;
425 else if (strcmp(optarg, "utf8") == 0) {
426 optional_archive_kanji_code = CODE_UTF8;
428 else if (strcmp(optarg, "cap") == 0) {
429 optional_archive_kanji_code = CODE_CAP;
432 error("unknown kanji code \"%s\"", optarg);
437 case TRADITIONAL_BEHAVIOR:
441 case IGNORE_MAC_FILES:
442 /* ignore Mac specific files (._*, .DS_Store and Icon\r)
444 for (i = 0; exclude_files && exclude_files[i]; i++)
446 exclude_files = (char**)xrealloc(exclude_files,
447 sizeof(char*) * (i+4));
449 exclude_files[i] = xstrdup("._*");
450 exclude_files[i+1] = xstrdup(".DS_Store");
451 exclude_files[i+2] = xstrdup("Icon\r");
452 exclude_files[i+3] = 0;
457 error("debugging item is not specified for --%s",
458 long_options[option_index].name);
461 if (strcmp(optarg, "nosort") == 0) {
462 sort_contents = FALSE;
464 else if (strcmp(optarg, "norecursion") == 0) {
465 recursive_archiving = FALSE;
467 else if (strcmp(optarg, "dumplzss") == 0) {
472 error("unknown debugging item \"%s\" for --%s",
473 optarg, long_options[option_index].name);
479 error("unknown option `-%c'.", c);
488 archive_name = *argv++;
499 Parse LHA command and options.
502 parse_option(int argc, char **argv)
506 if (argv[1] == NULL || strcmp(argv[1], "--help") == 0) {
511 if (strcmp(argv[1], "--version") == 0) {
516 if (argc == 2 && *argv[1] != '-') {
517 archive_name = argv[1];
526 if (cmd_char[0] == '-')
537 output_to_stdout = TRUE;
555 update_if_newer = TRUE;
560 delete_after_append = TRUE;
565 verbose_listing = TRUE;
579 error("unknown command `-%c'", *cmd_char);
583 if (cmd_char[1] == '\0') {
584 /* argv[1] is command name */
590 /* Eliminate command character
591 e.g.) lha cv foo.lzh -> lha -v foo.lzh
592 lha -cv foo.lzh -> lha -v foo.lzh
598 return parse_suboption(argc, argv);
601 /* ------------------------------------------------------------------------ */
611 init_variable(); /* Added N.Watazaki */
613 if (parse_option(argc, argv) == -1) {
620 error("archive file does not specified");
626 if (!strcmp(archive_name, "-")) {
627 if (!isatty(1) && cmd == CMD_ADD)
630 #if 0 /* Comment out; IMHO, this feature is useless. by Koji Arai */
632 if (argc == 3 && !isatty(0)) { /* 1999.7.18 */
633 /* Bug(?) on MinGW, isatty() return 0 on Cygwin console.
634 mingw-runtime-1.3-2 and Cygwin 1.3.10(0.51/3/2) on Win2000 */
635 get_filename_from_stdin = TRUE;
640 /* target file name */
641 if (get_filename_from_stdin) {
647 xfilev = (char **)xmalloc(sizeof(char *) * xfilec);
648 while (fgets(inpbuf, sizeof(inpbuf), stdin)) {
649 /* delete \n if it exist */
651 while (i < sizeof(inpbuf) && p != 0) {
659 if (cmd_filec >= xfilec) {
661 xfilev = (char **) xrealloc(xfilev,
662 sizeof(char *) * xfilec);
664 if (strlen(inpbuf) < 1)
666 xfilev[cmd_filec++] = xstrdup(inpbuf);
668 xfilev[cmd_filec] = NULL;
698 /* ------------------------------------------------------------------------ */
702 /* macro PACKAGE_NAME, PACKAGE_VERSION and PLATFORM are
703 defined in config.h by configure script */
704 fprintf(stderr, "%s version %s (%s)\n",
705 PACKAGE_NAME, PACKAGE_VERSION, PLATFORM);
706 fprintf(stderr, " configure options: %s\n", LHA_CONFIGURE_OPTIONS);
711 message(char *fmt, ...)
713 message(fmt, va_alist)
718 int errno_sv = errno;
721 fprintf(stderr, "LHa: ");
724 vfprintf(stderr, fmt, v);
732 /* ------------------------------------------------------------------------ */
735 warning(char *fmt, ...)
737 warning(fmt, va_alist)
742 int errno_sv = errno;
745 fprintf(stderr, "LHa: Warning: ");
748 vfprintf(stderr, fmt, v);
756 /* ------------------------------------------------------------------------ */
759 error(char *fmt, ...)
766 int errno_sv = errno;
769 fprintf(stderr, "LHa: Error: ");
772 vfprintf(stderr, fmt, v);
784 fatal_error(char *fmt, ...)
786 fatal_error(fmt, va_alist)
791 int errno_sv = errno;
794 fprintf(stderr, "LHa: Fatal error: ");
797 vfprintf(stderr, fmt, v);
801 fprintf(stderr, ": %s\n", strerror(errno_sv));
811 if (temporary_fd != -1) {
814 unlink(temporary_name);
817 if (recover_archive_when_interrupt) {
818 rename(backup_archive_name, archive_name);
819 recover_archive_when_interrupt = FALSE;
821 if (remove_extracting_file_when_interrupt) {
822 message("Removing: %s", writing_filename);
823 unlink(writing_filename);
824 remove_extracting_file_when_interrupt = FALSE;
832 message("Interrupted");
836 signal(SIGINT, SIG_DFL);
838 signal(SIGHUP, SIG_DFL);
840 kill(getpid(), signo);
843 /* ------------------------------------------------------------------------ */
845 /* ------------------------------------------------------------------------ */
850 register char *p, *q;
854 if (generic_format) {
856 c1 = *(unsigned char *) p++;
857 c2 = *(unsigned char *) q++;
869 while (*p == *q && *p != '\0')
871 return *(unsigned char *) p - *(unsigned char *) q;
875 /* ------------------------------------------------------------------------ */
879 if (cmd_filec > 1 && sort_contents)
880 qsort(cmd_filev, cmd_filec, sizeof(char *), sort_by_ascii);
883 /* ------------------------------------------------------------------------ */
888 void *p = malloc(size);
890 fatal_error("Not enough memory");
894 /* ------------------------------------------------------------------------ */
900 void *p = (char *) realloc(old, size);
902 fatal_error("Not enough memory");
910 int len = strlen(str);
911 char *p = (char *)xmalloc(len + 1);
912 strcpy(p, str); /* ok */
916 /* ------------------------------------------------------------------------ */
918 /* ------------------------------------------------------------------------ */
921 +-------------+-------------+------+-------------+----------+
922 | N A M E 1 \0| N A M E 2 \0| .... | N A M E n \0| |
923 +-------------+-------------+------+-------------+----------+
924 ^ ^ ^ buffer+0 buffer+used buffer+size
927 +---------------+---------------+------------- -----------------+
928 | pointer to | pointer to | pointer to ... pointer to |
929 | stringpool | N A M E 1 | N A M E 2 ... N A M E n |
930 +---------------+---------------+------------- -------------+
931 ^ malloc base returned
934 /* ------------------------------------------------------------------------ */
937 struct string_pool *sp;
939 sp->size = 1024 - 8; /* any ( >=0 ) */
942 sp->buffer = (char *) xmalloc(sp->size * sizeof(char));
945 /* ------------------------------------------------------------------------ */
947 add_sp(sp, name, len)
948 struct string_pool *sp;
949 char *name; /* stored '\0' at tail */
950 int len; /* include '\0' */
952 while (sp->used + len > sp->size) {
954 sp->buffer = (char *) xrealloc(sp->buffer, sp->size * sizeof(char));
956 memmove(sp->buffer + sp->used, name, len);
961 /* ------------------------------------------------------------------------ */
963 finish_sp(sp, v_count, v_vector)
964 register struct string_pool *sp;
972 v = (char **) xmalloc((sp->n + 1) * sizeof(char *));
977 for (i = sp->n; i; i--) {
984 /* ------------------------------------------------------------------------ */
990 free(*vector); /* free string pool */
995 /* ------------------------------------------------------------------------ */
996 /* READ DIRECTORY FILES */
997 /* ------------------------------------------------------------------------ */
999 include_path_p(path, name)
1004 if (*path++ != *n++)
1005 return (path[-1] == '/' && *n == '\0');
1006 return (*n == '/' || (n != name && path[-1] == '/' && n[-1] == '/'));
1009 /* ------------------------------------------------------------------------ */
1011 cleaning_files(v_filec, v_filev)
1018 register char **filev = *v_filev;
1019 register int filec = *v_filec;
1026 flags = xmalloc(filec * sizeof(char));
1028 /* flags & 0x01 : 1: ignore */
1029 /* flags & 0x02 : 1: directory, 0 : regular file */
1030 /* flags & 0x04 : 1: need delete */
1032 for (i = 0; i < filec; i++)
1033 if (GETSTAT(filev[i], &stbuf) < 0) {
1035 warning("Cannot access \"%s\" : %s; ignored.", filev[i],
1039 if (is_regularfile(&stbuf))
1041 else if (is_directory(&stbuf))
1044 else if (is_symlink(&stbuf)) /* t.okamoto */
1049 warning("Cannot archive \"%s\", ignored.", filev[i]);
1053 for (i = 0; i < filec; i++) {
1055 if ((flags[i] & 0x07) == 0x00) { /* regular file, not
1056 * deleted/ignored */
1057 for (j = i + 1; j < filec; j++) {
1058 if ((flags[j] & 0x07) == 0x00) { /* regular file, not
1059 * deleted/ignored */
1060 if (STREQU(p, filev[j]))
1061 flags[j] = 0x04; /* delete */
1065 else if ((flags[i] & 0x07) == 0x02) { /* directory, not
1066 * deleted/ignored */
1067 for (j = i + 1; j < filec; j++) {
1068 if ((flags[j] & 0x07) == 0x00) { /* regular file, not
1069 * deleted/ignored */
1070 if (include_path_p(p, filev[j]))
1071 flags[j] = 0x04; /* delete */
1073 else if ((flags[j] & 0x07) == 0x02) { /* directory, not
1074 * deleted/ignored */
1075 if (include_path_p(p, filev[j]))
1076 flags[j] = 0x04; /* delete */
1082 for (i = j = 0; i < filec; i++) {
1083 if ((flags[i] & 0x04) == 0) {
1085 filev[j] = filev[i];
1094 /* ------------------------------------------------------------------------ */
1096 find_files(name, v_filec, v_filev)
1101 struct string_pool sp;
1102 char newname[FILENAME_LENGTH];
1106 struct stat tmp_stbuf, arc_stbuf, fil_stbuf;
1107 int exist_tmp = 1, exist_arc = 1;
1109 len = str_safe_copy(newname, name, sizeof(newname));
1110 if (len > 0 && newname[len - 1] != '/') {
1111 if (len < sizeof(newname)-1)
1112 strcpy(&newname[len++], "/"); /* ok */
1114 warning("the length of pathname \"%s\" is too long.", name);
1117 dirp = opendir(name);
1123 if (GETSTAT(temporary_name, &tmp_stbuf) == -1)
1125 if (GETSTAT(archive_name, &arc_stbuf) == -1)
1128 while ((dp = readdir(dirp)) != NULL) {
1131 /* exclude '.' and '..' */
1132 if (strncmp(dp->d_name, ".", n) == 0
1133 || strncmp(dp->d_name, "..", n) == 0)
1136 /* exclude exclude_files supplied by user */
1137 for (i = 0; exclude_files && exclude_files[i]; i++) {
1138 if (fnmatch(exclude_files[i], dp->d_name,
1139 FNM_PATHNAME|FNM_NOESCAPE|FNM_PERIOD) == 0)
1143 if (len + n >= sizeof(newname)) {
1144 warning("filename is too long");
1148 strncpy(newname + len, dp->d_name, n);
1149 newname[len + n] = '\0';
1150 if (GETSTAT(newname, &fil_stbuf) < 0)
1153 #if defined(HAVE_STRUCT_STAT_ST_INO) && !__MINGW32__
1154 /* MinGW has meaningless st_ino */
1156 /* exclude temporary file, archive file and these links */
1158 tmp_stbuf.st_dev == fil_stbuf.st_dev &&
1159 tmp_stbuf.st_ino == fil_stbuf.st_ino)
1163 arc_stbuf.st_dev == fil_stbuf.st_dev &&
1164 arc_stbuf.st_ino == fil_stbuf.st_ino)
1167 add_sp(&sp, newname, len+n+1);
1172 finish_sp(&sp, v_filec, v_filev);
1173 if (*v_filec > 1 && sort_contents)
1174 qsort(*v_filev, *v_filec, sizeof(char *), sort_by_ascii);
1175 cleaning_files(v_filec, v_filev);
1180 /* ------------------------------------------------------------------------ */
1182 free_files(filec, filev)
1189 /* ------------------------------------------------------------------------ */
1191 /* ------------------------------------------------------------------------ */
1192 /* Build temporary file name and store to TEMPORARY_NAME */
1194 build_temporary_name()
1196 #ifdef TMP_FILENAME_TEMPLATE
1197 /* "/tmp/lhXXXXXX" etc. */
1198 if (extract_directory == NULL) {
1199 str_safe_copy(temporary_name, TMP_FILENAME_TEMPLATE,
1200 sizeof(temporary_name));
1203 xsnprintf(temporary_name, sizeof(temporary_name),
1204 "%s/lhXXXXXX", extract_directory);
1209 str_safe_copy(temporary_name, archive_name, sizeof(temporary_name));
1210 s = strrchr(temporary_name, '/');
1213 len = s - temporary_name;
1214 if (len + strlen("lhXXXXXX") < sizeof(temporary_name))
1215 /* use directory at archive file */
1216 strcpy(s, "lhXXXXXX"); /* ok */
1218 /* use current directory */
1219 str_safe_copy(temporary_name, "lhXXXXXX", sizeof(temporary_name));
1222 /* use current directory */
1223 str_safe_copy(temporary_name, "lhXXXXXX", sizeof(temporary_name));
1229 old_umask = umask(077);
1230 fd = mkstemp(temporary_name);
1238 mktemp(temporary_name);
1239 flags = O_CREAT|O_EXCL|O_RDWR;
1243 return open(temporary_name, flags, 0600);
1248 /* ------------------------------------------------------------------------ */
1250 modify_filename_extention(buffer, ext, size)
1255 register char *p, *dot;
1257 for (p = buffer, dot = (char *) 0; *p; p++) {
1267 str_safe_copy(p, ext, size - (p - buffer));
1270 /* ------------------------------------------------------------------------ */
1271 /* build backup file name */
1273 build_backup_name(buffer, original, size)
1278 str_safe_copy(buffer, original, size);
1279 modify_filename_extention(buffer, BACKUPNAME_EXTENTION, size); /* ".bak" */
1282 /* ------------------------------------------------------------------------ */
1284 build_standard_archive_name(buffer, original, size)
1289 str_safe_copy(buffer, original, size);
1290 modify_filename_extention(buffer, ARCHIVENAME_EXTENTION, size); /* ".lzh" */
1293 /* ------------------------------------------------------------------------ */
1295 /* ------------------------------------------------------------------------ */
1305 for (i = 0; i < cmd_filec; i++) {
1306 if (patmatch(cmd_filev[i], name, 0))
1319 if ((fp = fopen(name, mode)) == NULL)
1320 fatal_error("Cannot open file \"%s\"", name);
1325 /* ------------------------------------------------------------------------ */
1327 /* ------------------------------------------------------------------------ */
1329 open_old_archive_1(name, v_fp)
1336 if (stat(name, &stbuf) >= 0 &&
1337 is_regularfile(&stbuf) &&
1338 (fp = fopen(name, READ_BINARY)) != NULL) {
1340 archive_file_gid = stbuf.st_gid;
1341 archive_file_mode = stbuf.st_mode;
1346 archive_file_gid = -1;
1350 /* ------------------------------------------------------------------------ */
1355 char *p = NULL, *ext, *ext2;
1356 static char expanded_archive_name[FILENAME_LENGTH];
1358 if (!strcmp(archive_name, "-")) {
1359 if (cmd == CMD_EXTRACT || cmd == CMD_LIST) {
1360 #if defined(__MINGW32__) || defined(__DJGPP__)
1361 setmode(fileno(stdin), O_BINARY);
1369 ext2 = strrchr(archive_name, '.');
1378 p = xstrdup("lzh," ADDITIONAL_SUFFIXES);
1379 for (ext = strtok(p, ",");
1381 ext = strtok(NULL, ",")) {
1383 if (*ext == 0) continue;
1385 if (strcasecmp(ext, ext2)) {
1386 /* Try to open file just specified filename
1387 with usual suffixes.
1388 Return NULL if the file is not exist. */
1390 open_old_archive_1(archive_name, &fp);
1391 goto ret; /* found or not */
1398 /* Try to open file just specified filename */
1399 if (open_old_archive_1(archive_name, &fp))
1400 goto ret; /* found */
1402 /* Try to open file with implicit suffixes */
1403 p = xstrdup("lzh," ADDITIONAL_SUFFIXES);
1404 for (ext = strtok(p, ",");
1406 ext = strtok(NULL, ",")) {
1408 if (*ext == 0) continue;
1410 xsnprintf(expanded_archive_name, sizeof(expanded_archive_name),
1411 "%s.%s", archive_name, ext);
1413 if (open_old_archive_1(expanded_archive_name, &fp)) {
1414 archive_name = expanded_archive_name;
1415 goto ret; /* found */
1424 /* ------------------------------------------------------------------------ */
1426 inquire(msg, name, selective)
1427 char *msg, *name, *selective;
1433 fprintf(stderr, "%s %s ", name, msg);
1436 fgets(buffer, 1024, stdin);
1438 for (p = selective; *p; p++)
1439 if (buffer[0] == *p)
1440 return p - selective;
1445 /* ------------------------------------------------------------------------ */
1447 write_archive_tail(nafp)
1453 /* ------------------------------------------------------------------------ */