From 3bc95d6b4b307993a21d29b5f406e75a736603f8 Mon Sep 17 00:00:00 2001 From: "Andrew L. Moore" Date: Wed, 3 Apr 2024 07:43:22 +0000 Subject: [PATCH] Apply script to each file on command line. For this to work, the script must be seekable. And to avoid infinite loops, if the script modifies the input file list, it is only applied to the first file on the command line. --- src/buf.c | 6 +++--- src/io.c | 5 +++-- src/main.c | 9 ++++++++- 3 files changed, 14 insertions(+), 6 deletions(-) diff --git a/src/buf.c b/src/buf.c index 8c24892..55d0a78 100644 --- a/src/buf.c +++ b/src/buf.c @@ -1,6 +1,6 @@ /* buf.c: Buffer routines for the ed line editor. * - * Copyright © 1993-2022 Andrew L. Moore, SlewSys Research + * Copyright © 1993-2024 Andrew L. Moore, SlewSys Research * * SPDX-License-Identifier: BSD-2-Clause OR GPL-2.0-or-later OR MIT */ @@ -193,8 +193,8 @@ init_ed_command (int init_glob, ed_buffer_t *ed) /* File info */ #ifdef WANT_FILE_LOCK - /* GNU/Linux double-frees on fclose() here... */ -#if __linux__ + /* XXX: GNU/Linux double-frees on fclose(). */ +#ifndef __linux__ if (ed->file->handle) /* Assert: No writes since fopen(3). */ diff --git a/src/io.c b/src/io.c index 60f5c98..a7c9738 100644 --- a/src/io.c +++ b/src/io.c @@ -1,6 +1,6 @@ /* io.c: I/O routines for the ed line editor. * - * Copyright © 1993-2022 Andrew L. Moore, SlewSys Research + * Copyright © 1993-2024 Andrew L. Moore, SlewSys Research * * SPDX-License-Identifier: BSD-2-Clause OR GPL-2.0-or-later OR MIT */ @@ -615,7 +615,8 @@ write_file (const char *fn, int is_default, off_t from, off_t to, * File-locking requires file write access, so this case should not * be reached. */ - else if (file_already_open && fclose (ed->file->handle) < 0) + else if (file_already_open + && (fclose (ed->file->handle) < 0 || !(ed->file->handle = NULL))) { fprintf (stderr, "%s: %s\n", ed->file->name, strerror (errno)); ed->exec->err = _("File close error"); diff --git a/src/main.c b/src/main.c index fb7891b..546687b 100644 --- a/src/main.c +++ b/src/main.c @@ -1,6 +1,6 @@ /* main.c: Entry point for the ed line editor. * - * Copyright © 1993-2023 Andrew L. Moore, SlewSys Research + * Copyright © 1993-2024 Andrew L. Moore, SlewSys Research * * SPDX-License-Identifier: BSD-2-Clause OR GPL-2.0-or-later OR MIT */ @@ -410,6 +410,13 @@ main (int argc, char **argv) goto error; #endif + /* Command-line files still available and stdin seekable... */ + if (ed->file->list->gl_offs && ed->file->list->gl_pathc > 1 + && FSEEK(stdin, 0L, SEEK_SET) != -1) + { + status = EOF_NEXT; + goto next; + } quit (ed->exec->status, ed); case EMOD: ed->exec->err = _("WARNING: Buffer modified since last write");