TOR: fix compilation
[tomato.git] / release / src / router / e2fsprogs / contrib / make-sparse.c
blob1b3d5506d79faa5d3eaeabd7866b6850d1d9ceeb
1 /*
2 * make-sparse.c --- make a sparse file from stdin
3 *
4 * Copyright 2004 by Theodore Ts'o.
6 * %Begin-Header%
7 * This file may be redistributed under the terms of the GNU Public
8 * License.
9 * %End-Header%
12 #define _LARGEFILE_SOURCE
13 #define _LARGEFILE64_SOURCE
15 #include <stdio.h>
16 #include <unistd.h>
17 #include <stdlib.h>
18 #include <sys/types.h>
19 #include <sys/stat.h>
20 #include <fcntl.h>
21 #include <errno.h>
23 int full_read(int fd, char *buf, size_t count)
25 int got, total = 0;
26 int pass = 0;
28 while (count > 0) {
29 got = read(fd, buf, count);
30 if (got == -1) {
31 if ((errno == EINTR) || (errno == EAGAIN))
32 continue;
33 return total ? total : -1;
35 if (got == 0) {
36 if (pass++ >= 3)
37 return total;
38 continue;
40 pass = 0;
41 buf += got;
42 total += got;
43 count -= got;
45 return total;
48 int main(int argc, char **argv)
50 int fd, got, i;
51 int zflag = 0;
52 char buf[1024];
54 if (argc != 2) {
55 fprintf(stderr, "Usage: make-sparse out-file\n");
56 exit(1);
58 fd = open(argv[1], O_WRONLY|O_CREAT|O_TRUNC|O_LARGEFILE, 0777);
59 if (fd < 0) {
60 perror(argv[1]);
61 exit(1);
63 while (1) {
64 got = full_read(0, buf, sizeof(buf));
65 if (got == 0)
66 break;
67 if (got == sizeof(buf)) {
68 for (i=0; i < sizeof(buf); i++)
69 if (buf[i])
70 break;
71 if (i == sizeof(buf)) {
72 lseek(fd, sizeof(buf), SEEK_CUR);
73 zflag = 1;
74 continue;
77 zflag = 0;
78 write(fd, buf, got);
80 if (zflag) {
81 lseek(fd, -1, SEEK_CUR);
82 buf[0] = 0;
83 write(fd, buf, 1);
85 return 0;