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

Alfred Perlstein alfred at freebsd.org
Wed Dec 15 03:46:01 PST 2004


* 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
@@ -66,7 +66,7 @@
 	wchar_t *p, *t;
 	FILE *fp;
 	size_t len;
-	int ch, rval;
+	int ch, rval, newline;
 
 	setlocale(LC_ALL, "");
 
@@ -94,12 +94,17 @@
 			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


More information about the cvs-src mailing list