From: Joey Hess Date: Thu, 10 Apr 2008 18:11:21 +0000 (-0400) Subject: sponge, ifne: Ensure that suspending/resuming doesn't result in partial writes of... X-Git-Tag: 0.29~22 X-Git-Url: https://err.no/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=ddcbf562e3b6bb3a4441cc640c756127be4cf26b;p=moreutils sponge, ifne: Ensure that suspending/resuming doesn't result in partial writes of the data, by using fwrite() rather than write(). --- diff --git a/debian/changelog b/debian/changelog index e3424ed..ba8a707 100644 --- a/debian/changelog +++ b/debian/changelog @@ -1,8 +1,9 @@ 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(). + * sponge, ifne: 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/ifne.c b/ifne.c index 9af19fa..817b1c5 100644 --- a/ifne.c +++ b/ifne.c @@ -1,4 +1,3 @@ - /* * * Copyright 2008 Javier Merino @@ -30,6 +29,7 @@ int main(int argc, char **argv) { int child_status; pid_t child_pid; char buf[BUFSIZ]; + FILE *outf; if (argc < 2) { /* Noop */ @@ -68,11 +68,15 @@ int main(int argc, char **argv) { return EXIT_FAILURE; } - /* Parent: write in fds[1] our stdin */ + /* Parent: write stdin to fds[1] */ close(fds[0]); - + outf = fdopen(fds[1], "w"); + if (! outf) { + perror("fdopen"); + exit(1); + } do { - if (write(fds[1], buf, r*sizeof(char)) == -1) { + if (fwrite(buf, r*sizeof(char), 1, outf) < 1) { fprintf(stderr, "Write error to %s\n", argv[1]); exit(EXIT_FAILURE); } @@ -82,8 +86,8 @@ int main(int argc, char **argv) { perror("read"); exit(EXIT_FAILURE); } - - close(fds[1]); + fclose(outf); + if (waitpid(child_pid, &child_status, 0) != child_pid) { perror("waitpid"); return EXIT_FAILURE;