diff options
author | Joey Hess <joey@kodama.kitenet.net> | 2008-04-10 14:11:21 -0400 |
---|---|---|
committer | Joey Hess <joey@kodama.kitenet.net> | 2008-04-10 14:11:21 -0400 |
commit | ddcbf562e3b6bb3a4441cc640c756127be4cf26b (patch) | |
tree | a13e693d1c005cc04c9ea4619437ee7b365d6804 | |
parent | 71349b5ae83878f4e59fc6517976998150856933 (diff) | |
download | moreutils-ddcbf562e3b6bb3a4441cc640c756127be4cf26b.tar.gz |
sponge, ifne: Ensure that suspending/resuming doesn't result in partial writes of the data, by using fwrite() rather than write().
-rw-r--r-- | debian/changelog | 5 | ||||
-rw-r--r-- | ifne.c | 16 |
2 files changed, 13 insertions, 8 deletions
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 <joeyh@debian.org> Thu, 20 Mar 2008 12:56:42 -0400 @@ -1,4 +1,3 @@ - /* * * Copyright 2008 Javier Merino <cibervicho@gmail.com> @@ -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; |