Whamcloud - gitweb
Add make-sparse.c to contrib
[tools/e2fsprogs.git] / contrib / make-sparse.c
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
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
48 int main(int argc, char **argv)
49 {
50         int fd, got, i;
51         char buf[1024];
52
53         if (argc != 2) {
54                 fprintf(stderr, "Usage: make-sparse out-file\n");
55                 exit(1);
56         }
57         fd = open(argv[1], O_WRONLY|O_CREAT|O_TRUNC|O_LARGEFILE, 0777);
58         if (fd < 0) {
59                 perror(argv[1]);
60                 exit(1);
61         }
62         while (1) {
63                 got = full_read(0, buf, sizeof(buf));
64                 if (got == 0)
65                         break;
66                 if (got == sizeof(buf)) {
67                         for (i=0; i < sizeof(buf); i++) 
68                                 if (buf[i])
69                                         break;
70                         if (i == sizeof(buf)) {
71                                 lseek(fd, sizeof(buf), SEEK_CUR);
72                                 continue;
73                         }
74                 }
75                 write(fd, buf, got);
76         }
77         return 0;
78 }
79