On Wed, Jan 08, 2003 at 05:25:21AM -0800, Burke, Thomas G. wrote: > -----BEGIN PGP SIGNED MESSAGE----- > Hash: SHA1 > > Here's a little C program I wrote once... Try compiling & running > it. You might need to add a "sytem("cd what/ever/directory")", or
This change of directory would only affect the spawned subshell which would die immediately, having no affect on your current process. This would mean that your next call to system would start off in the same directory you started in, no change. If you want the change of directories to have any affect on the environment your script is going to run in you must do the cd within the subshell, eg. - within your script. OR you could use a call to "chdir" within your program (see "man 2 chdir") to change the directory your current process is actually in. that state would then be inherited bu the subshell which the "system" call would spawn off. > maybe a table with a list of different files to be run... > > #include <stdio.h> > #include <system.h> > > void main(int noargs, char *args[]) > { int i; > for(i=1 ; i<noargs ; i++) > system(args[i]); > } "system" actually performs "/bin/sh -c <your args>". The assumptions inherent here can cause problems. The "system" man page actually warns against using it for SUID or SGID scripts. Here is the relevant text from the "man 3 system" page: ############################################################################ Do not use system() from a program with suid or sgid privileges, because strange values for some environment variables might be used to subvert system integrity. Use the exec(3) family of functions instead, but not execlp(3) or execvp(3). system() will not, in fact, work properly from programs with suid or sgid privileges on systems on which /bin/sh is bash version 2, since bash 2 drops privileges on startup. .... The check for the availability of /bin/sh is not actually performed; it is always assumed to be available. ISO C specifies the check, but POSIX.2 specifies that the return shall always be non-zero, since a system without the shell is not conforming, and it is this that is implemented. ############################################################################ This is why I use execl. My example (posted yesterday), was cut down from a much larger program which had a specific purpose. It can be made smaller, and I like the way you presented the system call in your example. I recommend using the execl call but implement the small single purpose program in the fashion you have constructed your example. -- Jeff Kinz, Emergent Research, Hudson, MA. "[EMAIL PROTECTED]" "[EMAIL PROTECTED]" copyright 2002. Use is restricted. Any use is an acceptance of the offer at http://users.rcn.com/jkinz/policy.html. -- redhat-list mailing list unsubscribe mailto:[EMAIL PROTECTED]?subject=unsubscribe https://listman.redhat.com/mailman/listinfo/redhat-list