7 extern unsigned long compsize, origsize;
10 store(FILE *rfp, FILE *wfp)
17 while ((n = fread(buf, 1, sizeof(buf), rfp)) != 0) {
18 fwrite_crc(buf, n, wfp);
25 add_dir(struct lzh_ostream *wp, int replace_flag, struct lzh_header *h)
27 long headerpos, arcpos;
30 h->origsize = h->compsize = 0;
31 h->file_crc = INIT_CRC;
33 headerpos = ftell(wp->fp);
34 write_header(wp->fp, h);
35 arcpos = ftell(wp->fp);
38 printf(" %d.%d%%\n", r / 10, r % 10);
39 return 1; /* success */
43 add_1(struct lzh_ostream *wp, int replace_flag, struct lzh_header *h)
45 long headerpos, arcpos;
48 if ((infile = fopen(h->filename, "rb")) == NULL) {
49 fprintf(stderr, "Can't open %s\n", h->filename);
50 return 0; /* failure */
54 printf("Replacing %s ", h->filename);
58 printf("Adding %s ", h->filename);
61 headerpos = ftell(wp->fp);
62 write_header(wp->fp, h);
63 arcpos = ftell(wp->fp);
65 origsize = compsize = 0;
67 if (opts.nocompress) {
76 memcpy(h->method, "-lh0-", sizeof(h->method)); /* store */
78 fseek(wp->fp, arcpos, SEEK_SET);
79 h->compsize = h->origsize = store(infile, wp->fp);
82 h->compsize = compsize;
83 h->origsize = origsize;
85 h->file_crc = crc ^ INIT_CRC;
88 fseek(wp->fp, headerpos, SEEK_SET);
89 write_header(wp->fp, h);
90 fseek(wp->fp, 0L, SEEK_END);
91 r = ratio(compsize, origsize);
93 printf(" %d.%d%%\n", r / 10, r % 10);
94 return 1; /* success */
98 add(struct lzh_ostream *wp, int replace_flag, char *filename, int namelen)
103 memset(&h, 0, sizeof(h));
105 h.level = opts.header_level;
107 strcpy(h.filename, filename);
110 stat(h.filename, &st);
112 h.mtime = st.st_mtime;
113 if (S_ISDIR(st.st_mode)) {
117 memcpy(h.method, "-lhd-", sizeof(h.method)); /* directory */
118 add_dir(wp, replace_flag, &h);
120 dir = opendir(h.filename);
122 error("cannot open directory: \"%s\"", h.filename);
124 while ((ent = readdir(dir)) != 0) {
127 if (string_equal(ent->d_name, ".") ||
128 string_equal(ent->d_name, ".."))
131 h.namelen = path_addsep(h.filename, sizeof(h.filename));
133 string_cat(filename, sizeof(filename),
134 h.filename, ent->d_name, NULL);
136 add(wp, replace_flag, filename, strlen(filename));
141 memcpy(h.method, opts.method->id, sizeof(h.method)); /* compress */
142 add_1(wp, replace_flag, &h);