home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 2 BBS / 02-BBS.zip / BTMTSRC3.ZIP / B_SPAWN.C < prev    next >
C/C++ Source or Header  |  1991-08-15  |  7KB  |  194 lines

  1. /*--------------------------------------------------------------------------*/
  2. /*                                                                          */
  3. /*                                                                          */
  4. /*      ------------         Bit-Bucket Software, Co.                       */
  5. /*      \ 10001101 /         Writers and Distributors of                    */
  6. /*       \ 011110 /          Freely Available<tm> Software.                 */
  7. /*        \ 1011 /                                                          */
  8. /*         ------                                                           */
  9. /*                                                                          */
  10. /*  (C) Copyright 1987-90, Bit Bucket Software Co., a Delaware Corporation. */
  11. /*                                                                          */
  12. /*                                                                          */
  13. /*            This module was originally written by Bob Hartman             */
  14. /*                                                                          */
  15. /*                                                                          */
  16. /*                        BinkleyTerm "Spawn" module                        */
  17. /*                                                                          */
  18. /*                                                                          */
  19. /*    For complete  details  of the licensing restrictions, please refer    */
  20. /*    to the License  agreement,  which  is published in its entirety in    */
  21. /*    the MAKEFILE and BT.C, and also contained in the file LICENSE.240.    */
  22. /*                                                                          */
  23. /*    USE  OF THIS FILE IS SUBJECT TO THE  RESTRICTIONS CONTAINED IN THE    */
  24. /*    BINKLEYTERM  LICENSING  AGREEMENT.  IF YOU DO NOT FIND THE TEXT OF    */
  25. /*    THIS  AGREEMENT IN ANY OF THE  AFOREMENTIONED FILES,  OR IF YOU DO    */
  26. /*    NOT HAVE THESE FILES,  YOU  SHOULD  IMMEDIATELY CONTACT BIT BUCKET    */
  27. /*    SOFTWARE CO.  AT ONE OF THE  ADDRESSES  LISTED BELOW.  IN NO EVENT    */
  28. /*    SHOULD YOU  PROCEED TO USE THIS FILE  WITHOUT HAVING  ACCEPTED THE    */
  29. /*    TERMS  OF  THE  BINKLEYTERM  LICENSING  AGREEMENT,  OR  SUCH OTHER    */
  30. /*    AGREEMENT AS YOU ARE ABLE TO REACH WITH BIT BUCKET SOFTWARE, CO.      */
  31. /*                                                                          */
  32. /*                                                                          */
  33. /* You can contact Bit Bucket Software Co. at any one of the following      */
  34. /* addresses:                                                               */
  35. /*                                                                          */
  36. /* Bit Bucket Software Co.        FidoNet  1:104/501, 1:132/491, 1:141/491  */
  37. /* P.O. Box 460398                AlterNet 7:491/0                          */
  38. /* Aurora, CO 80046               BBS-Net  86:2030/1                        */
  39. /*                                Internet f491.n132.z1.fidonet.org         */
  40. /*                                                                          */
  41. /* Please feel free to contact us at any time to share your comments about  */
  42. /* our software and/or licensing policies.                                  */
  43. /*                                                                          */
  44. /*--------------------------------------------------------------------------*/
  45.  
  46. #include <stdio.h>
  47. #include <time.h>
  48. #include <stdlib.h>
  49. #include <process.h>
  50. #include <string.h>
  51. #include <dos.h>
  52.  
  53. #ifdef OS_2
  54. #define  INCL_DOS
  55. #include <os2.h>
  56. #endif
  57.  
  58. #ifdef __TURBOC__
  59. #include <dir.h>
  60. #include <mem.h>
  61. #else
  62. #include <direct.h>
  63. #include <memory.h>
  64. #endif
  65.  
  66. #include "com.h"
  67. #include "xfer.h"
  68. #include "zmodem.h"
  69. #include "keybd.h"
  70. #include "sbuf.h"
  71. #include "sched.h"
  72. #include "externs.h"
  73. #include "prototyp.h"
  74.  
  75.  
  76.  
  77. extern void hold_timer(void);            /* CML */
  78. extern void restart_timer(void);         /* CML */
  79. extern void hold_comthread(void);        /* CML */
  80. extern void restart_comthread(void);    /* CML */
  81.  
  82.  
  83.  
  84. void b_spawn (cmd_str)
  85. char *cmd_str;
  86. {
  87.  
  88. #ifdef OS_2
  89.     char this_dir[80];
  90.     char *comspec;
  91.    
  92.     getcwd (this_dir,79);
  93.     hold_timer();            /* CML */
  94.     hold_comthread();        /* CML */
  95.     if (cmd_str)
  96.         system (cmd_str);
  97.     else
  98.         {
  99.         if (comspec = getenv ("COMSPEC"))
  100.             spawnlp (P_WAIT, comspec, comspec, NULL);
  101.         }
  102.  
  103.     restart_comthread();        /* CML */
  104.  
  105.     /* Re-enable ourselves -- CML's code*/
  106.     if (Cominit (port_ptr) != 0x1954)             /* FOSSIL back on */
  107.         {
  108.         printf ("\n%s\n", msgtxt[M_FOSSIL_GONE]);
  109.         exit (1);
  110.         }
  111.  
  112.     restart_timer();            /* CML */
  113.     DosSelectDisk (*this_dir - 'A' + 1);
  114.     chdir (this_dir);
  115.  
  116. #else   /* ifdef OS_2 */
  117.  
  118.    char *p, *p1;
  119.    char **envp, *env, *save, **vp;
  120.    char this_dir[80];
  121.    int ecount;
  122.    unsigned j;
  123.  
  124.    /* Save where we are */
  125.    (void) fflush (status_log);
  126.    (void) real_flush (fileno (status_log));
  127.    need_update = 0;
  128.  
  129.    (void) getcwd (this_dir, 79);
  130.  
  131.    if ((p = getenv ("COMSPEC")) == NULL)
  132.       p = "COMMAND.COM";
  133.  
  134.    if (swapdir == NULL)
  135.       {
  136.       if (cmd_str != NULL)
  137.          (void) system (cmd_str);
  138.       else
  139.          (void) spawnlp (P_WAIT, p, p, NULL);
  140.       }
  141.    else
  142.       {
  143.       /*
  144.        * The following lines are really wierd.  Basically, we need to
  145.        * pass a valid environment to DOS when we do the spawn.  Unfortunately,
  146.        * it is very likely that we have changed the environment (like when
  147.        * we alter the prompt), and therefore we have to make yet another
  148.        * copy of it, and make sure it can be paragraph aligned.  We pass the
  149.        * offset in paragraph format so it can just be added to the DS in
  150.        * order to get the actual paragraph location.
  151.        */
  152.       ecount = 0;
  153.       envp = environ;
  154.  
  155.       /* Count the chars in the environment */
  156.       for (vp = envp; *vp; ecount += (int) strlen (*vp++) + 1)
  157.          ;
  158.  
  159.       /* Add some fudge for nulls, etc */
  160.       ecount += 5;
  161.  
  162.       /* Allocate space for a copy of the environment to be para aligned */
  163.       save = env = malloc ((unsigned int) (ecount + 15));
  164.  
  165.       /* Align on a paragraph boundary - yucky, but it works */
  166.       *((unsigned int *)&env) = (*((unsigned int *)&env)+0xf) & (~0xf);
  167.  
  168.       p1 = env;
  169.  
  170.       /* Copy old environment to the new */
  171.       for (vp = envp; *vp; vp++)
  172.          p1 = strchr(strcpy(p1, *vp), '\0') + 1;
  173.       *p1++ = '\0';
  174.       *p1++ = '\0';
  175.  
  176.       /* Now actually call our code that does the swapping */
  177.       swapper (p, swapdir, (int) strlen (cmd_str), cmd_str, ((unsigned) (env)) >> 4);
  178.  
  179.       /* Free the stuff we allocated */
  180.       free (save);
  181.       }
  182.  
  183.    /* Go back to the proper directory */
  184. #ifdef __TURBOC__
  185.    setdisk ((unsigned) (this_dir[0] - 'A'));
  186. #else /* __TURBOC__ */
  187.    _dos_setdrive ((unsigned) ((int) this_dir[0] - 'A' + 1), &j);
  188. #endif
  189.    (void) chdir (this_dir);
  190.  
  191. #endif /* OS_2 */
  192.  
  193. }
  194.