Calling a command line program in Objective-C

Discussion in 'Web Design and Development (archive)' started by smknappy, Jan 29, 2004.

  1. macrumors member

    Joined:
    Nov 21, 2003
    #1
    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!
     
  2. macrumors 6502a

    Rincewind42

    Joined:
    Mar 3, 2003
    Location:
    Orlando, FL
    #2
    Re: Calling a command line program in Objective-C

    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.
     
  3. macrumors 6502a

    FattyMembrane

    Joined:
    Apr 14, 2002
    Location:
    bat country
    #3
    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;)
     

Share This Page