DOS Environment Variables

Shaun Case shaunc at gold.gvg.tek.com
Tue May 7 13:34:42 AEST 1991


In article <91125.051531AURPS at ASUACAD.BITNET> AURPS at ASUACAD.BITNET writes:
>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.

I've tried a few things, and the following is what has worked for me.  It
also happens to be portable betweem different MSDOS C compilers and versions
of MSDOS.

Additionally, this is really an MSDOS-specific question, so I have directed
followups to comp.os.msdos.programmer.

To set a master environment variable, try using the following batchfile
with your program:


File GO.BAT:
------------- cut here --------------
echo off
REM use '@echo off' if you have dos 3.3 or higher
set

REM your program name here:
test

command /c setvar
REM use 'call setvar' if you have dos 3.3 or higher
set
------------- cut here --------------



here is test.c:
------------- cut here --------------
/*********************
   set_env_var.c
   Shaun Case, 1991
   Public Domain
 *********************/

#include <stdio.h>

int main()
{
    FILE *batfile;
    char varname[134];
    char value[134];

    printf("\nEnter variable name: ");
    gets(varname);
    printf("Enter value: ");
    gets(value);
    puts("");

    if ((batfile = fopen("SETVAR.BAT", "w")) == NULL)
    {
        puts("Unable to open SETVAR.BAT, Omot.  Bailing.\n\n");
        return 1;
    }

    fprintf(batfile, "SET %s=%s\n", varname, value);

    fclose(batfile);

    return 0;
}
------------- cut here --------------

I got the following output:

COMSPEC=C:\4DOS.COM
CMDLINE=go
PATH=f:\tmp;c:\sys\util;c:\sys\bat;d:\borlandc\bin;d:\tc;C:\DOS;C:\WIN386;C:\;C:\TCP;c:\vga;c:\f-prot
TEMP=f:\tmp
TMP=f:\tmp
FTPINIT=c:\tcp\init.tbl
FTP_ATTR=0x71,0x74,0x21
FTP_CONFIG=c:\tcp\ftp.cfg
USER=@Man
PROMPT=$p$g

Enter variable name: hodag
Enter value: badger_nemesis

COMSPEC=C:\4DOS.COM
CMDLINE=setvar
PATH=f:\tmp;c:\sys\util;c:\sys\bat;d:\borlandc\bin;d:\tc;C:\DOS;C:\WIN386;C:\;C:\TCP;c:\vga;c:\f-prot
TEMP=f:\tmp
TMP=f:\tmp
FTPINIT=c:\tcp\init.tbl
FTP_ATTR=0x71,0x74,0x21
FTP_CONFIG=c:\tcp\ftp.cfg
USER=@Man
PROMPT=$p$g
HODAG=badger_nemesis

-- 
shaunc at gold.gvg.tek.com  atman%ecst.csuchico.edu at RELAY.CS.NET 
Postmaster of 1:119/666  1 at 9651 (WWIVnet)

It's enough to destroy a young moose's faith! -- Bullwinkle



More information about the Comp.lang.c mailing list