Reputation:
I have written a simple NASM program:
printtest.asm
section .data
str_out db "val = %d",10,0
section .text
global main
extern printf
main:
PUSH 5
PUSH DWORD str_out
CALL printf
ADD ESP, 8
MOV EAX, 1
INT 80h
I am linking and creating an executable with the following commands:
nasm -f elf -l printtest.lst printtest.asm
gcc -o printtest printtest.o
When linked and executed, this prints "val = 5" to the console no problem. As far as I'm aware, calling printf
by default writes on stdout
. So why when I try and pipe this to another program does the other program seem to receive no input?
E.g
./printtest | cat
Seems to do nothing
I am sure I am fundamentally misunderstanding something here.
Upvotes: 9
Views: 144
Reputation: 126418
C stdio functions may be buffered by default, so writing to stdout with printf
doesn't always actually output anything -- sometimes it just writes to the buffer, awaiting a subsequent flush. Often times, whether a given stdio stream is buffered or not depends on whether it is connected to a terminal or a pipe or a file or something else.
When you call the exit system call (as you do), any data still in buffers will be lost. If you instead call the C library exit
function, it will flush all buffers before actually exiting.
Upvotes: 10