|
|
1.1 root 1: #include <stddef.h>
2: #include <stdio.h>
3: #include <stdlib.h>
4: #include "misc.h"
5:
6: #define MIN(A, B) ((A) < (B) ? (A) : (B))
7:
8: char *prog;
9:
10: void *
11: xmalloc(size_t size)
12: {
13: void *result;
14:
15: result = malloc(size);
16: if (size && !result) {
17: fprintf(stderr, "%s: malloc failure\n", prog);
18: exit(1);
19: }
20: return result;
21: }
22:
23: char *
24: xstrdup(char *s)
25: {
26: char *t;
27:
28: t = xmalloc(strlen(s) + 1);
29: strcpy(t, s);
30: return t;
31: }
32:
33: long
34: fpcopy(FILE *dst, FILE *src, long size)
35: {
36: char buf[4096];
37: long cc, total;
38:
39: total = 0;
40: while (total < size) {
41: cc = fread(buf, 1, MIN(sizeof buf, size - total), src);
42: if (cc <= 0)
43: break;
44: if (fwrite(buf, 1, cc, dst) != cc)
45: break;
46: total += cc;
47: }
48: if (ferror(dst) || ferror(src))
49: return -1;
50: return total;
51: }
52:
53: void
54: discard(FILE *fp, long n)
55: {
56: char buf[4096];
57: long cc;
58:
59: while (n >= sizeof buf) {
60: cc = fread(buf, 1, sizeof buf, fp);
61: if (cc <= 0)
62: return;
63: n -= cc;
64: }
65: fread(buf, 1, n, fp);
66: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.