[SOLVED] Parse c external command output in real time
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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!
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
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?
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.
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!
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.