home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / INFO / C / DLIBSSRC.ZIP / SYSTEM.C < prev    next >
Encoding:
C/C++ Source or Header  |  1987-10-08  |  1.5 KB  |  53 lines

  1. #include <osbind.h>
  2. #include <stdio.h>
  3.  
  4. extern char    *_base;
  5.  
  6. int system(command)
  7. char *command;
  8. /*
  9.  *    Attempts to pass <command> to the shell program pointed to by
  10.  *    the system variable "_shell_p".  If a valid shell can't be found
  11.  *    there, the "SHELL" environment variable is searched for.  If it
  12.  *    exists and is not empty, it will be the name of the shell program
  13.  *    to execute the given command.  If "SHELL" is not valid, the
  14.  *    "PATH" variable is searched for.  This would specify the paths
  15.  *    to search for the program name which is the first token of the
  16.  *    command.  If "PATH" is not valid, the current directory is
  17.  *    searched as for the given command.  The extensions tried (if none
  18.  *    is specified) are ".TTP", ".TOS", ".PRG" and ".APP".
  19.  */
  20. {
  21.     register char *p, *q;
  22.     register int rv = 0;
  23.     register int (*shell)();
  24.     char *getenv(), *strtok();
  25.  
  26.     if(!command)
  27.         return(ERROR);
  28.  
  29.     /* get _shell_p value */
  30.     p = Super(0L);
  31.     shell = *((long *) 0x4F6L);
  32.     Super(p);
  33.  
  34.     /* validate _shell_p */
  35.     if((shell) &&                /* Shell available. */
  36.        (((long) shell) < ((long) _base)) &&    /* Reasonable shell pointer. */
  37.        (strncmp(shell, "PATH", 4))) {    /* Not corrupted */
  38.  
  39.         /* execute the command */
  40.         rv = (*shell)(command);
  41.         return(rv);
  42.     }
  43.  
  44.     /* SHELL= variable? */
  45.     if((p = getenv("SHELL")) && (*p))
  46.         return(spawnp(p, command));
  47.  
  48.     /* parse for spawnp() attempt */
  49.     p = strtok(command, " \t\r\n");
  50.     q = strtok(NULL, "\r\n");
  51.     return(spawnp(p, (q ? q : "")));
  52. }
  53.