home *** CD-ROM | disk | FTP | other *** search
/ Simtel MSDOS - Coast to Coast / simteldosarchivecoasttocoast.iso / pathutil / desetp12.zip / FINDENV.C next >
C/C++ Source or Header  |  1990-06-04  |  3KB  |  107 lines

  1. /**
  2.  **
  3.  ** FINDENV -- A utility for finding the active DOS environment
  4.  **
  5.  ** Version 1.1  05/26/90  (for Microsoft C Version 5.1)
  6.  ** Copyright (c) D. E. Ekman 1990.  All rights reserved.
  7.  **
  8.  ** FINDENV locates the active DOS environment.  This is the copy used by the
  9.  ** current DOS shell (usually COMMAND.COM), and the one that the shell
  10.  ** passes to programs that it invokes.  After finding the active environment,
  11.  ** FINDENV prints the segment location (in Hex), the size, the amount used,
  12.  ** and the remaining space in the environment (all in decimal bytes).  It then
  13.  ** prints the contents of the environment for inspection.
  14.  **
  15.  ** This program forms the basis for other programs that may change the active
  16.  ** environment.  For example, it provides a means of generating PATH statements
  17.  ** that are not limited by the 128-byte size of the command buffer.
  18.  **
  19.  **/ 
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22.  
  23. main()
  24. {
  25.     unsigned envseg, envsize, envinuse;
  26.     int count;
  27.     char far *ptr;
  28.  
  29.     getsysenv(&envseg, &envsize, &envinuse);
  30.     printf("\nThe Ekman Utility FINDENV, V1.1\n");
  31.     printf("Copyright (c) Donald E. Ekman 1990.  All rights reserved.\n");
  32.     printf("\nThe active environment begins at %X:0 (Hex).\n",envseg);
  33.     printf("The size of the environment is %d bytes,\n",envsize);
  34.     printf(" of which %d bytes are used,",envinuse);
  35.     printf(" leaving %d bytes free.\n\n",(envsize-envinuse));
  36.     printf("Environment contents:\n\n");
  37.     ptr = (char far *) ((long) envseg << 16);
  38.     for (count=0; count<envinuse; count++)
  39.         {
  40.         if(*(ptr + count) == '\0')
  41.             putchar('\370');
  42.         else
  43.             putchar(*(ptr + count));
  44.         }
  45.     printf("\n");
  46.     return(0);
  47. }
  48.  
  49. getsysenv(envseg, envsize, envinuse)
  50. unsigned *envseg, *envsize, *envinuse;
  51. {
  52.     unsigned getdospsp(), computeenvinuse(), dospsp, temp;
  53.     unsigned far *ptr;
  54.  
  55.     dospsp = getdospsp();
  56.     ptr = (unsigned far *) (((long) dospsp << 16) | 0x2c);
  57.     temp = *ptr;
  58.     if((temp != 0) && (_osmajor != 3 || _osminor <= 19 || _osminor >= 30))
  59.         {
  60.         *envseg = temp;
  61.         }
  62.     else
  63.         {
  64.         ptr = (unsigned far *) (((long) (dospsp-1) << 16) | 3);
  65.         *envseg = dospsp + (*ptr) + 1;
  66.         }
  67.     ptr = (unsigned far *) (((long) ((*envseg)-1) << 16) | 3);
  68.     *envsize = 16*(*ptr);
  69.     *envinuse = computeenvinuse(*envsize, *envseg);
  70.     return(0);
  71. }
  72.  
  73. unsigned getdospsp()
  74. {
  75.     unsigned temp, temp1;
  76.     unsigned far *ptr;
  77.  
  78.     ptr = (unsigned far *) (((long) _psp << 16) | 0x16);
  79.     temp = *ptr;    
  80.     while(1)
  81.         {
  82.         ptr = (unsigned far *) (((long) temp << 16) | 0x16);
  83.         temp1 = *ptr;
  84.         if((!temp1) || (temp1 == temp))
  85.             return(temp);
  86.         else
  87.             temp = temp1;
  88.         }
  89. }
  90.  
  91. unsigned computeenvinuse(envsize, envseg)
  92. unsigned envsize, envseg;
  93. {
  94.     unsigned j;
  95.     unsigned far *ptr;
  96.  
  97.     for(j=0; j<=(envsize-3); j++)
  98.         {
  99.         ptr = (unsigned far *) (((long) envseg << 16) | j);
  100.         if(*ptr == 0)
  101.             {
  102.             return(j+2);
  103.             }
  104.         }
  105.     return(envsize);
  106. }
  107.