[comp.unix.questions] How to issue a C SHELL command with

kahlers@ux1.cso.uiuc.edu (08/15/90)

> How can I issue a statment that executes a C SHELL command within a C program?
> I would appreciate any helps.

You can use the "system" call.  Use "man system" for more info.
=============================================================================\n\
     Kem Ahlers                 kahlers.ux1.cso.uiuc.edu  (Internet) \n\
     Caterpillar, Inc.          u36009@ncsagate           (Bitnet)   \n\
     Peoria, IL  USA "

gt0178a@prism.gatech.EDU (BURNS) (08/15/90)

in article <22000008@ux1.cso.uiuc.edu>, kahlers@ux1.cso.uiuc.edu says:
  Nf-ID: #R:<25279:26:ux1.cso.uiuc.edu:22000008:000:429
  Nf-From: ux1.cso.uiuc.edu!kahlers    Aug 14 16:42:00 1990
>> How can I issue a statment that executes a C SHELL command within a C program?
>> I would appreciate any helps.
> 
> You can use the "system" call.  Use "man system" for more info.

The system call uses bourne sh, so this won't work unless the command you
pass to system is 'csh mycommand', which would be inefficient (sh calls
csh calls mycommand). Probably a skeleton like the following is needed:

#include <stdio.h>
#include <sys/wait.h>
char mycommand[] = "history";
main() {
   union wait status;
   int pid,some_status=1,some_other_status=2;
/*   char mycommand[10] = "history";*/

   fflush(0);             /* optional - check syntax on your system */
   switch (fork()) {
      case -1: perror();
	       exit(some_status);         /* fork failed */
	       break;
      case 0:                             /* child proc */
	       execlp("/bin/csh","csh","-c",mycommand,(char *) 0);
	       /* the -c is used when mycommand is a builtin */
	       exit(some_other_status);   /* exec failed */
	       break;
      default:            /* parent */
	       pid=wait(status);
	       break;
   }
}
-- 
BURNS,JIM
Georgia Institute of Technology, Box 30178, Atlanta Georgia, 30332
uucp:	  ...!{decvax,hplabs,ncar,purdue,rutgers}!gatech!prism!gt0178a
Internet: gt0178a@prism.gatech.edu

guy@auspex.auspex.com (Guy Harris) (08/16/90)

>> How can I issue a statment that executes a C SHELL command within a C program?
>> I would appreciate any helps.
>
>You can use the "system" call.  Use "man system" for more info.

Well, sort of.  In order to run a *C* shell command, as the original
poster specified, rather than a *Bourne* shell command, as "system()"
does on any valid implementation, you have to construct a Bourne shell
command that runs the C shell, asking it to run a C shell command....

In general, I'd recommend replacing the C shell command in question with
a Bourne shell command, if at all possible, and just using "system()".