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 LHA-PMA for UNIX V 2 PMA added 2000 Maarten ter Huurne\n\
163 Autoconfiscated 2001-2008 Koji Arai\n\
169 commands: options:\n\
170 a Add(or replace) to archive q{num} quiet (num:quiet mode)\n\
171 x,e EXtract from archive v verbose\n\
172 l,v List / Verbose List n not execute\n\
173 u Update newer files to archive f force (over write at extract)\n\
174 d Delete from archive t FILES are TEXT file\n");
177 m Move to archive (means 'ad') o[567] compression method (a/u/c)\n\
182 m Move to archive (means 'ad') o use LHarc compatible method (a/u/c)\n\
186 c re-Construct new archive d delete FILES after (a/u/c)\n\
187 p Print to STDOUT from archive i ignore directory path (x/e)\n\
188 t Test file CRC in archive z files not compress (a/u/c)\n\
189 g Generic format (for compatibility)\n\
190 0/1/2 header level (a/u/c)\n\
194 e TEXT code convert from/to EUC\n\
197 #if HAVE_LIBAPPLEFILE
199 b decode MacBinary (x/e)\n\
203 w=<dir> specify extract directory (x/e)\n\
204 x=<pattern> eXclude files (a/u/c)\n\
208 #include "getopt_long.h"
214 parse_suboption(int argc, char **argv)
219 SYSTEM_KANJI_CODE_OPTION,
220 ARCHIVE_KANJI_CODE_OPTION,
221 TRADITIONAL_BEHAVIOR,
226 struct option long_options[] = {
227 /* These options set a flag. */
228 {"help", no_argument, 0, HELP_OPTION},
229 {"version", no_argument, 0, VERSION_OPTION},
231 {"system-kanji-code", required_argument, 0, SYSTEM_KANJI_CODE_OPTION},
232 {"archive-kanji-code", required_argument, 0, ARCHIVE_KANJI_CODE_OPTION},
233 {"extract-broken-archive", no_argument, &extract_broken_archive, 1},
234 {"convert-filename-case", no_argument, &convertcase, TRUE},
235 {"traditional", no_argument, 0, TRADITIONAL_BEHAVIOR},
236 {"ignore-mac-files", no_argument, 0, IGNORE_MAC_FILES},
237 {"timestamp-archive", no_argument, ×tamp_archive, 1},
238 {"debug", required_argument, 0, DEBUG_OPTION},
243 char short_options[256] = "q[012]vnfto[567]dizg012ew:x:";
244 /* "[...]" means optional 1 byte argument (original extention) */
246 #if HAVE_LIBAPPLEFILE
247 strncat(short_options, "b", sizeof(short_options)-strlen(short_options)-1);
252 int option_index = 0;
253 int c = getopt_long(argc, argv,
254 short_options, long_options,
257 if (c == -1) break; /* end of options */
261 /* Already set a flag variable by the definition of the
276 /* In quiet mode, no confirm to overwrite */
283 case '0': /* no quiet */
284 case '1': /* no use the incremental indicator */
285 quiet_mode = *optarg - '0';
287 case '2': /* no output */
310 #if HAVE_LIBAPPLEFILE
312 decode_macbinary_contents = TRUE;
319 generic_format = TRUE;
323 delete_after_append = TRUE;
327 compress_method = LZHUFF1_METHOD_NUM;
333 compress_method = LZHUFF5_METHOD_NUM;
337 compress_method = LZHUFF6_METHOD_NUM;
340 compress_method = LZHUFF7_METHOD_NUM;
344 error("invalid compression method 'o%c'", *optarg);
349 compress_method = LZHUFF0_METHOD_NUM; /* Changed N.Watazaki */
352 ignore_directory = TRUE;
356 error("exclude files does not specified for `-x'");
360 for (i = 0; exclude_files && exclude_files[i]; i++)
362 exclude_files = (char**)xrealloc(exclude_files,
363 sizeof(char*) * (i+2));
367 exclude_files[i] = optarg;
368 exclude_files[i+1] = 0;
373 error("working directory does not specified for `-w'");
379 extract_directory = optarg;
390 case SYSTEM_KANJI_CODE_OPTION:
392 error("kanji code not specified for --%s",
393 long_options[option_index].name);
396 if (strcmp(optarg, "euc") == 0) {
397 optional_system_kanji_code = CODE_EUC;
399 else if (strcmp(optarg, "sjis") == 0) {
400 optional_system_kanji_code = CODE_SJIS;
402 else if (strcmp(optarg, "utf8") == 0) {
403 optional_system_kanji_code = CODE_UTF8;
405 else if (strcmp(optarg, "cap") == 0) {
406 optional_system_kanji_code = CODE_CAP;
409 error("unknown kanji code \"%s\"", optarg);
414 case ARCHIVE_KANJI_CODE_OPTION:
416 error("kanji code not specified for --%s",
417 long_options[option_index].name);
420 if (strcmp(optarg, "euc") == 0) {
421 optional_archive_kanji_code = CODE_EUC;
423 else if (strcmp(optarg, "sjis") == 0) {
424 optional_archive_kanji_code = CODE_SJIS;
426 else if (strcmp(optarg, "utf8") == 0) {
427 optional_archive_kanji_code = CODE_UTF8;
429 else if (strcmp(optarg, "cap") == 0) {
430 optional_archive_kanji_code = CODE_CAP;
433 error("unknown kanji code \"%s\"", optarg);
438 case TRADITIONAL_BEHAVIOR:
442 case IGNORE_MAC_FILES:
443 /* ignore Mac specific files (._*, .DS_Store and Icon\r)
445 for (i = 0; exclude_files && exclude_files[i]; i++)
447 exclude_files = (char**)xrealloc(exclude_files,
448 sizeof(char*) * (i+4));
450 exclude_files[i] = xstrdup("._*");
451 exclude_files[i+1] = xstrdup(".DS_Store");
452 exclude_files[i+2] = xstrdup("Icon\r");
453 exclude_files[i+3] = 0;
458 error("debugging item is not specified for --%s",
459 long_options[option_index].name);
462 if (strcmp(optarg, "nosort") == 0) {
463 sort_contents = FALSE;
465 else if (strcmp(optarg, "norecursion") == 0) {
466 recursive_archiving = FALSE;
468 else if (strcmp(optarg, "dumplzss") == 0) {
473 error("unknown debugging item \"%s\" for --%s",
474 optarg, long_options[option_index].name);
480 error("unknown option `-%c'.", c);
489 archive_name = *argv++;
500 Parse LHA command and options.
503 parse_option(int argc, char **argv)
507 if (argv[1] == NULL || strcmp(argv[1], "--help") == 0) {
512 if (strcmp(argv[1], "--version") == 0) {
517 if (argc == 2 && *argv[1] != '-') {
518 archive_name = argv[1];
527 if (cmd_char[0] == '-')
538 output_to_stdout = TRUE;
556 update_if_newer = TRUE;
561 delete_after_append = TRUE;
566 verbose_listing = TRUE;
580 error("unknown command `-%c'", *cmd_char);
584 if (cmd_char[1] == '\0') {
585 /* argv[1] is command name */
591 /* Eliminate command character
592 e.g.) lha cv foo.lzh -> lha -v foo.lzh
593 lha -cv foo.lzh -> lha -v foo.lzh
599 return parse_suboption(argc, argv);
602 /* ------------------------------------------------------------------------ */
612 init_variable(); /* Added N.Watazaki */
614 if (parse_option(argc, argv) == -1) {
621 error("archive file does not specified");
627 if (!strcmp(archive_name, "-")) {
628 if (!isatty(1) && cmd == CMD_ADD)
631 #if 0 /* Comment out; IMHO, this feature is useless. by Koji Arai */
633 if (argc == 3 && !isatty(0)) { /* 1999.7.18 */
634 /* Bug(?) on MinGW, isatty() return 0 on Cygwin console.
635 mingw-runtime-1.3-2 and Cygwin 1.3.10(0.51/3/2) on Win2000 */
636 get_filename_from_stdin = TRUE;
641 /* target file name */
642 if (get_filename_from_stdin) {
648 xfilev = (char **)xmalloc(sizeof(char *) * xfilec);
649 while (fgets(inpbuf, sizeof(inpbuf), stdin)) {
650 /* delete \n if it exist */
652 while (i < sizeof(inpbuf) && p != 0) {
660 if (cmd_filec >= xfilec) {
662 xfilev = (char **) xrealloc(xfilev,
663 sizeof(char *) * xfilec);
665 if (strlen(inpbuf) < 1)
667 xfilev[cmd_filec++] = xstrdup(inpbuf);
669 xfilev[cmd_filec] = NULL;
699 /* ------------------------------------------------------------------------ */
703 /* macro PACKAGE_NAME, PACKAGE_VERSION and PLATFORM are
704 defined in config.h by configure script */
705 fprintf(stderr, "%s version %s (%s)\n",
706 PACKAGE_NAME, PACKAGE_VERSION, PLATFORM);
707 fprintf(stderr, " configure options: %s\n", LHA_CONFIGURE_OPTIONS);
712 message(char *fmt, ...)
714 message(fmt, va_alist)
719 int errno_sv = errno;
722 fprintf(stderr, "LHa: ");
725 vfprintf(stderr, fmt, v);
733 /* ------------------------------------------------------------------------ */
736 warning(char *fmt, ...)
738 warning(fmt, va_alist)
743 int errno_sv = errno;
746 fprintf(stderr, "LHa: Warning: ");
749 vfprintf(stderr, fmt, v);
757 /* ------------------------------------------------------------------------ */
760 error(char *fmt, ...)
767 int errno_sv = errno;
770 fprintf(stderr, "LHa: Error: ");
773 vfprintf(stderr, fmt, v);
785 fatal_error(char *fmt, ...)
787 fatal_error(fmt, va_alist)
792 int errno_sv = errno;
795 fprintf(stderr, "LHa: Fatal error: ");
798 vfprintf(stderr, fmt, v);
802 fprintf(stderr, ": %s\n", strerror(errno_sv));
812 if (temporary_fd != -1) {
815 unlink(temporary_name);
818 if (recover_archive_when_interrupt) {
819 rename(backup_archive_name, archive_name);
820 recover_archive_when_interrupt = FALSE;
822 if (remove_extracting_file_when_interrupt) {
823 message("Removing: %s", writing_filename);
824 unlink(writing_filename);
825 remove_extracting_file_when_interrupt = FALSE;
833 message("Interrupted");
837 signal(SIGINT, SIG_DFL);
839 signal(SIGHUP, SIG_DFL);
841 kill(getpid(), signo);
844 /* ------------------------------------------------------------------------ */
846 /* ------------------------------------------------------------------------ */
851 register char *p, *q;
855 if (generic_format) {
857 c1 = *(unsigned char *) p++;
858 c2 = *(unsigned char *) q++;
870 while (*p == *q && *p != '\0')
872 return *(unsigned char *) p - *(unsigned char *) q;
876 /* ------------------------------------------------------------------------ */
880 if (cmd_filec > 1 && sort_contents)
881 qsort(cmd_filev, cmd_filec, sizeof(char *), sort_by_ascii);
884 /* ------------------------------------------------------------------------ */
889 void *p = malloc(size);
891 fatal_error("Not enough memory");
895 /* ------------------------------------------------------------------------ */
901 void *p = (char *) realloc(old, size);
903 fatal_error("Not enough memory");
911 int len = strlen(str);
912 char *p = (char *)xmalloc(len + 1);
913 strcpy(p, str); /* ok */
917 /* ------------------------------------------------------------------------ */
919 /* ------------------------------------------------------------------------ */
922 +-------------+-------------+------+-------------+----------+
923 | N A M E 1 \0| N A M E 2 \0| .... | N A M E n \0| |
924 +-------------+-------------+------+-------------+----------+
925 ^ ^ ^ buffer+0 buffer+used buffer+size
928 +---------------+---------------+------------- -----------------+
929 | pointer to | pointer to | pointer to ... pointer to |
930 | stringpool | N A M E 1 | N A M E 2 ... N A M E n |
931 +---------------+---------------+------------- -------------+
932 ^ malloc base returned
935 /* ------------------------------------------------------------------------ */
938 struct string_pool *sp;
940 sp->size = 1024 - 8; /* any ( >=0 ) */
943 sp->buffer = (char *) xmalloc(sp->size * sizeof(char));
946 /* ------------------------------------------------------------------------ */
948 add_sp(sp, name, len)
949 struct string_pool *sp;
950 char *name; /* stored '\0' at tail */
951 int len; /* include '\0' */
953 while (sp->used + len > sp->size) {
955 sp->buffer = (char *) xrealloc(sp->buffer, sp->size * sizeof(char));
957 memmove(sp->buffer + sp->used, name, len);
962 /* ------------------------------------------------------------------------ */
964 finish_sp(sp, v_count, v_vector)
965 register struct string_pool *sp;
973 v = (char **) xmalloc((sp->n + 1) * sizeof(char *));
978 for (i = sp->n; i; i--) {
985 /* ------------------------------------------------------------------------ */
991 free(*vector); /* free string pool */
996 /* ------------------------------------------------------------------------ */
997 /* READ DIRECTORY FILES */
998 /* ------------------------------------------------------------------------ */
1000 include_path_p(path, name)
1005 if (*path++ != *n++)
1006 return (path[-1] == '/' && *n == '\0');
1007 return (*n == '/' || (n != name && path[-1] == '/' && n[-1] == '/'));
1010 /* ------------------------------------------------------------------------ */
1012 cleaning_files(v_filec, v_filev)
1019 register char **filev = *v_filev;
1020 register int filec = *v_filec;
1027 flags = xmalloc(filec * sizeof(char));
1029 /* flags & 0x01 : 1: ignore */
1030 /* flags & 0x02 : 1: directory, 0 : regular file */
1031 /* flags & 0x04 : 1: need delete */
1033 for (i = 0; i < filec; i++)
1034 if (GETSTAT(filev[i], &stbuf) < 0) {
1036 warning("Cannot access \"%s\" : %s; ignored.", filev[i],
1040 if (is_regularfile(&stbuf))
1042 else if (is_directory(&stbuf))
1045 else if (is_symlink(&stbuf)) /* t.okamoto */
1050 warning("Cannot archive \"%s\", ignored.", filev[i]);
1054 for (i = 0; i < filec; i++) {
1056 if ((flags[i] & 0x07) == 0x00) { /* regular file, not
1057 * deleted/ignored */
1058 for (j = i + 1; j < filec; j++) {
1059 if ((flags[j] & 0x07) == 0x00) { /* regular file, not
1060 * deleted/ignored */
1061 if (STREQU(p, filev[j]))
1062 flags[j] = 0x04; /* delete */
1066 else if ((flags[i] & 0x07) == 0x02) { /* directory, not
1067 * deleted/ignored */
1068 for (j = i + 1; j < filec; j++) {
1069 if ((flags[j] & 0x07) == 0x00) { /* regular file, not
1070 * deleted/ignored */
1071 if (include_path_p(p, filev[j]))
1072 flags[j] = 0x04; /* delete */
1074 else if ((flags[j] & 0x07) == 0x02) { /* directory, not
1075 * deleted/ignored */
1076 if (include_path_p(p, filev[j]))
1077 flags[j] = 0x04; /* delete */
1083 for (i = j = 0; i < filec; i++) {
1084 if ((flags[i] & 0x04) == 0) {
1086 filev[j] = filev[i];
1095 /* ------------------------------------------------------------------------ */
1097 find_files(name, v_filec, v_filev)
1102 struct string_pool sp;
1103 char newname[FILENAME_LENGTH];
1107 struct stat tmp_stbuf, arc_stbuf, fil_stbuf;
1108 int exist_tmp = 1, exist_arc = 1;
1110 len = str_safe_copy(newname, name, sizeof(newname));
1111 if (len > 0 && newname[len - 1] != '/') {
1112 if (len < sizeof(newname)-1)
1113 strcpy(&newname[len++], "/"); /* ok */
1115 warning("the length of pathname \"%s\" is too long.", name);
1118 dirp = opendir(name);
1124 if (GETSTAT(temporary_name, &tmp_stbuf) == -1)
1126 if (GETSTAT(archive_name, &arc_stbuf) == -1)
1129 while ((dp = readdir(dirp)) != NULL) {
1132 /* exclude '.' and '..' */
1133 if (strncmp(dp->d_name, ".", n) == 0
1134 || strncmp(dp->d_name, "..", n) == 0)
1137 /* exclude exclude_files supplied by user */
1138 for (i = 0; exclude_files && exclude_files[i]; i++) {
1139 if (fnmatch(exclude_files[i], dp->d_name,
1140 FNM_PATHNAME|FNM_NOESCAPE|FNM_PERIOD) == 0)
1144 if (len + n >= sizeof(newname)) {
1145 warning("filename is too long");
1149 strncpy(newname + len, dp->d_name, n);
1150 newname[len + n] = '\0';
1151 if (GETSTAT(newname, &fil_stbuf) < 0)
1154 #if defined(HAVE_STRUCT_STAT_ST_INO) && !__MINGW32__
1155 /* MinGW has meaningless st_ino */
1157 /* exclude temporary file, archive file and these links */
1159 tmp_stbuf.st_dev == fil_stbuf.st_dev &&
1160 tmp_stbuf.st_ino == fil_stbuf.st_ino)
1164 arc_stbuf.st_dev == fil_stbuf.st_dev &&
1165 arc_stbuf.st_ino == fil_stbuf.st_ino)
1168 add_sp(&sp, newname, len+n+1);
1173 finish_sp(&sp, v_filec, v_filev);
1174 if (*v_filec > 1 && sort_contents)
1175 qsort(*v_filev, *v_filec, sizeof(char *), sort_by_ascii);
1176 cleaning_files(v_filec, v_filev);
1181 /* ------------------------------------------------------------------------ */
1183 free_files(filec, filev)
1190 /* ------------------------------------------------------------------------ */
1192 /* ------------------------------------------------------------------------ */
1193 /* Build temporary file name and store to TEMPORARY_NAME */
1195 build_temporary_name()
1197 #ifdef TMP_FILENAME_TEMPLATE
1198 /* "/tmp/lhXXXXXX" etc. */
1199 if (extract_directory == NULL) {
1200 str_safe_copy(temporary_name, TMP_FILENAME_TEMPLATE,
1201 sizeof(temporary_name));
1204 xsnprintf(temporary_name, sizeof(temporary_name),
1205 "%s/lhXXXXXX", extract_directory);
1210 str_safe_copy(temporary_name, archive_name, sizeof(temporary_name));
1211 s = strrchr(temporary_name, '/');
1214 len = s - temporary_name;
1215 if (len + strlen("lhXXXXXX") < sizeof(temporary_name))
1216 /* use directory at archive file */
1217 strcpy(s, "lhXXXXXX"); /* ok */
1219 /* use current directory */
1220 str_safe_copy(temporary_name, "lhXXXXXX", sizeof(temporary_name));
1223 /* use current directory */
1224 str_safe_copy(temporary_name, "lhXXXXXX", sizeof(temporary_name));
1230 old_umask = umask(077);
1231 fd = mkstemp(temporary_name);
1239 mktemp(temporary_name);
1240 flags = O_CREAT|O_EXCL|O_RDWR;
1244 return open(temporary_name, flags, 0600);
1249 /* ------------------------------------------------------------------------ */
1251 modify_filename_extention(buffer, ext, size)
1256 register char *p, *dot;
1258 for (p = buffer, dot = (char *) 0; *p; p++) {
1268 str_safe_copy(p, ext, size - (p - buffer));
1271 /* ------------------------------------------------------------------------ */
1272 /* build backup file name */
1274 build_backup_name(buffer, original, size)
1279 str_safe_copy(buffer, original, size);
1280 modify_filename_extention(buffer, BACKUPNAME_EXTENTION, size); /* ".bak" */
1283 /* ------------------------------------------------------------------------ */
1285 build_standard_archive_name(buffer, original, size)
1290 str_safe_copy(buffer, original, size);
1291 modify_filename_extention(buffer, ARCHIVENAME_EXTENTION, size); /* ".lzh" */
1294 /* ------------------------------------------------------------------------ */
1296 /* ------------------------------------------------------------------------ */
1306 for (i = 0; i < cmd_filec; i++) {
1307 if (patmatch(cmd_filev[i], name, 0))
1320 if ((fp = fopen(name, mode)) == NULL)
1321 fatal_error("Cannot open file \"%s\"", name);
1326 /* ------------------------------------------------------------------------ */
1328 /* ------------------------------------------------------------------------ */
1330 open_old_archive_1(name, v_fp)
1337 if (stat(name, &stbuf) >= 0 &&
1338 is_regularfile(&stbuf) &&
1339 (fp = fopen(name, READ_BINARY)) != NULL) {
1341 archive_file_gid = stbuf.st_gid;
1342 archive_file_mode = stbuf.st_mode;
1347 archive_file_gid = -1;
1351 /* ------------------------------------------------------------------------ */
1356 char *p = NULL, *ext, *ext2;
1357 static char expanded_archive_name[FILENAME_LENGTH];
1359 if (!strcmp(archive_name, "-")) {
1360 if (cmd == CMD_EXTRACT || cmd == CMD_LIST) {
1361 #if defined(__MINGW32__) || defined(__DJGPP__)
1362 setmode(fileno(stdin), O_BINARY);
1370 ext2 = strrchr(archive_name, '.');
1379 p = xstrdup("lzh," ADDITIONAL_SUFFIXES);
1380 for (ext = strtok(p, ",");
1382 ext = strtok(NULL, ",")) {
1384 if (*ext == 0) continue;
1386 if (strcasecmp(ext, ext2)) {
1387 /* Try to open file just specified filename
1388 with usual suffixes.
1389 Return NULL if the file is not exist. */
1391 open_old_archive_1(archive_name, &fp);
1392 goto ret; /* found or not */
1399 /* Try to open file just specified filename */
1400 if (open_old_archive_1(archive_name, &fp))
1401 goto ret; /* found */
1403 /* Try to open file with implicit suffixes */
1404 p = xstrdup("lzh," ADDITIONAL_SUFFIXES);
1405 for (ext = strtok(p, ",");
1407 ext = strtok(NULL, ",")) {
1409 if (*ext == 0) continue;
1411 xsnprintf(expanded_archive_name, sizeof(expanded_archive_name),
1412 "%s.%s", archive_name, ext);
1414 if (open_old_archive_1(expanded_archive_name, &fp)) {
1415 archive_name = expanded_archive_name;
1416 goto ret; /* found */
1425 /* ------------------------------------------------------------------------ */
1427 inquire(msg, name, selective)
1428 char *msg, *name, *selective;
1434 fprintf(stderr, "%s %s ", name, msg);
1437 fgets(buffer, 1024, stdin);
1439 for (p = selective; *p; p++)
1440 if (buffer[0] == *p)
1441 return p - selective;
1446 /* ------------------------------------------------------------------------ */
1448 write_archive_tail(nafp)
1454 /* ------------------------------------------------------------------------ */