[git commit] awk: fix closing of non-opened file

Denys Vlasenko vda.linux at googlemail.com
Sun May 28 15:25:56 UTC 2023


commit: https://git.busybox.net/busybox/commit/?id=b76b420b5da1aadad823faf12327b610614f5951
branch: https://git.busybox.net/busybox/commit/?id=refs/heads/master

function                                             old     new   delta
setvar_ERRNO                                           -      53     +53
.rodata                                           105252  105246      -6
awk_getline                                          639     620     -19
evaluate                                            3402    3377     -25
------------------------------------------------------------------------------
(add/remove: 1/0 grow/shrink: 0/3 up/down: 53/-50)              Total: 3 bytes

Signed-off-by: Denys Vlasenko <vda.linux at googlemail.com>
---
 editors/awk.c | 23 +++++++++++++++--------
 1 file changed, 15 insertions(+), 8 deletions(-)

diff --git a/editors/awk.c b/editors/awk.c
index 77e0b0aab..83a08aa95 100644
--- a/editors/awk.c
+++ b/editors/awk.c
@@ -1006,6 +1006,11 @@ static var *setvar_i(var *v, double value)
 	return v;
 }
 
+static void setvar_ERRNO(void)
+{
+	setvar_i(intvar[ERRNO], errno);
+}
+
 static const char *getvar_s(var *v)
 {
 	/* if v is numeric and has no cached string, convert it to string */
@@ -2305,7 +2310,7 @@ static int awk_getline(rstream *rsm, var *v)
 		if (p < pp) {
 			p = 0;
 			r = 0;
-			setvar_i(intvar[ERRNO], errno);
+			setvar_ERRNO();
 		}
 		b[p] = '\0';
 	} while (p > pp);
@@ -3249,7 +3254,7 @@ static var *evaluate(node *op, var *res)
 			}
 
 			if (!rsm->F) {
-				setvar_i(intvar[ERRNO], errno);
+				setvar_ERRNO();
 				setvar_i(res, -1);
 				break;
 			}
@@ -3388,16 +3393,18 @@ static var *evaluate(node *op, var *res)
 					 */
 					if (rsm->F)
 						err = rsm->is_pipe ? pclose(rsm->F) : fclose(rsm->F);
-//TODO: fix this case:
-// $ awk 'BEGIN { print close(""); print ERRNO }'
-// -1
-// close of redirection that was never opened
-// (we print 0, 0)
 					free(rsm->buffer);
 					hash_remove(fdhash, L.s);
+				} else {
+					err = -1;
+					/* gawk 'BEGIN { print close(""); print ERRNO }'
+					 * -1
+					 * close of redirection that was never opened
+					 */
+					errno = ENOENT;
 				}
 				if (err)
-					setvar_i(intvar[ERRNO], errno);
+					setvar_ERRNO();
 				R_d = (double)err;
 				break;
 			}


More information about the busybox-cvs mailing list