diff options
Diffstat (limited to 'builtins/mapfile.def')
-rw-r--r-- | builtins/mapfile.def | 21 |
1 files changed, 13 insertions, 8 deletions
diff --git a/builtins/mapfile.def b/builtins/mapfile.def index 65c3cb4f..62db816c 100644 --- a/builtins/mapfile.def +++ b/builtins/mapfile.def @@ -2,7 +2,7 @@ This file is mapfile.def, from which is created mapfile.c. It implements the builtin "mapfile" in Bash. Copyright (C) 2005-2006 Rocky Bernstein for Free Software Foundation, Inc. -Copyright (C) 2008-2020 Free Software Foundation, Inc. +Copyright (C) 2008-2021 Free Software Foundation, Inc. This file is part of GNU Bash, the Bourne Again SHell. @@ -68,6 +68,7 @@ $END #include <config.h> #include "builtins.h" +#include "../bashtypes.h" #include "posixstat.h" #if defined (HAVE_UNISTD_H) @@ -156,6 +157,7 @@ mapfile (fd, line_count_goal, origin, nskip, callback_quantum, callback, array_n size_t line_length; unsigned int array_index, line_count; SHELL_VAR *entry; + struct stat sb; int unbuffered_read; line = NULL; @@ -180,19 +182,22 @@ mapfile (fd, line_count_goal, origin, nskip, callback_quantum, callback, array_n } else if (invisible_p (entry)) VUNSETATTR (entry, att_invisible); /* no longer invisible */ - + if (flags & MAPF_CLEARARRAY) array_flush (array_cell (entry)); #ifndef __CYGWIN__ - unbuffered_read = (lseek (fd, 0L, SEEK_CUR) < 0) && (errno == ESPIPE); + /* If the delimiter is a newline, turn on unbuffered reads for pipes + (terminals are ok). If the delimiter is not a newline, unbuffered reads + for every file descriptor that's not a regular file. */ + if (delim == '\n') + unbuffered_read = (lseek (fd, 0L, SEEK_CUR) < 0) && (errno == ESPIPE); + else + unbuffered_read = (fstat (fd, &sb) != 0) || (S_ISREG (sb.st_mode) == 0); #else unbuffered_read = 1; #endif - if (delim != '\n') - unbuffered_read = 1; - zreset (); /* Skip any lines at beginning of file? */ @@ -215,11 +220,11 @@ mapfile (fd, line_count_goal, origin, nskip, callback_quantum, callback, array_n /* Has a callback been registered and if so is it time to call it? */ if (callback && line_count && (line_count % callback_quantum) == 0) { - run_callback (callback, array_index, line); - /* Reset the buffer for bash own stream. */ if (unbuffered_read == 0) zsyncfd (fd); + + run_callback (callback, array_index, line); } /* XXX - bad things can happen if the callback modifies ENTRY, e.g., |