From 71349b5ae83878f4e59fc6517976998150856933 Mon Sep 17 00:00:00 2001 From: Joey Hess Date: Thu, 10 Apr 2008 14:05:25 -0400 Subject: [PATCH] sponge: Ensure that suspending/resuming doesn't result in partial writes of the data, by using fwrite() rather than write(). --- debian/changelog | 2 ++ sponge.c | 18 ++++++++---------- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/debian/changelog b/debian/changelog index 6e74f85..e3424ed 100644 --- a/debian/changelog +++ b/debian/changelog @@ -1,6 +1,8 @@ moreutils (0.29) UNRELEASED; urgency=low * Add ifne, contributed by Javier Merino. + * sponge: Ensure that suspending/resuming doesn't result in partial writes + of the data, by using fwrite() rather than write(). -- Joey Hess Thu, 20 Mar 2008 12:56:42 -0400 diff --git a/sponge.c b/sponge.c index a7914ea..3fd3ab4 100644 --- a/sponge.c +++ b/sponge.c @@ -39,7 +39,7 @@ int main(int argc, char **argv) { size_t bufsize = 8192; size_t bufused = 0; ssize_t i = 0; - int outfd; + FILE *outf; if (argc > 2 || (argc == 2 && strcmp(argv[1], "-h") == 0)) { usage(); @@ -69,25 +69,23 @@ int main(int argc, char **argv) { } if (argc == 2) { - outfd = open(argv[1], O_CREAT | O_TRUNC | O_WRONLY, 0666); - if (outfd == -1) { + outf = fopen(argv[1], "w"); + if (! outf) { fprintf(stderr, "Can't open %s: %s\n", argv[1], strerror(errno)); exit(1); } } else { - outfd = 1; + outf = stdout; } - i = write(outfd, bufstart, bufused); - if (i == -1) { - perror("write"); + if (fwrite(bufstart, bufused, 1, outf) < 1) { + perror("fwrite"); exit(1); } - i = close(outfd); - if (i == -1) { - perror("close"); + if (fclose(outf) != 0) { + perror("fclose"); exit(1); } -- 2.39.5