unix shell script execution
Author |
Message |
Helmut Wenisc #1 / 8
|
 unix shell script execution
I have an unix shell script, which executes a commercial program. The shell script is called with two parameters and returns a value to stdout. I want to call this shell script inside a C-Function definition. The C-Function should have two parameters (passed to the shell script). The shell script's standard output should be the return value of the C-Function. Any suggestions welcome! Helmut Wenisch
|
Tue, 27 Jan 2004 14:11:29 GMT |
|
 |
Duncan Bayn #2 / 8
|
 unix shell script execution
Quote:
> I have an unix shell script, which executes a commercial program. The shell > script is called with two parameters and returns a value to stdout. > I want to call this shell script inside a C-Function definition. The > C-Function should have two parameters (passed to the shell script). The > shell script's standard output should be the return value of the C-Function. > Any suggestions welcome!
AFAIK, snprintf() isn't ISO C, so my answer is strictly off-topic. Generally, you'd be best off trying a UNIX programming newsgroup for questions like yours. However, I suggest trying something like the snippet below (untested). I suggest also checking your systems manpage for the system() function. int call_program(char *param1, char *param2) { char command[1024]; int result; if ( (param1 == NULL) || (param2 == NULL) ) { return -1; } snprintf(command, 1023, "/usr/bin/foo %s %s", param1, param2); result = system(command); return result; Quote: }
-- Duncan Bayne 1974 Yamaha RD350 +---------------------------+---------------------------------------+
+---------------------------+---------------------------------------+ | "Retreat Hell! We're just attacking in another direction." | | -- Major General Oliver P. Smith | +-------------------------------------------------------------------+
|
Tue, 27 Jan 2004 14:32:00 GMT |
|
 |
Daniel Fische #3 / 8
|
 unix shell script execution
Hi,
Quote: > AFAIK, snprintf() isn't ISO C, so my answer is strictly off-topic.
snprintf is part of the current C standard. Daniel (followup-to comp.lang.c) -- IMO, anyway.
acllc-c++ FAQ: http://snurse-l.org/acllc-c++/faq.html 08/10 Independence Day in Ecuador
|
Tue, 27 Jan 2004 16:40:24 GMT |
|
 |
Jens.Toerr.. #4 / 8
|
 unix shell script execution
Quote:
>> I have an unix shell script, which executes a commercial program. The shell >> script is called with two parameters and returns a value to stdout. >> I want to call this shell script inside a C-Function definition. The >> C-Function should have two parameters (passed to the shell script). The >> shell script's standard output should be the return value of the C-Function. >> Any suggestions welcome! > AFAIK, snprintf() isn't ISO C, so my answer is strictly off-topic. > Generally, you'd be best off trying a UNIX programming newsgroup for > questions like yours. > However, I suggest trying something like the snippet below (untested). > I suggest also checking your systems manpage for the system() function. > int call_program(char *param1, char *param2) > { > char command[1024]; > int result; > if ( (param1 == NULL) || (param2 == NULL) ) > { > return -1; > } > snprintf(command, 1023, "/usr/bin/foo %s %s", param1, param2); > result = system(command); > return result; > }
That's not exactly what was asked for, because the standard output of the script, not it's return status is needed. But, unfortunately, the full answer gets really off topic for comp.lang.c because this needs the popen()/pclose() function which definitely is not ISO C. So it should better have been asked and answered in comp.unix.programmer... But anyway, you may try a slightly changed version of Duncans function: const char *call_program( const char *param1, const char *param2 ) { FILE *fp; char command[ 1024 ]; static char result[ 1024 ]; /* make this long enough ! */ int chars_read; if ( param1 == NULL || param2 == NULL ) return NULL; snprintf( command, 1023, "/usr/bin/foo %s %s", param1, param2 ); if ( ( fp = popen( command, "r" ) ) == NULL ) return NULL; chars_read = fscanf( fp, "%1023c", result ); pclose( fp ); if ( chars_read == EOF ) return NULL; else result[ chars_read ] = '\0'; return result; Quote: }
Some comments: 1. Make sure the 'result' buffer is long enough to hold the output of the script (and, if necessary also change the format string in fscanf()) . 2. Make sure to strcpy the string received from the function before calling it again. 3. If you're catching SIGCHLD signals in your program don't wait() for the return status - pclose() will already have reaped it - if this is a problem you'll have to roll your own popen() using pipe()/fork()/exec(). 4 I did *NOT* test it. Regards, Jens -- _ _____ _____
_ | | | | | | AG Moebius, Institut fuer Molekuelphysik | |_| | | | | | Fachbereich Physik, Freie Universitaet Berlin \___/ens|_|homs|_|oerring Tel: ++49 (0)30 838 - 53394 / FAX: - 56046
|
Tue, 27 Jan 2004 17:32:09 GMT |
|
 |
James Dennet #5 / 8
|
 unix shell script execution
Quote:
> AFAIK, snprintf() isn't ISO C, so my answer is strictly off-topic.
You're in luck. snprintf made it into the latest C Standard, C99. -- James Dennett
|
Tue, 27 Jan 2004 17:50:43 GMT |
|
 |
Richard B #6 / 8
|
 unix shell script execution
Quote:
> > I want to call this shell script inside a C-Function definition. The > > C-Function should have two parameters (passed to the shell script). > AFAIK, snprintf() isn't ISO C, so my answer is strictly off-topic.
It is in C99. Quote: > int call_program(char *param1, char *param2) > { > char command[1024]; > int result; > snprintf(command, 1023, "/usr/bin/foo %s %s", param1, param2);
Even better, you can use snprintf() with a size of 0; it will then write nothing, but it will still return the number of characters that _would_ have been written (but not counting the null terminator) had the size been large enough. Then, if your array is too small, you can try to malloc() a sufficiently large one, and snprintf() to that instead. Do I have to tell you to be careful what you pass to the shell? No? Thought not. Richard
|
Tue, 27 Jan 2004 18:03:04 GMT |
|
 |
Greg Come #7 / 8
|
 unix shell script execution
Quote: >AFAIK, snprintf() isn't ISO C, so my answer is strictly off-topic.
It's not C90, but it is C99. -- Greg Comeau Countdown to "export": December 1, 2001 Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout Tasty C99/C++ Combo: Dinkumware libs + Comeau Compilers == WOW!!!
|
Tue, 27 Jan 2004 22:02:27 GMT |
|
 |
Gael Trinqua #8 / 8
|
 unix shell script execution
Quote:
> I have an unix shell script, which executes a commercial program. The shell > script is called with two parameters and returns a value to stdout. > I want to call this shell script inside a C-Function definition. The > C-Function should have two parameters (passed to the shell script). The > shell script's standard output should be the return value of the C-Function. > Any suggestions welcome! > Helmut Wenisch
There's a function like system() in stdlib.h that does the same job but where you can get back stdout. Have a look at the header. Gael.
|
Tue, 27 Jan 2004 22:15:51 GMT |
|
|
|