sed -i: do not treat "-" as a file name

Most GNU utilities treat "-" as standard input, sed itself does that --
in most contexts.  However, since the addition of support for
--in-place (-i) in sed-4.0, sed -i has treated a "-" argument as a
file name, i.e., like ./-.  Now, that usage evokes a diagnostic:

  $ sed -i s/a/b/ -
  sed: couldn't edit -: is a terminal

If you require the old behavior, specify the file name as "./-".
Prompted by the report/patch from Stanislav Brabec in
http://bugs.gnu.org/20796 to document the strangely
inconsistent legacy behavior.

* sed/execute.c (open_next_file): Call panic before
even attempting to operate on the file descriptor.
* testsuite/in-place-hyphen.sh: New file.  Test for this.
* testsuite/Makefile.am (T): Add it.
* NEWS (Feature removal): Mention it. Admittedly, the old behavior
feels more like a misfeature.
This commit is contained in:
Jim Meyering committed 2015-06-20 19:58:08 -07:00
1 parent 2714d42345
commit c033bdee41
4 files changed
+37 -1

No files matched your search

+3
View File
@@ -43,6 +43,9 @@ GNU sed NEWS -*- outline -*-
has been listed in the documentation as a failed experiment for at
least 10 years. That command is now removed.
sed -i s/a/b/ - now evokes an error, rather than attempting to operate
on the file named ./-
** Build-related
"make dist" now builds .tar.xz files, rather than .tar.gz ones.
+4 -1
View File
@@ -549,8 +549,11 @@ open_next_file(const char *name, struct input *input)
{
buffer.length = 0;
if (name[0] == '-' && name[1] == '\0' && !in_place_extension)
if (name[0] == '-' && name[1] == '\0')
{
if (in_place_extension)
panic(_("couldn't edit %s: is a terminal"), name);
clearerr(stdin); /* clear any stale EOF indication */
#if defined WIN32 || defined _WIN32 || defined __CYGWIN__ \
|| defined MSDOS || defined __EMX__
+1
View File
@@ -7,6 +7,7 @@ SH_LOG_COMPILER = $(SHELL)
# is listed in only one place.
T = \
help-version.sh \
in-place-hyphen.sh \
invalid-mb-seq-UMR.sh \
range-overlap.sh \
temp-file-cleanup.sh
+29
View File
@@ -0,0 +1,29 @@
#!/bin/sh
# Verify that "-" is no longer treated as a file name with --in-place.
# Copyright (C) 2015 Free Software Foundation, Inc.
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
. "${srcdir=.}/init.sh"; path_prepend_ ../sed
print_ver_ sed
echo "sed: couldn't edit -: is a terminal" > exp-err || framework_failure_
fail=0
sed -i 's/a/b/' - > out 2> err && fail=1
compare /dev/null out || fail=1
compare exp-err err || fail=1
Exit $fail