home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 11 Util / 11-Util.zip / hotkey01.zip / OS2EXE.ZIP / OS2EXEC.C < prev    next >
C/C++ Source or Header  |  1993-01-05  |  3KB  |  119 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;
  45.   int i;
  46.   long n;
  47.  
  48.   unfmt = MAKEP (_psp, 0x80);
  49.   unfmt[unfmt[0]+1] = 0;
  50.   p = unfmt + 1;
  51.   while (WHITE (*p)) ++p;
  52.   cmd_char = 'C';
  53.   if (p[0] == '-' && p[1] == 'q' && p[2] == 0)
  54.     cmd_char = 'Q';
  55.   else if (p[0] == '-' && p[1] == 'x' && WHITE (p[2]))
  56.     {
  57.       cmd_char = 'X';
  58.       p += 3;
  59.       while (WHITE (*p)) ++p;
  60.     }
  61.   _fstrcpy (cmd_line, p);
  62.   fh = open ("/pipe/os2exec.em", O_RDWR | O_BINARY, S_IREAD | S_IWRITE);
  63.   if (fh < 0)
  64.     {
  65.       fprintf (stderr, "os2execd not running\n");
  66.       return (2);
  67.     }
  68.   if (cmd_char == 'Q')
  69.     send ('Q', "", 0);
  70.   else
  71.     {
  72.       getcwd (cwd, sizeof (cwd));
  73.       send ('W', cwd, strlen (cwd));
  74.       for (i = 0; envp[i] != NULL; ++i)
  75.         if (memcmp (envp[i], "COMSPEC=", 8) != 0
  76.             && memcmp (envp[i], "PROMPT=", 7) != 0
  77.             && memcmp (envp[i], "PATH=", 5) != 0)
  78.           send ('E', envp[i], strlen (envp[i]));
  79.       send (cmd_char, cmd_line, strlen (cmd_line));
  80.     }
  81.   i = read (fh, header, 2);
  82.   if (i < 0)
  83.     {
  84.       perror ("read1");
  85.       return (2);
  86.     }
  87.   if (i != 2)
  88.     {
  89.       fprintf (stderr, "Return code not available --- invalid message size\n");
  90.       return (2);
  91.     }
  92.   i = header[0] + 256 * header[1];
  93.   if (read (fh, buf, i) != i)
  94.     perror ("read2");
  95.   if (cmd_char == 'Q')
  96.     {
  97.       if (buf[0] != 'A')
  98.         {
  99.           fprintf (stderr, "Quit not acknowledged\n");
  100.           return (2);
  101.         }
  102.       n = 0;
  103.     }
  104.   else
  105.     {
  106.       if (buf[0] != 'R')
  107.         {
  108.           fprintf (stderr, "Return code not available --- "
  109.                    "invalid message type\n");
  110.           return (2);
  111.         }
  112.       buf[i] = 0;
  113.       n = strtol (buf+1, NULL, 10);
  114.       send ('A', " ", 1);
  115.     }
  116.   close (fh);
  117.   return ((int)n);
  118. }
  119.