diff options
Diffstat (limited to 'contrib/one-true-awk/lib.c')
-rw-r--r-- | contrib/one-true-awk/lib.c | 31 |
1 files changed, 19 insertions, 12 deletions
diff --git a/contrib/one-true-awk/lib.c b/contrib/one-true-awk/lib.c index 9e64247..c260a05 100644 --- a/contrib/one-true-awk/lib.c +++ b/contrib/one-true-awk/lib.c @@ -57,13 +57,11 @@ static Cell dollar1 = { OCELL, CFLD, NULL, "", 0.0, FLD|STR|DONTFREE }; void recinit(unsigned int n) { - record = (char *) malloc(n); - fields = (char *) malloc(n); - fldtab = (Cell **) malloc((nfields+1) * sizeof(Cell *)); - if (record == NULL || fields == NULL || fldtab == NULL) + if ( (record = (char *) malloc(n)) == NULL + || (fields = (char *) malloc(n)) == NULL + || (fldtab = (Cell **) malloc((nfields+1) * sizeof(Cell *))) == NULL + || (fldtab[0] = (Cell *) malloc(sizeof(Cell))) == NULL ) FATAL("out of space for $0 and fields"); - - fldtab[0] = (Cell *) malloc(sizeof (Cell)); *fldtab[0] = dollar0; fldtab[0]->sval = record; fldtab[0]->nval = tostring("0"); @@ -101,12 +99,14 @@ 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; + uschar saveb0; + int bufsize = *pbufsize, savebufsize = bufsize; if (firsttime) { firsttime = 0; @@ -118,6 +118,7 @@ int getrec(char **pbuf, int *pbufsize, int isrecord) /* get next input record */ donefld = 0; donerec = 1; } + saveb0 = buf[0]; buf[0] = 0; while (argno < *ARGC || infile == stdin) { dprintf( ("argno=%d, file=|%s|\n", argno, file) ); @@ -164,8 +165,9 @@ int getrec(char **pbuf, int *pbufsize, int isrecord) /* get next input record */ infile = NULL; argno++; } + buf[0] = saveb0; *pbuf = buf; - *pbufsize = bufsize; + *pbufsize = savebufsize; return 0; /* true end of file */ } @@ -378,7 +380,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]); @@ -387,10 +389,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); @@ -484,7 +491,7 @@ int errorflag = 0; void yyerror(const char *s) { - SYNTAX(s); + SYNTAX("%s", s); } void SYNTAX(const char *fmt, ...) |