DOS Environment
█▌Overview▐█
DOS maintains an area of memory that holds a set of ASCIIZ▲ strings that
can be used to obtain certain system-level information and to pass data to
other programs.
■ The environment is a series of strings in the form: name=value.
■ The Set command is a general purpose interface to the environment. The
Path and Prompt commands also affect its contents.
■ In DOS 3.0+, you can obtain the name by which your program was started
(and therefore, your startup directory) by examining the end of the
environment.
■ In DOS 3.3+ batch files, any text surrounded by percent marks is
replaced with the environment value that matches the name. Examples:
┌─────────────────────────────────────────
│ Path c:\my_dir;%PATH%
│ If (%USER%) == (dan) GOTO dan_label
│ Echo %SET_CMPRS% > %CUR_LPT%
■ In DOS 6.0+, you can use SET in CONFIG.SYS.
The environment is limited to an arbitrary size, but a larger DOS
environment can be created with the SHELL= directive in the CONFIG.SYS
file or the command 'COMMAND /e:nnnn' which starts a secondary copy of the
command interpreter, providing nnnn bytes of environment space.
Layout: Example using ASM mnemonics:
▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀ ▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀
name_1=value_10 db 'COMSPEC=c:\command.com',0
name_2=value_20 db 'PROMPT=$p$g',0
: db 'WP=c:\wordproc',0
name_N=value_N0 db 'PATH=d:\;c:\dos;c:\utils',0
0 db 0
┌─ xxxx ───────────┐ DOS ┌─ dw 1 ───────────────────────┐
│ EXEC_string_10 ╞═► 3.0+ ◄═╡ db 'C:\ACCTNG\AR001.EXE',0 │
│ : │ only └─ db 0 ───────────────────────┘
│ EXEC_string_NN0 │
└─ 0 ──────────────┘
where 0 is the ASCII NUL character (00H)
xxxx is a 16-bit binary value (usually 0001H)
█▌Using the Environment▐█
The environment is less than 32K bytes and it starts on a paragraph
boundary. Offset 2cH of the PSP of the current program contains the
paragraph number of the environment for the active program.
Alas, there are no DOS services to help access the environment. You can
find a selected name by using a series of ASCIIZ string comparisons, until
you come to a null string (zero length), indicating the end of the
environment. Normally, the name portion of each string in the environment
is in uppercase, but this is not enforced.
One typical operation with the environment is used by shell-type programs
which execute a secondary copy of COMMAND.COM. Shells typically search
for the name of "COMSPEC" and use the value as the drive, path, and
filespec of the DOS command interpreter--the program to EXEC via fn 4bH
Some programs (including some DOS commands) request the operator to store
application-specific information in the environment via the SET command.
The application can use that information each time it is executed. For
instance, a word processor may search the environment for a name of
"DICTIONARY" and use the text of the value as the filespec for a
dictionary data file.
See Predefined Environment Variables for related info.
█▌Locating Your Load Path▐█
Starting with DOS 3.0, DOS stores an additional string after the end of
the formal environment that can be used to locate the drive and directory
from which a program was loaded. Applications can use this to help locate
program overlays and data files.
This becaem important with DOS 3.0, when DOS began allowing an operator to
invoke a program by prefixing a pathname before the program filename.
Following the final string of the environment is a byte of 0, indicating
the end of the formal environment. The next two bytes are a 16-bit binary
count of additional strings (this value is normally 0001H). Following
this binary value, you can expect to find an ASCIIZ string of the filespec
(including the drive, path, and extension) which was used by DOS Fn 4bH
(EXEC) to load and execute the program.
Even if the program file was not in the default directory and DOS or
Windows needed to use the command search path to locate the program file,
the correct full filespec (d:\path\filename.ext,0) will be stored after
the end of the environment.
█▌The "Master" Environment▐█
Each program receives a static copy of the initial environment. Thus,
any changes you make to an environment will be in effect for all your
child processes (if any), but will be lost when control is returned to
your parent process. See Program Startup & Exit.
The DOS Set command affects the master environment of the active copy of
COMMAND.COM, but there is no simple or documented way to do so in your own
programs. The undocumented way to locate the "root" or "master"
environment is:
■ Examine the word at offset 16H of your PSP; that's the PSP of your
parent (undocumented).
■ Examine the same offset in that "parent PSP". Keep working backward
up the chain until the "parent PSP" is the same as the PSP
itself--that will be the PSP of COMMAND.COM.
■ The word at offset 2cH in that PSP is the segment of the environment
for COMMAND.COM (that's the "master" environment).
■ The size of the master environment can be obtained by examining the
Memory Control Block (MCB) which is in the paragraph just before the
environment. The word at offset 03H is the size of the memory block,
in 16-byte paragraphs.
If you want to change anything in the master environment, you'll need to
be careful to avoid overwriting the end and to leave it in good shape.
Also remember that when running multiple DOS sessions (e.g., under
Windows), this will affect the environment of ONLY the current session.
See Also: Predefined Environment Variables
PSP
Program Startup & Exit
DOS Functions
-♦-