From:	HENRY::IN%"leichter-jerry%yale-eng-venus.arpa%kl.sri.com%csnet-relay.CSNET%relay.cs.net@RCA.COM" 11-JUN-1987 08:56
To:	Ron Ueberschaer <ubi@unix.sri.com>
Subj:	Re: Why no system() under VAX C

    ...
    Speaking of glaring [failures to emulate Unix], why did DEC decide not
    to implement system() under VMS???  It's bad enough that I can't exec
    anything except C programs.  I hate having to work under VMS, but if I
    could say
    
    	system("@foo bare");   ,
    
    or
    	system("show time");   ,
    
    or even

    	system("run $disk:[mydir]foo");   ,

    ...

The purpose of VAX C's emulation of the Unix library is primarily to allow
Unix code to be ported to VMS as easily as possible.  Any Unix program that
uses system() is applying it to a shell command, which isn't likely to work
on VMS anyway.

Besides, it's pretty easy to implement system() on top of LIB$SPAWN; I've
included code below.

Given the history of the VAX C RTL, which has picked up more and more Unix
emulation functions as time has gone on, a version of system() could easily
show up at some point - i.e., this could be simply a matter of implementation
priorities.  Should this happen, the enclosed code would become obsolete,
and you should get rid of it.  (Note that the VAX C RTL ships with VMS these
days, so you have to watch the VMS release notes.)  The code could be sped
up (considerably), and still do what you usually want it to do, by including
such flags as NOCLISYM and NOLOGNAM, thus eliminating the possibly long wait
while DCL copies over all your defined symbols and process logicals.  The
current code, while slower, is more general, since it allows you to SPAWN
commands you've defined in DCL.

    I'd stop complaining for a few hours, anyway.

Amazing.  A Unix programmer who'll stop complaining about VMS, even if only
for a few hours.  It's just because of that promise that I've enclosed this
code....  :-)
							-- Jerry

---------------------------------Cut here-------------------------------------
/*-----------------SYSTEM emulation-------------------------------------
 *
 *  Executes the VMS DCL command in string command_line.
 *  The command may ask the terminal for further input.
 *  After the VMS command exits, control is returned to the calling
 *  function.
 *
 *  Example:
 *           system("show def");
 *
 *  Beware: This call is painfully slow on a loaded system
 *
 *  for VAX-VMS 4-Nov-85  J. Faricelli
 *  modified    2-Dec-85  J. Faricelli (allow command to ask for input)
 *
 *----------------------------------------------------------------------*/
#include stdio.h

int system(command_line)
char *command_line;
{
#include descrip   /* VMS descriptors header file */
#include ssdef     /* VMS system call header file */

    static $DESCRIPTOR(spawn_line,"");
    int lib$spawn();
    int spawn_flags, spawn_rc, i;
    unsigned int len;
    char *string;

    /* Create local storage for command line */
    len = strlen(command_line);
    string = (char *) malloc(len);
    strncpy(string,command_line,len);

    /* VMS doesn't grok a newline at the end of the string,
          so don't send that to spawn (make it a blank). */
    /* command_line[len-1] is end of string (remember, index is
       0..len-1) */
    if ( command_line[len-1] == '\n' ) {
       string[len-1] = ' ';
       }

    /* Prepare descriptor for call to spawn */
    spawn_line.dsc$w_length  = len;
    spawn_line.dsc$a_pointer = string;
    spawn_flags = 0;
    spawn_rc = lib$spawn(&spawn_line,&0,&0,&spawn_flags);
    if( spawn_rc != SS$_NORMAL ) {
       fprintf(stderr,"Internal error in VMS emulation of system() call\n\
Error during spawn of subprocess:\n\
VMS return code was: %d\n",spawn_rc);
       free(string);
       return(-1);
    }
    free(string);
    return(0);
}
-------
