----- Original Message -----
From: "arun kumar" <dha_arunbe@yahoo.com>
To: <C-Guru@yahoogroups.com>
Sent: Thursday, March 18, 2004 5:19 PM
Subject: [C-Guru] to set dos variables


> hi every body
>
> can we SET DOS variables (PATH ...) using c program
> if so how is it possible
>
> plz refer
>
>       Arun Kumar D
>

 1  #include <stdio.h>
 2  #include <stdlib.h>
 3  #include <string.h>
 4
 5  #define ENV_VAR "PATH"
 6  #define NEW_VAL "F:\\Calvin"
 7
 8  int
 9  main ( void )
10  {
11      char *ptr = getenv ( ENV_VAR );
12      char *env;
13
14      if ( !ptr )
15      {
16          perror ( "" );
17          return EXIT_FAILURE;
18      }
19
20      printf ( "Current value for the environment variable:\n %s is %s\n",
21                              ENV_VAR, ptr );
22      env = malloc ( strlen ( ptr ) + strlen ( ENV_VAR ) + 1 );
23
24      if ( !env )
25      {
26          perror ( "" );
27          return EXIT_FAILURE;
28      }
29      sprintf ( env, "set %s=%%%s%%;%s", ENV_VAR, ENV_VAR, NEW_VAL );
30
31      if ( system ( env ) == EXIT_SUCCESS )
32      {
33          ptr = getenv ( ENV_VAR );
34          if ( ptr )
35              printf ( "Updated value for the environment variable:\n %s is %s\n",
36                        ENV_VAR, ptr );
37      }
38      return EXIT_SUCCESS;
39  }

F:\Vijay\C> gcc environ.c -Wall

There are few points which are worth mentioning here:

    *   The working of a shell.  A command like this

            F:\Vijay\C> environ.exe

        is handled by the shell in this manner:

                     _____________
                    |  Parent     |  (Command Interpreter)
                    |       Shell |
                     -------------
                          |  Creates a clone of itself;
                   clone  |  Exports the current enivornment
                          |  to the new shell
                     _____________
                    |   Command   |  (environ.exe)
                     -------------
                          |  This program launches another shell
                          |  using the `system()' function
                     _____________
                    | set PATH=.. |
                     -------------
                          |  Actually, it's this new shell's enviornment
                          |  that is set, not that of the program!
                     _____________
                    |   Command   |  (environ.exe)
                     -------------
                          |  Control returns back to the program with
                          |  original environment restored


    *   Some implementations provide an extension to set the environmental
        values.  Example:

        int     putenv(const char *_val);
        int     setenv(const char *_var, const char *_val, int _replace);

    *   If the program really depends on specific environmental variables,
        either a shell script or a batch file, depending on the OS type,
        can be used to launch the program after setting the environment.
        For example, in a DOS related application:

        F:\Vijay\C> type launch.bat

        @REM  blah blah blah ...
        set PATH=%PATH%;F:\Calvin
        environ.exe

        REM -- end of launch.bat --