Wrapping existing UNIX commands in C

C

Chicken McNuggets

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.
 
E

Eric Sosman

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.
 
H

Heinrich Wolf

Chicken McNuggets said:
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);
}
 
C

Chicken McNuggets

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.
 
C

Chicken McNuggets

"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.
 
S

Stephen Sprunk

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
 
K

Kenny McCormack

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.
 
K

Keith Thompson

China Blue [Tor] said:
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
 
R

Rui Maciel

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
 
N

Nick Keighley

China Blue [Tor] said:
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.

how would I do this on a Windows system?
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 wherespent
just giving a straightfotward and useful answer?

where was your "straightfotward and useful answer"?
 
J

jacob navia

Le 29/07/12 23:00, China Blue [Tor], Meersburg a écrit :
Did you know comp.lang.c predates 1989? And that we weren't obnoxious twits back
then?

Of course! We were 22 years younger!

Some people tend to change for the worst when they get old... They
forget what youth means, even.
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?

Nobody has appointed Mr Thompson as a moderator of this group but he
is playing the moderator since such a long time that somehow people
(including me) got used to that situation.

Happily he tells everywhere that I am in his killfile so that now
he can't answer my postings, so I can say and do whatever I want.

:)

You should try the same solution: just get into his killfile and then
you will live happily thereafter.
 
N

Nobody

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?

Because that requires learning the API, whereas someone who already knows
about system() and popen() can just write half-baked code and avoid the
effort of learning to do it correctly.

I have seen real-world code which did:

char buf[100];
sprintf(buf, "rm %s", filename);
system(buf);
 
J

jacob navia

Le 30/07/12 13:13, Nobody a écrit :
Because that requires learning the API, whereas someone who already knows
about system() and popen() can just write half-baked code and avoid the
effort of learning to do it correctly.

Why is that not correct?

Why bothering to learn the API, and spend hours debugging a new
version of "ls"?


If performance is not a big concern the code below will work correctly
even if in this case a call to remove() would be shorter.

I have seen real-world code which did:

char buf[100];
sprintf(buf, "rm %s", filename);
system(buf);

Yes, the hard coded buffer size is a problem but in principle
this thing will work as intended.
 
B

Ben Bacarisse

jacob navia said:
Le 30/07/12 13:13, Nobody a écrit :
I have seen real-world code which did:

char buf[100];
sprintf(buf, "rm %s", filename);
system(buf);

Yes, the hard coded buffer size is a problem but in principle
this thing will work as intended.

....and you just hope that 'filename' does not contain a space, or a '*'
or a ';' followed by something worse.

Of course one can imagine an implementation of system that keeps such
things safe, but I don't know of any.
 
M

Mark Bluemel

jacob navia said:
Le 30/07/12 13:13, Nobody a écrit :
I have seen real-world code which did:

char buf[100];
sprintf(buf, "rm %s", filename);
system(buf);

Yes, the hard coded buffer size is a problem but in principle
this thing will work as intended.

...and you just hope that 'filename' does not contain a space, or a '*'
or a ';' followed by something worse.

Obligatory XKCD reference follows - <http://xkcd.com/327/>
 
J

Jorgen Grahn

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


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.

As far as I can tell, it wasn't "China Blue" who asked; that guy
learned about popen() (and comp.unix.programmer, I hope), thanked
and left.

I do think the first "this is offtopic, try c.u.p." post could have
been formulated better.

/Jorgen
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,225
Members
46,815
Latest member
treekmostly22

Latest Threads

Top