=================================================================== RCS file: /cvsrepo/anoncvs/cvs/src/usr.bin/awk/lib.c,v retrieving revision 1.14 retrieving revision 1.15 diff -u -r1.14 -r1.15 --- src/usr.bin/awk/lib.c 2003/12/01 15:34:26 1.14 +++ src/usr.bin/awk/lib.c 2004/12/30 01:52:48 1.15 @@ -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 @@ 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 @@ } *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 @@ 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 @@ 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 @@ 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);