diff options
author | Todd C. Miller <millert@cvs.openbsd.org> | 2020-06-10 21:01:33 +0000 |
---|---|---|
committer | Todd C. Miller <millert@cvs.openbsd.org> | 2020-06-10 21:01:33 +0000 |
commit | d4a9f9a2c15918d627426cc99b76e43a7c447eca (patch) | |
tree | 84c6a0bce5c22985ad844a069a48f84064c271f4 /usr.bin/awk/lib.c | |
parent | be327fab72911917be9697b93d39784ee7bf571a (diff) |
Update awk to Jan 25, 2019 version.
Diffstat (limited to 'usr.bin/awk/lib.c')
-rw-r--r-- | usr.bin/awk/lib.c | 20 |
1 files changed, 13 insertions, 7 deletions
diff --git a/usr.bin/awk/lib.c b/usr.bin/awk/lib.c index bd51ca41b62..63918c1fb99 100644 --- a/usr.bin/awk/lib.c +++ b/usr.bin/awk/lib.c @@ -1,4 +1,4 @@ -/* $OpenBSD: lib.c,v 1.26 2020/06/10 21:00:01 millert Exp $ */ +/* $OpenBSD: lib.c,v 1.27 2020/06/10 21:01:32 millert Exp $ */ /**************************************************************** Copyright (C) Lucent Technologies 1997 All Rights Reserved @@ -61,7 +61,7 @@ void recinit(unsigned int n) { if ( (record = (char *) malloc(n)) == NULL || (fields = (char *) malloc(n+1)) == NULL - || (fldtab = (Cell **) calloc(nfields+1, sizeof(Cell *))) == NULL + || (fldtab = (Cell **) calloc(nfields+2, sizeof(Cell *))) == NULL || (fldtab[0] = (Cell *) malloc(sizeof(Cell))) == NULL ) FATAL("out of space for $0 and fields"); *record = '\0'; @@ -192,12 +192,13 @@ int readrec(char **pbuf, int *pbufsize, FILE *inf) /* read one record into buf * int sep, c; char *rr, *buf = *pbuf; int bufsize = *pbufsize; + char *rs = getsval(rsloc); - if (strlen(*FS) >= sizeof(inputFS)) + if (strlen(getsval(fsloc)) >= sizeof (inputFS)) FATAL("field separator %.10s... is too long", *FS); /*fflush(stdout); avoids some buffering problem but makes it 25% slower*/ strlcpy(inputFS, *FS, sizeof inputFS); /* for subsequent field splitting */ - if ((sep = **RS) == 0) { + if ((sep = *rs) == 0) { sep = '\n'; while ((c=getc(inf)) == '\n' && c != EOF) /* skip leading \n's */ ; @@ -211,7 +212,7 @@ int readrec(char **pbuf, int *pbufsize, FILE *inf) /* read one record into buf * FATAL("input record `%.30s...' too long", buf); *rr++ = c; } - if (**RS == sep || c == EOF) + if (*rs == sep || c == EOF) break; if ((c = getc(inf)) == '\n' || c == EOF) /* 2 in a row */ break; @@ -286,6 +287,8 @@ void fldbld(void) /* create fields from current record */ } fr = fields; i = 0; /* number of fields accumulated here */ + if (strlen(getsval(fsloc)) >= sizeof (inputFS)) + FATAL("field separator %.10s... is too long", *FS); strlcpy(inputFS, *FS, sizeof(inputFS)); if (strlen(inputFS) > 1) { /* it's a regular expression */ i = refldbld(r, inputFS); @@ -393,6 +396,8 @@ void newfld(int n) /* add field n after end of existing lastfld */ void setlastfld(int n) /* set lastfld cleaning fldtab cells if necessary */ { + if (n < 0) + FATAL("cannot set NF to a negative value"); if (n > nfields) growfldtab(n); @@ -482,6 +487,7 @@ void recbld(void) /* create $0 from $1..$NF if necessary */ { int i; char *r, *p; + char *sep = getsval(ofsloc); if (donerec == 1) return; @@ -493,9 +499,9 @@ void recbld(void) /* create $0 from $1..$NF if necessary */ while ((*r = *p++) != 0) r++; if (i < *NF) { - if (!adjbuf(&record, &recsize, 2+strlen(*OFS)+r-record, recsize, &r, "recbld 2")) + if (!adjbuf(&record, &recsize, 2+strlen(sep)+r-record, recsize, &r, "recbld 2")) FATAL("created $0 `%.30s...' too long", record); - for (p = *OFS; (*r = *p++) != 0; ) + for (p = sep; (*r = *p++) != 0; ) r++; } } |