home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-385-Vol-1of3.iso / k / ksh48.zip / sh / os2.c < prev    next >
C/C++ Source or Header  |  1992-09-24  |  4KB  |  189 lines

  1. #define INCL_DOS
  2. #define INCL_DOSSESMGR
  3. #define INCL_DOSERRORS
  4. #include <os2.h>
  5.  
  6. #include <sys/emx.h>
  7. #include <sys/ioctl.h>
  8. #include <fcntl.h>
  9. #include <io.h>
  10. #include <process.h>
  11. #include <errno.h>
  12. #include <malloc.h>
  13. #include <string.h>
  14.  
  15. int getegid(void)
  16. {
  17.   return 0;
  18. }
  19.  
  20. int geteuid(void)
  21. {
  22.   return 0;
  23. }
  24.  
  25. void noinherit(int fd)
  26. {
  27.   DosSetFHState(fd, OPEN_FLAGS_NOINHERIT);
  28. }
  29.  
  30. int getdup(int fd, int base)
  31. {
  32.   int newfd, type;
  33.  
  34.   for (newfd = base; newfd < _NFILES; newfd++)
  35.     if (ioctl(newfd, FGETHTYPE, &type) != -1)
  36.       continue;
  37.     else
  38.       return dup2(fd, newfd);
  39.  
  40.   return -1;
  41. }
  42.  
  43. static int isfullscreen(void)
  44. {
  45.   PTIB ptib;
  46.   PPIB ppib;
  47.  
  48.   DosGetInfoBlocks(&ptib, &ppib);
  49.   return (ppib -> pib_ultype != SSF_TYPE_WINDOWABLEVIO);
  50. }
  51.  
  52. static int newsession(int type, int mode, char *cmd, char **args, char **env)
  53. {
  54.   STARTDATA sd;
  55.   STATUSDATA st;
  56.   REQUESTDATA qr;
  57.   ULONG sid, pid, len, cnt, rc;
  58.   PVOID ptr;
  59.   BYTE prio;
  60.   static char queue[18];
  61.   static HQUEUE qid = -1;
  62.   char *ap, *ep, *p;
  63.  
  64.   for ( cnt = 1, len = 0; args[cnt] != NULL; cnt++ )
  65.     len += strlen(args[cnt]) + 1;
  66.   p = ap = alloca(len + 2);
  67.   *p = 0;
  68.   for ( cnt = 1, len = 0; args[cnt] != NULL; cnt++ )
  69.   {
  70.     if ( cnt > 1 )
  71.       *p++ = ' ';
  72.     strcpy(p, args[cnt]);
  73.     p += strlen(p);
  74.   }
  75.  
  76.   for ( cnt = 0, len = 0; env[cnt] != NULL; cnt++ )
  77.     len += strlen(env[cnt]) + 1;
  78.   p = ep = alloca(len + 2);
  79.   *p = 0;
  80.   for ( cnt = 0, len = 0; env[cnt] != NULL; cnt++ )
  81.   {
  82.     strcpy(p, env[cnt]);
  83.     p += strlen(p) + 1;
  84.   }
  85.   *p = 0;
  86.  
  87.   if ( mode == P_WAIT && qid == -1 )
  88.   {
  89.     sprintf(queue, "\\queues\\ksh%04d", getpid());
  90.     if ( DosCreateQueue(&qid, QUE_FIFO, queue) )
  91.       return -1;
  92.   }
  93.  
  94.   sd.Length = sizeof(sd);
  95.   sd.Related = (mode == P_WAIT);
  96.   sd.FgBg = FALSE;
  97.   sd.TraceOpt = FALSE;
  98.   sd.PgmTitle = NULL;
  99.   sd.PgmName = cmd;
  100.   sd.PgmInputs = (PBYTE) ap;
  101.   sd.TermQ = (mode == P_WAIT) ? (PBYTE) queue : NULL;
  102.   sd.Environment = (PBYTE) ep;
  103.   sd.InheritOpt = FALSE;
  104.   sd.SessionType = type;
  105.   sd.IconFile = NULL;
  106.   sd.PgmHandle = 0;
  107.   sd.PgmControl = 0;
  108.  
  109.   if ( DosStartSession(&sd, &sid, &pid) )
  110.     return errno = ENOEXEC, -1;
  111.  
  112.   if ( mode == P_WAIT )
  113.   {
  114.     st.Length = sizeof(st);
  115.     st.SelectInd = SET_SESSION_UNCHANGED;
  116.     st.BondInd = SET_SESSION_BOND;
  117.     DosSetSession(sid, &st);
  118.     if ( DosReadQueue(qid, &qr, &len, &ptr, 0, DCWW_WAIT, &prio, 0) )
  119.       return -1;
  120.     rc = ((PUSHORT)ptr)[1];
  121.     DosFreeMem(ptr);
  122.     exit(rc);
  123.   }
  124.   else
  125.     exit(0);
  126. }
  127.  
  128. int _execve(char *cmd, char **args, char **env)
  129. {
  130.   ULONG apptype;
  131.   char path[256], *p;
  132.   int rc;
  133.  
  134.   strcpy(path, cmd);
  135.   
  136.   for ( p = path; *p; p++ )
  137.     if ( *p == '/' )
  138.       *p = '\\';
  139.  
  140.   if ( DosQueryAppType(path, &apptype) == 0 )
  141.   {
  142.     if (apptype & FAPPTYP_DOS)
  143.       return newsession(isfullscreen() ? SSF_TYPE_VDM : SSF_TYPE_WINDOWEDVDM, 
  144.             P_NOWAIT, path, args, env);
  145.  
  146.     if ((apptype & FAPPTYP_WINDOWSREAL) || (apptype & FAPPTYP_WINDOWSPROT) )
  147.       return newsession(isfullscreen() ? SSF_TYPE_VDM : SSF_TYPE_WINDOWEDVDM, 
  148.             P_NOWAIT, path, args, env);
  149.  
  150.     if ( (apptype & FAPPTYP_EXETYPE) == FAPPTYP_WINDOWAPI )
  151.       return newsession(SSF_TYPE_PM, P_NOWAIT, path, args, env);
  152.  
  153.     if ( (apptype & FAPPTYP_EXETYPE) == FAPPTYP_NOTWINDOWCOMPAT ||
  154.          (apptype & FAPPTYP_EXETYPE) == FAPPTYP_NOTSPEC )
  155.       if ( !isfullscreen() )
  156.         return newsession(SSF_TYPE_FULLSCREEN, P_WAIT, path, args, env);
  157.   }
  158.  
  159.   if ( (rc = spawnve(P_WAIT, path, args, env)) != -1 )
  160.     exit(rc);
  161.  
  162.   return -1;
  163. }
  164.  
  165. void UnixName(char *path)
  166. {
  167.   for ( ; *path; path++ )
  168.     if ( *path == '\\' )
  169.       *path = '/';
  170. }
  171.  
  172. char *index_sep(char *path)
  173. {
  174.   char *p1 = strchr(path, '\\');
  175.   char *p2 = strchr(path, '/');
  176.   if ( !p1 ) return p2;
  177.   if ( !p2 ) return p1;
  178.   return (p1 > p2) ? p2 : p1;
  179. }
  180.  
  181. char *rindex_sep(char *path)
  182. {
  183.   char *p1 = strrchr(path, '\\');
  184.   char *p2 = strrchr(path, '/');
  185.   if ( !p1 ) return p2;
  186.   if ( !p2 ) return p1;
  187.   return (p1 > p2) ? p1 : p2;
  188. }
  189.