summaryrefslogtreecommitdiff
path: root/sponge.c
diff options
context:
space:
mode:
authorJoey Hess <joey@kodama.kitenet.net>2008-04-10 14:05:25 -0400
committerJoey Hess <joey@kodama.kitenet.net>2008-04-10 14:05:25 -0400
commit71349b5ae83878f4e59fc6517976998150856933 (patch)
tree16a3bf4ae9dfa933cfa1065a0373796d9e80543e /sponge.c
parent2fbf7a9636fae8012298901e98e63cc1b32745ee (diff)
downloadmoreutils-71349b5ae83878f4e59fc6517976998150856933.tar.gz
sponge: Ensure that suspending/resuming doesn't result in partial writes of the data, by using fwrite() rather than write().
Diffstat (limited to 'sponge.c')
-rw-r--r--sponge.c18
1 files changed, 8 insertions, 10 deletions
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);
}