Source-Changes-HG archive
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]
[src/trunk]: src/usr.bin/comm Don't include newlines when comparing to preven...
details: https://anonhg.NetBSD.org/src/rev/edc989b6b46d
branches: trunk
changeset: 749372:edc989b6b46d
user: darcy <darcy%NetBSD.org@localhost>
date: Sat Nov 28 03:56:38 2009 +0000
description:
Don't include newlines when comparing to prevent errors when lines have
characters that sort lower such as tabs.
This is a temporary fix to allow pullups to existing, supported versions
of NetBSD. I will follow up with a version for current using the new
getline function.
diffstat:
usr.bin/comm/comm.c | 38 ++++++++++++++++++++++++++++++--------
1 files changed, 30 insertions(+), 8 deletions(-)
diffs (97 lines):
diff -r 2114159a5969 -r edc989b6b46d usr.bin/comm/comm.c
--- a/usr.bin/comm/comm.c Sat Nov 28 03:10:09 2009 +0000
+++ b/usr.bin/comm/comm.c Sat Nov 28 03:56:38 2009 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: comm.c,v 1.17 2009/04/11 12:18:45 lukem Exp $ */
+/* $NetBSD: comm.c,v 1.18 2009/11/28 03:56:38 darcy 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.17 2009/04/11 12:18:45 lukem Exp $");
+__RCSID("$NetBSD: comm.c,v 1.18 2009/11/28 03:56:38 darcy Exp $");
#endif /* not lint */
#include <err.h>
@@ -60,6 +60,7 @@
FILE *file(const char *);
void show(FILE *, const 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, const 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;
+}
+
Home |
Main Index |
Thread Index |
Old Index