lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On Mon, Aug 27, 2007 at 02:51:03AM +0400, Alexey Tourbin wrote:
> Hello,
> 
> popen(3) manual page says:
> 
> 	Since the standard input of a command opened for reading shares
> 	its seek offset with the process that called popen(), if the
> 	original process has done a buffered read, the command's input
> 	position may not be as expected. Similarly, the output from a
> 	command opened for writing may become intermingled with that of
> 	the original process. The latter can be avoided by calling
> 	fflush(3) before popen().
> 
> And at least some scripting languages call something like "fflush(NULL)"
> before spawning (see e.g. "perldoc -f system").
> 
> (And also "fflush(NULL)" is K&R-compatible.)

I wouldn't enforce this... If you want to disable output buffering, you can already do this.

io.stdout:write("From Lua...")
io.popen("echo -n From Pipe...", "w")
io.stdout:write("Again from Lua...")

bash-3.2$ lua unflushed.lua >unflushed.txt; cat unflushed.txt
>From Pipe...From Lua...Again from Lua...

Now flush stdout before calling popen:

io.stdout:write("From Lua...")
io.stdout:flush()
io.popen("echo -n From Pipe...", "w")
io.stdout:write("Again from Lua...")

bash-3.2$ lua flushed.lua >flushed.txt; cat flushed.txt
>From Lua...From Pipe...Again from Lua...

Jürgen