summaryrefslogtreecommitdiffstats
path: root/contrib/make-sparse.c
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-19 09:25:10 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-19 09:25:10 +0000
commit5dced3d1b3deca80e01415a2e35dc7972dcbfae7 (patch)
tree6a403684e0978f0287d7f0ec0e5aab1fd31a59e1 /contrib/make-sparse.c
parentInitial commit. (diff)
downloade2fsprogs-5dced3d1b3deca80e01415a2e35dc7972dcbfae7.tar.xz
e2fsprogs-5dced3d1b3deca80e01415a2e35dc7972dcbfae7.zip
Adding upstream version 1.47.0.upstream/1.47.0
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to '')
-rw-r--r--contrib/make-sparse.c91
1 files changed, 91 insertions, 0 deletions
diff --git a/contrib/make-sparse.c b/contrib/make-sparse.c
new file mode 100644
index 0000000..98b86b0
--- /dev/null
+++ b/contrib/make-sparse.c
@@ -0,0 +1,91 @@
+/*
+ * make-sparse.c --- make a sparse file from stdin
+ *
+ * Copyright 2004 by Theodore Ts'o.
+ *
+ * %Begin-Header%
+ * This file may be redistributed under the terms of the GNU Public
+ * License.
+ * %End-Header%
+ */
+
+#ifndef _LARGEFILE_SOURCE
+#define _LARGEFILE_SOURCE
+#endif
+#ifndef _LARGEFILE64_SOURCE
+#define _LARGEFILE64_SOURCE
+#endif
+
+#include <stdio.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <errno.h>
+
+int full_read(int fd, char *buf, size_t count)
+{
+ int got, total = 0;
+ int pass = 0;
+
+ while (count > 0) {
+ got = read(fd, buf, count);
+ if (got == -1) {
+ if ((errno == EINTR) || (errno == EAGAIN))
+ continue;
+ return total ? total : -1;
+ }
+ if (got == 0) {
+ if (pass++ >= 3)
+ return total;
+ continue;
+ }
+ pass = 0;
+ buf += got;
+ total += got;
+ count -= got;
+ }
+ return total;
+}
+
+int main(int argc, char **argv)
+{
+ int fd, got, i;
+ int zflag = 0;
+ char buf[1024];
+
+ if (argc != 2) {
+ fprintf(stderr, "Usage: make-sparse out-file\n");
+ exit(1);
+ }
+ fd = open(argv[1], O_WRONLY|O_CREAT|O_TRUNC|O_LARGEFILE, 0777);
+ if (fd < 0) {
+ perror(argv[1]);
+ exit(1);
+ }
+ while (1) {
+ got = full_read(0, buf, sizeof(buf));
+ if (got == 0)
+ break;
+ if (got == sizeof(buf)) {
+ for (i=0; i < sizeof(buf); i++)
+ if (buf[i])
+ break;
+ if (i == sizeof(buf)) {
+ lseek(fd, sizeof(buf), SEEK_CUR);
+ zflag = 1;
+ continue;
+ }
+ }
+ zflag = 0;
+ write(fd, buf, got);
+ }
+ if (zflag) {
+ lseek(fd, -1, SEEK_CUR);
+ buf[0] = 0;
+ write(fd, buf, 1);
+ }
+ return 0;
+}
+