1 /*
2 * make-sparse.c --- make a sparse file from stdin
3 *
4 * Copyright 2004 by Theodore Ts'o.
5 *
6 * %Begin-Header%
7 * This file may be redistributed under the terms of the GNU Public
8 * License.
9 * %End-Header%
10 */
11
12 #define _LARGEFILE_SOURCE
13 #define _LARGEFILE64_SOURCE
14
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>
22
full_read(int fd,char * buf,size_t count)23 int full_read(int fd, char *buf, size_t count)
24 {
25 int got, total = 0;
26 int pass = 0;
27
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;
34 }
35 if (got == 0) {
36 if (pass++ >= 3)
37 return total;
38 continue;
39 }
40 pass = 0;
41 buf += got;
42 total += got;
43 count -= got;
44 }
45 return total;
46 }
47
main(int argc,char ** argv)48 int main(int argc, char **argv)
49 {
50 int fd, got, i;
51 int zflag = 0;
52 char buf[1024];
53
54 if (argc != 2) {
55 fprintf(stderr, "Usage: make-sparse out-file\n");
56 exit(1);
57 }
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);
62 }
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;
75 }
76 }
77 zflag = 0;
78 write(fd, buf, got);
79 }
80 if (zflag) {
81 lseek(fd, -1, SEEK_CUR);
82 buf[0] = 0;
83 write(fd, buf, 1);
84 }
85 return 0;
86 }
87
88