[comp.lang.pascal] Re Getting environment strings

kirsch@braggvax.arpa (David Kirschbaum) (12/12/88)

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.

RDK%vm.temple.edu@cunyvm.cuny.edu (Robert Keiser) (12/13/88)

I think I managed to get a reply to the original poster but if not, he should
be able to see this.  In Turbo 5.0, there is a function that will give you
an environment variable.  The function is GETENV, you pass it the name of the
variable you want.  For the path variable, the call would look something like
this:
      pathstring := GETENV('PATH');
If there is not a path define, a null string will be returned.


Robert Keiser
Temple University Computer Activities

Bitnet   : RDK@Templevm
Internet : RDK@VM.TEMPLE.EDU
US Mail  : Temple University
           Philadelphia, PA 19122