home *** CD-ROM | disk | FTP | other *** search
- {ENVIRONM.INC :
- This provides the Boolean function FoundInEnv(WordIn, var WordOut) which
- searches the DOS Environment. WordIn is either a string which may be found in
- the Environment (eg, PATH) or the null string '', in which case an attempt will
- be made to determine, from the Environment, in which directory the program
- being executed resides.
-
- If the DOS is 1.X, FoundInEnv returns False, with WordOut unchanged.
-
- If the DOS is 2.X, FoundInEnv returns:
-
- False, with WordOut unchanged, if
- WordIn is '', or if WordIn is not found in the Environment.
- True, with WordIn changed to the
- Environment string = to WordIn, if WordIn is found in the Environment. (Eg,
- FoundInEnv('foo',WordOut) returns True with WordOut = 'nothing much' if the
- Environment contains the string "FOO=nothing much".)
-
- If the DOS is 3.X, FoundInEnv returns:
-
- the same as for DOS 2.X, except:
- True, with WordOut changed to the
- path to the directory in which resides the program being executed, and the file
- name of the program, if WordIn is ''. Eg, FoundInEnv returns True with WordOut
- = "C:\TURBO\ENVIRONM.COM" if the executed program is ENVIRONM.COM, located in
- subdirectory \TURBO on disk C:. (It is worth noting that if a program is
- compiled in memory and run, a call of if FoundInEnv('',WordOut) with DOS 3.X
- will resultin WordOut being set to "<d:>[path]TURBO.COM", etc. The program
- compiled and run in memory has no environment space of its own, but accesses
- the environment which DOS has handed to the compiler.)
- }
-
- {The file DOS.INC must be included prior to the inclusion of ENVIRONM.INC.}
-
- {type BigString must precede the function declaration because it is used as
- the type of FoundInEnv's parameters. Other variables used as the actual
- parameters do not have to be of the same type, altho they should be
- string[255]. If the compiler director $V is turned off, the actual parameters
- can be shorter types; this is dangerous if the value to be returned thru
- WordOut is longer than the declared length of WordOut: other data may then
- be overwritten.}
-
- TYPE
- BigString = STRING[255];
-
- FUNCTION FoundInEnv(WordIn : BigString; VAR WordOut : BigString) : Boolean;
-
- CONST
- ESTM : Char = ^@; {Environment strings are null-terminated.}
- EEM : STRING[2] = ^@^@; {marker for end of Environment}
-
- VAR
- MaxStr : BigString; {MaxStr is the target string during the search}
- SEnv : Integer ABSOLUTE CSeg : $2C; {SEnv is the segment of Environment}
- OEnv : Integer; {OEnv is the offset within Environment}
- EEPos : Integer; {offset of end of Environment marker}
- WPos : Integer; {offset of Word, if it is found}
- EST : Integer; {offset of end of Word string}
-
- { If an Environment variable corresponding to Word is found, FoundWord
- returns True, with SetTo set to the value of Word in the Environment.
- Otherwise, FoundWord returns False, with SetTo unchanged. }
-
- FUNCTION FoundWord(VAR Word, SetTo : BigString) : Boolean;
- VAR
- LW : Integer; {length of Word}
- I : Integer; {general counter}
- Found, {True if we find Word}
- Done : Boolean; {True if we run out of Environment space}
- BEGIN {FoundWord}
-
- { In the Environment, Word will be WORD= }
-
- Word := Word+'=';
- LW := Length(Word);
- FOR I := 1 TO LW - 1 DO Word[I] := UpCase(Word[I]);
-
- { We haven't found or done anything yet. }
-
- Found := False;
- Done := False;
-
- { We will start at the beginning of the Environment space. }
- OEnv := 0;
-
- { We don't care what's in MaxStr right now, just that it acts like it's
- got 255 chars in it. }
-
- MaxStr[0] := Char(255);
- WHILE NOT(Done OR Found) DO BEGIN
-
- { If "WORD=" is the first thing in the Environment, then that's what we'll
- be trying to match. It it is further into the environment, then we will
- match "^@WORD=", where ^@ is a byte of 0. }
-
- IF (OEnv <> 0) AND (Word[1] <> ^@) THEN BEGIN
- Word := ^@+Word;
- LW := LW+1;
- END; {IF (OEnv <> 0) AND (Word[1] <> ^@)}
-
- { Copy 255 bytes of the Environment space into MaxStr. }
-
- Move(Mem[SEnv:OEnv], MaxStr[1], 255);
-
- { If we find EEPos in those 255 bytes, then we've found the end of the
- Environment space, so Done will be true and we must shorten MaxStr. }
-
- EEPos := Pos(EEM, MaxStr);
- IF EEPos <> 0 THEN BEGIN
- Done := True;
- MaxStr[0] := Char(EEPos);
- END; {if EEPos<>0}
-
- { I let the standard Turbo function Pos do the searching for me. That's
- a lazy trade-off: It was easier for me to write and debug this search
- routine than to write one using a search which would extend more than
- 255 bytes. Searching the typical DOS Environment, I think there's little
- value to be gained from a faster search, anyway. }
-
- { It we find Word in those 255 (or less) bytes, then we have found
- what we're looking for, IF:
- we are at the start of the Environment space [OEnv = 0] AND
- Word is the very first thing there;
- OR we are in the first pass thru here [OEnv = 0] AND Word is
- preceded by ^@;
- OR we are in a subsequent pass thru here [OEnv<>0 AND we have put
- ^@ on the front of Word ourselves. }
-
- WPos := Pos(Word, MaxStr);
- IF WPos <> 0 THEN
- IF (OEnv <> 0) OR ((OEnv = 0) AND ((WPos = 1) OR
- (MaxStr[WPos-1] = Char(0))))
- THEN Found := True;
-
- { If we haven't found Word or reached the end of the Environment space, then
- we'll offset (255 - the length of Word - 1) bytes further into the
- Environment space. (The funny increment is in case Word is on the boundary
- between two 255 byte blocks.) }
-
- IF NOT(Done OR Found) THEN
- OEnv := OEnv+255-(LW-1);
- END; {while not (Done or Found)}
-
- { If we have found Word, we'll set the offset to the first byte after "WORD=".
- We don't know how long the string will be, but is should be less that 255
- bytes. I really don't know for certain that an Environment string can't be
- longer than that--I just know that you can't enter that long a string from the
- keyboard or a batch file, and I don't think the string is likely to be entered
- any other way. If you do--or if that possibility worries you, you're free to
- modify this routine accordingly. }
-
- IF Found THEN BEGIN
- OEnv := OEnv+WPos+LW-1;
- Move(Mem[SEnv:OEnv], MaxStr[1], 255);
- MaxStr[0] := Char(255);
-
- { So I'm saying that if the end-of-string marker is not in the first 255 bytes,
- then we haven't found a value for Word. If it is, then we'll copy all of
- the string up to ESTM to SetTo. }
-
- EST := Pos(ESTM, MaxStr);
- IF EST <> 0 THEN SetTo := Copy(MaxStr, 1, EST-1)
- ELSE Found := False;
- END; {if Found}
- FoundWord := Found;
- END; {FoundWord}
-
- { What I call "Home" is found immediately AFTER the Environment space
- with DOS 3.X. Since it's AFTER, maybe it should have a routine all to
- itself. In my mine, tho, it's part of the Environment. "Home" consists
- of a drive specifier, the path to the directory where our program is
- located, and the name of the program as DOS knows it. }
-
-
- FUNCTION FoundHome(VAR PathHome : BigString) : Boolean;
- BEGIN {FoundHome}
-
- { Unfortunately, DOS 2.X doesn't have this information. }
-
- IF DOS < 3.0 THEN FoundHome := False
- ELSE BEGIN {DOS>=3.0}
-
- { We'll use MaxStr here just as we did above, but we'll be searching
- for a "word" of two bytes of 0, which will mark the end of the
- formal Environment space. Understanding what we're looking for here,
- the comments above should clarify the first part of this code. Note that,
- here, we are not Done when we reach the EEM--we've just gotten to where
- we were going. }
-
- MaxStr[0] := Char(255);
- EEPos := 0;
- OEnv := 0;
- REPEAT {until EEPos<>0}
- Move(Mem[SEnv:OEnv], MaxStr[1], 255);
- EEPos := Pos(EEM, MaxStr);
- IF EEPos = 0 THEN OEnv := OEnv+244;
- UNTIL EEPos <> 0;
-
- { Set the offset to the first byte after the end-of-environment marker.
- Then we reveal the same conditions as above: If the end-of-string marker
- isn't in the next 255 bytes then we give up. But that condition isn't a
- limitation here--this string is limited to 79 characters. }
-
- OEnv := OEnv+EEPos+3;
- Move(Mem[SEnv:OEnv], MaxStr[1], 255);
- EST := Pos(ESTM, MaxStr);
- IF EST = 0 THEN FoundHome := False
- ELSE BEGIN {EST<>0}
- FoundHome := True;
- PathHome := Copy(MaxStr, 1, EST-1);
- END; {else EST<>0}
- END; {DOS>=3.0}
- END; {FoundHome}
-
- BEGIN {FoundInEnv}
-
- { If we're running DOS 1.X [or 0.X?] then there is no Environment space. }
-
- IF DOS < 2.0 THEN FoundInEnv := False
-
- { Otherwise, if WordIn = '', that means we want to find "Home". }
-
- ELSE IF WordIn = '' THEN FoundInEnv := FoundHome(WordOut)
-
- { Else WordIn = something, and we want to search the Environment space for
- that something. }
-
- ELSE FoundInEnv := FoundWord(WordIn, WordOut);
- END; {FoundInEnv}
-
-
- { All of this horribly over-commented code, which should ARC admirably,
- is the product of
- Karl Brendel, CIS 73307,3101
- 718 East B Avenue
- Hutchinson, Kansas 67501 }