home *** CD-ROM | disk | FTP | other *** search
/ PC Extra Super CD 1998 January / PCPLUS131.iso / DJGPP / V2 / DJLSR201.ZIP / src / libc / dos / process / dosexec.old < prev    next >
Encoding:
Text File  |  1996-09-01  |  28.3 KB  |  1,081 lines

  1. /* Copyright (C) 1996 DJ Delorie, see COPYING.DJ for details */
  2. /* Copyright (C) 1995 DJ Delorie, see COPYING.DJ for details */
  3. #include <libc/stubs.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include <errno.h>
  8. #include <limits.h>
  9. #include <fcntl.h>
  10. #include <unistd.h>
  11. #include <process.h>
  12. #include <go32.h>
  13. #include <dpmi.h>
  14. #include <ctype.h>
  15. #include <sys/movedata.h>
  16. #include <libc/dosexec.h>
  17. #include <libc/unconst.h>
  18. #include <libc/dosio.h>
  19. #include <libc/farptrgs.h>
  20.  
  21. /* FIXME: this is not LFN-clean.  Win95 has a way to
  22.    pass long command lines, but we don't support it here.  */
  23. #define CMDLEN_LIMIT 125
  24.  
  25. extern char **environ;
  26.  
  27. int __dosexec_in_system = 0;
  28.  
  29. typedef struct {
  30.   unsigned short eseg;
  31.   unsigned short argoff;
  32.   unsigned short argseg;
  33.   unsigned short fcb1_off;
  34.   unsigned short fcb1_seg;
  35.   unsigned short fcb2_off;
  36.   unsigned short fcb2_seg;
  37. } Execp;
  38.  
  39. static Execp parm;
  40.  
  41. static unsigned long tbuf_ptr;
  42. static unsigned long tbuf_beg;
  43. static unsigned long tbuf_end;
  44. static unsigned long tbuf_len;
  45. #if 0
  46. static int         tbuf_selector;
  47. #endif
  48.  
  49. static int script_exec(const char *, char **, char **);
  50.  
  51. /* Allocate AMT bytes off the transfer buffer.  */
  52. static unsigned long talloc(size_t amt)
  53. {
  54.   unsigned long rv = tbuf_ptr;
  55.   tbuf_ptr += amt;
  56.   return rv;
  57. }
  58.  
  59. /* Make sure we can allocate AMT bytes off the transfer buffer
  60.    without overflowing it.  Return non-zero if we can, zero otherwise.
  61.  
  62.    WARNING: This can relocate the data already in the transfer buffer,
  63.         so all linear addresses which use it should be relative to
  64.         TBUF_BEG!  */
  65. static int check_talloc(size_t amt)
  66. {
  67.   int retval = 1;
  68.  
  69.   if (tbuf_ptr + amt > tbuf_end)
  70.   {
  71. #if 0
  72.     /* Code that reallocs the transfer buffer; currently disabled.  */
  73.     unsigned long new_tb;
  74.     unsigned long min_len = tbuf_len + amt;
  75.     unsigned long max_len = 0x10000; /* 64KB */
  76.     int old_selector = tbuf_selector;
  77.     int max_avail;
  78.     int e = errno;
  79.  
  80.     errno = E2BIG;
  81.  
  82.     /* Try to allocate new, larger DOS buffer, upto 64KB.  */
  83.     if (min_len > max_len)
  84.     {
  85.       retval = 0;
  86.       goto done;
  87.     }
  88.     while (tbuf_len <= max_len && tbuf_len < min_len)
  89.       tbuf_len *= 2;
  90.     if (tbuf_len < min_len)
  91.     {
  92.       retval = 0;
  93.       goto done;
  94.     }
  95.  
  96.     tbuf_len = (tbuf_len + 15) & 0xffff0; /* round to nearest paragraph */
  97.  
  98.     if ((new_tb =
  99.      __dpmi_allocate_dos_memory(tbuf_len/16, &max_avail)) == -1)
  100.     {
  101.       if (max_avail*16 < min_len
  102.       || (new_tb =
  103.           __dpmi_allocate_dos_memory(max_avail, &tbuf_selector)) == -1)
  104.       {
  105.     retval = 0;
  106.     goto done;
  107.       }
  108.       tbuf_len = max_avail*16;
  109.     }
  110.     else
  111.       tbuf_selector = max_avail;
  112.  
  113.     new_tb *= 16;  /* convert to linear address */
  114.     movedata (_dos_ds, tbuf_beg, _dos_ds, new_tb, tbuf_ptr - tbuf_beg);
  115.     tbuf_ptr = new_tb + tbuf_ptr - tbuf_beg;
  116.     tbuf_beg = new_tb;
  117.     tbuf_end = tbuf_beg + tbuf_len - 1;
  118.  
  119.     errno = e;
  120.  
  121.   done:
  122.     /* Assume caller will return immediately in case of
  123.        failure to reallocate, so they won't need the old data.  */
  124.     if (!retval)
  125.       tbuf_selector = 0;
  126.     if (old_selector)
  127.       __dpmi_free_dos_memory(old_selector);
  128. #else
  129.     errno = E2BIG;
  130.     retval = 0;
  131. #endif
  132.   }
  133.  
  134.   return retval;
  135. }
  136.  
  137. extern char   __PROXY[];    /* defined on crt0/crt1.c */
  138. extern size_t __PROXY_LEN;
  139.  
  140. /* Functions that call `direct_exec_tail' after they've put
  141.    some data into the transfer buffer, should set LFN parameter
  142.    to either 0 (no LFN support) or 1 (LFN supported), but NOT 2!
  143.    if LFN is 2, there is a possiblity that the contents of the
  144.    transfer buffer will be overrun!  */
  145. static int
  146. direct_exec_tail(const char *program, const char *args,
  147.          char * const envp[], const char *proxy, int lfn)
  148. {
  149.   __dpmi_regs r;
  150.   unsigned long program_la;
  151.   unsigned long arg_la;
  152.   unsigned long parm_la;
  153.   unsigned long env_la, env_e_la;
  154.   size_t proxy_len = proxy ? strlen(proxy)+1 : 0;
  155.   int seen_proxy = 0;
  156.   char arg_header[3];
  157.   char short_name[FILENAME_MAX];
  158.   const char *progname;
  159.   unsigned proglen;
  160.   int i;
  161.   unsigned long fcb1_la, fcb2_la, fname_la;
  162.   
  163.   sync();
  164.  
  165.   if (lfn == 2)        /* don't know yet */
  166.     lfn = _USE_LFN;
  167.  
  168.   /* The pathname of the executable to run.  */
  169.   proglen = strlen(program)+1;
  170.   if (!check_talloc(proglen))
  171.     return -1;
  172.   if(lfn) {
  173.     dosmemput(program, proglen, tbuf_ptr);
  174.     r.x.ax = 0x7160;            /* Truename */
  175.     r.x.cx = 1;                /* Get short name */
  176.     r.x.ds = r.x.es = tbuf_ptr / 16;
  177.     r.x.si = r.x.di = tbuf_ptr & 15;
  178.     __dpmi_int(0x21, &r);
  179.     if (r.x.flags & 1)
  180.     {
  181.       errno = __doserr_to_errno(r.x.ax);
  182.       return -1;
  183.     }
  184.     dosmemget(tbuf_ptr, FILENAME_MAX, short_name);
  185.     progname = short_name;
  186.     proglen = strlen(short_name)+1;
  187.   } else
  188.     progname = program;
  189.  
  190.   if (!check_talloc(proglen + strlen(args) + 3 + sizeof(Execp) + 32))
  191.     return -1;
  192.   program_la = talloc(proglen);
  193.   arg_la     = talloc(strlen(args)+3);
  194.   parm_la    = talloc(sizeof(Execp));
  195.  
  196.   dosmemput(progname, proglen, program_la);
  197.  
  198.   /* The command-line tail.  */
  199.   arg_header[0] = strlen(args);
  200.   arg_header[1] = '\r';
  201.   dosmemput(arg_header, 1, arg_la);
  202.   dosmemput(args, strlen(args), arg_la+1);
  203.   dosmemput(arg_header+1, 1, arg_la+1+strlen(args));
  204.  
  205.   /* The 2 FCBs.  Some programs (like XCOPY from DOS 6.x) need them.  */
  206.   fcb1_la = talloc(16);           /* allocate space for 1st FCB */
  207.   fname_la = arg_la + 1;       /* first character of command tail */
  208.   r.x.ax = 0x2901;           /* AL = 1 means skip leading separators */
  209.   r.x.ds = fname_la / 16;      /* pointer to 1st cmd argument */
  210.   r.x.si = fname_la & 15;
  211.   r.x.es = fcb1_la / 16;       /* pointer to FCB buffer */
  212.   r.x.di = fcb1_la & 15;
  213.   __dpmi_int (0x21, &r);
  214.  
  215.   /* We cannot be sure that Int 21h/AX=2901h parsed the entire
  216.      first command-line argument (it might not be a filename
  217.      at all!).  We need to get to the next command-line arg
  218.      before calling 2901 again.  2901 returns the pointer to
  219.      first unparsed character in DS:SI.
  220.  
  221.      Note that, in case there is no second command-line argument,
  222.      the following loop is terminated by the trailing CR which
  223.      ends the command-line tail.  */
  224.   for (_farsetsel(_dos_ds), fname_la = ((unsigned)r.x.ds) * 16 + r.x.si;
  225.        !isspace(_farnspeekb(fname_la));
  226.        fname_la++)
  227.     ;
  228.  
  229.   fcb2_la = talloc(16);
  230.   r.x.ax = 0x2901;
  231.   r.x.ds = fname_la / 16;      /* begin parsing 2nd arg from here */
  232.   r.x.si = fname_la & 15;
  233.   r.x.es = fcb2_la / 16;
  234.   r.x.di = fcb2_la & 15;
  235.   __dpmi_int (0x21, &r);
  236.  
  237.   /* The environment must be on a segment boundary, so get
  238.      to the first location in the transfer buffer whose
  239.      linear address is divisable by 16.  */
  240.   do {
  241.     env_la = talloc(1);
  242.   } while (env_la & 15);
  243.   talloc(-1);
  244.  
  245. #if 0
  246.   /* Convert to relative, since `check_talloc' may relocate.  */
  247.   arg_la  -= tbuf_beg;
  248.   env_la  -= tbuf_beg;
  249.   fcb1_la -= tbuf_beg;
  250.   fcb2_la -= tbuf_beg;
  251.   parm_la -= tbuf_beg;
  252.   program_la -= tbuf_beg;
  253. #endif
  254.   if (!check_talloc(0))
  255.     return -1;
  256.  
  257.   /* The environment.  Replace the !proxy variable, if there is
  258.      one (for nested programs) if we are called from `system',
  259.      or skip it, if we are called from `spawnXX'.  */
  260.   for (i=0; envp[i]; i++)
  261.   {
  262.     const char *ep = envp[i];
  263.     size_t env_len = strlen(ep)+1;
  264.  
  265.     if (strncmp(ep, __PROXY, __PROXY_LEN) == 0 && ep[__PROXY_LEN] == '=')
  266.     {
  267.       seen_proxy = 1;
  268.       if (proxy)
  269.       {
  270.     ep = proxy;
  271.     env_len = proxy_len;
  272.       }
  273.       else
  274.     continue;
  275.     }
  276.     if (!check_talloc(env_len))
  277.       return -1;
  278.     env_e_la = talloc(env_len);
  279.     dosmemput(ep, env_len, env_e_la);
  280.   }
  281.  
  282.   /* If no !proxy variable was found, create one.  */
  283.   if (proxy && !seen_proxy)
  284.   {
  285.     if (!check_talloc(proxy_len))
  286.       return -1;
  287.     env_e_la = talloc(proxy_len);
  288.     dosmemput(proxy, proxy_len, env_e_la);
  289.   }
  290.  
  291.   /* Terminate by an extra NULL char.  */
  292.   arg_header[0] = 0;
  293.  
  294.   /* The name of the program that owns the environment.  */
  295.   arg_header[1] = 1;    /* the number of strings (1, little-endian) */
  296.   arg_header[2] = 0;
  297.   if (!check_talloc(3 + proglen))
  298.     return -1;
  299.   dosmemput(arg_header, 3, talloc(3));
  300.   env_e_la = talloc(proglen);
  301.   dosmemput(progname, proglen, env_e_la);
  302.  
  303.   /* Prepare the parameter block and call Int 21h/AX=4B00h.  */
  304. #if 0
  305.   arg_la  += tbuf_beg;
  306.   env_la  += tbuf_beg;
  307.   fcb1_la += tbuf_beg;
  308.   fcb2_la += tbuf_beg;
  309.   parm_la += tbuf_beg;
  310.   program_la += tbuf_beg;
  311. #endif
  312.   parm.eseg     = env_la / 16;
  313.   parm.argseg    = arg_la / 16;
  314.   parm.argoff    = arg_la & 15;
  315.   parm.fcb1_seg = fcb1_la / 16;
  316.   parm.fcb1_off = fcb1_la & 15;
  317.   parm.fcb2_seg = fcb2_la / 16;
  318.   parm.fcb2_off = fcb2_la & 15;
  319.   dosmemput(&parm, sizeof(parm), parm_la);
  320.  
  321.   r.x.ax = 0x4b00;
  322.   r.x.ds = program_la / 16;
  323.   r.x.dx = program_la & 15;
  324.   r.x.es = parm_la / 16;
  325.   r.x.bx = parm_la & 15;
  326.   __dpmi_int(0x21, &r);
  327. #if 0
  328.   if (tbuf_selector)
  329.     __dpmi_free_dos_memory (tbuf_selector);
  330.   tbuf_selector = 0;
  331. #endif
  332.   if (r.x.flags & 1)
  333.   {
  334.     errno = __doserr_to_errno(r.x.ax);
  335.     return -1;
  336.   }
  337.   
  338.   r.h.ah = 0x4d;
  339.   __dpmi_int(0x21, &r);
  340.   
  341.   if (r.x.flags & 1)
  342.   {
  343.     errno = __doserr_to_errno(r.x.ax);
  344.     return -1;
  345.   }
  346.  
  347.   /* AH holds the ``system exit code'' which is non-zero if the
  348.      child was aborted by Ctrl-C, or Critical Device error (also
  349.      if the child installs itself as a TSR).  */
  350.   if (r.h.ah && r.h.ah != 3) /* 3 means it exited as TSR (is it ``normal''?) */
  351.     errno = EINTR;    /* what else can we put in `errno'? */
  352.   return r.h.al;    /* AL holds the child exit code */
  353. }
  354.  
  355. int
  356. _dos_exec(const char *program, const char *args, char * const envp[])
  357. {
  358.   tbuf_beg = tbuf_ptr = __tb;
  359.   tbuf_len = _go32_info_block.size_of_transfer_buffer;
  360.   tbuf_end = tbuf_beg + tbuf_len - 1;
  361.   return direct_exec_tail(program, args, envp, 0, 2);
  362. }
  363.  
  364. static char GO32_V2_STRING[] = "go32-v2.exe";
  365. static char GO32_STRING[]    = "go32.exe";
  366.  
  367. /* A list of known shells which require we DON'T quote command
  368.    lines that are passed to them with the /c or -c switch.  */
  369. static const char *shell_brokets[] = {
  370.   "COMMAND.COM",
  371.   "4DOS.COM",
  372.   "NDOS.COM",
  373.   0
  374. };
  375.  
  376. /* A list of Unix-like shells and other non-DJGPP programs
  377.    which treat single quote specially.  */
  378. static const char *unix_shells[] = {
  379.   "SH.EXE",
  380.   "SH16.EXE",
  381.   "SH32.EXE",
  382.   "TCSH.EXE",
  383.   "BASH.EXE",
  384.   0
  385. };
  386.  
  387. static int direct_exec(const char *program, char **argv, char **envp)
  388. {
  389.   int i, arglen;
  390.   char *args, *argp;
  391.   int need_quote = !__dosexec_in_system;
  392.   int unescape_quote = __dosexec_in_system;
  393.   const char *ptail = program;
  394.  
  395.   {
  396.     const char *p = ptail;
  397.  
  398.     while (*p)
  399.     {
  400.       if (*p == '/' || *p == ':' || *p == '\\')
  401.     ptail = p + 1;
  402.       p++;
  403.     }
  404.   }
  405.  
  406.  
  407.   /* PROGRAM can be a shell which expects a single argument
  408.      (beyond the /c or -c switch) that is the entire command
  409.      line.  With some shells, we must NOT quote that command
  410.      line, because that will confuse the shell.
  411.  
  412.      The hard problem is to know when PROGRAM names a shell
  413.      that doesn't like its command line quoted...  */
  414.  
  415.   if (need_quote && !strcmp (argv[1], "/c") && argv[2] && !argv[3])
  416.   {
  417.     for (i = 0; shell_brokets[i]; i++)
  418.     {
  419.       if (!stricmp (ptail, shell_brokets[i]))
  420.       {
  421.     need_quote = 0;
  422.     break;
  423.       }
  424.     }
  425.   }
  426.  
  427.   if (unescape_quote)
  428.   {
  429.     for (i = 0; unix_shells[i]; i++)
  430.     {
  431.       if (!stricmp (ptail, unix_shells[i]))
  432.       {
  433.     unescape_quote = 0;
  434.     break;
  435.       }
  436.     }
  437.   }
  438.  
  439.   arglen = 0;
  440.   for (i=1; argv[i]; i++)
  441.     arglen += 2*strlen(argv[i]) + 1 + 2;
  442.  
  443.   args = (char *)alloca(arglen+1);
  444.   argp = args;
  445.   for (i=1; argv[i]; i++)
  446.   {
  447.     int quoted = 0;
  448.     const char *p = argv[i];
  449.  
  450.     if (argp - args > CMDLEN_LIMIT)
  451.       break;
  452.     *argp++ = ' ';
  453.     /* If invoked by `spawnXX' or `execXX' functions, we need to
  454.        quote arguments which include whitespace, so they end up
  455.        as a single argument on the child side.
  456.        We will invoke PROGRAM directly by DOS Exec function (not
  457.        through COMMAND.COM), therefore no need to quote characters
  458.        special only to COMMAND.COM.
  459.        We also assume that DJGPP programs aren't invoked through
  460.        here, so a single quote `\'' is also not special.  The only
  461.        programs other than DJGPP that treat a single quote specially
  462.        are Unix-like shells, but whoever uses them should know to
  463.        escape the quotes himself.  */
  464.     if (need_quote && strpbrk(p, " \t") != 0)
  465.     {
  466.       *argp++ = '"';
  467.       quoted = 1;
  468.     }
  469.     while (*p)
  470.     {
  471.       if (argp - args > CMDLEN_LIMIT)
  472.         break;
  473.       if (*p == '"' && (quoted || need_quote))
  474.     *argp++ = '\\';
  475.       /* Most non-DJGPP programs don't treat `\'' specially,
  476.      but our `system' requires we always escape it, so
  477.      we should undo the quoting here.  */
  478.       else if (*p == '\\' && p[1] == '\'' && unescape_quote)
  479.     p++;
  480.       *argp++ = *p++;
  481.     }
  482.     if (quoted && argp - args <= CMDLEN_LIMIT)
  483.       *argp++ = '"';
  484.   }
  485.   *argp = 0;
  486.  
  487.   if (argp - args > CMDLEN_LIMIT)
  488.     errno = E2BIG;
  489.   
  490.   tbuf_beg = tbuf_ptr = __tb;
  491.   tbuf_len = _go32_info_block.size_of_transfer_buffer;
  492.   tbuf_end = tbuf_beg + tbuf_len - 1;
  493.   return direct_exec_tail(program, args, envp, 0, 2);
  494. }
  495.  
  496. typedef struct {
  497.   char magic[16];
  498.   int struct_length;
  499.   char go32[16];
  500. } StubInfo;
  501. #define STUB_INFO_MAGIC "StubInfoMagic!!"
  502.  
  503. static int go32_exec(const char *program, char **argv, char **envp)
  504. {
  505.   char *save_argv0;
  506.   int is_stubbed = 0, is_coff = 0;
  507.   int found_si = 0;
  508.   StubInfo si;
  509.   unsigned short header[3];
  510.   int pf, i;
  511.   char *go32, *sip=0;
  512.   char rpath[FILENAME_MAX];
  513.   int stub_offset, argc=0;
  514.  
  515.   int v2_0 = 0;
  516.   int si_la=0, si_off=0, rm_off, argv_off;
  517.   char cmdline[CMDLEN_LIMIT+2], *cmdp = cmdline;
  518.   char *pcmd = cmdline, *pproxy = 0, *proxy_cmdline = 0;
  519.   int retval;
  520.   int lfn = 2;    /* means don't know yet */
  521.  
  522.   pf = open(program, O_RDONLY|O_BINARY);
  523.  
  524.   read(pf, header, sizeof(header));
  525.   if (header[0] == 0x010b || header[0] == 0x014c)
  526.   {
  527.     unsigned char firstbytes[1];
  528.     unsigned long coffhdr[40];
  529.  
  530.     /* Seems to be an unstubbed COFF.  See what the first opcode
  531.        is to determine if it's v1.x or v2 COFF (or an impostor).
  532.  
  533.        FIXME: the code here assumes that any COFF that's not a V1
  534.        can only be V2.  What about other compilers that use COFF?  */
  535.     is_coff = 1;
  536.     if (lseek(pf, 2, 1) < 0
  537.     || read(pf, coffhdr, sizeof(coffhdr)) != sizeof(coffhdr)
  538.     || lseek(pf, coffhdr[10 + 5], 0) < 0
  539.     || read(pf, firstbytes, 1) != 1) /* scnptr */
  540.       is_coff = 0;    /* "Aha! An impostor!" (The Adventure game) */
  541.     else if (firstbytes[0] != 0xa3) /* opcode of movl %eax, 0x12345678 (V1) */
  542.       v2_0 = 1;
  543.   }
  544.   else if (header[0] == 0x5a4d)    /* "MZ" */
  545.   {
  546.     int header_offset = (long)header[2]*512L;
  547.     is_stubbed = 1;
  548.     if (header[1])
  549.       header_offset += (long)header[1] - 512L;
  550.     lseek(pf, 512, 0);
  551.     read(pf, cmdline, 8);
  552.     cmdline[8] = 0;
  553.     if (strcmp(cmdline, "go32stub") == 0)
  554.     {
  555.       v2_0 = 1;
  556.       is_coff = 1;
  557.     }
  558.     else
  559.     {
  560.       lseek(pf, header_offset - 4, 0);
  561.       read(pf, &stub_offset, 4);
  562.       header[0] = 0;
  563.       read(pf, header, sizeof(header));
  564.       if (header[0] == 0x010b)
  565.     is_coff = 1;
  566.       if (header[0] == 0x014c)
  567.     is_coff = 1;
  568.       lseek(pf, stub_offset, 0);
  569.       read(pf, &si, sizeof(si));
  570.       if (memcmp(STUB_INFO_MAGIC, si.magic, 16) == 0)
  571.     found_si = 1;
  572.     }
  573.   }
  574.   else if (header[0] == 0x2123)    /* "#!" */
  575.   {
  576.     close(pf);
  577.     return script_exec(program, argv, envp);
  578.   }
  579.  
  580.   /* Non-DJGPP programs cannot be run by !proxy.  */
  581.   if (!is_coff)
  582.   {
  583.     close(pf);
  584.     return direct_exec(program, argv, envp);
  585.   }
  586.  
  587.   if (found_si)
  588.     go32 = si.go32;
  589.   else if (v2_0 && !is_stubbed)
  590.     go32 = GO32_V2_STRING;
  591.   else
  592.     go32 = GO32_STRING;
  593.  
  594.   if (v2_0 && is_stubbed)
  595.   {
  596.     strcpy(rpath, program);
  597.   }
  598.   else
  599.   {
  600.     int e = errno;
  601.     if (!__dosexec_find_on_path(go32, envp, rpath))
  602.     {
  603.       close(pf);
  604.       errno = e;
  605.       return direct_exec(program, argv, envp); /* give up and just run it */
  606.     }
  607.  
  608.     if (found_si)
  609.     {  
  610.       lseek(pf, stub_offset, 0);
  611.       sip = (char *)alloca(si.struct_length);
  612.       read(pf, sip, si.struct_length);
  613.     }
  614.   }
  615.   close(pf);
  616.  
  617.   /* V2.0 programs invoked by `system' must be run via
  618.      `direct_exec', because otherwise the command-line arguments
  619.      won't be globbed correctly by the child.  Only v2.01 and
  620.      later knows how to get long command lines from `system' AND
  621.      glob them correctly.  But we don't want to check with which
  622.      version was the child compiled, so we need to create both the
  623.      usual DOS command line and the !proxy one (which will be put
  624.      into the environment).  Sigh...  */
  625.   save_argv0 = argv[0];
  626.   argv[0] = unconst(program, char *); /* since that's where we really found it */
  627.   /* Construct the DOS command tail */
  628.   for (argc=0; argv[argc]; argc++);
  629.  
  630.   if (__dosexec_in_system)
  631.   {
  632.     /* If PROGRAM is an un-stubbed COFF, it must be passed in the
  633.        command tail as well, since we call GO32 to run it.  */
  634.     for (i = (is_stubbed ? 1 : 0); i < argc; i++)
  635.     {
  636.       const char *p = argv[i];
  637.       if (cmdp - cmdline > CMDLEN_LIMIT)
  638.     break;
  639.       *cmdp++ = ' ';
  640.       while (*p)
  641.       {
  642.     if (cmdp - cmdline > CMDLEN_LIMIT)
  643.       break;
  644.     *cmdp++ = *p++;
  645.       }
  646.     }
  647.     *cmdp = '\0';
  648.   }
  649.  
  650.   lfn = _USE_LFN;
  651.  
  652.   /* Can't call any functions that use the transfer buffer beyond
  653.      this point: they will overwrite the data already in __tb.  */
  654.   tbuf_beg = tbuf_ptr = __tb;
  655.   tbuf_len = _go32_info_block.size_of_transfer_buffer;
  656.   tbuf_end = tbuf_ptr + tbuf_len - 1;
  657.  
  658.   /* If called from `system' and we have a command line shorter
  659.      than the DOS limit, we don't need to use !proxy at all.  */
  660.   if (!__dosexec_in_system || cmdp - cmdline > CMDLEN_LIMIT)
  661.   {
  662.     if (!check_talloc(found_si ?
  663.               si.struct_length : 0
  664.               + (argc+1)*sizeof(short)))
  665.     {
  666.       argv[0] = save_argv0;
  667.       return -1;
  668.     }
  669.     if (found_si)
  670.     {
  671.       si_la = talloc(si.struct_length);
  672.       si_off = si_la - tbuf_beg;
  673.       dosmemput(sip, si.struct_length, si_la);
  674.     }
  675.  
  676.     rm_off = argv_off = talloc((argc+1) * sizeof(short)) - tbuf_beg;
  677. #if 0
  678.     /* `alloca' could be dangerous with long command lines.  We
  679.        will instead move the offsets one by one with `_farpokew'.  */
  680.     rm_argv = (short *)alloca((argc+1) * sizeof(short));
  681. #endif
  682.  
  683.     for (i=0; i<argc; i++)
  684.     {
  685.       char *pargv = argv[i];
  686.       int sl = strlen(pargv) + 1;
  687.       unsigned long q;
  688.  
  689.       if (check_talloc(sl))
  690.       {
  691.     q = talloc(sl);
  692.     dosmemput(pargv, sl, q);
  693.     _farpokew(_dos_ds, tbuf_beg + argv_off, (q - tbuf_beg) & 0xffff);
  694.     argv_off += sizeof(short);
  695.       }
  696.       else    /* not enough space to pass args */
  697.       {
  698.     argv[0] = save_argv0;
  699.     return -1;
  700.       }
  701.     }
  702.  
  703.     _farpokew (_dos_ds, tbuf_beg + argv_off, 0);
  704.     argv_off += sizeof(short);
  705.  
  706.     argv[0] = save_argv0;
  707.     /* Environment variables are all malloced.  */
  708.     proxy_cmdline = (char *)malloc (34);
  709.     if (!proxy_cmdline)
  710.       return -1;
  711.     
  712.     sprintf(proxy_cmdline, "%s=%04x %04x %04x %04x %04x",
  713.          __PROXY, argc,
  714.         (unsigned)(tbuf_beg >> 4), rm_off & 0xffff,
  715.         (unsigned)(tbuf_beg >> 4), si_off & 0xffff);
  716.     if (!found_si)
  717.       proxy_cmdline[22] = 0; /* remove stubinfo information */
  718.  
  719.     if (__dosexec_in_system && v2_0)
  720.       pproxy = proxy_cmdline;
  721.     else
  722.     {
  723.       /* `proxy_cmdline looks like an environment variable " !proxy=value".
  724.          This is used as the REAL command line specification by 2.01
  725.      and later executables when called by `system'.  But if that's
  726.      not the case, we need a blank instead of the `='.  */
  727.       proxy_cmdline[__PROXY_LEN] = ' ';
  728.       pcmd = proxy_cmdline;
  729.     }
  730.   }
  731.   else
  732.     argv[0] = save_argv0;
  733.  
  734.   retval = direct_exec_tail(rpath, pcmd, envp, pproxy, lfn);
  735.   if (proxy_cmdline)
  736.     free(proxy_cmdline);
  737.   return retval;
  738. }
  739.  
  740. int
  741. __dosexec_command_exec(const char *program, char **argv, char **envp)
  742. {
  743.   const char *comspec=0;
  744.   char *cmdline;
  745.   int cmdlen;
  746.   int i;
  747.   int was_quoted = 0;    /* was the program name quoted? */
  748.  
  749.   /* Add spare space for possible quote characters.  */
  750.   cmdlen = strlen(program) + 4 + 2;
  751.   for (i=0; argv[i]; i++)
  752.     cmdlen += 2*strlen(argv[i]) + 1;
  753.   cmdline = (char *)alloca(cmdlen);
  754.  
  755.   /* FIXME: is this LFN-clean?  What special characters can
  756.      the program name have and how should they be quoted?  */
  757.   strcpy(cmdline, "/c ");
  758.   if (strchr(program, ' ') || strchr(program, '\t'))
  759.   {
  760.     was_quoted = 1;
  761.     cmdline[3] = '"';
  762.   }
  763.   for (i = 0; program[i] > ' '; i++)
  764.   {
  765.     /* COMMAND.COM cannot grok program names with forward slashes.  */
  766.     if (program[i] == '/')
  767.       cmdline[i+3+was_quoted] = '\\';
  768.     else
  769.       cmdline[i+3+was_quoted] = program[i];
  770.   }
  771.   for (; program[i]; i++)
  772.     cmdline[i+3+was_quoted] = program[i];
  773.   if (was_quoted)
  774.   {
  775.     cmdline[i+3+was_quoted] = '"';
  776.     i++;
  777.   }
  778.   cmdline[i+3+was_quoted] = 0;
  779.   for (i=1; argv[i]; i++)
  780.   {
  781.     strcat(cmdline, " ");
  782.     /* If called by `spawnXX' or `execXX' functions, must quote
  783.        arguments that have embedded whitespace or characters which
  784.        are special to COMMAND.COM and its ilk.  We don't quote all
  785.        the arguments so the command line won't grow larger than
  786.        the 126-char limit, if it doesn't have to.  */
  787.     if (!__dosexec_in_system && strpbrk(argv[i], " \t<>|'\"%") != 0)
  788.     {
  789.       char *d = cmdline + strlen(cmdline);
  790.       char *s = argv[i];
  791.       /* COMMAND.COM doesn't understand escaped quotes, so we must
  792.      insert additional quotes around redirection characters if
  793.      it would seem to COMMAND.COM we're outside of quoted part.
  794.      This variable keeps track of whether we are in- or outside
  795.      quotes as far as COMMAND.COM is concerned.  */
  796.       int  outside_quote = 0;
  797.  
  798.       *d++ = '"';
  799.       while (*s)
  800.       {
  801.     if (*s == '"')
  802.     {
  803.       outside_quote = !outside_quote;
  804.       *d++ = '\\';
  805.     }
  806.     else if (outside_quote && (*s == '|' || *s == '<' || *s == '>'))
  807.     {
  808.       *d++ = '"';
  809.       *d++ = *s++;
  810.       *d++ = '"';
  811.       continue;
  812.     }
  813.     else if (*s == '%')
  814.       *d++ = '%';
  815.     *d++ = *s++;
  816.       }
  817.       *d++ = '"';
  818.       *d++ = '\0';
  819.     }
  820.     else
  821.       strcat(cmdline, argv[i]);
  822.   }
  823.   for (i=0; envp[i]; i++)
  824.     if (strncmp(envp[i], "COMSPEC=", 8) == 0)
  825.       comspec = envp[i]+8;
  826.   if (!comspec)
  827.     for (i=0; environ[i]; i++)
  828.       if (strncmp(environ[i], "COMSPEC=", 8) == 0)
  829.         comspec = environ[i]+8;
  830.   if (!comspec)
  831.     comspec = "c:\\command.com";
  832.  
  833.   /* FIXME: 126-char limit below isn't LFN-clean.  */
  834.   if (strlen(cmdline) > CMDLEN_LIMIT + 1)
  835.   {
  836.     cmdline[CMDLEN_LIMIT+1] = '\0';
  837.     errno = E2BIG;
  838.   }
  839.  
  840.   tbuf_beg = tbuf_ptr = __tb;
  841.   tbuf_len = _go32_info_block.size_of_transfer_buffer;
  842.   tbuf_end = tbuf_ptr + tbuf_len - 1;
  843.   i = direct_exec_tail(comspec, cmdline, envp, 0, 2);
  844.   return i;
  845. }
  846.  
  847. static int script_exec(const char *program, char **argv, char **envp)
  848. {
  849.   char line[130], interp[FILENAME_MAX], iargs[130];
  850.   FILE *f;
  851.   char **newargs;
  852.   int i, hasargs=0;
  853.  
  854.   f = fopen(program, "rt");
  855.   if (!f)
  856.   {
  857.     errno = ENOENT;
  858.     return -1;
  859.   }
  860.   fgets(line, sizeof(line), f);
  861.   fclose(f);
  862.  
  863.   /* Paranoia: is this at all a text file?  */
  864.   for (i=0; i < sizeof(line)-1 && line[i] != '\0'; i++)
  865.     if (line[i] < 7 && line[i] >= 0)
  866.       return go32_exec(program, argv, envp);
  867.  
  868.   iargs[0] = 0;
  869.   interp[0] = 0;
  870.   sscanf(line, "#! %s %[^\n]", interp, iargs);
  871.  
  872.   /* If no interpreter, invoke the default shell in $COMSPEC.  */
  873.   if (interp[0] == 0)
  874.     return __dosexec_command_exec(program, argv, envp); /* it couldn't be .exe or .com if here */
  875.   if (iargs[0])
  876.     hasargs=1;
  877.  
  878.   for (i=0; argv[i]; i++);
  879.   newargs = (char **)alloca((i+2+hasargs)*sizeof(char *));
  880.   for (i=0; argv[i]; i++)
  881.     newargs[i+1+hasargs] = unconst(argv[i], char *);
  882.   newargs[i+1+hasargs] = 0;
  883.   newargs[0] = unconst(argv[0], char *);
  884.   if (hasargs)
  885.     newargs[1] = iargs;
  886.  
  887.   i = spawnvpe(P_WAIT, interp, newargs, envp);
  888.   return i;
  889. }
  890.  
  891. /* Note: the following list is not supposed to mention *every*
  892.    possible extension of an executable file.  It only mentions
  893.    those extensions that can be *omitted* when you invoke the
  894.    executable from one of the shells used on MSDOS.  */
  895. static struct {
  896.   const char *extension;
  897.   int (*interp)(const char *, char **, char **);
  898. } interpreters[] = {
  899.   { ".com", direct_exec },
  900.   { ".exe", go32_exec },
  901.   { ".bat", __dosexec_command_exec },
  902.   { ".btm", __dosexec_command_exec },
  903.   { ".sh",  script_exec },  /* for compatibility with ms_sh */
  904.   { ".ksh", script_exec },
  905.   { "",     go32_exec },
  906.   { 0,      script_exec },  /* every extension not mentioned above calls it */
  907.   { 0,      0 },
  908. };
  909.  
  910. /* This is the index into the above array of the interpreter
  911.    which is called when the program filename has no extension.  */
  912. #define INTERP_NO_EXT (sizeof(interpreters)/sizeof(interpreters[0]) - 3)
  913.  
  914. /*-------------------------------------------------*/
  915.  
  916. char *
  917. __dosexec_find_on_path(const char *program, char *envp[], char *buf)
  918. {
  919.   char *pp, *rp, *pe;
  920.   const char *ptr;
  921.   int i, hasdot=0, haspath=0;
  922.   int tried_dot = 0;
  923.   int e = errno, blen = strlen(program);
  924.  
  925.   if (blen > FILENAME_MAX - 1)
  926.   {
  927.     errno = ENAMETOOLONG;
  928.     return 0;
  929.   }
  930.   strncpy(buf, program, blen + 1);
  931.   rp = buf + blen;
  932.  
  933.   for (ptr=program; *ptr; ptr++)
  934.   {
  935.     if (*ptr == '.')
  936.       hasdot = 1;
  937.     if (*ptr == '/' || *ptr == '\\' || *ptr == ':')
  938.     {
  939.       haspath = 1;
  940.       hasdot = 0;
  941.     }
  942.   }
  943.  
  944.   if (hasdot)
  945.   {
  946.     if (access(buf, 0) == 0 && access(buf, D_OK))
  947.     {
  948.       errno = e;
  949.       return buf;
  950.     }
  951.   }
  952.   else
  953.     for (i=0; interpreters[i].extension; i++)
  954.     {
  955.       strcpy(rp, interpreters[i].extension);
  956.       if (access(buf, 0) == 0 && access(buf, D_OK))
  957.       {
  958.     /* If some of the `access' calls failed, `errno' will hold
  959.        the reason for the failure which is irrelevant to the
  960.        caller (we *did* find the execuatble).  Restore the value
  961.        `errno' had when we were called.  */
  962.     errno = e;
  963.     return buf;
  964.       }
  965.     }
  966.  
  967.   if (haspath)
  968.     return 0;
  969.   *rp = 0;
  970.  
  971.   pp = 0;
  972.   for (i=0; envp[i]; i++)
  973.     if (strncmp(envp[i], "PATH=", 5) == 0)
  974.       pp = envp[i] + 5;
  975.   if (pp == 0)
  976.     return 0;
  977.  
  978.   while (1)
  979.   {
  980.     if (!tried_dot)
  981.     {
  982.       rp = buf;
  983.       pe = pp;
  984.       tried_dot = 1;
  985.     }
  986.     else
  987.     {
  988.       rp = buf;
  989.       for (pe = pp; *pe && *pe != ';'; pe++)
  990.         *rp++ = *pe;
  991.       pp = pe+1;
  992.       if (rp > buf && rp[-1] != '/' && rp[-1] != '\\' && rp[-1] != ':')
  993.         *rp++ = '/';
  994.     }
  995.     for (ptr = program; *ptr; ptr++)
  996.       *rp++ = *ptr;
  997.     *rp = 0;
  998.     
  999.     if (hasdot)
  1000.     {
  1001.       if (access(buf, 0) == 0 && access(buf, D_OK))
  1002.       {
  1003.     errno = e;
  1004.     return buf;
  1005.       }
  1006.     }
  1007.     else
  1008.     {
  1009.       for (i=0; interpreters[i].extension; i++)
  1010.       {
  1011.         strcpy(rp, interpreters[i].extension);
  1012.         if (access(buf, 0) == 0 && access(buf, D_OK))
  1013.     {
  1014.       errno = e;
  1015.           return buf;
  1016.     }
  1017.       }
  1018.     }
  1019.     if (*pe == 0)
  1020.       return 0;
  1021.   }
  1022. }
  1023.  
  1024. int __spawnve(int mode, const char *path, char *const argv[], char *const envp[])
  1025. {
  1026.   /* This is the one that does the work! */
  1027.   union { char *const *x; char **p; } u;
  1028.   int i = -1;
  1029.   char **argvp;
  1030.   char **envpp;
  1031.   char rpath[FILENAME_MAX], *rp, *rd=0;
  1032.   int e = errno;
  1033.  
  1034.   if (path == 0 || argv[0] == 0)
  1035.   {
  1036.     errno = EINVAL;
  1037.     return -1;
  1038.   }
  1039.   if (strlen(path) > FILENAME_MAX - 1)
  1040.   {
  1041.     errno = ENAMETOOLONG;
  1042.     return -1;
  1043.   }
  1044.  
  1045.   u.x = argv; argvp = u.p;
  1046.   u.x = envp; envpp = u.p;
  1047.  
  1048.   fflush(stdout); /* just in case */
  1049.   for (rp=rpath; *path; *rp++ = *path++)
  1050.   {
  1051.     if (*path == '.')
  1052.       rd = rp;
  1053.     if (*path == '\\' || *path == '/')
  1054.       rd = 0;
  1055.   }
  1056.   *rp = 0;
  1057.   if (rd)
  1058.   {
  1059.     for (i=0; interpreters[i].extension; i++)
  1060.       if (stricmp(rd, interpreters[i].extension) == 0)
  1061.         break;
  1062.   }
  1063.   while (access(rpath, 0) && access(rpath, D_OK))
  1064.   {
  1065.     i++;
  1066.     if (interpreters[i].extension == 0 || rd)
  1067.     {
  1068.       errno = ENOENT;
  1069.       return -1;
  1070.     }
  1071.     strcpy(rp, interpreters[i].extension);
  1072.   }
  1073.   errno = e;
  1074.   if (i == -1)
  1075.     i = INTERP_NO_EXT;
  1076.   i = interpreters[i].interp(rpath, argvp, envpp);
  1077.   if (mode == P_OVERLAY)
  1078.     exit(i);
  1079.   return i;
  1080. }
  1081.