[PATCH 1/3] text-tailf: clean up gcc warnings & fix use of errno

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



From: Randy Dunlap <randy.dunlap@xxxxxxxxxx>

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 remember the errno and print the message by using
strerror() instead.

Builds cleanly on x86_32 and x86_64.

Signed-off-by: Randy Dunlap <randy.dunlap@xxxxxxxxxx>
---
 text-utils/tailf.c |   22 +++++++++++++++++-----
 1 file changed, 17 insertions(+), 5 deletions(-)

--- util-linux-ng-2.13.orig/text-utils/tailf.c
+++ util-linux-ng-2.13/text-utils/tailf.c
@@ -30,6 +30,8 @@
 #include <stdlib.h>
 #include <unistd.h>
 #include <malloc.h>
+#include <errno.h>
+#include <string.h>
 #include <sys/stat.h>
 #include "nls.h"
 
@@ -50,8 +52,9 @@ static void tailf(const char *filename, 
     int  i;
 
     if (!(str = fopen(filename, "r"))) {
+	int err = errno;
 	fprintf(stderr, _("Cannot open \"%s\" for read\n"), filename);
-	perror("");
+	fprintf(stderr, _("fopen returned %d: %s\n"), err, strerror(err));
 	exit(1);
     }
 
@@ -83,7 +86,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 +105,22 @@ int main(int argc, char **argv)
 	nsize = filesize(filename);
 	if (nsize != osize) {
 	    if (!(str = fopen(filename, "r"))) {
+		int err = errno;
 		fprintf(stderr, _("Cannot open \"%s\" for read\n"), filename);
-		perror(argv[0]);
+		fprintf(stderr, _("%s: fopen returned %d: %s\n"),
+			argv[0], err, strerror(err));
 		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\"\n"),
+				filename);
+			fprintf (stderr, _("%s: fwrite returned %d, expected %d\n"),
+				argv[0], wcount, count);
+                    }
+		}
 	    fflush(stdout);
 	    fclose(str);
 	    osize = nsize;
-
To unsubscribe from this list: send the line "unsubscribe util-linux-ng" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html

[Index of Archives]     [Netdev]     [Ethernet Bridging]     [Linux Wireless]     [Kernel Newbies]     [Security]     [Linux for Hams]     [Netfilter]     [Bugtraq]     [Yosemite News]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux Admin]     [Samba]

  Powered by Linux