diff options
Diffstat (limited to 'editors')
-rw-r--r-- | editors/awk.c | 33 | ||||
-rw-r--r-- | editors/sed.c | 5 |
2 files changed, 22 insertions, 16 deletions
diff --git a/editors/awk.c b/editors/awk.c index 7fb629273..3ca0c5fce 100644 --- a/editors/awk.c +++ b/editors/awk.c | |||
@@ -555,8 +555,9 @@ struct globals { | |||
555 | //we are reusing ahash as fdhash, via define (see later) | 555 | //we are reusing ahash as fdhash, via define (see later) |
556 | const char *g_progname; | 556 | const char *g_progname; |
557 | int g_lineno; | 557 | int g_lineno; |
558 | int nfields; | 558 | int num_fields; /* number of existing $N's */ |
559 | unsigned maxfields; | 559 | unsigned num_alloc_fields; /* current size of Fields[] */ |
560 | /* NB: Fields[0] corresponds to $1, not to $0 */ | ||
560 | var *Fields; | 561 | var *Fields; |
561 | char *g_pos; | 562 | char *g_pos; |
562 | char g_saved_ch; | 563 | char g_saved_ch; |
@@ -631,8 +632,8 @@ struct globals2 { | |||
631 | // for fdhash in execution stage. | 632 | // for fdhash in execution stage. |
632 | #define g_progname (G1.g_progname ) | 633 | #define g_progname (G1.g_progname ) |
633 | #define g_lineno (G1.g_lineno ) | 634 | #define g_lineno (G1.g_lineno ) |
634 | #define nfields (G1.nfields ) | 635 | #define num_fields (G1.num_fields ) |
635 | #define maxfields (G1.maxfields ) | 636 | #define num_alloc_fields (G1.num_alloc_fields) |
636 | #define Fields (G1.Fields ) | 637 | #define Fields (G1.Fields ) |
637 | #define g_pos (G1.g_pos ) | 638 | #define g_pos (G1.g_pos ) |
638 | #define g_saved_ch (G1.g_saved_ch ) | 639 | #define g_saved_ch (G1.g_saved_ch ) |
@@ -1966,30 +1967,30 @@ static void fsrealloc(int size) | |||
1966 | { | 1967 | { |
1967 | int i, newsize; | 1968 | int i, newsize; |
1968 | 1969 | ||
1969 | if ((unsigned)size >= maxfields) { | 1970 | if ((unsigned)size >= num_alloc_fields) { |
1970 | /* Sanity cap, easier than catering for over/underflows */ | 1971 | /* Sanity cap, easier than catering for over/underflows */ |
1971 | if ((unsigned)size > 0xffffff) | 1972 | if ((unsigned)size > 0xffffff) |
1972 | bb_die_memory_exhausted(); | 1973 | bb_die_memory_exhausted(); |
1973 | 1974 | ||
1974 | i = maxfields; | 1975 | i = num_alloc_fields; |
1975 | maxfields = size + 16; | 1976 | num_alloc_fields = size + 16; |
1976 | 1977 | ||
1977 | newsize = maxfields * sizeof(Fields[0]); | 1978 | newsize = num_alloc_fields * sizeof(Fields[0]); |
1978 | debug_printf_eval("fsrealloc: xrealloc(%p, %u)\n", Fields, newsize); | 1979 | debug_printf_eval("fsrealloc: xrealloc(%p, %u)\n", Fields, newsize); |
1979 | Fields = xrealloc(Fields, newsize); | 1980 | Fields = xrealloc(Fields, newsize); |
1980 | debug_printf_eval("fsrealloc: Fields=%p..%p\n", Fields, (char*)Fields + newsize - 1); | 1981 | debug_printf_eval("fsrealloc: Fields=%p..%p\n", Fields, (char*)Fields + newsize - 1); |
1981 | /* ^^^ did Fields[] move? debug aid for L.v getting "upstaged" by R.v in evaluate() */ | 1982 | /* ^^^ did Fields[] move? debug aid for L.v getting "upstaged" by R.v in evaluate() */ |
1982 | 1983 | ||
1983 | for (; i < maxfields; i++) { | 1984 | for (; i < num_alloc_fields; i++) { |
1984 | Fields[i].type = VF_SPECIAL; | 1985 | Fields[i].type = VF_SPECIAL | VF_DIRTY; |
1985 | Fields[i].string = NULL; | 1986 | Fields[i].string = NULL; |
1986 | } | 1987 | } |
1987 | } | 1988 | } |
1988 | /* if size < nfields, clear extra field variables */ | 1989 | /* if size < num_fields, clear extra field variables */ |
1989 | for (i = size; i < nfields; i++) { | 1990 | for (i = size; i < num_fields; i++) { |
1990 | clrvar(Fields + i); | 1991 | clrvar(Fields + i); |
1991 | } | 1992 | } |
1992 | nfields = size; | 1993 | num_fields = size; |
1993 | } | 1994 | } |
1994 | 1995 | ||
1995 | static int regexec1_nonempty(const regex_t *preg, const char *s, regmatch_t pmatch[]) | 1996 | static int regexec1_nonempty(const regex_t *preg, const char *s, regmatch_t pmatch[]) |
@@ -2126,7 +2127,7 @@ static void split_f0(void) | |||
2126 | /* set NF manually to avoid side effects */ | 2127 | /* set NF manually to avoid side effects */ |
2127 | clrvar(intvar[NF]); | 2128 | clrvar(intvar[NF]); |
2128 | intvar[NF]->type = VF_NUMBER | VF_SPECIAL; | 2129 | intvar[NF]->type = VF_NUMBER | VF_SPECIAL; |
2129 | intvar[NF]->number = nfields; | 2130 | intvar[NF]->number = num_fields; |
2130 | #undef fstrings | 2131 | #undef fstrings |
2131 | } | 2132 | } |
2132 | 2133 | ||
@@ -2999,7 +3000,7 @@ static var *evaluate(node *op, var *res) | |||
2999 | syntax_error(EMSG_TOO_FEW_ARGS); | 3000 | syntax_error(EMSG_TOO_FEW_ARGS); |
3000 | L.v = evaluate(op1, TMPVAR0); | 3001 | L.v = evaluate(op1, TMPVAR0); |
3001 | /* Does L.v point to $n variable? */ | 3002 | /* Does L.v point to $n variable? */ |
3002 | if ((size_t)(L.v - Fields) < maxfields) { | 3003 | if ((size_t)(L.v - Fields) < num_alloc_fields) { |
3003 | /* yes, remember where Fields[] is */ | 3004 | /* yes, remember where Fields[] is */ |
3004 | old_Fields_ptr = Fields; | 3005 | old_Fields_ptr = Fields; |
3005 | } | 3006 | } |
@@ -3553,7 +3554,7 @@ static var *evaluate(node *op, var *res) | |||
3553 | res = intvar[F0]; | 3554 | res = intvar[F0]; |
3554 | } else { | 3555 | } else { |
3555 | split_f0(); | 3556 | split_f0(); |
3556 | if (i > nfields) | 3557 | if (i > num_fields) |
3557 | fsrealloc(i); | 3558 | fsrealloc(i); |
3558 | res = &Fields[i - 1]; | 3559 | res = &Fields[i - 1]; |
3559 | } | 3560 | } |
diff --git a/editors/sed.c b/editors/sed.c index 07f7b7a9b..107e664a0 100644 --- a/editors/sed.c +++ b/editors/sed.c | |||
@@ -1669,6 +1669,11 @@ int sed_main(int argc UNUSED_PARAM, char **argv) | |||
1669 | fchown(nonstdoutfd, statbuf.st_uid, statbuf.st_gid); | 1669 | fchown(nonstdoutfd, statbuf.st_uid, statbuf.st_gid); |
1670 | 1670 | ||
1671 | process_files(); | 1671 | process_files(); |
1672 | fflush(G.nonstdout); | ||
1673 | if (ferror(G.nonstdout)) { | ||
1674 | xfunc_error_retval = 4; /* It's what gnu sed exits with... */ | ||
1675 | bb_simple_error_msg_and_die(bb_msg_write_error); | ||
1676 | } | ||
1672 | fclose(G.nonstdout); | 1677 | fclose(G.nonstdout); |
1673 | G.nonstdout = stdout; | 1678 | G.nonstdout = stdout; |
1674 | 1679 | ||