obtain output of command to parse in in c / MacOSX

Tags: c++ c shell osx
Question!

I'm working on a Command Line app to help me on launchd tasks to know if a task is running by returning a BOOL, the problem comes when i need to do a command line and obtain the output for further parsing.

i'm coding it in C/C++ so i can't use NSTask for it, any ideas on how to achieve the goal?

The Command

sudo launchctl list -x [job_label]

If i use system(), i'm unable to get the output so in further research I came with popen(), but no success there.

Thanks in advance.



Answers

You'll want to create a pipe from which you can read the output of the program. This will involve using pipe, fork, exec*, and maybe even dup. There's a good tutorial on the linux documentation project.



You can do it the home-brew way with pipe(), fork(), and the exec*() family of functions, or you could use popen() if its constraints meet your requirements.



This video can help you solving your question :)
By: admin