Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!dali.cs.montana.edu!uakari.primate.wisc.edu!sdd.hp.com!decwrl!fernwood!portal!cup.portal.com!robc From: robc@cup.portal.com (Rob X Cowan) Newsgroups: comp.lang.c Subject: Re: DOS Environment Variables Message-ID: <42084@cup.portal.com> Date: 7 May 91 09:12:02 GMT References: <91125.051531AURPS@ASUACAD.BITNET> Organization: The Portal System (TM) Lines: 75 > Does anyone know how to permanently change a DOS environment variable from > within an executing program? PUTENV() changes/creates a variable but it > is only in effect while the program is running. As soon as I go back to > DOS, the environment is restored to its original variables. I'm using > Turbo C. > > Thanks for any help, > Rick Schatzman The following code demonstrates how to access the global environment string. It's not the only way, nor the best, but it'll get you started. masterenvstring is a pointer to the start of the environment list, in which each variable is an ASCIIZ string of the format =. The list is terminated by a double null. masterenvsize is the size of the Memory Control block that is allocated for the environment string; I didn't put any code to deal with its manipulation, but you'll have to deal with it if you plan on adding entries or you risk a system halt by COMMAND.COM.. fun. /* * Demonstrates traversal of master environment string * Rob S. Cowan 9105.07 */ #include char *env_seekend(char far *); void main(void) { union REGS in, out; struct SREGS segs; char far *masterenvstr; int masterenvsize; /* * I don't remember if 2E is a supported function or not.. Nothing * must be linked ahead of COMMAND.COM, or that address will be * returned. */ in.x.ax = 0x352E; /* Get PSP of COMMAND.COM (sortof) */ intdosx(&in, &out, &segs); /* Get env asciiz string : seg stored at offset 2C of PSP */ masterenvstr = MK_FP(*(unsigned *)MK_FP(segs.es, 0x2C), 0); /* Get size of env block from MCB preceding it */ masterenvsize = (*(unsigned *)MK_FP(FP_SEG(masterenvstr) - 1, 3)) << 4; /* * You must take care to accommodate the memory requirements of * the environment string. If you wish to add entries you will * have to allocate/deallocate it with the MCB that precedes the * environment. */ } /* * Environment strings are a list of consecutive ASCIIZ strings * terminated by a null entry. This function will traverse to the * end of this list and return the address of the null entry. */ char *env_seekend(char far *envtrav) { for(;;) { if (!*envtrav++) if (!*envtrav) return envtrav; } } Prost, -Rob robc@cup.portal.com