Skip site navigation (1)Skip section navigation (2)
Date:      Fri, 20 Apr 2001 01:15:35 -0700
From:      Dima Dorfman <dima@unixfreak.org>
To:        hackers@freebsd.org
Subject:   cut(1) and long lines
Message-ID:  <20010420081535.CE1113E09@bazooka.unixfreak.org>

next in thread | raw e-mail | index | archive | help
Attached is a rather short patch to make cut(1) properly deal with
long lines.  Basically, it converts it to use fgetln() instead of
fgets().  An an aside, this also fixed the case where the last line
doesn't have a trailing newline.  Note that specifying a list over
2048 on the command line (e.g., the -f option) still isn't supported,
but at least cut(1) doesn't choke when you want the second token but
feed it a couple thousand characters in one line.

Comments?

Thanks,

					Dima Dorfman
					dima@unixfreak.org


Index: cut.c
===================================================================
RCS file: /st/src/FreeBSD/src/usr.bin/cut/cut.c,v
retrieving revision 1.12
diff -u -r1.12 cut.c
--- cut.c	2001/02/06 20:03:48	1.12
+++ cut.c	2001/04/20 07:58:59
@@ -228,19 +228,22 @@
 	int ch, field, isdelim;
 	char *pos, *p, sep;
 	int output;
-	char lbuf[_POSIX2_LINE_MAX + 1];
+	char *lbuf;
+	size_t lbuflen;
 
-	for (sep = dchar; fgets(lbuf, sizeof(lbuf), fp);) {
+	for (sep = dchar; (lbuf = fgetln(fp, &lbuflen)) != NULL;) {
+		/* Assert EOL has a newline. */
+		if (*(lbuf + lbuflen - 1) != '\n')
+			*(lbuf + lbuflen) = '\n';
 		output = 0;
-		for (isdelim = 0, p = lbuf;; ++p) {
-			if (!(ch = *p))
-				errx(1, "%s: line too long.", fname);
+		for (isdelim = 0, p = lbuf; p < lbuf + lbuflen; ++p) {
+			ch = *p;
 			/* this should work if newline is delimiter */
 			if (ch == sep)
 				isdelim = 1;
 			if (ch == '\n') {
 				if (!isdelim && !sflag)
-					(void)printf("%s", lbuf);
+					(void)fwrite(lbuf, lbuflen, 1, stdout);
 				break;
 			}
 		}

To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-hackers" in the body of the message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20010420081535.CE1113E09>