From c6fc7f7b786ce47c35645054cdce73e83f39e7f3 Mon Sep 17 00:00:00 2001 From: Andy Fiddaman Date: Sun, 16 Aug 2020 11:46:40 +0000 Subject: 13051 sed: lastline check does not work properly with empty files Reviewed by: Robert Mustacchi Reviewed by: Dominik Hassler Approved by: Gordon Ross --- usr/src/cmd/sed/main.c | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) (limited to 'usr/src/cmd/sed/main.c') diff --git a/usr/src/cmd/sed/main.c b/usr/src/cmd/sed/main.c index 22937d9d92..c928131958 100644 --- a/usr/src/cmd/sed/main.c +++ b/usr/src/cmd/sed/main.c @@ -1,4 +1,5 @@ /* + * Copyright 2020 OmniOS Community Edition (OmniOSce) Association. * Copyright (c) 2013 Johann 'Myrkraverk' Oskarsson * Copyright (c) 2011 Gary Mills * Copyright 2011 Nexenta Systems, Inc. All rights reserved. @@ -511,10 +512,30 @@ lastline(void) { int ch; - if (files->next != NULL && (inplace == NULL || ispan)) - return (0); - if ((ch = getc(infile)) == EOF) + if (feof(infile) != 0 || (ch = getc(infile)) == EOF) { + struct s_flist *f; + + /* + * Reached the end of the current input file. + * If there are no more that contain data, then this is the + * last line. + */ + if (inplace != NULL && ispan == 0) + return (1); + + for (f = files->next; f != NULL; f = f->next) { + struct stat st; + + if (stat(f->fname, &st) == -1) { + /* Treat an error here as an empty file */ + continue; + } + if (st.st_size > 0) + return (0); + } return (1); + } + (void) ungetc(ch, infile); return (0); } -- cgit v1.2.3