home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 22 gnu / 22-gnu.zip / gnuawk.zip / atari / system.c < prev    next >
C/C++ Source or Header  |  1993-11-24  |  2KB  |  113 lines

  1. /*
  2.  * function system() - slightly modified from sources dLibs 1.2
  3.  * - a freely distributable C library for Atari ST.
  4.  * Authors: Dale Schumacher and John Stanley, I believe.
  5.  * Changes for gcc compiler and gnulib.olb - Michal Jaegermann
  6.  */
  7.  
  8. #include <osbind.h>
  9. #include <stdio.h>
  10. #include <string.h>
  11. #include <basepage.h>
  12. #ifdef __GNUC__
  13. #include <process.h>
  14. #define ERROR 2
  15. #endif
  16.  
  17. /* #define DEBUG  */
  18. #ifdef DEBUG
  19. #define _COOKIE(x) puts(x);putchar('\n')
  20. #endif
  21.  
  22. static void
  23. parse_args(char *cmdln, register char **argv)
  24. {
  25.     register char *p;
  26.     static char delim[] = " \t\r\n";
  27.  
  28.     if(NULL != (p = strtok(cmdln, delim))) {
  29.         do {
  30.             *argv++ = p;
  31.         } while(NULL != (p = strtok(NULL, delim)));
  32.     }
  33. }
  34.  
  35. #ifdef __GNUC__
  36. /* this is used by assembler statement to keep a copy of registers */
  37. static volatile long savearea[16];
  38. #endif
  39.  
  40. int
  41. system(const char *command)
  42. {
  43.     register char *p;
  44.     register int (*shell)();
  45. #ifndef __GNUC__
  46.     char rv[2];
  47. #endif
  48.     char cmdln[1024];
  49.     char *args[64];
  50.     char *getenv(const char *);
  51.  
  52.     if(!command)
  53.         return(ERROR);
  54.  
  55.     /* get _shell_p value */
  56.     p = (char *) Super(0L);  /* supervisor mode */
  57.     shell = (int (*)()) *((long *) 0x4F6L);
  58.     (void) Super(p);     /* restore user mode */
  59.  
  60.     /* validate _shell_p */
  61.     if((shell) &&                /* Shell available. */
  62.        (((long) shell) < ((long) _base)) &&    /* Reasonable shell pointer. */
  63.        (strncmp((char *)shell, "PATH", 4)))    /* Not corrupted */
  64.         {
  65. #ifdef __GNUC__
  66.         int ret;
  67. #endif
  68.         /* execute the command */
  69. #ifdef DEBUG
  70. _COOKIE("system: using _shell_p");
  71. printf("'shell' got value 0x%08lx\n", (long)shell);
  72. #endif
  73. /* a bit of paranoia caused by some misbehaving programs */
  74. #ifdef __GNUC__
  75. asm("moveml d1-d7/a0-a7,_savearea");    
  76.         ret = (*shell)(command);
  77. asm("moveml _savearea,d1-d7/a0-a7");
  78.         return (ret);
  79. #else
  80.         return ((*shell)(command));
  81. #endif
  82.         }
  83.  
  84.     strcpy(cmdln, command);    /* copy the command line for parsing */
  85.  
  86.     if((p = getenv("SHELL")) && (*p))    /* SHELL= variable? */
  87.         {
  88.         args[0] = p;
  89.         parse_args(cmdln, (args + 1));
  90. #ifdef DEBUG
  91. _COOKIE("system: executing SHELL");
  92. _COOKIE(p);
  93. #endif
  94.         }
  95.     else    /* attempt to find first token as a program on the path */
  96.         {
  97.         parse_args(cmdln, args);
  98.         p = args[0];
  99. #ifdef DEBUG
  100. _COOKIE("system: directly executing program");
  101. _COOKIE(p);
  102. #endif
  103.         }
  104.  
  105. #ifdef __GNUC__
  106.     return(spawnvp(0, p, args));
  107. #else   /* original from dLibs */
  108.     forkvpe(p, args, NULL);
  109.     wait(rv);
  110.     return((rv[1] == 0) ? rv[0] : rv[1]);
  111. #endif
  112. }
  113.