{***********************************************************************}
{									}
{ __RetEnUtl  Return an environment string				}
{									}
{ function  __RetEnUtl(var EnvPos : word) : string;			}
{									}
{	      EnvPos	The position within the program environment	}
{			where the string is to be extracted. Upon re-	}
{			turn, the value of EnvPos is the position of	}
{			next string in the environment. 		}
{									}
{ Description This function is used to extract null terminated strings	}
{	      from the environment.  Because the position of the next	}
{	      environment string is returned, it can be used to suc-	}
{	      cessively search the environment for all strings.  If no	}
{	      more strings are in the environment, the NULL string is	}
{	      returned, and in this case, the returned value of EnvPos	}
{	      is the total size of the environment in bytes.		}
{									}
{	      Notice that each string in the environment is terminated	}
{	      by a NUL byte (ASCII code 0).  In the parlance of DOS,	}
{	      these are ASCIIZ strings.  The end of the environment is	}
{	      terminated by an additional NUL byte.  Therefore, to	}
{	      extract the next string, the environment is scanned until }
{	      a NUL byte is encountered, copying the non NUL char-	}
{	      acters in the process.  If the first character encounter- }
{	      ed is NUL, the end of the environment is reached. 	}
{									}
{	      This function is used by both __GetEnUtl (get an environ- }
{	      ment parameter) and __PutEnUtl (put an environment	}
{	      string).	Because __PutEnUtl may be forced to make a copy }
{	      of the environment, the global pointer _EnvPtr is used	}
{	      point to the environment. 				}
{									}
{	      Two cautions:  The value of EnvPos must be chosen so that }
{	      it is less than the size of the environment.  If it is	}
{	      not, the null string is returned, and EnvPos is not	}
{	      altered.	Setting EnvPos to 1 starts the search at the	}
{	      beginning of the environment.  Because the Turbo Pascal	}
{	      string type is used to return the environment variable,	}
{	      no variable can be greater than 255 characters.  This is	}
{	      usually more than sufficient.				}
{									}
{ Version     4.00 (C)Copyright Blaise Computing Inc. 1987		}
{_______________________________________________________________________}

function __RetEnUtl(var EnvPos : word) : string;

  type
    EnvironmentPtr = ^_MemoryChar;

  var
    EnvPtr  : EnvironmentPtr;
    StrLen  : integer;
    I	    : integer;
    TempCh  : char;
    TempStr : string;
    TempPtr : pointer;

  begin {__RetEnUtl}

    { Initialize EnvPtr to point to the environment; this allows	}
    { indexing through the environment. 				}

    EnvPtr := EnvironmentPtr(_EnvPtr);
    StrLen := 0;
    I	   := EnvPos;
    TempCh := EnvPtr^[I];
    while (TempCh <> #0) do
      begin
	Inc(StrLen);		       { A non NUL is found, so store	}
	TempStr[StrLen] := TempCh;     { in the returned string.	}
	Inc(I); 		       { Look at the next character in	}
	TempCh := EnvPtr^[I]	       { the environment.		}
      end;

    TempStr[0] := Chr(StrLen);	       { Set the length, the new posi-	}
    if (StrLen <> 0) then	       { tion, the string and return.	}
       EnvPos  := I + 1;	       { Notice the new position skips	}
				       { past the NUL byte if not at	}
    __RetEnUtl := TempStr	       { the end of the environment.	}

  end;	{__RetEnUtl}
