1 /***********************************************************
3 ***********************************************************/
5 static char *version = "0.01";
8 "ar -- compression archiver -- written by Haruhiko Okumura\n"
9 " PC-VAN:SCIENCE CompuServe:74050,1022\n"
10 " NIFTY-Serve:PAF01022 INTERNET:74050.1022@compuserve.com\n"
11 "Usage: ar command archive [file ...]\n"
13 " a: Add files to archive (replace if present)\n"
14 " x: Extract files from archive\n"
15 " r: Replace files in archive\n"
16 " d: Delete files from archive\n"
17 " p: Print files on standard output\n"
18 " l: List contents of archive\n"
19 "If no files are named, all files in archive are processed,\n"
20 " except for commands 'a' and 'd'.\n"
21 "You may copy, distribute, and rewrite this program freely.\n";
23 /***********************************************************
25 Structure of archive block (low order byte first):
28 = 25 + strlen(filename) (= 0 if end of archive)
29 1 basic header algebraic sum (mod 256)
31 5 method ("-lh0-" = stored, "-lh5-" = compressed)
32 4 compressed size (including extended headers)
41 2 first extended header size (0 if none)
42 -----first extended header, etc.
45 ***********************************************************/
57 struct lha_method methods[] = {
58 /* id, dicbit, pbit, maxmatch */
59 /* note: dicbit == 0 means no compress */
60 {"-lh0-", 0, 0, 0}, /* 0: no compress */
61 {"-lh1-", 12, 0, 60}, /* 1: 2^12 = 4KB dynamic huffman (LHarc) */
62 {"-lh2-", 13, 0,256}, /* 2: 2^13 = 8KB dynamic huffman */
63 {"-lh3-", 13, 0,256}, /* 3: 2^13 = 8KB static huffman */
64 {"-lh4-", 12, 4,256}, /* 4: 2^12 = 4KB static huffman (pos and len)*/
65 {"-lh5-", 13, 4,256}, /* 5: 2^13 = 8KB static huffman (pos and len)*/
66 {"-lh6-", 15, 5,256}, /* 6: 2^15 = 32KB static huffman (pos and len)*/
67 {"-lh7-", 16, 5,256}, /* 7: 2^16 = 64KB static huffman (pos and len)*/
68 {"-lzs-", 11, 0, 17}, /* 8: 2^11 = 2KB (LArc) */
69 {"-lz5-", 12, 0, 17}, /* 9: 2^12 = 4KB (LArc) */
70 {"-lz4-", 0, 0, 0}, /*10: no compress (LArc) */
71 {"-lhd-", 0, 0, 0}, /*11: directory */
77 which_method(char *id)
81 for (i = 0; i < sizeof(methods)/sizeof(methods[0]); i++) {
82 if (strncmp(id, methods[i].id, sizeof(methods[0].id)) == 0) {
89 int unpackable; /* global, set in io.c */
90 ulong compsize, origsize; /* global */
92 static char *temp_name = NULL;
104 printf("version %s\n", version);
109 ratio(ulong a, ulong b)
110 { /* [(1000a + [b/2]) / b] */
113 for (i = 0; i < 3; i++)
114 if (a <= ULONG_MAX / 10)
118 if ((ulong) (a + (b >> 1)) < a) {
124 return (uint) ((a + (b >> 1)) / b);
128 skip(FILE *fp, struct lzh_header *h)
131 if (opts.archive_to_stdio)
132 for (i = 0; i < h->compsize; i++)
135 fseek(fp, h->compsize, SEEK_CUR);
139 copy(FILE *arcfile, FILE *outfile, struct lzh_header *h)
142 uchar buffer[MAXDICSIZ];
144 write_header(outfile, h);
145 while (h->compsize != 0) {
146 n = (uint) ((h->compsize > sizeof(buffer)) ? sizeof(buffer) : h->compsize);
147 if (fread((char *) buffer, 1, n, arcfile) != n)
149 if (fwrite((char *) buffer, 1, n, outfile) != n)
150 error("Can't write");
156 get_line(char *s, int n)
161 while ((c = getchar()) != EOF && c != '\n')
169 match(char *s1, char *s2)
172 while (*s2 == '*' || *s2 == '?') {
174 while (*s1 && *s1 != *s2)
191 search(int argc, char *argv[], struct lzh_header *h)
197 for (i = 0; i < argc; i++)
198 if (argv[i] && match(h->filename, argv[i]))
210 #include "getopt_long.h"
213 parse_args(int argc, char **argv)
218 /* int this_option_optind = optind ? optind : 1; */
219 int option_index = 0;
226 static struct option long_options[] = {
227 /* name, has_arg, *flag, val */
230 required_argument (1)
231 optional_argument (2)
233 NULL: getopt_long() return val
234 non-NULL: getopt_long() return 0, and *flag set val.
236 {"help", no_argument, NULL, LHA_OPT_HELP},
237 {"version", no_argument, NULL, LHA_OPT_VERSION},
241 c = getopt_long(argc, argv, "012fgo[567]q[012]vw:z",
242 long_options, &option_index);
244 if (c == -1) break; /* end of parsing options */
251 /* set value by long option */
253 case '0': case '1': case '2':
255 opts.header_level = c - '0';
258 opts.force_extract = 1;
262 opts.header_level = 0;
265 /* compress method */
267 int idx = 1; /* -o means -lh1- method */
270 idx = *optarg - '0'; /* -lh[567]- method */
272 opts.method = &methods[idx];
277 opts.quiet = 2; /* -q is equivalent to -q2 */
279 opts.quiet = *optarg - '0';
287 /* extract directory */
289 error("extract directory does not specified for `-w'");
293 opts.outdir = optarg;
295 case 'z': /* no compress */
301 case LHA_OPT_VERSION:
315 temp_name = tmpnam(NULL);
316 outfile = fopen(temp_name, "wb");
318 error("Can't open temporary file");
325 main(int argc, char *argv[])
327 int i, cmd, count, nfiles, found, done;
331 struct lzh_istream r, *rp;
332 struct lzh_ostream w, *wp;
333 FILE *arcfile = NULL;
334 FILE *outfile = NULL;
339 INITIALIZE_OPTS(opts);
344 /*take a command character */
356 /* -<cmd> -<opts> ... */
361 /* -<cmd><opts> => -<opts> */
366 parse_args(argc, argv);
370 archive_file = argv[0];
372 if (strcmp(archive_file, "-") == 0)
373 opts.archive_to_stdio = 1;
379 count = done = nfiles = 0;
385 if (opts.archive_to_stdio)
388 outfile = open_tempfile();
391 error("archived files are not specified.");
393 if (!opts.archive_to_stdio && (cmd == 'a' || cmd == 'u')) {
394 if (file_exists(archive_file)) {
395 arcfile = fopen(archive_file, "rb");
397 error("Can't open archive '%s'", archive_file);
402 for (i = 0; i < argc; i++) {
403 add(wp, 0, argv[i], strlen(argv[i]));
406 fputc(0, outfile); /* end of archive */
408 error("Can't write");
410 if (opts.archive_to_stdio) {
411 if (move_file_to_stream(temp_name, stdout) == -1)
412 error("fail to move_file_to_stream(): %s -> %s",temp_name,"stdout");
415 unlink(archive_file);
416 if (xrename(temp_name, archive_file) == -1)
417 error("fail to rename(): %s -> %s",temp_name,archive_file);
424 message("No files given in argument, do nothing.");
427 outfile = open_tempfile();
433 if (opts.archive_to_stdio) {
437 arcfile = fopen(archive_file, "rb");
440 error("Can't open archive '%s'", archive_file);
448 /* change directory to extract dir */
451 if (mkdir(opts.outdir, 0777) == -1) {
453 error("cannot make directory \"%s\"", opts.outdir);
456 if (chdir(opts.outdir) == -1)
457 error("cannot change directory \"%s\"", opts.outdir);
463 while (!done && read_header(arcfile, &h)) {
467 compsize = h.compsize;
468 origsize = h.origsize;
470 found = search(argc, argv, &h);
480 if (file_mtime(h.filename, &mtime) == -1 || h.mtime > mtime) {
481 copy(arcfile, outfile, &h);
486 if (add(wp, 1, h.filename, h.namelen)) {
491 copy(arcfile, outfile, &h);
494 copy(arcfile, outfile, &h);
499 message("'%s' deleted", h.filename);
503 copy(arcfile, outfile, &h);
509 extract(rp, cmd == 'x', &h);
510 if (++count == nfiles)
522 if (++count == nfiles)
530 if (cmd == 'a' || cmd == 'u') {
531 for (i = 0; i < argc; i++) {
534 add(wp, 0, argv[i], strlen(argv[i]));
541 printf(" %d files\n", count);
544 if (count > 0 && (cmd == 'd' || cmd == 'a' || cmd == 'u')) {
545 fputc(0, outfile); /* end of archive */
547 error("Can't write");
548 if (!opts.archive_to_stdio)
549 unlink(archive_file);
553 if (arc_count > count) {
554 if (xrename(temp_name, archive_file) == -1)
555 error("fail to rename(): %s -> %s",temp_name,archive_file);
558 message("The archive file \"%s\" was removed because it would be empty.", archive_file);
562 if (xrename(temp_name, archive_file) == -1)
563 error("fail to rename(): %s -> %s",temp_name,archive_file);