Re: cvs commit: src/usr.bin/rev rev.c

From: Alfred Perlstein <alfred_at_freebsd.org>
Date: Wed, 15 Dec 2004 03:46:01 -0800
* Suleiman Souhlal <ssouhlal_at_FreeBSD.org> [041214 22:24] wrote:
> ssouhlal    2004-12-15 06:24:57 UTC
> 
>   FreeBSD src repository
> 
>   Modified files:
>     usr.bin/rev          rev.c 
>   Log:
>   If ferror is true, we must reset the error indicator.

There's another possible bug here.

Note that the code seems to unconditionally emit an EOL marker even
if the last character of the line is not a newline.  If that is a bug
then here is a possible fix:

something like this may work: (untested)

Index: rev.c
===================================================================
RCS file: /home/ncvs/src/usr.bin/rev/rev.c,v
retrieving revision 1.8
diff -u -r1.8 rev.c
--- rev.c	15 Dec 2004 06:24:57 -0000	1.8
+++ rev.c	15 Dec 2004 11:44:48 -0000
_at__at_ -66,7 +66,7 _at__at_
 	wchar_t *p, *t;
 	FILE *fp;
 	size_t len;
-	int ch, rval;
+	int ch, rval, newline;
 
 	setlocale(LC_ALL, "");
 
_at__at_ -94,12 +94,17 _at__at_
 			filename = *argv++;
 		}
 		while ((p = fgetwln(fp, &len)) != NULL) {
-			if (p[len - 1] == '\n')
+			if (p[len - 1] == '\n') {
+				newline = 1;
 				--len;
+			} else {
+				newline = 0;
+			}
 			t = p + len - 1;
 			for (t = p + len - 1; t >= p; --t)
 				putwchar(*t);
-			putwchar('\n');
+			if (newline)
+				putwchar('\n');
 		}
 		if (ferror(fp)) {
 			warn("%s", filename);

-- 
- Alfred Perlstein
- Research Engineering Development Inc.
- email: bright_at_mu.org cell: 408-480-4684
Received on Wed Dec 15 2004 - 11:46:01 UTC