Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Wrapping existing UNIX commands in C

Reply
Thread Tools

Wrapping existing UNIX commands in C

 
 
Rui Maciel
Guest
Posts: n/a
 
      07-29-2012
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
 
 
 
 
Heinrich Wolf
Guest
Posts: n/a
 
      07-30-2012

"Ike Naar" <(E-Mail Removed)> schrieb im Newsbeitrag
news:(E-Mail Removed). ..
> On 2012-07-29, Heinrich Wolf <(E-Mail Removed)> wrote:
>> if ((fp = popen("ls")) != NULL)
>> {
>> while (fgets(Buffer, sizeof(Buffer), fp))
>> puts(Buffer);
>> fclose(fp);

>
> pclose(fp); ?


Yes of course. I am sorry.

 
Reply With Quote
 
 
 
 
Heinrich Wolf
Guest
Posts: n/a
 
      07-30-2012

"Heinrich Wolf" <(E-Mail Removed)> schrieb im Newsbeitrag
news:jv3kb3$fk6$(E-Mail Removed)-online.net...

....

> if ((fp = popen("ls")) != NULL)


popen("ls", "r")

> {
> while (fgets(Buffer, sizeof(Buffer), fp))
> puts(Buffer);
> fclose(fp);


pclose in place of fclose

> }


I'm sorry.
My code is untested.

 
Reply With Quote
 
Nick Keighley
Guest
Posts: n/a
 
      07-30-2012
On Jul 29, 10:00*pm, "China Blue [Tor], Meersburg"
<(E-Mail Removed)> wrote:
> In article <(E-Mail Removed)>, Keith Thompson <(E-Mail Removed)>
> > "China Blue [Tor], Meersburg" <(E-Mail Removed)> writes:
> > > In article <jv3k82$(E-Mail Removed)>,
> > > *Eric Sosman <(E-Mail Removed)> 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.


how would I do this on a Windows system?

> > 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 wherespent
> just giving a straightfotward and useful answer?


where was your "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?

 
Reply With Quote
 
jacob navia
Guest
Posts: n/a
 
      07-30-2012
Le 29/07/12 23:00, China Blue [Tor], Meersburg a ťcrit :
> In article <(E-Mail Removed)>, Keith Thompson <(E-Mail Removed)>
> wrote:
>
> 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.


 
Reply With Quote
 
Nobody
Guest
Posts: n/a
 
      07-30-2012
On Sun, 29 Jul 2012 11:57:32 -0500, Stephen Sprunk wrote:

> 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);

 
Reply With Quote
 
jacob navia
Guest
Posts: n/a
 
      07-30-2012
Le 30/07/12 13:13, Nobody a écrit :
> On Sun, 29 Jul 2012 11:57:32 -0500, Stephen Sprunk wrote:
>
>> 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.
>


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.

 
Reply With Quote
 
Ben Bacarisse
Guest
Posts: n/a
 
      07-30-2012
jacob navia <(E-Mail Removed)> writes:

> Le 30/07/12 13:13, Nobody a écrit :

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

--
Ben.
 
Reply With Quote
 
Mark Bluemel
Guest
Posts: n/a
 
      07-30-2012
On 30/07/2012 12:39, Ben Bacarisse wrote:
> jacob navia <(E-Mail Removed)> writes:
>
>> Le 30/07/12 13:13, Nobody a écrit :

> <snip>
>>> 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/>

 
Reply With Quote
 
Jorgen Grahn
Guest
Posts: n/a
 
      07-30-2012
On Sun, 2012-07-29, Rui Maciel wrote:
> 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.


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

--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .
 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
enhancing/wrapping an existing instance of a duck Neville Dempsey Python 1 09-01-2008 06:27 PM
Adding a timeout to commands by wrapping + thread - suggestions? dduck Java 11 09-04-2007 12:34 PM
wrapping existing instance in new interface insyte@gmail.com Python 1 11-15-2006 11:25 PM
Need Help Differentiating Bad Commands From Incomplete Commands Tim Stanka Python 1 08-02-2004 02:08 AM
Re: man pages for C commands (GCC commands) Ben Pfaff C Programming 4 06-28-2003 06:21 PM



Advertisments