Go Back   Rhinocerus > Newsgroup > Newsgroup comp.lang.c

Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old 07-29-2012, 03:11 PM
Chicken McNuggets
Guest
 
Posts: n/a
Default Wrapping existing UNIX commands in C

Hi,

So I am aware of the exec family of functions and the system function to
execute external commands but they do not seem to offer the
functionality to allow me to get the output of said executable so I can
use it in the rest of my program. For instance a simple example would be
to use the "ls" command. I would want to get the output of the directory
listing and be able to manipulate it later on in my program.

Is there an easy way to do this? I realise that this is technically not
standard C and is most likely a POSIX extension but it would be nice if
someone could offer some advice.

Thanks.
Reply With Quote
Alt Today
Advertising
 
and become member of Rhinocerus
Standard Sponsored Links

  #2 (permalink)  
Old 07-29-2012, 03:18 PM
Eric Sosman
Guest
 
Posts: n/a
Default Re: Wrapping existing UNIX commands in C

On 7/29/2012 11:11 AM, Chicken McNuggets wrote:
> Hi,
>
> So I am aware of the exec family of functions and the system function to
> execute external commands but they do not seem to offer the
> functionality to allow me to get the output of said executable so I can
> use it in the rest of my program. For instance a simple example would be
> to use the "ls" command. I would want to get the output of the directory
> listing and be able to manipulate it later on in my program.
>
> Is there an easy way to do this? I realise that this is technically not
> standard C and is most likely a POSIX extension but it would be nice if
> someone could offer some advice.


"Technically not Standard C" is an understatement: There is no
aspect of your question that has anything at all to do with C.[*]
[*] No, not even the passing mention of system() qualifies this
as a C question. On some platforms it is possible to use system()
to run a program and have the output go to a file the invoking
program can then open and read, but the form of the argument that
gets system() to do this is entirely platform-specific.

Try comp.unix.programmer.

--
Eric Sosman
esosman@ieee-dot-org.invalid
Reply With Quote
  #3 (permalink)  
Old 07-29-2012, 03:20 PM
Heinrich Wolf
Guest
 
Posts: n/a
Default Re: Wrapping existing UNIX commands in C


"Chicken McNuggets" <chicken@mcnuggets.com> schrieb im Newsbeitrag
news:jv3jqk$r1t$1@speranza.aioe.org...
> Hi,
>
> So I am aware of the exec family of functions and the system function to
> execute external commands but they do not seem to offer the functionality
> to allow me to get the output of said executable so I can use it in the
> rest of my program. For instance a simple example would be to use the "ls"
> command. I would want to get the output of the directory listing and be
> able to manipulate it later on in my program.
>
> Is there an easy way to do this? I realise that this is technically not
> standard C and is most likely a POSIX extension but it would be nice if
> someone could offer some advice.
>
> Thanks.


Do you also know pipes?

if ((fp = popen("ls")) != NULL)
{
while (fgets(Buffer, sizeof(Buffer), fp))
puts(Buffer);
fclose(fp);
}

Reply With Quote
  #4 (permalink)  
Old 07-29-2012, 03:24 PM
Chicken McNuggets
Guest
 
Posts: n/a
Default Re: Wrapping existing UNIX commands in C

On 29/07/2012 16:20, Heinrich Wolf wrote:

> Do you also know pipes?
>
> if ((fp = popen("ls")) != NULL)
> {
> while (fgets(Buffer, sizeof(Buffer), fp))
> puts(Buffer);
> fclose(fp);
> }


Ah. Thank you. popen() looks exactly like what I was looking for.
Reply With Quote
  #5 (permalink)  
Old 07-29-2012, 03:24 PM
Chicken McNuggets
Guest
 
Posts: n/a
Default Re: Wrapping existing UNIX commands in C

On 29/07/2012 16:18, Eric Sosman wrote:

>
> "Technically not Standard C" is an understatement: There is no
> aspect of your question that has anything at all to do with C.[*]
>
>[*] No, not even the passing mention of system() qualifies this
> as a C question. On some platforms it is possible to use system()
> to run a program and have the output go to a file the invoking
> program can then open and read, but the form of the argument that
> gets system() to do this is entirely platform-specific.
>
> Try comp.unix.programmer.
>


Apologies for being so off-topic.
Reply With Quote
  #6 (permalink)  
Old 07-29-2012, 03:31 PM
China Blue [Tor], Meersburg
Guest
 
Posts: n/a
Default Re: Wrapping existing UNIX commands in C

In article <jv3jqk$r1t$1@speranza.aioe.org>,
Chicken McNuggets <chicken@mcnuggets.com> wrote:

> Hi,
>
> So I am aware of the exec family of functions and the system function to
> execute external commands but they do not seem to offer the
> functionality to allow me to get the output of said executable so I can
> use it in the rest of my program. For instance a simple example would be
> to use the "ls" command. I would want to get the output of the directory
> listing and be able to manipulate it later on in my program.
>
> Is there an easy way to do this? I realise that this is technically not
> standard C and is most likely a POSIX extension but it would be nice if
> someone could offer some advice.


The easiest way is to use popen/pclose which opens an anonymous pipe to (mode
"w") or from (mode "r") a child process executing the command:
FILE *ls = popen("ls -l .", "r");
while (fgets(ls, line, sizeof line)) process(line);
pclose(ls);

Using named pipes, you can do this with just system():
system("mkfifo ../pipe");
FILE *ls = open("../pipe", "r");
system("ls -l . >../pipe &");
while (fgets(ls, line, sizeof line)) process(line);
fclose(ls);

You can also use exec by openning a pipe:
int pp[2]; pipe(pp);
int child = fork();
if (child) {
close(pp[1]);
FILE *ls = fdopen(pp[0],"r");
while (fgets(ls, line, sizeof line)) process(line);
fclose(ls);
int status; waitpid(child, &status, 0);
} else {
close(pp[0]);
dup2(pp[1], 1);
close(pp[1]);
execl("/bin/ls", "/bin/ls", "-l", ".", 0);
}

--
My name Indigo Montoya. | Die, Robbie Ferrier! Die!
You flamed my father. | I'm whoever you want me to be.
Prepare to be spanked. | Annoying Usenet one post at a time.
Stop posting that! | At least I can stay in character.
Reply With Quote
  #7 (permalink)  
Old 07-29-2012, 03:34 PM
China Blue [Tor], Meersburg
Guest
 
Posts: n/a
Default Re: Wrapping existing UNIX commands in C

In article <jv3k82$92v$1@dont-email.me>,
Eric Sosman <esosman@ieee-dot-org.invalid> wrote:

> On 7/29/2012 11:11 AM, Chicken McNuggets wrote:
> > Hi,
> >
> > So I am aware of the exec family of functions and the system function to
> > execute external commands but they do not seem to offer the
> > functionality to allow me to get the output of said executable so I can
> > use it in the rest of my program. For instance a simple example would be
> > to use the "ls" command. I would want to get the output of the directory
> > listing and be able to manipulate it later on in my program.
> >
> > Is there an easy way to do this? I realise that this is technically not
> > standard C and is most likely a POSIX extension but it would be nice if
> > someone could offer some advice.

>
> "Technically not Standard C" is an understatement: There is no
> aspect of your question that has anything at all to do with C.[*]


That's odd, because we have been doing this in C since the 1970s.

--
My name Indigo Montoya. | Die, Robbie Ferrier! Die!
You flamed my father. | I'm whoever you want me to be.
Prepare to be spanked. | Annoying Usenet one post at a time.
Stop posting that! | At least I can stay in character.
Reply With Quote
  #8 (permalink)  
Old 07-29-2012, 04:57 PM
Stephen Sprunk
Guest
 
Posts: n/a
Default Re: Wrapping existing UNIX commands in C

On 29-Jul-12 10:11, Chicken McNuggets wrote:
> So I am aware of the exec family of functions and the system function to
> execute external commands but they do not seem to offer the
> functionality to allow me to get the output of said executable so I can
> use it in the rest of my program. For instance a simple example would be
> to use the "ls" command. I would want to get the output of the directory
> listing and be able to manipulate it later on in my program.
>
> Is there an easy way to do this? I realise that this is technically not
> standard C and is most likely a POSIX extension but it would be nice if
> someone could offer some advice.


If you've already decided to limit portability to POSIX systems, pipes
are the general solution for what you're trying to do, but may not be
the optimal solution. For instance, in the case of "ls", why not just
call the functions in <dirent.h> yourself rather than opening a pipe to
a program that calls them for you and then writing an enormous amount of
code to parse its output to extract the same data?

S

--
Stephen Sprunk "God does not play dice." --Albert Einstein
CCIE #3723 "God is an inveterate gambler, and He throws the
K5SSS dice at every possible opportunity." --Stephen Hawking
Reply With Quote
  #9 (permalink)  
Old 07-29-2012, 05:10 PM
Kenny McCormack
Guest
 
Posts: n/a
Default Re: Wrapping existing UNIX commands in C

In article <jv3k82$92v$1@dont-email.me>,
Eric Sosman <esosman@ieee-dot-org.invalid> wrote:
>On 7/29/2012 11:11 AM, Chicken McNuggets wrote:
>> Hi,
>>
>> So I am aware of the exec family of functions and the system function to
>> execute external commands but they do not seem to offer the
>> functionality to allow me to get the output of said executable so I can
>> use it in the rest of my program. For instance a simple example would be
>> to use the "ls" command. I would want to get the output of the directory
>> listing and be able to manipulate it later on in my program.
>>
>> Is there an easy way to do this? I realise that this is technically not
>> standard C and is most likely a POSIX extension but it would be nice if
>> someone could offer some advice.

>
> "Technically not Standard C" is an understatement: There is no
>aspect of your question that has anything at all to do with C.[*]


blah, blah, blah, like a biddy old aunt - who hasn't gotten any since the
war (WWII, that is).

Isn't it funny how, in the rest of the Usenet (and online forums in
general), the ethic is "If you can't answer the question, then STFU!", but
here in comp.lang.c, people still get mileage out of:

Off topic. Not portable. Cant discuss it here. Blah, blah, blah.

--
Windows 95 n. (Win-doze): A 32 bit extension to a 16 bit user interface for
an 8 bit operating system based on a 4 bit architecture from a 2 bit company
that can't stand 1 bit of competition.

Modern day upgrade --> Windows XP Professional x64: Windows is now a 64 bit
tweak of a 32 bit extension to a 16 bit user interface for an 8 bit
operating system based on a 4 bit architecture from a 2 bit company that
can't stand 1 bit of competition.
Reply With Quote
  #10 (permalink)  
Old 07-29-2012, 07:05 PM
Keith Thompson
Guest
 
Posts: n/a
Default Re: Wrapping existing UNIX commands in C

"China Blue [Tor], Meersburg" <chine.bleu@yahoo.com> writes:
> In article <jv3k82$92v$1@dont-email.me>,
> Eric Sosman <esosman@ieee-dot-org.invalid> wrote:
>> On 7/29/2012 11:11 AM, Chicken McNuggets wrote:
>> > So I am aware of the exec family of functions and the system function to
>> > execute external commands but they do not seem to offer the
>> > functionality to allow me to get the output of said executable so I can
>> > use it in the rest of my program. For instance a simple example would be
>> > to use the "ls" command. I would want to get the output of the directory
>> > listing and be able to manipulate it later on in my program.
>> >
>> > Is there an easy way to do this? I realise that this is technically not
>> > standard C and is most likely a POSIX extension but it would be nice if
>> > someone could offer some advice.

>>
>> "Technically not Standard C" is an understatement: There is no
>> aspect of your question that has anything at all to do with C.[*]

>
> That's odd, because we have been doing this in C since the 1970s.


But you haven't been doing it in *standard* C. (Well, prior to 1989
there was no standard.)

Of course there's nothing wrong with writing non-standard C; the
ability to write system-specific code is one of the language's
greatest strengths. And yes, POSIX is also a standard. But as
you probably know, the people who know about it tend to hang out
in comp.unix.programmer, not in comp.lang.c

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
Will write code for food.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Reply With Quote
  #11 (permalink)  
Old 07-29-2012, 09:00 PM
China Blue [Tor], Meersburg
Guest
 
Posts: n/a
Default Re: Wrapping existing UNIX commands in C

In article <ln8ve24cce.fsf@nuthaus.mib.org>, Keith Thompson <kst-u@mib.org>
wrote:

> "China Blue [Tor], Meersburg" <chine.bleu@yahoo.com> writes:
> > In article <jv3k82$92v$1@dont-email.me>,
> > Eric Sosman <esosman@ieee-dot-org.invalid> wrote:
> >> On 7/29/2012 11:11 AM, Chicken McNuggets wrote:
> >> > So I am aware of the exec family of functions and the system function to
> >> > execute external commands but they do not seem to offer the
> >> > functionality to allow me to get the output of said executable so I can
> >> > use it in the rest of my program. For instance a simple example would be
> >> > to use the "ls" command. I would want to get the output of the directory
> >> > listing and be able to manipulate it later on in my program.
> >> >
> >> > Is there an easy way to do this? I realise that this is technically not
> >> > standard C and is most likely a POSIX extension but it would be nice if
> >> > someone could offer some advice.
> >>
> >> "Technically not Standard C" is an understatement: There is no
> >> aspect of your question that has anything at all to do with C.[*]

> >
> > That's odd, because we have been doing this in C since the 1970s.

>
> But you haven't been doing it in *standard* C. (Well, prior to 1989
> there was no standard.)
>
> Of course there's nothing wrong with writing non-standard C; the
> ability to write system-specific code is one of the language's
> greatest strengths. And yes, POSIX is also a standard. But as
> you probably know, the people who know about it tend to hang out
> in comp.unix.programmer, not in comp.lang.c


Did you know comp.lang.c predates 1989? And that we weren't obnoxious twits back
then? Are you happy you're wasting more electrons being a dick than where spent
just giving a straightfotward and useful answer? Do you know that you are
allowed to create a moderated newsgroup that is actually moderated instead of
flouncing around on an unmoderated newsgroup?

--
My name Indigo Montoya. | Die, Robbie Ferrier! Die!
You flamed my father. | I'm whoever you want me to be.
Prepare to be spanked. | Annoying Usenet one post at a time.
Stop posting that! | At least I can stay in character.
Reply With Quote
  #12 (permalink)  
Old 07-29-2012, 09:41 PM
Ike Naar
Guest
 
Posts: n/a
Default Re: Wrapping existing UNIX commands in C

On 2012-07-29, Heinrich Wolf <invalid@invalid.invalid> wrote:
> if ((fp = popen("ls")) != NULL)
> {
> while (fgets(Buffer, sizeof(Buffer), fp))
> puts(Buffer);
> fclose(fp);


pclose(fp); ?
Reply With Quote
  #13 (permalink)  
Old 07-29-2012, 09:46 PM
Ike Naar
Guest
 
Posts: n/a
Default Re: Wrapping existing UNIX commands in C

On 2012-07-29, China Blue [Tor], Meersburg <chine.bleu@yahoo.com> wrote:
> system("mkfifo ../pipe");
> FILE *ls = open("../pipe", "r");


FILE *ls = fopen("../pipe", "r"); ?
Reply With Quote
  #14 (permalink)  
Old 07-29-2012, 10:19 PM
Keith Thompson
Guest
 
Posts: n/a
Default Re: Wrapping existing UNIX commands in C

"China Blue [Tor], Meersburg" <chine.bleu@yahoo.com> writes:
> In article <ln8ve24cce.fsf@nuthaus.mib.org>, Keith Thompson <kst-u@mib.org>
> wrote:
>> "China Blue [Tor], Meersburg" <chine.bleu@yahoo.com> writes:

[...]
>> > That's odd, because we have been doing this in C since the 1970s.

>>
>> But you haven't been doing it in *standard* C. (Well, prior to 1989
>> there was no standard.)
>>
>> Of course there's nothing wrong with writing non-standard C; the
>> ability to write system-specific code is one of the language's
>> greatest strengths. And yes, POSIX is also a standard. But as
>> you probably know, the people who know about it tend to hang out
>> in comp.unix.programmer, not in comp.lang.c

>
> Did you know comp.lang.c predates 1989?


Yes, I did. Did you know that it also postdates 1989?

> And that we weren't obnoxious
> twits back then? Are you happy you're wasting more electrons being a
> dick than where spent just giving a straightfotward and useful answer?
> Do you know that you are allowed to create a moderated newsgroup that
> is actually moderated instead of flouncing around on an unmoderated
> newsgroup?


If you have a question about POSIX, which is the best place to ask it
("best" meaning most likely to get good answer), comp.lang.c or
comp.unix.programmer?

And kindly explain how my offering advice and expression my opinion
about which questions are best posted where makes me an "obnoxious
twit".

How many electrons are you wasting by publicly insulting me?

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
Will write code for food.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Reply With Quote
  #15 (permalink)  
Old 07-29-2012, 11:35 PM
Rui Maciel
Guest
 
Posts: n/a
Default Re: Wrapping existing UNIX commands in C

China Blue [Tor], Meersburg wrote:

> Did you know comp.lang.c predates 1989? And that we weren't obnoxious
> twits back then?


Well, it appears that you are now contributing for that turn of events.


> Are you happy you're wasting more electrons being a dick
> than where spent just giving a straightfotward and useful answer?


If you took the time to actually read what has been repeatedly pointed out
to you, you would notice that the replies you've got were actually straight
forwrad and useful. It's pretty obvious that you would be better served if
you posted questions regarding unix APIs to a newsgroup dedicated to unix
programming. Yet, instead of following that advice you decided to ignore it
and instead waste "more electrons being a dick", as you've put it. Go
figure.


Rui Maciel
Reply With Quote
 
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




All times are GMT. The time now is 04:57 PM.


Copyright ©2009

LinkBacks Enabled by vBSEO 3.3.0 RC2 © 2009, Crawlability, Inc.