home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 11 Util / 11-Util.zip / os2exec1.zip / OS2EXEC.C < prev    next >
C/C++ Source or Header  |  1993-12-07  |  4KB  |  153 lines

  1. /* os2exec.c */
  2.  
  3. /* Copyright (c) 1992-1993 by Eberhard Mattes */
  4.  
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include <direct.h>
  9. #include <io.h>
  10. #include <fcntl.h>
  11. #include <sys/types.h>
  12. #include <sys/stat.h>
  13.  
  14. typedef unsigned char byte;
  15.  
  16. static int fh;
  17. static byte buf[4096+2];
  18.  
  19. #define MAKEP(seg,off) ((void far *)(((unsigned long)(seg)<<16)|(unsigned long)(off)))
  20. #define WHITE(c) ((c)==' ' || (c)=='\t')
  21.  
  22. static void send (byte cmd, byte *src, int len);
  23. int main (int argc, char *argv[], char *envp[]);
  24.  
  25. static void send (byte cmd, byte *src, int len)
  26. {
  27.   buf[0] = (byte)((len + 1) % 256);
  28.   buf[1] = (byte)((len + 1) / 256);
  29.   buf[2] = cmd;
  30.   memmove (buf+3, src, len);
  31.   if (write (fh, buf, len+3) != len+3)
  32.     {
  33.       fprintf (stderr, "Cannot write to pipe\n");
  34.       exit (2);
  35.     }
  36. }
  37.  
  38.  
  39. int main (int argc, char *argv[], char *envp[])
  40. {
  41.   byte far *unfmt, far *p;
  42.   byte cmd_line[257];
  43.   byte cwd[_MAX_PATH];
  44.   byte header[2], cmd_char, sw, output;
  45.   int i;
  46.   long n;
  47.  
  48.   setmode (1, O_BINARY);
  49.   unfmt = MAKEP (_psp, 0x80);
  50.   unfmt[unfmt[0]+1] = 0;
  51.   p = unfmt + 1;
  52.   while (WHITE (*p)) ++p;
  53.   cmd_char = 'C'; sw = 0; output = 0;
  54.   for (;;)
  55.     {
  56.       if (p[0] == '-' && p[1] == 'q' && p[2] == 0)
  57.         {
  58.           cmd_char = 'Q';
  59.           break;
  60.         }
  61.       else if (p[0] == '-' && p[1] == 'x' && WHITE (p[2]))
  62.         {
  63.           cmd_char = 'X';
  64.           p += 3;
  65.           while (WHITE (*p)) ++p;
  66.         }
  67.       else if (p[0] == '-' && p[1] == 's' && (p[2] == 'n' || p[2] == 'y')
  68.                && WHITE (p[3]))
  69.         {
  70.           sw = (byte)(p[2] == 'y' ? 'Y' : 'N');
  71.           p += 4;
  72.           while (WHITE (*p)) ++p;
  73.         }
  74.       else if (p[0] == '-' && p[1] == 'o' && WHITE (p[2]))
  75.         {
  76.           output = 'O';
  77.           p += 3;
  78.           while (WHITE (*p)) ++p;
  79.         }
  80.       else
  81.         break;
  82.     }
  83.   _fstrcpy (cmd_line, p);
  84.   fh = open ("/pipe/os2exec.em", O_RDWR | O_BINARY, S_IREAD | S_IWRITE);
  85.   if (fh < 0)
  86.     {
  87.       fprintf (stderr, "os2execd not running\n");
  88.       return (2);
  89.     }
  90.   if (cmd_char == 'Q')
  91.     send ('Q', "", 0);
  92.   else
  93.     {
  94.       if (sw != 0)
  95.         send ('S', &sw, 1);
  96.       if (output != 0)
  97.         send (output, "", 0);
  98.       getcwd (cwd, sizeof (cwd));
  99.       send ('W', cwd, strlen (cwd));
  100.       for (i = 0; envp[i] != NULL; ++i)
  101.         if (memcmp (envp[i], "COMSPEC=", 8) != 0
  102.             && memcmp (envp[i], "PROMPT=", 7) != 0
  103.             && memcmp (envp[i], "PATH=", 5) != 0)
  104.           send ('E', envp[i], strlen (envp[i]));
  105.       send (cmd_char, cmd_line, strlen (cmd_line));
  106.     }
  107.   for (;;)
  108.     {
  109.       i = read (fh, header, 2);
  110.       if (i < 0)
  111.         {
  112.           perror ("read1");
  113.           return (2);
  114.         }
  115.       if (i != 2)
  116.         {
  117.           fprintf (stderr,
  118.                    "Return code not available --- invalid message size\n");
  119.           return (2);
  120.         }
  121.       i = header[0] + 256 * header[1];
  122.       if (read (fh, buf, i) != i)
  123.         perror ("read2");
  124.       if (buf[0] == 'O')
  125.         write (1, buf+1, i-1);
  126.       else if (cmd_char == 'Q')
  127.         {
  128.           if (buf[0] != 'A')
  129.             {
  130.               fprintf (stderr, "Quit not acknowledged\n");
  131.               return (2);
  132.             }
  133.           n = 0;
  134.           break;
  135.         }
  136.       else
  137.         {
  138.           if (buf[0] != 'R')
  139.             {
  140.               fprintf (stderr, "Return code not available --- "
  141.                        "invalid message type\n");
  142.               return (2);
  143.             }
  144.           buf[i] = 0;
  145.           n = strtol (buf+1, NULL, 10);
  146.           send ('A', " ", 1);
  147.           break;
  148.         }
  149.     }
  150.   close (fh);
  151.   return ((int)n);
  152. }
  153.