Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!uunet!seismo!gatech!hubcap!ncrcae!ncr-sd!ivory!mike From: mike@ivory.SanDiego.NCR.COM (Michael Lodman) Newsgroups: comp.lang.pascal,comp.sys.ibm.pc Subject: Re: Environment variables in Turbo Pascal Message-ID: <153@ivory.SanDiego.NCR.COM> Date: Mon, 3-Aug-87 12:12:20 EDT Article-I.D.: ivory.153 Posted: Mon Aug 3 12:12:20 1987 Date-Received: Tue, 4-Aug-87 04:08:07 EDT References: <151@ivory.SanDiego.NCR.COM> Reply-To: mike@ivory.UUCP (Michael Lodman) Distribution: world Organization: NCR Corporation - Advanced Development Lines: 168 Xref: mnetor comp.lang.pascal:234 comp.sys.ibm.pc:6299 I received requests for this information from several people by email so I am posting the response I received from John Knutson. I have not tried it as yet. From: hp-sdd!sdcsvax!husc6.harvard.edu!harvard!ut-sally!ut-ngp!auscso!knutson6 (John Knutson) { -------------------------------------------------------------------------- } { READENV.INC } { Copyright (C) 1985 24 Karat Consulting } { 4702 S. Alaska St. } { Seattle, Washington 98118 } { } { These Turbo Pascal functions can be used to search the DOS environment } { area. } { } { ENVSTR returns a Turbo string containing the DOS environment area. } { } { NEXTENVSTR returns the next string (keyword and value) in the environment, } { starting at the index passed. } { } { READENV returns a string whose value is the current environment setting } { for the keyword passed, or a null string if the keyword is not } { found. } { } { Typical environment keywords are PATH (the DOS search path) and COMSPEC } { (the name of the command processor). Remember that the environment passed } { to a program is a READ-ONLY copy of the real DOS environment...thus you } { can't put new strings in from an executing program to be read by } { another...but you can use it to communicate between two separate programs. } { The Pascal statements to use these functions are: } { } { StringVar := EnvStr; (* move environment into string *) } { StringVar := ReadEnv(KeywordName); (* read environment for a key *) } { StringVar := NextEnvStr(Env,N); (* read string in environment, *) } { (* starting at N *) } { e.g., } { Type } { Lstr = string[255]; } { Var } { Env, } { Works : lstr; } { Worki : integer; } { begin } { Env := EnvStr; } { writeln('Value for COMSPEC is ',ReadEnv('COMSPEC')); } { Worki := 1; } { writeln('Environment settings are: '); } { repeat } { Works := NextEnvStr(Env,Worki); } { if length(Works) > 0 then writeln(Works) } { until length(Works) = 0 } { end; } { } { The DOS command to put variables into the environment is: } { SET keyword=value } { Keywords are stored uppercase, but their values are stored as is. You can } { use this (undocumented) feature in .BAT files to read environment } { variables: } { } { ECHO The value of COMSPEC is %COMSPEC% } { } { that is, enclose the environment variable name in %'s to read its value. } { } { WARNING: These functions assume an environment area of not more than 255 } { bytes; that is, the size of a Turbo string. DOS sets up an initial } { environment area of 127 bytes. DOS will expand this dynamically when you } { use the SET command for more environment variables, and more space is } { needed. (This only applies to SET commands issued at the DOS prompt, not } { from within .BAT files). However, if you have installed any resident } { routines, you can't expand the environment area any further. There are } { several public domain routines to get around this limitation...however, } { this routine assumes you haven't got more than the 255 bytes that Turbo } { can handle in a string. If you do, don't use this routine. } { } { This routine has been placed in the Public Domain by the author and copies } { may be freely made for non-commercial, demonstration, or evaluation } { purposes. If you use this routine in a program for sale or for commercial } { purposes, please send money ($2 to $5 would be sufficient) to the address } { above; or if you make some wonderful correction or enhancement, please } { notify us at the same address. Thank you. } { } { Turbo Pascal is a Copyright of Borland International Inc. } { -------------------------------------------------------------------------- } type lstr = string[255]; function EnvStr: lstr; { -------------------------------------------------------------------------- } { This function returns a string containing the DOS environment. } { -------------------------------------------------------------------------- } var i : integer; ev : integer absolute cseg:$002c; { pointer to environment in PSP } evstr: lstr; begin i := 0; { find real length of environment } while memw[ev:i] <> 0 do i := i+1; { 2 null bytes ends the environment } if i > 255 then i := 255; { limit of Turbo string length } Move(mem[ev:0],evstr[1],i+1); { move used bytes to Turbo string } evstr[0] := chr(i+1); { and set length field } end; function NextEnvStr(evstr: lstr; var strt: integer): lstr; { -------------------------------------------------------------------------- } { This function gets the string starting at STRT in EVSTR, delimited by } { CHR(0). A null string returned indicates the end of the environment. } { -------------------------------------------------------------------------- } var lst : lstr; i : integer; begin i := strt; if i > 1 then delete(evstr,1,i-1); { remove unneeded part of string } i := pos(chr(0),evstr); { find the next null char } if i < 2 then EnvStr := '' { 1-end of env; 0-ERROR!! } else begin move(evstr[1],lst[1],i-1); { move string to temp string } lst[0] := chr(i-1); { set string length field } strt := strt+i; { increment next field starting pos } EnvStr := lst { return string } end; end; function ReadEnv(keyword: lstr): lstr; { -------------------------------------------------------------------------- } { This function looks in the DOS environment for KEYWORD, and returns its } { current value. If KEYWORD isn't found, a null string is returned. } { -------------------------------------------------------------------------- } var i,lp : integer; { work variables } evarea, { Turbo string for environment } estr : lstr; begin for i := 1 to length(keyword) do keyword[i] := UpCase(keyword[i]); { environment parms are uppercase} if keyword[length(keyword)] <> '=' then { environment format is: } keyword := keyword + '='; { KEYWORD=value } lp := Length(keyword); { get length of parameter } evarea := EnvStr; { get DOS environment into string} i := 1; { set index to start at 1 } estr := NextEnvStr(evarea,i); { get first environment string } while (estr <> '') and { null string means exhausted search } (Copy(estr,1,lp) <> keyword) do { if it's not our keyword, skip it, } estr := NextEnvStr(evarea,i); { and extract the next string } if estr <> '' then { we found the string requested } Delete(estr,1,lp); { remove the KEYWORD= part } ReadEnv := estr; { set return value } end; \032\032\032\032\032\032\032\032\032\032\032\032\032\032\032\032\032\032-- John Knutson {ihnp4,allegra,ut-sally}!ut-ngp!auscso!knutson6 "I see everything once!" -- Catch-22 --- John Knutson {ihnp4,allegra,ut-sally}!ut-ngp!auscso!knutson6 "I see everything once!" -- Catch-22 -- Michael Lodman (619) 485-3335 Advanced Development NCR Corporation E&M San Diego mike.lodman@ivory.SanDiego.NCR.COM {sdcsvax,cbatt,dcdwest,nosc.ARPA,ihnp4}!ncr-sd!ivory!lodman