summaryrefslogtreecommitdiff
path: root/io.c
diff options
context:
space:
mode:
authorArnold D. Robbins <arnold@skeeve.com>2023-04-14 13:27:37 +0300
committerArnold D. Robbins <arnold@skeeve.com>2023-04-14 13:27:37 +0300
commit75d8ed16ad265469b1631ea828143519703a0c09 (patch)
treee117daf152b95e8356ed968b56bd726307124e61 /io.c
parent818e413643676b2fd2ab2bf272af6be5bb79e5b2 (diff)
parentb735d1ef4696b6920a7c7bc3e4b9a2fe6ccadd51 (diff)
downloadgawk-75d8ed16ad265469b1631ea828143519703a0c09.tar.gz
Merge branch 'gawk-5.2-stable'
Diffstat (limited to 'io.c')
-rw-r--r--io.c53
1 files changed, 28 insertions, 25 deletions
diff --git a/io.c b/io.c
index dccf3952..c595c009 100644
--- a/io.c
+++ b/io.c
@@ -3,7 +3,7 @@
*/
/*
- * Copyright (C) 1986, 1988, 1989, 1991-2022,
+ * Copyright (C) 1986, 1988, 1989, 1991-2023,
* the Free Software Foundation, Inc.
*
* This file is part of GAWK, the GNU implementation of the
@@ -269,7 +269,7 @@ static RECVALUE csvscan(IOBUF *iop, struct recmatch *recm, SCANSTATE *state);
static RECVALUE (*matchrec)(IOBUF *iop, struct recmatch *recm, SCANSTATE *state) = rs1scan;
-static int get_a_record(char **out, IOBUF *iop, int *errcode, const awk_fieldwidth_info_t **field_width);
+static int get_a_record(char **out, size_t *len, IOBUF *iop, int *errcode, const awk_fieldwidth_info_t **field_width);
static void free_rp(struct redirect *rp);
@@ -576,21 +576,19 @@ bool
inrec(IOBUF *iop, int *errcode)
{
char *begin;
- int cnt;
- bool retval = true;
+ size_t cnt;
+ bool retval;
const awk_fieldwidth_info_t *field_width = NULL;
if (at_eof(iop) && no_data_left(iop))
- cnt = EOF;
+ retval = false;
else if ((iop->flag & IOP_CLOSED) != 0)
- cnt = EOF;
+ retval = false;
else
- cnt = get_a_record(& begin, iop, errcode, & field_width);
+ /* Note that get_a_record may return -2 when I/O would block */
+ retval = (get_a_record(& begin, & cnt, iop, errcode, & field_width) == 0);
- /* Note that get_a_record may return -2 when I/O would block */
- if (cnt < 0) {
- retval = false;
- } else {
+ if (retval) {
INCREMENT_REC(NR);
INCREMENT_REC(FNR);
set_record(begin, cnt, field_width);
@@ -2823,7 +2821,8 @@ do_getline_redir(int into_variable, enum redirval redirtype)
{
struct redirect *rp = NULL;
IOBUF *iop;
- int cnt = EOF;
+ size_t cnt;
+ int retval = EOF;
char *s = NULL;
int errcode;
NODE *redir_exp = NULL;
@@ -2858,14 +2857,14 @@ do_getline_redir(int into_variable, enum redirval redirtype)
return make_number((AWKNUM) 0.0);
errcode = 0;
- cnt = get_a_record(& s, iop, & errcode, (lhs ? NULL : & field_width));
+ retval = get_a_record(& s, & cnt, iop, & errcode, (lhs ? NULL : & field_width));
if (errcode != 0) {
if (! do_traditional && (errcode != -1))
update_ERRNO_int(errcode);
- return make_number((AWKNUM) cnt);
+ return make_number((AWKNUM) retval);
}
- if (cnt == EOF) {
+ if (retval == EOF) {
/*
* Don't do iop_close() here if we are
* reading from a pipe; otherwise
@@ -2897,7 +2896,8 @@ do_getline_redir(int into_variable, enum redirval redirtype)
NODE *
do_getline(int into_variable, IOBUF *iop)
{
- int cnt = EOF;
+ size_t cnt;
+ int retval = EOF;
char *s = NULL;
int errcode;
const awk_fieldwidth_info_t *field_width = NULL;
@@ -2909,16 +2909,16 @@ do_getline(int into_variable, IOBUF *iop)
}
errcode = 0;
- cnt = get_a_record(& s, iop, & errcode, (into_variable ? NULL : & field_width));
+ retval = get_a_record(& s, & cnt, iop, & errcode, (into_variable ? NULL : & field_width));
if (errcode != 0) {
if (! do_traditional && (errcode != -1))
update_ERRNO_int(errcode);
if (into_variable)
(void) POP_ADDRESS();
- return make_number((AWKNUM) cnt);
+ return make_number((AWKNUM) retval);
}
- if (cnt == EOF)
+ if (retval == EOF)
return NULL; /* try next file */
INCREMENT_REC(NR);
INCREMENT_REC(FNR);
@@ -3916,13 +3916,14 @@ errno_io_retry(void)
/*
* get_a_record --- read a record from IOP into out,
- * return length or EOF, set RT.
+ * its length into len, and set RT.
+ * return 0 on success, EOF when out of data, and -2 if I/O would block.
* Note that errcode is never NULL, and the caller initializes *errcode to 0.
- * If I/O would block, return -2.
*/
static int
get_a_record(char **out, /* pointer to pointer to data */
+ size_t *len, /* pointer to record length */
IOBUF *iop, /* input IOP */
int *errcode, /* pointer to error variable */
const awk_fieldwidth_info_t **field_width)
@@ -3931,7 +3932,6 @@ get_a_record(char **out, /* pointer to pointer to data */
struct recmatch recm;
SCANSTATE state;
RECVALUE ret;
- int retval;
NODE *rtval = NULL;
static RECVALUE (*lastmatchrec)(IOBUF *iop, struct recmatch *recm, SCANSTATE *state) = NULL;
@@ -3950,6 +3950,9 @@ get_a_record(char **out, /* pointer to pointer to data */
if (rc == EOF)
iop->flag |= IOP_AT_EOF;
else {
+ assert(rc >= 0);
+ *len = rc;
+ rc = 0;
if (rt_len != 0)
set_RT(rt_start, rt_len);
else
@@ -4109,11 +4112,11 @@ get_a_record(char **out, /* pointer to pointer to data */
if (recm.len == 0) {
*out = NULL;
- retval = 0;
+ *len = 0;
} else {
assert(recm.start != NULL);
*out = recm.start;
- retval = recm.len;
+ *len = recm.len;
}
iop->off += recm.len + recm.rt_len;
@@ -4121,7 +4124,7 @@ get_a_record(char **out, /* pointer to pointer to data */
if (recm.len == 0 && recm.rt_len == 0 && at_eof(iop))
return EOF;
else
- return retval;
+ return 0;
}
/* set_RS --- update things as appropriate when RS is set */