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.c31
1 files changed, 21 insertions, 10 deletions
diff --git a/usr.bin/awk/lib.c b/usr.bin/awk/lib.c
index ee3e187aa77..f00a5ec856b 100644
--- a/usr.bin/awk/lib.c
+++ b/usr.bin/awk/lib.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: lib.c,v 1.32 2020/06/10 21:03:56 millert Exp $ */
+/* $OpenBSD: lib.c,v 1.33 2020/06/10 21:05:02 millert Exp $ */
/****************************************************************
Copyright (C) Lucent Technologies 1997
All Rights Reserved
@@ -31,18 +31,21 @@ THIS SOFTWARE.
#include <stdlib.h>
#include <unistd.h>
#include <stdarg.h>
+#include <limits.h>
#include "awk.h"
#include "ytab.h"
+char EMPTY[] = { '\0' };
FILE *infile = NULL;
-char *file = "";
+char *file = EMPTY;
char *record;
int recsize = RECSIZE;
char *fields;
int fieldssize = RECSIZE;
Cell **fldtab; /* pointers to Cells */
-char inputFS[100] = " ";
+static size_t len_inputFS = 0;
+static char *inputFS = NULL; /* FS at time of input, for field splitting */
#define MAXFLD 2
int nfields = MAXFLD; /* last allocated slot for $i */
@@ -54,8 +57,8 @@ int lastfld = 0; /* last used field */
int argno = 1; /* current input argument number */
extern Awkfloat *ARGC;
-static Cell dollar0 = { OCELL, CFLD, NULL, "", 0.0, REC|STR|DONTFREE };
-static Cell dollar1 = { OCELL, CFLD, NULL, "", 0.0, FLD|STR|DONTFREE };
+static Cell dollar0 = { OCELL, CFLD, NULL, EMPTY, 0.0, REC|STR|DONTFREE };
+static Cell dollar1 = { OCELL, CFLD, NULL, EMPTY, 0.0, FLD|STR|DONTFREE };
void recinit(unsigned int n)
{
@@ -118,9 +121,17 @@ void initgetrec(void)
*/
void savefs(void)
{
- if (strlen(getsval(fsloc)) >= sizeof (inputFS))
+ size_t len;
+ if ((len = strlen(getsval(fsloc))) < len_inputFS) {
+ strlcpy(inputFS, *FS, sizeof(inputFS)); /* for subsequent field splitting */
+ return;
+ }
+
+ len_inputFS = len + 1;
+ inputFS = realloc(inputFS, len_inputFS);
+ if (inputFS == NULL)
FATAL("field separator %.10s... is too long", *FS);
- strlcpy(inputFS, *FS, sizeof(inputFS));
+ memcpy(inputFS, *FS, len_inputFS);
}
static bool firsttime = true;
@@ -335,14 +346,14 @@ void fldbld(void) /* create fields from current record */
*fr = 0;
} else if ((sep = *inputFS) == 0) { /* new: FS="" => 1 char/field */
for (i = 0; *r != '\0'; r += n) {
- char buf[MB_CUR_MAX + 1];
+ char buf[MB_LEN_MAX + 1];
i++;
if (i > nfields)
growfldtab(i);
if (freeable(fldtab[i]))
xfree(fldtab[i]->sval);
- n = mblen(r, MB_CUR_MAX);
+ n = mblen(r, MB_LEN_MAX);
if (n < 0)
n = 1;
memcpy(buf, r, n);
@@ -406,7 +417,7 @@ void cleanfld(int n1, int n2) /* clean out fields n1 .. n2 inclusive */
p = fldtab[i];
if (freeable(p))
xfree(p->sval);
- p->sval = "";
+ p->sval = EMPTY,
p->tval = FLD | STR | DONTFREE;
}
}