@@ -223,6 +223,7 @@ struct more_control {
223223 is_eof , /* EOF detected */
224224 is_paused , /* is output paused */
225225 is_secure , /* is running in secure mode */
226+ no_eol , /* last byte written to stdout was not \n */
226227 no_quit_dialog , /* suppress quit dialog */
227228 no_scroll , /* do not scroll, clear the screen and then display text */
228229 no_tty_in , /* is input in interactive mode */
@@ -418,10 +419,12 @@ static int check_magic(struct more_control *ctl, char *fs)
418419 if (magic_error_msg ) {
419420 printf ("%s: %s: %s\n" , program_invocation_short_name ,
420421 _ ("magic failed" ), magic_error_msg );
422+ ctl -> no_eol = 0 ;
421423 return 0 ;
422424 }
423425 if (!mime_encoding || !(strcmp ("binary" , mime_encoding ))) {
424426 printf (_ ("\n******** %s: Not a text file ********\n\n" ), fs );
427+ ctl -> no_eol = 0 ;
425428 return 1 ;
426429 }
427430#else
@@ -442,6 +445,7 @@ static int check_magic(struct more_control *ctl, char *fs)
442445 case 0x457f : /* simple ELF detection */
443446 printf (_ ("\n******** %s: Not a text file ********\n\n" ),
444447 fs );
448+ ctl -> no_eol = 0 ;
445449 return 1 ;
446450 }
447451 }
@@ -475,6 +479,7 @@ static void checkf(struct more_control *ctl, char *fs)
475479 }
476480 if ((st .st_mode & S_IFMT ) == S_IFDIR ) {
477481 printf (_ ("\n*** %s: directory ***\n\n" ), fs );
482+ ctl -> no_eol = 0 ;
478483 ctl -> current_file = NULL ;
479484 return ;
480485 }
@@ -1970,13 +1975,19 @@ static void screen(struct more_control *ctl, int num_lines)
19701975 }
19711976}
19721977
1973- static void copy_file (FILE * f )
1978+ /*
1979+ * Copy the file to stdout and keep track of whether the output ended
1980+ * without a trailing newline.
1981+ */
1982+ static void copy_file (struct more_control * ctl , FILE * f )
19741983{
19751984 char buf [BUFSIZ ];
19761985 size_t sz ;
19771986
1978- while ((sz = fread (& buf , sizeof (char ), sizeof (buf ), f )) > 0 )
1987+ while ((sz = fread (& buf , sizeof (char ), sizeof (buf ), f )) > 0 ) {
19791988 fwrite (& buf , sizeof (char ), sz , stdout );
1989+ ctl -> no_eol = buf [sz - 1 ] != '\n' ;
1990+ }
19801991}
19811992
19821993
@@ -2007,6 +2018,10 @@ static void display_file(struct more_control *ctl, int left)
20072018 more_clear_screen (ctl );
20082019 }
20092020 if (ctl -> print_banner ) {
2021+ /* the banner has to start on a new line */
2022+ if (ctl -> no_eol )
2023+ putchar ('\n' );
2024+ ctl -> no_eol = 0 ;
20102025 if (ctl -> bad_stdout )
20112026 erase_to_col (ctl , 0 );
20122027 if (ctl -> clear_line_ends )
@@ -2026,7 +2041,7 @@ static void display_file(struct more_control *ctl, int left)
20262041 left = ctl -> lines_per_page - 4 ;
20272042 }
20282043 if (ctl -> no_tty_out )
2029- copy_file (ctl -> current_file );
2044+ copy_file (ctl , ctl -> current_file );
20302045 else
20312046 screen (ctl , left );
20322047 }
@@ -2213,7 +2228,7 @@ int main(int argc, char **argv)
22132228 ctl .sigfd = signalfd (-1 , & ctl .sigset , SFD_CLOEXEC );
22142229 if (ctl .no_tty_in ) {
22152230 if (ctl .no_tty_out )
2216- copy_file (stdin );
2231+ copy_file (& ctl , stdin );
22172232 else {
22182233 ctl .current_file = stdin ;
22192234 display_file (& ctl , left );
0 commit comments