The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
Google UNIX.COM


High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
c system call rangaswamy High Level Programming 1 02-19-2008 09:53 AM
how to differentiate system call from library call muru UNIX for Advanced & Expert Users 2 07-19-2007 08:20 PM
Making Socket System Call From Linux Kernel Module? mian_m_hamayun Linux 0 04-06-2005 06:34 AM
Making a SOAP call from within unix shell scripts Laud12345 Shell Programming and Scripting 2 02-16-2005 10:23 AM
exec() system call a25khan High Level Programming 2 02-02-2004 08:06 PM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-05-2006
Registered User
 

Join Date: Jul 2006
Posts: 2
Stumble this Post!
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!!!
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 07-05-2006
reborg's Avatar
Administrator
 
Join Date: Mar 2005
Location: Ireland
Posts: 3,513
Stumble this Post!
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.
Reply With Quote
  #3 (permalink)  
Old 07-06-2006
blowtorch's Avatar
Supporter
 
Join Date: Dec 2004
Location: Singapore
Posts: 2,323
Stumble this Post!
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:
Code:
#include<stdlib.h>
int main() {
   system("full_path_to_script");
}
Reply With Quote
  #4 (permalink)  
Old 07-06-2006
Registered User
 

Join Date: Jul 2006
Posts: 2
Stumble this Post!
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!
Reply With Quote
  #5 (permalink)  
Old 07-06-2006
Registered User
 

Join Date: Aug 2005
Location: Saskatchewan
Posts: 923
Stumble this Post!
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.
Reply With Quote
  #6 (permalink)  
Old 07-06-2006
blowtorch's Avatar
Supporter
 
Join Date: Dec 2004
Location: Singapore
Posts: 2,323
Stumble this Post!
Quote:
Originally Posted by hardwickj
Any other ideas? Could I use a popen() instead of sytem() to do this?
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...
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 11:03 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0