I modified GNU tee to cycletee Source Code (You can download the binary from https://github.com/vls/cycletee/tree/master/bin)
What it does can be explained by the following example:
seq 10 | cycletee 1.txt 2.txt 3.txt
cat 1.txt // prints 1, 4, 7, 10
cat 2.txt // prints 2, 5, 8
cat 3.txt // prints 3, 6, 9
Then there is a all.tgz (See Appendix for building script)
all.tgz has three text file and totally 9000000 lines.
Everything is fine. Like:
seq 10000000 | ./cycletee 1.txt 2.txt 3.txt
zcat all.tgz | tee 1.txt > /dev/null
zcat all.tgz | tail // got 9000000 at the last line
except calling:
zcat all.tgz | ./cycletee 1.txt 2.txt 3.txt
when it reads the No.3000000 line, it exits.
strace it I got this message and it exited:
_llseek(2, 0, 0xffbec3d0, SEEK_CUR) = -1 ESPIPE (Illegal seek)
Question
-
Anyone can point out the problem of my source code?
-
Any debugging technique to figure out the problem will be appreciated. I don’t know how to use
gdbin this situation.
Appendix
-
all.tgzcan be built by this Python sciprt https://gist.github.com/1500742 -
Environment: Ubuntu 10.04 32bit, CentOS 5.4 64bit
From the source:
I don’t think this will work on binary input (input that contains NULs).
[ Given the signedness of bytes_read, I have the strong suspicion that fread() has been replaced by fgets() + strlen(); ] This may or may not be the cause of the PIPE error, but it looks very wrong.