misc/176886: [PATCH] tail -F file1 file2 > file3 places filenames in file3 incorrectly

Ilya Bakulin ilya at bakulin.de
Tue Mar 12 18:00:01 UTC 2013

>Number:         176886
>Category:       misc
>Synopsis:       [PATCH] tail -F file1 file2 > file3 places filenames in file3 incorrectly
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    freebsd-bugs
>State:          open
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Tue Mar 12 18:00:00 UTC 2013
>Originator:     Ilya Bakulin
>Release:        FreeBSD 9.0-RELEASE-p3 amd64
Deglitch Networks
uname -a 
FreeBSD olymp.kibab.com 9.0-RELEASE-p3 FreeBSD 9.0-RELEASE-p3 #0: Tue Jun 12 02:52:29 UTC 2012     root at amd64-builder.daemonology.net:/usr/obj/usr/src/sys/GENERIC  amd64

When using tail -F to watch several files and redirecting the output to file, the file name markers come _after_ the contents of the respective file, while when output is going directly to terminal everything is fine.
[kibab at olymp ~/tmp]$ echo "File1_Content" > file1.txt
[kibab at olymp ~/tmp]$ echo "File2_Content" > file2.txt
[kibab at olymp ~/tmp]$ tail -F file1.txt file2.txt

==> file1.txt <==

==> file2.txt <==

^^^ This is correct output. First we see filename marker for file1, then follows its content, then the same for file2.

[kibab at olymp ~/tmp]$ tail -F file1.txt file2.txt > file3.txt
[kibab at olymp ~/tmp]$ cat file3.txt 

==> file1.txt <==

==> file2.txt <==

^^^ This is wrong!

Attached patch solves the problem for me. It adds an explicit flush for stdout right after printing file name marker. What I don't understand is why it is not flushed by stdio automatically -- there is an '\n' at the end of the string being printf'ed...

After applying the patch the output looks correct:

[kibab at olymp ~/tmp]$ ~/repos/tail/tail -F file1.txt file2.txt > file3.txt
[kibab at olymp ~/tmp]$ cat file3.txt 

==> file1.txt <==

==> file2.txt <==

Patch attached with submission follows:

diff -ru /usr/src/usr.bin/tail/forward.c ./forward.c
--- /usr/src/usr.bin/tail/forward.c	2012-01-03 04:23:53.000000000 +0100
+++ ./forward.c	2013-03-12 18:18:22.000000000 +0100
@@ -319,8 +319,10 @@
 		if (file->fp) {
 			active = 1;
-			if (no_files > 1 && !qflag)
+			if (no_files > 1 && !qflag) {
 				(void)printf("\n==> %s <==\n", file->file_name);
+				(void)fflush(stdout);
+			}
 			forward(file->fp, file->file_name, style, off, &file->st);
 			if (Fflag && fileno(file->fp) != STDIN_FILENO)
Only in .: forward.o
Only in .: misc.o
Only in .: read.o
Only in .: reverse.o
Only in .: tail
Only in .: tail.1.gz
Only in .: tail.o


More information about the freebsd-bugs mailing list