moominmolly: (geek feet)
[personal profile] moominmolly

OK, before I tear the cow book into little bits and fling them out the window, how do I just run a system command using something like system() but pass a variable to it? And get the output back? Everything else works fine...

Date: 2003-12-02 05:38 am (UTC)
From: [identity profile] justinjs.livejournal.com
The command you want to run must be a char*, so you have to use string-handling functions to create the argument before you hand it to anything like system(). There are a couple of different ways. Look at strncat for one.

You can't get the output of a command directly from system(), as it will only return the exit code of your command. Try popen() instead.


Date: 2003-12-02 06:45 am (UTC)
From: [identity profile] moominmolly.livejournal.com
It is a char*. Thanks...

Date: 2003-12-02 08:45 am (UTC)
From: [identity profile] justinjs.livejournal.com
Sorry, what I meant is that if you want to use a variable in the argument passed to any of those functions, you may want to assemble a single command string from the variable and other strings, then pass that string.

Between strnfoo and popen, you should be all set.

Date: 2003-12-02 06:50 am (UTC)
From: [identity profile] network.livejournal.com
To get the output back, the official command is 'popen' (short for 'pipe open'). Look that up, and get familiar with it. You'll note that the function is potentially insecure when called in certain ways, but there's documentation about that online too, if you're concerned. Good luck!

Date: 2003-12-02 06:52 am (UTC)
From: [identity profile] moominmolly.livejournal.com
Sweet. You rock. I appreciate the warning, too.

Profile

moominmolly: (Default)
moominmolly

April 2018

S M T W T F S
12 34567
891011121314
15161718192021
22232425262728
2930     

Most Popular Tags

Style Credit

Expand Cut Tags

No cut tags
Page generated Jun. 8th, 2026 09:04 am
Powered by Dreamwidth Studios