changeset 14066:dac943a98583

3820 /usr/bin/sed doesn't handle binary files. Reviewed by: Josef 'Jeff' Sipek <jeffpc@josefsipek.net> Reviewed by: Richard Lowe <richlowe@richlowe.net> Reviewed by: Marcel Telka <marcel@telka.sk>
author Johann 'Myrkraverk' Oskarsson <johann@myrkraverk.com>
date Fri, 21 Jun 2013 16:28:00 +0000
parents 72597f7a144f
children a847fb624ac7
files usr/src/cmd/sed/main.c
diffstat 1 files changed, 10 insertions(+), 47 deletions(-) [+]
line wrap: on
line diff
--- a/usr/src/cmd/sed/main.c	Fri Jun 21 14:56:44 2013 +0000
+++ b/usr/src/cmd/sed/main.c	Fri Jun 21 16:28:00 2013 +0000
@@ -1,4 +1,5 @@
 /*
+ * Copyright (c) 2013 Johann 'Myrkraverk' Oskarsson <johann@myrkraverk.com>
  * Copyright (c) 2011 Gary Mills
  * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
  * Copyright (c) 1992 Diomidis Spinellis.
@@ -114,7 +115,6 @@
 static void add_compunit(enum e_cut, char *);
 static void add_file(char *);
 static void usage(void);
-static char *getln(FILE *, size_t *);
 
 
 int
@@ -318,8 +318,9 @@
 mf_fgets(SPACE *sp, enum e_spflag spflag)
 {
 	struct stat sb, nsb;
-	size_t len;
-	char *p;
+	ssize_t len;
+	static char *p = NULL;
+	static size_t plen = 0;
 	int c;
 	static int firstfile;
 
@@ -454,13 +455,13 @@
 	 * We are here only when infile is open and we still have something
 	 * to read from it.
 	 *
-	 * Use fgetln so that we can handle essentially infinite input data.
-	 * Can't use the pointer into the stdio buffer as the process space
-	 * because the ungetc() can cause it to move.
+	 * Use getline() so that we can handle essentially infinite
+	 * input data.  The p and plen are static so each invocation gives
+	 * getline() the same buffer which is expanded as needed.
 	 */
-	p = getln(infile, &len);
-	if (ferror(infile))
-		errx(1, "%s: %s", fname, strerror(errno ? errno : EIO));
+	len = getline(&p, &plen, infile);
+	if (len == -1)
+		err(1, "%s", fname);
 	if (len != 0 && p[len - 1] == '\n')
 		len--;
 	cspace(sp, p, len, spflag);
@@ -515,41 +516,3 @@
 	(void) ungetc(ch, infile);
 	return (0);
 }
-
-char *
-getln(FILE *in, size_t *lenp)
-{
-	static char	*buffer = NULL;
-	static size_t	sz = 0;
-
-	size_t		len = 0;
-
-	for (;;) {
-		if (sz <= (len + 1)) {
-			char *nb;
-			if ((nb = realloc(buffer, sz + LINE_MAX)) == NULL) {
-				err(1, "realloc");
-			}
-			buffer = nb;
-			sz += LINE_MAX;
-		}
-
-		buffer[len] = 0;
-
-		if (fgets(buffer + len, sz - len, in) == NULL) {
-			/* END OF FILE */
-			*lenp = len;
-			break;
-		}
-
-		len += strlen(buffer + len);
-
-		if (buffer[len - 1] == '\n') {
-			/* got the new line */
-			*lenp = len;
-			break;
-		}
-	}
-
-	return (buffer);
-}