Module Name:    src
Committed By:   riz
Date:           Mon Nov 22 03:08:37 UTC 2010

Modified Files:
        src/usr.bin/comm [netbsd-5]: comm.c

Log Message:
Pull up following revision(s) (requested by darcy in ticket #1172):
        usr.bin/comm/comm.c: revision 1.18
Don't include newlines when comparing to prevent errors when lines have
characters that sort lower such as tabs.


To generate a diff of this commit:
cvs rdiff -u -r1.16 -r1.16.4.1 src/usr.bin/comm/comm.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/usr.bin/comm/comm.c
diff -u src/usr.bin/comm/comm.c:1.16 src/usr.bin/comm/comm.c:1.16.4.1
--- src/usr.bin/comm/comm.c:1.16	Mon Jul 21 14:19:22 2008
+++ src/usr.bin/comm/comm.c	Mon Nov 22 03:08:36 2010
@@ -1,4 +1,4 @@
-/*	$NetBSD: comm.c,v 1.16 2008/07/21 14:19:22 lukem Exp $	*/
+/*	$NetBSD: comm.c,v 1.16.4.1 2010/11/22 03:08:36 riz Exp $	*/
 
 /*
  * Copyright (c) 1989, 1993, 1994
@@ -42,7 +42,7 @@
 #if 0
 static char sccsid[] = "@(#)comm.c	8.4 (Berkeley) 5/4/95";
 #endif
-__RCSID("$NetBSD: comm.c,v 1.16 2008/07/21 14:19:22 lukem Exp $");
+__RCSID("$NetBSD: comm.c,v 1.16.4.1 2010/11/22 03:08:36 riz Exp $");
 #endif /* not lint */
 
 #include <err.h>
@@ -60,6 +60,7 @@
 FILE   *file(const char *);
 void	show(FILE *, char *, char *);
 void	usage(void);
+char   *getnextln(char *buf, FILE *);
 
 int
 main(int argc, char **argv)
@@ -116,9 +117,9 @@
 	for (read1 = read2 = 1;;) {
 		/* read next line, check for EOF */
 		if (read1)
-			file1done = !fgets(line1, MAXLINELEN, fp1);
+			file1done = !getnextln(line1, fp1);
 		if (read2)
-			file2done = !fgets(line2, MAXLINELEN, fp2);
+			file2done = !getnextln(line2, fp2);
 
 		/* if one file done, display the rest of the other file */
 		if (file1done) {
@@ -136,7 +137,7 @@
 		if (!(comp = compare(line1, line2))) {
 			read1 = read2 = 1;
 			if (col3)
-				if (printf("%s%s", col3, line1) < 0)
+				if (printf("%s%s\n", col3, line1) < 0)
 					break;
 			continue;
 		}
@@ -146,13 +147,13 @@
 			read1 = 1;
 			read2 = 0;
 			if (col1)
-				if (printf("%s%s", col1, line1) < 0)
+				if (printf("%s%s\n", col1, line1) < 0)
 					break;
 		} else {
 			read1 = 0;
 			read2 = 1;
 			if (col2)
-				if (printf("%s%s", col2, line2) < 0)
+				if (printf("%s%s\n", col2, line2) < 0)
 					break;
 		}
 	}
@@ -166,7 +167,7 @@
 void
 show(FILE *fp, char *offset, char *buf)
 {
-	while (printf("%s%s", offset, buf) >= 0 && fgets(buf, MAXLINELEN, fp))
+	while (printf("%s%s\n", offset, buf) >= 0 && getnextln(buf, fp))
 		;
 }
 
@@ -189,3 +190,24 @@
 	(void)fprintf(stderr, "usage: comm [-123f] file1 file2\n");
 	exit(1);
 }
+
+char *
+getnextln(char *buf, FILE *fp)
+{
+	size_t i = 0;
+	int c;
+
+	while ((c = fgetc(fp)) != '\n' && c != EOF) {
+		buf[i++] = c;
+
+		if (i >= MAXLINELEN)
+			i--; /* consumes extra characters till newline */
+	}
+
+	if (c == EOF && !i)
+		return NULL;
+
+	buf[i] = 0;
+	return buf;
+}
+

Reply via email to