7 store(FILE *rfp, FILE *wfp, unsigned int *crc)
14 while ((n = fread(buf, 1, sizeof(buf), rfp)) != 0) {
15 fwrite_crc(buf, n, wfp, crc);
22 add_dir(struct lzh_ostream *wp, int replace_flag, struct lzh_header *h)
24 long headerpos, arcpos;
27 h->origsize = h->compsize = 0;
28 h->file_crc = INIT_CRC;
30 headerpos = ftell(wp->fp);
31 write_header(wp->fp, h);
32 arcpos = ftell(wp->fp);
35 printf(" %d.%d%%\n", r / 10, r % 10);
36 return 1; /* success */
40 add_1(struct lzh_ostream *wp, int replace_flag, struct lzh_header *h)
42 long headerpos, arcpos;
46 if ((rfp = fopen(h->filename, "rb")) == NULL) {
47 fprintf(stderr, "Can't open %s\n", h->filename);
48 return 0; /* failure */
52 printf("Replacing %s ", h->filename);
56 printf("Adding %s ", h->filename);
59 headerpos = ftell(wp->fp);
60 write_header(wp->fp, h);
61 arcpos = ftell(wp->fp);
63 wp->origsize = wp->compsize = 0;
65 if (opts.nocompress) {
74 memcpy(h->method, "-lh0-", sizeof(h->method)); /* store */
76 fseek(wp->fp, arcpos, SEEK_SET);
77 h->compsize = h->origsize = store(rfp, wp->fp, &wp->crc);
80 h->compsize = wp->compsize;
81 h->origsize = wp->origsize;
83 h->file_crc = wp->crc ^ INIT_CRC;
86 fseek(wp->fp, headerpos, SEEK_SET);
87 write_header(wp->fp, h);
88 fseek(wp->fp, 0L, SEEK_END);
89 r = ratio(wp->compsize, wp->origsize);
91 printf(" %d.%d%%\n", r / 10, r % 10);
92 return 1; /* success */
96 add(struct lzh_ostream *wp, int replace_flag, char *filename, int namelen)
101 memset(&h, 0, sizeof(h));
103 h.level = opts.header_level;
105 strcpy(h.filename, filename);
108 stat(h.filename, &st);
110 h.mtime = st.st_mtime;
111 if (S_ISDIR(st.st_mode)) {
115 memcpy(h.method, "-lhd-", sizeof(h.method)); /* directory */
116 add_dir(wp, replace_flag, &h);
118 dir = opendir(h.filename);
120 error("cannot open directory: \"%s\"", h.filename);
122 while ((ent = readdir(dir)) != 0) {
125 if (string_equal(ent->d_name, ".") ||
126 string_equal(ent->d_name, ".."))
129 h.namelen = path_addsep(h.filename, sizeof(h.filename));
131 string_cat(filename, sizeof(filename),
132 h.filename, ent->d_name, NULL);
134 add(wp, replace_flag, filename, strlen(filename));
139 memcpy(h.method, opts.method->id, sizeof(h.method)); /* compress */
140 add_1(wp, replace_flag, &h);