home *** CD-ROM | disk | FTP | other *** search
/ Monster Media 1994 #1 / monster.zip / monster / PROG_GEN / MAP_S1.ZIP / NET_ENV.CPP < prev    next >
C/C++ Source or Header  |  1994-01-31  |  7KB  |  256 lines

  1. // Mofify DOS Environment
  2. #include <dos.h>
  3. #include <alloc.h>
  4. #include <stdio.h>
  5.  
  6. #include <nwcalls.h>
  7.  
  8. #undef   MK_FP
  9. #define  MK_FP(seg, ofs)  ((void far *) (((unsigned long)(seg) << 16) | (unsigned)(ofs)))
  10. #undef   FP_OFF
  11. #define  FP_OFF(fp)       ((unsigned)((unsigned long)(fp)))
  12. #undef   FP_SEG
  13. #define  FP_SEG(fp)       ((unsigned)((unsigned long)(fp) >> 16))
  14.  
  15. /*
  16.  *   Mstr_FindEnvironment:
  17.  *     Scans for the "Master" Environment area, and returns
  18.  *     a pointer to it, and the size of the environment.
  19. */
  20. void Mstr_FindEnvironment ( char far **NetxEnv , unsigned *EnvSize )
  21. {
  22.  
  23.  
  24.    int envSeg;
  25.  
  26.    asm {
  27.       push    si
  28.       push    di
  29.       mov     ah,0xba
  30.       int     0x21
  31.       mov     ax,dx
  32.       mov     envSeg, ax
  33.       pop     di
  34.       pop     si
  35.       }
  36.  
  37.         *EnvSize = *(WORD far *)MK_FP(envSeg - 1, 3);
  38.  
  39.       /* Only for DOS 2.00 & greater, size is one off */
  40.  
  41.     (*EnvSize)--;
  42.     *EnvSize <<= 4;
  43.  
  44.     *NetxEnv = (char far *)MK_FP( envSeg, 0) ;
  45.  
  46. }
  47. ////////////////////////////////////////////////////////////////////
  48. ///  The following section is the original Mstr_FindEnvironment ////
  49. ///////////  It locates the COMMAND.COM environment  ///////////////
  50.  
  51. /*
  52.     unsigned int far *CommandSeg, far *TempSeg ;
  53.     char far *BlockSeg ;
  54.  
  55.    /*  Scan through PSP's looking for a block that is its own father.
  56.     *  This block is the PSP of COMMAND.COM                    */
  57.  
  58. /*    TempSeg = (unsigned int far *)MK_FP ( _psp , 0 ) ;
  59.     do
  60.     {
  61.         CommandSeg = TempSeg ;
  62.         TempSeg = (unsigned int far *)MK_FP ( *(TempSeg+8) , 0 ) ;
  63.     }
  64.     while ( TempSeg != CommandSeg ) ;
  65.  
  66.    /*  Scan forward through memory looking for the correct MSB.
  67.     *  This will have COMMAND.COM's PSP as owner, and begin with
  68.     *  the character M */
  69. /*
  70.     BlockSeg = (char far *)CommandSeg ;
  71.     do
  72.     {
  73.         BlockSeg = (char far *)MK_FP ( FP_SEG(BlockSeg)+1 , 0 ) ;
  74.     }
  75.     while ( ( *(unsigned int far *)(BlockSeg+1) != FP_SEG ( CommandSeg ) ) ||
  76.            ( *BlockSeg != 'M' ) ) ;
  77.  
  78.    /*  The environment is the NEXT segment of memory
  79.     *  and bytes 4 and 5 are the size in paragraphs  */
  80. /*
  81.     *Env =(char far *) MK_FP ( FP_SEG(BlockSeg)+1 , 0 ) ;
  82.     *EnvSize = 16 * *(unsigned int far *)(BlockSeg+3) ;
  83. }
  84. */
  85. /////////////////////////////////////////////////////////////////
  86. /////////////////////////////////////////////////////////////////
  87.  
  88. /*  Mstr_getenv:
  89.  *     Scans the "Master" Environment for a given "sub string"
  90.  *     and returns a pointer to it.
  91.  *     Similar to Turbo routine "getenv" but uses the Master copy of the
  92.  *     environment table.
  93. */
  94. char far *Mstr_getenv (char far *Env , char far *name)
  95. {
  96.     char far *Sub_Env, far *str1, far *str2 ;
  97.  
  98.    /*  Start at the beginning of the environment */
  99.     Sub_Env = Env ;
  100.  
  101.    /*  While the "sub string" we're looking at is non-zero   */
  102.     for ( ; *Sub_Env ; )
  103.     {
  104.      /*  Simulate a "strcmp" on the "sub string" of the environment
  105.       *  and the string we're looking for                     */
  106.  
  107.         for ( str1 = Sub_Env , str2 = name ;
  108.            (*str1) && (*str2) && ( *str1 == *str2) ;
  109.            str1++ , str2++ ) ;
  110.      /*  If we reached the end of the string we're looking for
  111.       *  we've found the correct portion of the environment.
  112.       *  Return the ptr to the start of this "sub string"  */
  113.  
  114.         if ( !*str2 )
  115.             return ( Sub_Env ) ;
  116.  
  117.      /*  Otherwise, advance to the next "sub string" in the environment
  118.       *  by performing a "strchr" function                 */
  119.  
  120.         for ( ; *(Sub_Env++) ; ) ;
  121.     }
  122.  
  123.    /*  Obviously, the string is not present in the environment.
  124.     *  Return this fact.                 */
  125.  
  126.   return ( NULL ) ;
  127. }
  128.  
  129. /*   Mstr_delenv:
  130.  *     Scans the "Master" Environment for a given "sub string"
  131.  *     and removes it.               */
  132.  
  133. int Mstr_delenv (char far *Env , unsigned EnvSize , char far *name)
  134. {
  135.     char far *Sub_Env , far *New_Env ;
  136.     char huge *Dest , far *Src , huge *End_Env ;
  137.  
  138.     int Done  ;
  139.     unsigned Ctr ;
  140.  
  141.    /*  Allocate a chunk of storage to act as a "working" copy of
  142.     *  the Environment table             */
  143.  
  144.     New_Env = (char far *)farmalloc ( EnvSize ) ;
  145.  
  146.    /*  Copy the data from the Master to Working copy of the
  147.     *  Environment table.
  148.     *  Simulates a "memcpy" function.            */
  149.  
  150.     for ( Src = Env , Dest = (char far *)New_Env , Ctr = 0 ;
  151.          Ctr < EnvSize ;
  152.          *(Dest++) = *(Src++) , Ctr++ ) ;
  153.  
  154.    /*  Scan the working copy of the environment for the desired
  155.     *  sub string                */
  156.  
  157.     Sub_Env = Mstr_getenv ( New_Env , name ) ;
  158.  
  159.     if ( Sub_Env == NULL )
  160.     {
  161.         Done = -1 ;      // If not found, do nothing
  162.     }
  163.     else {
  164.      /*  Locate the end of the string to delete
  165.       *  Simulate a "strchr" call     */
  166.         for ( Src = Sub_Env ; *(Src++) ; ) ;
  167.  
  168.      /*  Move the rest of the environment back over the "sub string"
  169.       *  being deleted.
  170.       *  Simulated "memcpy" function.
  171.       *  Huge pointers used for pointer comparison purposes.     */
  172.         for ( Dest = (char huge *)Sub_Env ,
  173.             End_Env = (char huge *) (New_Env + EnvSize ) ;
  174.             ( Dest < End_Env ) ;
  175.             *(Dest++) = *(Src++) ) ;
  176.  
  177.      /*  Copy the data from the Working to Master copy of the
  178.       *  Environment table.
  179.       *  Simulates a "memcpy" function.            */
  180.         for ( Src = New_Env , Dest = (char huge *)Env , Ctr = 0 ;
  181.             Ctr < EnvSize ;
  182.             *(Dest++) = *(Src++) , Ctr++ ) ;
  183.  
  184.         Done = 0 ;        // Signal all-done
  185.     }
  186.  
  187.     farfree ( New_Env ) ;    // Free all working storage
  188.  
  189.     return ( Done ) ;
  190. }
  191.  
  192. /*   Mstr_putenv:
  193.  *     Adds/Replaces a given "sub string" in the Master Environment.
  194.  *     Similar to Turbo routine "putenv" but uses the Master copy of the
  195.  *     environment table.            */
  196. int Mstr_putenv (char far *Env , unsigned EnvSize , char far *name )
  197. {
  198.     char far *Sub_Env , far *Temp_Name ;
  199.     char huge *Dest , far *Src , huge *End_Env ;
  200.     int Done ;
  201.  
  202.    /*  Allocate a chunk of storage to create the Variable name to add
  203.     *  to the Environment table       */
  204.     Temp_Name = (char far *)farmalloc ( 256 ) ;
  205.  
  206. /* Extract only the Name portion of the data to add to the Environment */
  207.     for ( Src = name , Dest = Temp_Name ;
  208.          *Src && ( *Src != '=' ) ;
  209.          *(Dest++) = *(Src++) ) ;
  210.  
  211.    /*  Ensure that the resulting name is well formed.   */
  212.  
  213.     *(Dest++) = '=' ;
  214.     *Dest = 0 ;
  215.  
  216.    /*  Delete this sub string if found in the environment   */
  217.     Mstr_delenv ( Env , EnvSize , Temp_Name ) ;
  218.  
  219.    /*  Locate the END of the Master table by locating a zero length
  220.     *  String in it                */
  221.     Sub_Env = Env ;
  222.     for ( ; *Sub_Env ; )
  223.     {
  224.         for ( ; *(Sub_Env++) ; ) ;
  225.     }
  226.  
  227.    /*  Add the new string to the END of the existing environment, with
  228.     *  trincation IF needed               */
  229.     for ( Dest = (char huge *)(Sub_Env) , Src = name ,
  230.          End_Env = (char huge *) (Env + EnvSize ) ;
  231.          ( Dest < End_Env ) && (*Src) ;
  232.          *(Dest++) = *(Src++) ) ;
  233.  
  234.     Done = -1 ;
  235.     if ( !*Src )
  236.     {
  237.      /*  If the string to add was FULLY added, ensure that the
  238.       *  newly updated environment is properly finished        */
  239.         Done = 0 ;
  240.         *(Dest++) = 0 ;
  241.         *Dest = 0 ;
  242.    }
  243.  
  244.    /*  As a real safety measure, ensure that the FINAL two bytes of the
  245.     *  Environment are both 0. This will finish the last string AND
  246.     *  ensure that a zero length string is also present       */
  247.     *(End_Env-1) = 0 ;
  248.     *(End_Env-2) = 0 ;
  249.  
  250.    /*  Free all working storage   */
  251.     farfree ( Temp_Name ) ;
  252.  
  253.     return ( Done ) ;
  254. }
  255.  
  256.