From 642035150ec8fcb76244cf8475c5efdc7541760d Mon Sep 17 00:00:00 2001 From: Karel Zak Date: Wed, 3 Oct 2007 14:15:03 -0700 Subject: [PATCH] tailf: clean up gcc warnings & fix use of errno Fix strict gcc warnings in tailf that come from using: ("-Wall -Wp,-D_FORTIFY_SOURCE=2") tailf.c:111: warning: ignoring return value of 'fwrite', declared with attribute warn_unused_result Also, tailf uses perror() for error reporting, but it inserts an fprintf call first, so perror() is actually reporting the result of the fprintf() call, not the failing call; change the code to print the message by using strerror() instead. Builds cleanly on x86_32 and x86_64. Signed-off-by: Randy Dunlap Signed-off-by: Karel Zak --- text-utils/tailf.c | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/text-utils/tailf.c b/text-utils/tailf.c index e10243f9..5b1d1a4e 100644 --- a/text-utils/tailf.c +++ b/text-utils/tailf.c @@ -30,6 +30,8 @@ #include #include #include +#include +#include #include #include "nls.h" @@ -50,8 +52,8 @@ static void tailf(const char *filename, int lines) int i; if (!(str = fopen(filename, "r"))) { - fprintf(stderr, _("Cannot open \"%s\" for read\n"), filename); - perror(""); + fprintf(stderr, _("Cannot open \"%s\" for read: %s\n"), + filename, strerror(errno)); exit(1); } @@ -83,7 +85,7 @@ int main(int argc, char **argv) size_t osize, nsize; FILE *str; const char *filename; - int count; + int count, wcount; setlocale(LC_ALL, ""); bindtextdomain(PACKAGE, LOCALEDIR); @@ -102,13 +104,17 @@ int main(int argc, char **argv) nsize = filesize(filename); if (nsize != osize) { if (!(str = fopen(filename, "r"))) { - fprintf(stderr, _("Cannot open \"%s\" for read\n"), filename); - perror(argv[0]); + fprintf(stderr, _("Cannot open \"%s\" for read: %s\n"), + filename, strerror(errno)); exit(1); } if (!fseek(str, osize, SEEK_SET)) - while ((count = fread(buffer, 1, sizeof(buffer), str)) > 0) - fwrite(buffer, 1, count, stdout); + while ((count = fread(buffer, 1, sizeof(buffer), str)) > 0) { + wcount = fwrite(buffer, 1, count, stdout); + if (wcount != count) + fprintf (stderr, _("Incomplete write to \"%s\" (written %d, expected %d)\n"), + filename, wcount, count); + } fflush(stdout); fclose(str); osize = nsize; -- 2.39.5