Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Syntax for System Calls!

Status
Not open for further replies.

sera

Technical User
Jun 29, 2000
360
US
Hello all,
I am trying to remember the syntax for system calls using c++. I was pretty sure that the syntax was
system("some command");
but apparently this is not correct!
Also what include file do you use? Is it <system>?
Thanks!
Sera
 
Do you mean executing system commands like an ls or something like that?
Then yes, the syntax is
Code:
system(&quot;command&quot;);
You should write
Code:
#include <stdlib.h>
into your code.
 
Thanks Kenrae, I appreciate you response!
Sera
 
Nice tip. I was searching for this thru google and you answered
my question.
 
How would the following be done
system(&quot;ls -l $@&quot;);
I've tried to pass the command line args
to a system call and it doesn't like it.
Ideas?
 
Did you include the library <stdlib.h>???
Sera
 
It should work. You can execute it directly, anyway, via an exec call.
 
Here's my code.
I'ts not &quot;seeing&quot; the args.

// gcc testthis.c -o testthis
#include &quot;/usr/include/stdio.h&quot;
#include &quot;/usr/include/stdlib.h&quot;

int main()
{
(void) system(&quot;ls -l $@&quot;);
}

I'm a scratch beginner at C and want to thank
everyone for your help so far. (FYI, I can give
Bourne script help in return.)
--Sean

 
First of all. This:
Code:
#include &quot;/usr/include/stdio.h&quot;
#include &quot;/usr/include/stdlib.h&quot;
isn't bad, but it's better to write:
Code:
#include <stdio.h>
#include <stdlib.h>
This way compiler search for standard library wherever it's located. I've seen some machines where this files where in another path (it isn't usual, but...).

And now your problem.
This parameter,
Code:
$@
, is Bourne shell-related, isn't it? Is this shell your system's standard shell? system call executes a shell to execute your command, maybe it's executing a different shell, and it doesn't understand it.
Also, it executes a non-interactive shell, which has some restrictions. You could try to write it to a simple script and then execute it instead of the command.

If the problem persists, then you'll only be able to correct it using exec calls. Ask here if you need it.
 
Ok folks!
Here's the program version in a shell script:

#!/bin/sh
ls -l $@

Now here's the same thing in C:

/* gcc testthis.c -o testthis */
#include <stdio.h>
#include <stdlib.h>

int i;
char argstring[1024];
char execstring[1024];
/* 1024 is an arbitrary value I just picked. */

int main(int argc, char **argv)
{
for(i=1; i<=argc-1; i++) {
sprintf(argstring, &quot;%s%s &quot;, argstring, argv);
}
sprintf(execstring,&quot;%s %s&quot;, &quot;ls -l &quot;,argstring);
system(execstring);
return(0);
}

I have to give kudos to my Unix SA here at my day job
for a hint about the for loop and subsequent sprintf line.
FYI, I couldn't find a way to concat the arguments
with a command string using the system command,
so I concatted them beforehand using sprintf.

E.g.
system(&quot;some command &quot;, arguments) /* illegal */
system(&quot;some command &quot;arguments) /* illegal */
system(&quot;some command &quot; + arguments) /* illegal */

Thanks again for the help.
Sean aka &quot;Time Traveler&quot;
 
First of all, you should &quot;clean&quot; your strings, what I mean is:
Code:
bzero(argstring,sizeof(argstring));
bzero(execstring,sizeof(execstring));
Then, write to console your execstring before executing it, to see what it's like.
What's your program output? It executes only an ls? or an ls -l?If the former is true, then maybe your problem is that system call doesn't read your actual environment, it has its own environment (path, shell variables, etc...).
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top