[comp.lang.pascal] Getting Environment Strings

kirsch%BRAGGVAX.ARPA@cunyvm.cuny.edu (David Kirschbaum) (12/21/88)

NetLandians,
I usually don't post listings because it clutters up the nets.  However, I've
been getting requests from all over the known world (and one from a Klingon)
for my little sample code .. and some of the return addresses are 30 lines
deep!  So here it is.
David Kirschbaum
Toad Hall
kirsch@braggvax.ARPA
(Oh, yeah .. this is NOT a universal tool .. it only looks for specific
strings in the environment .. but that's what the original query asked for.
It's pretty easy to also return EVERYTHING in the environment, or test each
environment string against a known string.  But this'll do.)
---- 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.