diff options
-rw-r--r-- | usr.bin/sed/main.c | 30 |
1 files changed, 21 insertions, 9 deletions
diff --git a/usr.bin/sed/main.c b/usr.bin/sed/main.c index 5cffd9a11b9..62644543a43 100644 --- a/usr.bin/sed/main.c +++ b/usr.bin/sed/main.c @@ -1,4 +1,4 @@ -/* $OpenBSD: main.c,v 1.39 2018/12/06 20:16:04 martijn Exp $ */ +/* $OpenBSD: main.c,v 1.40 2018/12/08 23:11:24 schwarze Exp $ */ /*- * Copyright (c) 1992 Diomidis Spinellis. @@ -345,13 +345,32 @@ mf_fgets(SPACE *sp, enum e_spflag spflag) size_t len; char *p; int c, fd; - static int firstfile = 1; + static int firstfile; + + if (infile == NULL) { + /* stdin? */ + if (files->fname == NULL) { + if (inplace != NULL) + error(FATAL, "-i may not be used with stdin"); + infile = stdin; + fname = "stdin"; + outfile = stdout; + outfname = "stdout"; + } + + firstfile = 1; + } for (;;) { if (infile != NULL && (c = getc(infile)) != EOF) { (void)ungetc(c, infile); break; } + /* If we are here then either eof or no files are open yet */ + if (infile == stdin) { + sp->len = 0; + return (0); + } finish_file(); if (firstfile == 0) files = files->next; @@ -362,13 +381,6 @@ mf_fgets(SPACE *sp, enum e_spflag spflag) return (0); } fname = files->fname; - if (fname == NULL || strcmp(fname, "-") == 0) { - infile = stdin; - fname = "stdin"; - outfile = stdout; - outfname = "stdout"; - break; - } if (inplace != NULL) { if (lstat(fname, &sb) != 0) error(FATAL, "%s: %s", fname, |