system calls from C.

system calls from C.

Post by doug l. ka » Fri, 15 Jul 1994 04:49:29



When using the system("command") instruction in C, the command is executed in Bourne Shell (sh). Is
there anyway to force it to execute in C Shell, or is there anyway to make sh call csh?
Specifically I want to use the source command from csh. I am using it to call several files that
set up certain paths in the users environment. These scripts are written in C Shell, so even if there is
a similar command to source in sh, it will not work.
Thanks in advance,

Doug Kaye,

 
 
 

system calls from C.

Post by Charlie Bro » Fri, 15 Jul 1994 09:05:47


doug l. kaye spewed:

: When using the system("command") instruction in C, the command is executed
  in Bourne Shell (sh). Is
: there anyway to force it to execute in C Shell, or is there anyway to make
  sh call csh?

: Doug Kaye,

I think this is what you want to do:

main(){
char *cmd="/bin/csh";
system(cmd);

Quote:}

Try this.  A command cannot be executed under the unix operating system
unless it is through an interpreter (a shell), or by strictly system calls.  
Be default system() uses /bin/sh, so this is the best way that I know of
doing it.

Chuck
--
        #include <std.disclaimer.h>;      cat all.flames > /dev/null;
   _  ___   _

  | |  |   |            EE / Comp. Sci. Student at GMI, Flint, MI
  \_/  |   \_/   Intern Electrical Engineer at OTC Division, Owatonna, MN
  ____________________________________________________________________________  
 | SPx                                                Exponent of Excellence

 
 
 

system calls from C.

Post by Matthew P Donad » Fri, 15 Jul 1994 23:06:23



Quote:> When using the system("command") instruction in C, the command is
> executed in Bourne Shell (sh). Is there anyway to force it to execute
> in C Shell, or is there anyway to make sh call csh?  Specifically I
> want to use the source command from csh. I am using it to call several
> files that set up certain paths in the users environment. These
> scripts are written in C Shell, so even if there is a similar command
> to source in sh, it will not work.

Not directly.  system(3) calls execl(2) and (I think) has "/bin/sh"
hard-coded as the first argument.  The following might work.  YMMV.

#include <sys/types.h>
#include <sys/wait.h>

/* a system function that calls csh instead of sh.  no error checking */
/* untested */
int csh_system(char *cmd)
{
        int status;
        pid_t pid;

        if((pid = fork()) < 0) {
                perror("fork");
                status = -1;
        } else if(pid == 0) {
                execl("/bin/csh", "csh", "-c", cmd, (char *) 0);
        } else {
                waitpid(pid, &status, 0);
        }

        return(status);

Quote:}

 
 
 

system calls from C.

Post by Philip Shott » Fri, 15 Jul 1994 20:42:50



   When using the system("command") instruction in C, the command is executed in Bourne Shell (sh). Is
   there anyway to force it to execute in C Shell, or is there anyway to make sh call csh?
   Specifically I want to use the source command from csh. I am using it to call several files that
   set up certain paths in the users environment. These scripts are written in C Shell, so even if there is
   a similar command to source in sh, it will not work.
   Thanks in advance,

   Doug Kaye,

If your scripts have the magic:

#!/bin/csh

as the first line, then the shell will execute them within the
specified shell.

--

#### Investment Intelligence Systems Corporation, London, UK    ####
#### Phone: +44 (0) 71 628 6960.  Fax: +44 (0) 71 638 7528      ####
###!                It's life Jim, but not as we know it        !###

 
 
 

system calls from C.

Post by Anka Novkov » Sat, 16 Jul 1994 03:25:49


Quote:> When using the system("command") instruction in C, the command is executed in
> there anyway to force it to execute in C Shell, or is there anyway to make sh
> Specifically I want to use the source command from csh. I am using it to call
> set up certain paths in the users environment. These scripts are written in C
> a similar command to source in sh, it will not work.

    If you put

    #!/bin/csh  (should be the very first line of the script)

    at the beginning of the script you call by system("script") it should
    be executed by csh.

        Anka

 
 
 

system calls from C.

Post by Joseph A. Reut » Sat, 16 Jul 1994 00:39:31



calgary.chevron.com (doug l. kaye) writes:
Quote:

>When using the system("command") instruction in C, the command is executed in

Bourne Shell (sh). Is
Quote:>there anyway to force it to execute in C Shell, or is there anyway to make sh
call csh?
>Specifically I want to use the source command from csh. I am using it to call

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
There are several ways to get to the C shell which have been covered by others.
However, your quest is fundamentally doomed -- no matter how you get to the C
shell, it will be running in a subprocess!  The source command will update the
environment of that subprocess, not the environment of the process that made
the system call!  It appears that you want to update the environment of the
C program that makes the system call, or maybe even that of the login shell.

I believe this is discusses in one of the Unix FAQs.  Check rtfm.mit.edu.
--------------------------------------------------------------------------------
Joseph A. Reuter, Wizard-in-Training,

"Olorin I was in my youth in the West that is forgotten."--J. R. R. Tolkien

 
 
 

system calls from C.

Post by Guruswamy Kows » Sat, 16 Jul 1994 00:42:09




>doug l. kaye spewed:

>: When using the system("command") instruction in C, the command is executed
>  in Bourne Shell (sh). Is
>: there anyway to force it to execute in C Shell, or is there anyway to make
>  sh call csh?
>I think this is what you want to do:

>main(){
>char *cmd="/bin/csh";
>system(cmd);
>}

Maybe I am missing something. But doesn't this invoke a Bourne shell to
execute a C shell to execute our program? In effect there are three
processes created and destroyed by the time the system call returns. Is
there some other way of doing it? Any suggestions?

K.
--
Warning! Perl newbie...Dont you smell Camels around me? *grin*
This line has been intentionally left blank!

University of SouthWestern Louisiana