summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohann 'Myrkraverk' Oskarsson <johann@myrkraverk.com>2013-06-21 16:28:00 +0000
committerRobert Mustacchi <rm@joyent.com>2013-06-24 15:11:26 -0700
commit9384cec630155c229c70dfb8a445c6ccf433045a (patch)
tree5e5f8084783e921b68852707e82d927f1615a2b4
parentdeeb0f36019670ade13450cb986b946c2e4002e6 (diff)
downloadillumos-joyent-9384cec630155c229c70dfb8a445c6ccf433045a.tar.gz
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>
-rw-r--r--usr/src/cmd/sed/main.c57
1 files changed, 10 insertions, 47 deletions
diff --git a/usr/src/cmd/sed/main.c b/usr/src/cmd/sed/main.c
index 93e14bb09b..204583b877 100644
--- a/usr/src/cmd/sed/main.c
+++ b/usr/src/cmd/sed/main.c
@@ -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 const struct option lopts[] = {
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 @@ int
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 @@ mf_fgets(SPACE *sp, enum e_spflag spflag)
* 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 @@ lastline(void)
(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);
-}