user1166981
user1166981

Reputation: 1746

(C / Linux) Issue using bin/sh shell via netcat- the printfs not appearing on screen until program ends

I am accessing my linux box via netcat using bin/sh as my shell. I coded a small program, extract below:

printf("Enter command to exec \n");

fgets(abc,128,stdin);

....

What is happening is that when I run the program within the shell over netcat, the "enter command to send to system" doesn't print until after I have actually exited the program.

It should be the first thing that appears on screen after ./program, and strangely it takes my input upon run as if skipping the printf command. My input gets sent to the system fine (and prints the output), but the screen is otherwise blank with no printfs I have coded appearing.

However, once I exit the program, all the printfs actually appear (as if buffered) before it return to the shell. Any thoughts as to why this happens? If there is more information you need just let me know and I will gladly update the thread.

Thank you very much

Upvotes: 1

Views: 700

Answers (1)

ouah
ouah

Reputation: 145899

On a terminal stdout is usually line buffered, you have to either fflush(stdout) or a print a new line '\n' to see your characters printed.

On non interactive devices, streams are often full-buffered and you have to flush stdout to have your characters printed:

printf("Enter command to send to system \n");
fflush(stdout);

Upvotes: 2

Related Questions