Carbon: execute terminal program from Carbon App

takeo

Registered
I'm looking for a way to execute a terminal program and read it's output from a pipe in a Carbon application. In Cocoa, I can use NSTask and NSPipe to access the pipe... but I need the same thing implemented in a Carbon program. Does anyone here know how to do this?
 
If by Carbon you mean procedural C:

Code:
int myPipe[2];
int err, child_pid;

err = pipe(&myPipe); //creates the pipe - data written to myPipe[1] can be read from myPipe[0]

child_pid = fork();

if(child_pid == 0)
{
    err = dup2(myPipe[1], 1); //set myprogram's standard output to the input of the pipe

    execl("/path/to/myprogram", "arg1", "arg2");
}

int pipefd = myPipe[0];
char buffer[255];
err read(pipefd, buffer, 255); // etc etc

// Ideally you would check that err and child_pid != -1

Carbon is a compatibility API for porting programs to OS X. Since it must run on OS 9, it can't really support terminal programs or pipes.

You could, however, just add Cocoa.framework to your project, and use the NSTask and NSPipe. You would need to put your Cocoa code in .m files, either by creating new ones and creating C functions for other files to call, or by renaming any of your existing files in which you would use Cocoa.
 
I think I understand what you wrote, I'll give it a try. Thanks for the quick and helpful response!
 
Back
Top