🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.os.msdos.programmer
Article #7967

Re: How do I change an enviroment var?

#7967
From: Rick
Date: Sat, 16 Aug 1997 00:00
111 lines
3864 bytes
Grant Slender <grants@comtech.com.au> wrote:

>I need to get the list of current environment variables in a Dos based C
>program....I can do that using the "main( " argument lists....

>But how do I write a changed value back???

>Thanks for any tips

An example program

cseg	segment
;1) Getting all environment vars

;A simple program which uses the stdlib from Rhyde en co..
;include	stdlib.inc; that is both all includes and the shell..

print
db	'Display of environment vars ',cr,lf,cr,lf,0

;get ptr to psp
mov	ah,	62h	;stdlib sets es to dgroup on startup
int	21h		;normally es points to PSP at startup	
mov	es,	bx
;get ptr to env
mov	bx,	es:[2Ch]	es:[0] points to your PSP
mov	es,	bx	
xor	di,	di	es:[di] points to your environment
jmp	short	startit
printLP: mov	ah,	0eh
	int	10h
startit:	mov	al,	es:[di]
	inc	di
	or	al,	al
	jnz	printLP
	putcr
	mov	al,	es:[di]
	inc	di
	or	al,	al
	jnz	printLP
	exitPGM
	putcr

2)	Getting on environment variables value
	print
	db	'Searching for path',cr,lf,cr,lf,0
        cld                     ;forward search
        mov     al,     '='
        mov     dx,     di      ;di points to end of environment
        xor     di,     di
        mov     cx,     dx      ;set cx to len environ first
searchLP:
        repnz   scasb
        jnz     notfound
        mov     bx,     di      ;save di
        mov     cx,     env_var_len
        sub     di,     cx
        lea     si,     env_var
        repz    cmpsb           ;compare strings
        jz      found
        mov     di,     bx      ;restore di
        mov     cx,     dx      ;restore cx
        sub     cx,     bx
        jmp     searchLP

found:  puts
        putcr
;set a specific environ variable
        print
        db      'Changed path value to ',cr,lf,cr,lf,0
        lea     si,     new_path
        mov     dx,     di      ;save di again
copyLP:	lodsb
        stosb
        or      al,     al
        jnz     copyLP
        mov     di,     dx      ;restore di
        puts
        putcr

notfound:
        exitPgm


cseg    ends
dseg    segment

env_var         db      'PATH='
env_var_len     dw      $-env_var
new_path        db      'C:\DOS\',0

dseg    ends


end     main

I did not tested this program thoroughly but it should be
enough to get you started. The environment is really
not that difficult. A few other things:

;environment variables are zero terminated in the format:
; ' NAME= ', value,0
;environment is ended with 0,0


R.Hummel has made some nice utility available for doing this
in PCmagazine as envedit. You should look for that 
maybe.

Rick
Technical Details
Message-ID: <eGlZ2wyq8GA.176@ntdwwaaw.compuserve.com>
Path: arc1-ca.pugleaf.net!unknown!not-for-mail
References: <33EA8504.49DA453@comtech.com.au>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 10f6aa,b241f3541a61f304
X-Google-Attributes: gid10f6aa,public
X-Google-Thread: 103f30,b241f3541a61f304
X-Google-Attributes: gid103f30,public
X-Google-Thread: 102bfa,b241f3541a61f304
X-Google-Attributes: gid102bfa,public
X-Google-Thread: f9e32,91146e1c4400f0ce,start
X-Google-Attributes: gidf9e32,public
X-Google-Thread: 10b565,b241f3541a61f304
X-Google-Attributes: gid10b565,public
X-Google-Thread: fc546,b241f3541a61f304
X-Google-Attributes: gidfc546,public
X-Google-Thread: f75c9,b241f3541a61f304
X-Google-Attributes: gidf75c9,public
X-Google-Thread: 104805,b241f3541a61f304
X-Google-Attributes: gid104805,public
From: 113346.2611@compuserve.com (Rick)
Subject: Re: How do I change an enviroment var?
Date: 1997/08/16
Message-ID: <eGlZ2wyq8GA.176@ntdwwaaw.compuserve.com>
X-Deja-AN: 264797221
References: <33EA8504.49DA453@comtech.com.au>
Newsgroups: comp.os.msdos.programmer,unix-pc.general,it.comp.dos,abg.ms-dos,alt.msdos.batch,alt.msdos.programmer,backbone.fragen.dos,comp.msdos.programmer,comp.os.msdos,comp.unix.dos-under-unix,de.comp.os.msdos,fr.comp.os.msdos,ger.pc.dos,relcom.msdos