summaryrefslogtreecommitdiff
path: root/usr.bin/awk/lib.c
diff options
context:
space:
mode:
Diffstat (limited to 'usr.bin/awk/lib.c')
-rw-r--r--usr.bin/awk/lib.c23
1 files changed, 18 insertions, 5 deletions
diff --git a/usr.bin/awk/lib.c b/usr.bin/awk/lib.c
index 51a1845c58c..8b969b78f48 100644
--- a/usr.bin/awk/lib.c
+++ b/usr.bin/awk/lib.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: lib.c,v 1.14 2003/12/01 15:34:26 grange Exp $ */
+/* $OpenBSD: lib.c,v 1.15 2004/12/30 01:52:48 millert Exp $ */
/****************************************************************
Copyright (C) Lucent Technologies 1997
All Rights Reserved
@@ -105,10 +105,11 @@ void initgetrec(void)
infile = stdin; /* no filenames, so use stdin */
}
+static int firsttime = 1;
+
int getrec(char **pbuf, int *pbufsize, int isrecord) /* get next input record */
{ /* note: cares whether buf == record */
int c;
- static int firsttime = 1;
char *buf = *pbuf;
int bufsize = *pbufsize;
@@ -312,6 +313,13 @@ void fldbld(void) /* create fields from current record */
}
*fr = 0;
} else if (*r != 0) { /* if 0, it's a null field */
+ /* subtlecase : if length(FS) == 1 && length(RS > 0)
+ * \n is NOT a field separator (cf awk book 61,84).
+ * this variable is tested in the inner while loop.
+ */
+ int rtest = '\n'; /* normal case */
+ if (strlen(*RS) > 0)
+ rtest = '\0';
for (;;) {
i++;
if (i > nfields)
@@ -320,7 +328,7 @@ void fldbld(void) /* create fields from current record */
xfree(fldtab[i]->sval);
fldtab[i]->sval = fr;
fldtab[i]->tval = FLD | STR | DONTFREE;
- while (*r != sep && *r != '\n' && *r != '\0') /* \n is always a separator */
+ while (*r != sep && *r != rtest && *r != '\0') /* \n is always a separator */
*fr++ = *r++;
*fr++ = 0;
if (*r++ == 0)
@@ -375,7 +383,7 @@ void newfld(int n) /* add field n after end of existing lastfld */
Cell *fieldadr(int n) /* get nth field */
{
if (n < 0)
- FATAL("trying to access field %d", n);
+ FATAL("trying to access out of range field %d", n);
if (n > nfields) /* fields after NF are empty */
growfldtab(n); /* but does not increase NF */
return(fldtab[n]);
@@ -384,10 +392,15 @@ Cell *fieldadr(int n) /* get nth field */
void growfldtab(int n) /* make new fields up to at least $n */
{
int nf = 2 * nfields;
+ size_t s;
if (n > nf)
nf = n;
- fldtab = (Cell **) realloc(fldtab, (nf+1) * (sizeof (struct Cell *)));
+ s = (nf+1) * (sizeof (struct Cell *)); /* freebsd: how much do we need? */
+ if (s / sizeof(struct Cell *) - 1 == nf) /* didn't overflow */
+ fldtab = (Cell **) realloc(fldtab, s);
+ else /* overflow sizeof int */
+ xfree(fldtab); /* make it null */
if (fldtab == NULL)
FATAL("out of space creating %d fields", nf);
makefields(nfields+1, nf);