Making a system call

This is a total nooby question, but here goes...

I am trying to do something in C for the very first time, and doing it in Solaris, for the very first time. It would otherwise be easy enough to test out and find out, but I wont be able to do so until the week after next for various reasons, so I was just wondering if this would work...

I am replacing a script file with C code. The script starts out such as:

#!/bin/ksh
export BUILD_DIR=`cat /nssn/cm/current_build`
export HOSTNAME=`hostname`
echo ColdStart>$BUILD_DIR/logs/StartType
etc...

so, can I just replace that with system calls such as:

system("#!/bin/ksh");
system("export BUILD_DIR=`cat /nssn/cm/current_build`");
system("export HOSTNAME=`hostname`");
etc...

At first I thought yes but then I wondered, does each system call sort of call in a different shell? If so, would the environment variable defined in one system call be seen from another system call?

If that is a problem, is there any way to do all of the system calls in a single system call?

Help a noob out...and thanks for any help in advance!!!

If you want to do what the script does in C why do it that way?

Write the functionality in C, not just embed the script in C.

reborg is right. Using system will not help if you are going to do it that way.

Understand that system() actually forks and execs whatever command you have given. So your export command would be executed in a child process of the current running process and the environment variables would be set in the child process. When the system() call completes, the child process would end and its environment will not be visible to any further child processes that the main parent process may have. If you want to execute the script through a C program, you have to run it as:

#include<stdlib.h>
int main() {
   system("full_path_to_script");
}

Right, I know I can do it just by calling the script, however, this is actually what I am trying to avoid. Why you ask? Because the C code is being made "Privilege Aware" (something unique to Solaris 10 I believe) and will have the privilege to start up something that the user normally would not have privilege to do. If it calls a script, that script can be modified to start up a terminal per se. Thus the reason I am trying to do this in C instead of what is currently in place (a script), so it cannot be easily modified.

Any other ideas? Could I use a popen() instead of sytem() to do this? Right about now I wish I would have taken that Systems Programming course in school!

I greatly appreciate any of your help!

Are you sure that launching shells from a C program obtains the permissions you want? Since that's what system() does -- launches a new shell.

That'd seem pretty pointless to me if it worked -- anyone with a shell could compile a C program to bypass it. I think you're going to actually have to write the program.

popen() is going to do just what system() is doing, only it will allow you to read the output of the command, or to feed input to the command. Apart from that, pretty much the same.

You will actually have to write a C program to do what you are doing. Just from the first few lines of the script that you have in the first post, you are going to have to use getenv, setenv, fopen, fprintf... most probably fork() and exec() too...