[comp.os.msdos.programmer] DOS Environment Variables

shaunc@gold.gvg.tek.com (Shaun Case) (05/07/91)

In article <91125.051531AURPS@ASUACAD.BITNET> AURPS@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@gold.gvg.tek.com  atman%ecst.csuchico.edu@RELAY.CS.NET 
Postmaster of 1:119/666  1@9651 (WWIVnet)

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

Norbert.Zacharias@arbi.informatik.uni-oldenburg.de (Norbert Zacharias) (05/10/91)

shaunc@gold.gvg.tek.com (Shaun Case) writes:

>In article <91125.051531AURPS@ASUACAD.BITNET> AURPS@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.

The problem is to find the environmentspace of command.com because of the
PUTENV() works on the environment of the current program. Sometimes ago
i read an program in the Ct (a german computer magazin) witch deals with this
task. I think it was in 1988 or 87.
If i able to find this article i'll post it
Norbert




























































































>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@gold.gvg.tek.com  atman%ecst.csuchico.edu@RELAY.CS.NET 
>Postmaster of 1:119/666  1@9651 (WWIVnet)

>It's enough to destroy a young moose's faith! -- Bullwinkle
-- 
=============================================================================
Norbert Zacharias          Norbert.Zacharias@arbi.informatik.uni-oldenburg.de
FB Physik                                               148964@DOLUNI1.bitnet
Carl-von-Ossietzky-Universitaet
Tel. 0049-441-7983527
 Was Du nicht willst das man Dir tu, das will auch nicht was willst denn Du?
							   Heinz Erhard
=============================================================================