• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: talking to shell from cocoa/obj-c
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: talking to shell from cocoa/obj-c


  • Subject: Re: talking to shell from cocoa/obj-c
  • From: Brian Luft <email@hidden>
  • Date: Thu, 06 Dec 2001 17:21:07 -0600

On 12/6/01 11:58 AM, "Matt Neuburg" <email@hidden> wrote:
> From within a Cocoa / Objective-C program, how do I speak to the
> shell in such a way that the response, which would normally go to
> STDOUT, comes back to me? As a simple example, how can I say "date"
> (the Unix command) in such a way as to learn the date?
>
> I see how to do this sort of thing with an NSTask by using a file on
> disk as an intermediary, but I want the Unix command's STDOUT to pipe
> directly back to me somehow. I tried to do this with an NSPipe but
> failed.

I'm not sure of the Cocoa way of doing it, but from within Objective-C you
can use the standard C way, which is to use a pipe:

char buf[100];
FILE *pipe = popen("date", "r");
fgets(buf, 100, pipe);
pclose(pipe);

The pipe works in the same way a normal FILE does, so you can use all of the
standard buffered C file calls.

Thanks. Brian


References: 
 >talking to shell from cocoa/obj-c (From: Matt Neuburg <email@hidden>)

  • Prev by Date: Re: [ANN] Mac OS X Metadata Petition
  • Next by Date: Re: talking to shell from cocoa/obj-c
  • Previous by thread: talking to shell from cocoa/obj-c
  • Next by thread: Re: talking to shell from cocoa/obj-c
  • Index(es):
    • Date
    • Thread