[PATCH] don't ignore write failures

Jim Meyering jim at meyering.net
Tue Oct 5 06:21:38 UTC 2010


Pete Zaitcev wrote:
> On Mon, 04 Oct 2010 22:50:55 +0200
> Jim Meyering <jim at meyering.net> wrote:
>
>>  		printf("my location is \"%s\"\n",me);
>> -		fflush(stdout);
>> +		if (!!ferror(stdout) + !!fflush(stdout))
>> +			error(EXIT_FAILURE, 0, "write failed");
>>  	}
>
> This is unnecesserily confusing. Worse, the order of evaluation
> for + is not specified.

You must be thinking of something else.
In C, "binary +" operands are specified to be evaluated left to right.

However, I realized that I don't need to check ferror before
fflush like I would have to before fclose, so can simplify it:

		if (fflush(stdout) || ferror(stdout))
			error(EXIT_FAILURE, 0, "write failed");

Thanks for pointing that out.
I've adjusted and will push the following:

> Do you want to know the error state of
> the stream before flush or after flush?

>From 6c2fe0153226238cdf342e13333cec65a2b769d0 Mon Sep 17 00:00:00 2001
From: Jim Meyering <meyering at redhat.com>
Date: Fri, 1 Oct 2010 16:01:20 +0200
Subject: [PATCH] don't ignore write failures

* proxy.c (junk_writer): Don't ignore fwrite failure.
Warn upon fwrite or fflush failure.
* rest.c (main): Don't ignore fflush failure.
Exit upon fflush or preceding printf failure.
---
 proxy.c |    5 ++++-
 rest.c  |    3 ++-
 2 files changed, 6 insertions(+), 2 deletions(-)

diff --git a/proxy.c b/proxy.c
index e89b3df..bfdde25 100644
--- a/proxy.c
+++ b/proxy.c
@@ -271,7 +271,10 @@ junk_writer (void *ptr, size_t size, size_t nmemb, void *stream)
 	size_t	n;

 	n = fwrite(ptr,size,nmemb,stream);
-	fflush(stream);
+	if (n != nmemb)
+		error(0, 0, "warning: write failed");
+	if (fflush(stream))
+		error(0, 0, "warning: write failed");
 	DPRINTF("in %s(%zu,%zu) => %zu\n",__func__,size,nmemb,n);

 	return n;
diff --git a/rest.c b/rest.c
index c808d1a..f501674 100644
--- a/rest.c
+++ b/rest.c
@@ -1793,7 +1793,8 @@ args_done:
 		printf("db is at %s:%u\n",db_host,db_port);
 		printf("will listen on port %u\n",my_port);
 		printf("my location is \"%s\"\n",me);
-		fflush(stdout);
+		if (fflush(stdout) || ferror(stdout))
+			error(EXIT_FAILURE, 0, "write failed");
 	}

 	backend_init();
--
1.7.3.1.45.g9855b


More information about the iwhd-devel mailing list