PDA

View Full Version : Calling a command line program in Objective-C


smknappy
Jan 29, 2004, 11:20 PM
Hi all,

I'm new to Mac dev and was wondering if there is an easy way to execute a command line program from an objective-c app.

Thanks!

Rincewind42
Jan 29, 2004, 11:47 PM
Originally posted by smknappy
Hi all,

I'm new to Mac dev and was wondering if there is an easy way to execute a command line program from an objective-c app.

Thanks!

To start you may want to join some number of Mac developer lists http://lists.apple.com/ . And second to actually do this is something like this:

#include <stdio.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>

int main()
{
pid_t pid = fork();
if(pid != 0)
{
// parent
waitpid(pid, &status, 0);
}
else
{
// child
execlp("grep", "grep", "foo", NULL);
}

return 0;
}

This will only run the command line tool, any output it makes will still be directed elsewhere, so if you need that you will need to dig a bit deeper.

FattyMembrane
Jan 30, 2004, 12:19 AM
if you search apple's developer connection, you can find a cocoa wrapper for the "mpg123" utility that will allow you to make a quick and easy mp3 player by calling the command line utility. you can just lift the code right from that example.

p.s. i think this is in the wrong section;)