home *** CD-ROM | disk | FTP | other *** search
- FINDENV
-
- A utility for finding the DOS active environment
- (Version 1.0 - February 24, 1990)
- Copyright (c) Donald E. Ekman 1990. All rights reserved.
-
-
- FINDENV locates the active DOS environment and displays its location, size,
- free space, and contents on the screen. As the display is generated, the
- string-terminating nulls are replaced with a visible character. FINDENV has
- been tested with DOS Versions 2.01 through 3.3, 4.01 and 5.0; it has not been
- tested with Version 4.0. Because FINDENV uses undocumented features of DOS to
- find the active environment, its correct operation for versions of DOS with
- which it has not been tested is not assured.
-
-
- USAGE
-
- FINDENV is invoked by the command line:
-
- FINDENV
-
-
- MESSAGES
-
- FINDENV first displays a copyright notice, followed by the location of the
- active environment, its size, the amount used, and the free space. The
- contents of the environment are then displayed, with the string-terminating
- nulls presented as visible characters.
-
- There are no error messages. FINDENV might be made more robust in a variety
- of ways. For example, during the recursive pointer chasing to find the active
- shell, a count of pointers might be kept, with some sort of exit if the count
- exceeds some reasonably large value.
-
-
- NOTES
-
- It might seem that FINDENV has little utility. Certainly its output is not
- remarkably entertaining. It's real utility is in providing a means for finding
- the active DOS environment, thus making various other programs possible, such
- as SETPATH, which permits path environment variables longer than 128 bytes to
- be inserted into the environment. It is also useful for verifying that SETPATH
- works properly with MS-DOS 5.0. In that version of DOS, the SET and PATH
- commands, when used to display environment variables, refuse to display any more
- than the first 128 bytes of any variable. Since the command line buffer size
- ordinarily prevents one from setting variables any longer than that, DOS 5.0
- doesn't bother to try to display more than the first 128 bytes.
-
- There are a number of methods in general use for finding the DOS environment.
- Not all of these methods are reliable, and not all distinguish between the root
- environment and the active environment. That having been stated, some
- definitions are in order.
-
- The active environment is that copy of the environment that is associated with
- the currently active command processor (normally COMMAND.COM), and of which a
- copy is passed to programs by that command processor. This environment stores
- the current path, prompt, and comspec environment variables, along with any
- other environment variables created with the SET command.
-
- When the command processor causes a program to be loaded for execution, it
- creates a copy of the active environment and passes it to the program to be
- executed. This program environment is ephemeral; when the program terminates,
- the program environment disappears. A program is given the address of its
- program environment through its Program Segment Prefix (PSP). The word at
- offset 2C in the PSP contains the segment address of the program environment.
- (The address offset is always zero and so is not included in the PSP.) A
- program may, using documented features of DOS, operate on its program
- environment. (A program may not, using only documented DOS features, gain
- access to the active environment, however.)
-
- There may or may not be a third environment, which may be called the root
- environment. This is the environment that is set up when the system boots.
- Ordinarily this is identical to the active environment. However, if you
- invoke a secondary command processor, the environment inherited by that
- secondary command processor is the active environment, and is a copy of the
- root environment. It is this copy that is passed to programs, and which
- controls the active path. Thus it is the active environment that we want to
- find, and the active environment is not necessarily the root environment.
-
- To locate the active environment FINDENV first finds the active command
- processor (normally COMMAND.COM). It then finds its environment, which
- is by definition the active environment. Both steps involve using features
- of DOS that are not officially documented. This means that what works with
- current versions of DOS may not work for subsequent versions. However, it
- should be noted that the techniques used to find the active environment in
- DOS 5.0 haven't changed since DOS 3.3.
-
- An environment is simply a memory block. In DOS, memory is allocated in groups
- of (16-byte) paragraphs, aligned at paragraph boundaries, called memory blocks.
- Each memory block contains a 16-byte header called the Memory Control Block
- (MCB) for that memory block. In DOS Versions through 3.3, only the first 5
- bytes out of 16 are used. The first byte contains a MCB identifier in the
- form of the letter M for all but the last memory block, for which the letter Z
- is used as the identifier. (The M and Z, which also show up as a signature in
- .exe files, are said to be the initials of Mark Zbikowski, one of the authors of
- DOS 2.0.) DOS uses these bytes to verify the integrity of the memory
- allocation.
-
- The 2nd and 3rd bytes of the MCB contain the segment address of the owner of the
- memory block. That is, these bytes contain the segment address of the PSP of
- the program that requested the block. This segment address is stored in the
- usual way words are stored in Intel architecture: low-order part in byte 2,
- and high-order part in byte 3. If a program deallocates memory located in the
- middle of the MCB chain, DOS changes bytes 2 and 3 in the MCB to zeros. This
- memory block is then available for other uses.
-
- Bytes 4 and 5 of the MCB contain the number of 16-byte paragraphs in the memory
- block. The count does not include the size of the MCB. Using this information,
- DOS can trace through the chain of MCBs. To find the first MCB, it uses the
- undocumented DOS service 52h. It calls interrupt 21 with AH=52; on return,
- ES:(BX-2) contains a pointer to the first MCB.
-
- Starting with DOS 4.0, the last eight bytes of the MCB contain the name of the
- program that owns the block. In DOS 5.0, the memory arena may also include
- Upper Memory Blocks (UMBs). (The DOS allocation method call includes or
- excludes UMBs. UMBs are those available blocks of memory between 640k and
- 1 meg.) If UMBs are included, the 16 bytes at the end of the regular memory
- will contain an MCB to skip over video or other memory to the first UMB area.
-
- FINDENV completes the first step--locating the active command processor--by
- noting that offset 16h of the PSP points to the PSP of the parent of the
- current process. The pointer points to itself when the active command processor
- is reached.
-
- Having found the PSP of the active command processor, FINDENV can then locate
- the active environment, but doing so is DOS-version-dependent. FINDENV looks at
- offset 2C in the PSP of the active command processor. If the active command
- processor is the root command processor, the word at 2C is 0, and the active
- environment is the memory block after the command processor, regardless of the
- DOS version.
-
- If the active command processor is not the root command processor, then the
- location of the active environment is version dependent. The active command
- processor is a program, and so has its own program environment. For DOS 3.1
- and earlier, this program environment is also the active environment. For
- versions 3.2 and later, the active command processor places a memory block
- immediately following itself, which contains the active environment. It does
- not use the program environment.
-
- For DOS versions 3.2 and earlier, offset 2C points to the program environment.
- For versions 3.3 and later, offset 2C points to the active environment. While
- the placing of the active environment was changed with version 3.2, the address
- in 2C changed only in version 3.3. This curiosity accounts for the testing
- of the variables _osmajor and _osminor in the FINDENV code. The active
- environment is the next block in memory if offset 2C=0 or if the DOS version is
- 3.2. Otherwise, the active environment is pointed to by 2C.
-
- Having located the active environment, FINDENV uses a simple MCB calculation
- to determine the size of the environment. To determine the amount in use,
- FINDENV notes that the strings are stored with a single ASCII zero terminating
- each, and a double ASCII zero at the end of all strings. A scan for a double
- zero locates the end of the used part of the environment. Subtraction gives
- the amount free.
-
-
- PROGRAMMING
-
- FINDENV was written in C, and compiled with the Microsoft C Version 5.10
- compiler, and linked with the Microsoft Version 5.03 linker. The small memory
- model was used, which required careful treatment of the environment space,
- which is accessible only with far pointers. The code is not robust; no
- error checking is performed. As to style, that is a matter for individual
- evaluation.
-
-
- LICENSE
-
- You may use this program and distribute it freely, provided that no charge is
- made for its distribution, and all copyright notices are retained. The general
- information for which the code is illustrative maybe used in derivative works.
-
- The techniques of which the code is exemplary may, of course, be freely used in
- other software. However, the actual code may not be used in any derivative work
- without the express permission of the author. (Besides, you can certainly write
- better code than this.)
-
- If you distribute this program, please keep this documentation file with it
- in some archived form. Comments and suggestions for improvement should be
- sent to:
-
- Donald E. Ekman
- 3586 Berry Way
- Santa Clara, CA 95051-1902
-
- Internet: ekman@wdl30.wdl.loral.com
- ekman@ssd168.ssd.loral.com
-
-