LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Parse c external command output in real time (https://www.linuxquestions.org/questions/programming-9/parse-c-external-command-output-in-real-time-942631/)

szzed 04-30-2012 05:23 PM

Parse c external command output in real time
 
Hi All!
I would like to write in C a gtk2.0 application, that reads the output of clamscan program. I've tried popen, glib spawn, but everything seems to be not real time. The clamscan starts, writes a lot of things to the stdout, but I could read it from the calling process, when clamscan stops. I need to write out the lines on the gtk window (into a textview widget) as it would be the stdout.
Is there any way to solve this? Of course I want to do this in C.
Thanks for replies!

ta0kira 04-30-2012 09:11 PM

Try non-blocking reads, or read one character at a time. Also, are you sure clamscan sends output in real time (an not just when the buffer is full)?
Kevin Barry

szzed 05-01-2012 02:38 AM

It seems that clamscan sends output when the buffer is full, but just when I run it as external program. If I run it from console, the output comes real time.
But not just clamscan behaves like this. I wrote a little program, that writes output to the stdout and to stderr. What is written to stderr, appears real time, but what is written to stdout, appears at the end of the program. If I redirect all the output to the stderr, the program fails. But I don't think this should be the way these programs work. But then how?

zsd 05-01-2012 08:27 AM

If you have expect and unbuffer on your system, you may be able to use unbuffer as part of your popen() command to help you out.

It is considered to be a "feature" that stderr output is unbuffered, whereas stdout is buffered. (Of course, someone writing a C program can use fflush(stdout) to flush the stdout buffer if they want. I don't know clamscan, but it may be that clamscan does fflush(stdout) if isatty(fileno(stdout)), and otherwise it just buffers it, which may explain what you are seeing).

Anyway, unbuffer is written for exactly this situation, so you may find it useful.

Cheers.

Jim

szzed 05-04-2012 01:48 PM

Thank you all helping me out this question.
zsd's suggestion solved my problem, now I have a fully functional program. But this took me into a new problem, but it's gonna be subject of a new thread.
Again thank you all!


All times are GMT -5. The time now is 01:42 AM.