Path: utzoo!attcan!uunet!ncrlnk!ncr-sd!hp-sdd!hplabs!decwrl!labrea!rutgers!cmcl2!adm!xadmx!kirsch@braggvax.arpa From: kirsch@braggvax.arpa (David Kirschbaum) Newsgroups: comp.lang.pascal Subject: Re Getting environment strings Message-ID: <17771@adm.BRL.MIL> Date: 11 Dec 88 21:38:09 GMT Sender: news@adm.BRL.MIL Lines: 92 One of the subscribers asked how to read in the system environment variable PATH. His return address is a horror of 'Received:' listings, so I'm not even gonna puzzle out how to reach him directly! Regrets if I've barraged the whole world with an unwanted listing .. but it isn't THAT big. Here's one solution to his problem. David Kirschbaum Toad Hall kirsch@braggvax.ARPA ------ cut here ------ {Environment code from SUPERCOMM package Rewritten into a little demo program that'll seek out and display the three most common environment specs (and one not-so-common one). This is written for Turbo Pascal v3.0 .. donno if it can be better done in v4.0 or v5.0. David Kirschbaum Toad Hall kirsch@braggvax.ARPA } TYPE Str255 = STRING[255]; FUNCTION GetEnvStr(SearchString : Str255) : Str255; TYPE Env = ARRAY [0..32767] OF Char; VAR EPtr: ^Env; EStr: Str255; Done: BOOLEAN; i: INTEGER; BEGIN GetEnvStr := ''; IF SearchString <> '' THEN BEGIN EPtr := Ptr(MemW[CSeg:$002C],0); i := 0; SearchString := SearchString + '='; Done := FALSE; EStr := ''; REPEAT IF EPtr^[i] = #0 THEN BEGIN IF EPtr^[SUCC(i)] = #0 THEN BEGIN Done := TRUE; IF SearchString='==' THEN BEGIN EStr := ''; i := i + 4; WHILE EPtr^[i] <> #0 DO BEGIN EStr := EStr + EPtr^[i]; i := SUCC(i); END; GetEnvStr := EStr; END; END; IF COPY(EStr,1,LENGTH(SearchString)) = SearchString THEN BEGIN GetEnvStr := COPY(EStr,SUCC(LENGTH(SearchString)),255); Done := TRUE; END; EStr := ''; END ELSE EStr := EStr + EPtr^[i]; i := SUCC(i); Until Done; END; END; {of GetEnvStr} FUNCTION ComSpec: Str255; BEGIN ComSpec := GetEnvStr('COMSPEC'); END; {of ComSpec} CONST {let's define some environment specs to look for...} NRSPECS = 4; Spec : ARRAY[1..NRSPECS] OF STRING[7] = ('PATH', 'COMSPEC', 'PROMPT', 'DSZPORT'); VAR i : INTEGER; BEGIN {main} FOR i := 1 TO NRSPECS DO WRITELN(Spec[i]:7, ': [', GetEnvStr(Spec[i]), ']'); WRITELN('Rivvvvt!'); END.