🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: comp.os.msdos.programmer
4 messages
4 total messages Started by Grant Slender Fri, 08 Aug 1997 00:00
How do I change an enviroment var?
#7966
Author: Grant Slender
Date: Fri, 08 Aug 1997 00:00
7 lines
1373 bytes
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
Re: How do I change an enviroment var?
#7964
Author: Mike Morris
Date: Thu, 07 Aug 1997 00:00
41 lines
2826 bytes
In article <33EA8504.49DA453@comtech.com.au> Grant Slender <grants@comtech.com.au> writes:
>From: Grant Slender <grants@comtech.com.au>
>Subject: How do I change an enviroment var?
>Date: Fri, 08 Aug 1997 12:31:32 +1000

>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

Since the running program inherits a duplicate set of the variables,
any changes get lost when the program exits back to DOS.

I got around that years ago with a very bad hack - my only excuse
is that it was a one-time program.

In the program I created and opened a file named $$!$$!$$.BAT,
and wrote a batch file into it.  It was two lines:

@SET VARAIBLE=FOO               <<-- make this line what you wnat to do.
@DEL $$!$$!$$.BAT               <<-- then delete the file that did it.

Before I exited my program, I poked the batch file name into the 
keyboard buffer. It worked.

There are better ways to do this but, but as I said, 
this was a quick one-time hack for a single-user system 
that I knew quite well.

Hoe that this stimulates the brain into thinking about alternative 
methods.


---------------------------------------------------------------------------
Mike Morris	ham radio: WA6ILQ     ICBM: 34:07:54.9 by 118:03:46.2
The above is my own opinion and should not be construed to be that of 
my former or my current employers.  Reply address spoofed to discourage 
junk email, please remove the "*" from the address.
Re: How do I change an enviroment var?
#7965
Author: Artur Anjos
Date: Thu, 07 Aug 1997 00:00
19 lines
1657 bytes

Hi,

The borland site have the answer to your problem: do a search using
environment in their techicnal bolletins. They have a class in C++, but if
you want, I can write you back in C.

By,

Artur
 Grant Slender wrote in article <33EA8504.49DA453@comtech.com.au>...
>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
> 
Re: How do I change an enviroment var?
#7967
Author: 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
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads