From owner-freebsd-hackers Fri Apr 20 1:15:39 2001 Delivered-To: freebsd-hackers@freebsd.org Received: from bazooka.unixfreak.org (bazooka.unixfreak.org [63.198.170.138]) by hub.freebsd.org (Postfix) with ESMTP id 989F337B424 for ; Fri, 20 Apr 2001 01:15:36 -0700 (PDT) (envelope-from dima@unixfreak.org) Received: from spike.unixfreak.org (spike [63.198.170.139]) by bazooka.unixfreak.org (Postfix) with ESMTP id CE1113E09 for ; Fri, 20 Apr 2001 01:15:35 -0700 (PDT) To: hackers@freebsd.org Subject: cut(1) and long lines Date: Fri, 20 Apr 2001 01:15:35 -0700 From: Dima Dorfman Message-Id: <20010420081535.CE1113E09@bazooka.unixfreak.org> Sender: owner-freebsd-hackers@FreeBSD.ORG Precedence: bulk X-Loop: FreeBSD.ORG 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