Environment Variables

From Nutwiki
Revision as of 17:02, 27 October 2016 by Harald (Talk | contribs) (1 revision imported)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Test Environment

Hardware Comments Nut/OS
4.8.3
Nut/OS
4.8.7
Ethernut 1.3 H OK
Binaries
Compiler: ARM-GCC 4.3.2
OK
Binaries
Compiler: ARM-GCC 4.3.2
Ethernut 2.1 B OK
Binaries
Compiler: ARM-GCC 4.3.2
OK
Binaries
Compiler: ARM-GCC 4.3.2
Ethernut 3.0 E OK
Binaries
Compiler: ARM-GCC 4.3.3
OK
Binaries
Compiler: ARM-GCC 4.3.3

Description

In this example you will see how environment variables are used with Nut/OS.

In some environments "unsetenv" has a void return type while in some it has a return type of int. Since some versions newlib (ARM and AVR32) declares unsetenv as an int function, older versions expect it to be of type void. If you get compiler errors related to the return type of unsetenv, you have to enable it for your project.

File:Unsetenv.jpg

After enabling this option you should rebuild Nut/OS before running the example.

Source Code

<source lang="c">

  1. include <dev/board.h>
  2. include <stdio.h>
  3. include <stdlib.h>
  4. include <string.h>
  5. include <io.h>

int main(void) {

   unsigned long baud = 115200;
   char *env_val;              /* we'll use this to save the value of environment variables */
   NutRegisterDevice(&DEV_DEBUG, 0, 0);
   freopen(DEV_DEBUG_NAME, "w", stdout);
   _ioctl(_fileno(stdout), UART_SETSPEED, &baud);
   printf("\nEnvironment demonstration: getenv, setenv, putenv and unsetenv\n\n");
   env_val = getenv("example");
   if ((env_val == NULL) || (strcmp(env_val, "") == 0)) {
       printf("Environment variable 'example' doesn't exist yet and will be created.\n");
       putenv("example=created");
       printf("Please reset your board now to continue the demonstration.\n");
   } else {
       if (strcmp(env_val, "created") == 0) {
           printf("Environment variable 'example' already exists and will be updated.\n");
           setenv("example", "updated", 1);
           printf("Please reset your board now to continue the demonstration.\n");
       } else if (strcmp(env_val, "updated") == 0) {
           printf
               ("Environment variable 'example' already exists and has already been updated. It will now be cleared to allow this application to restart from it's initial state.\n");
           //unsetenv("example");
           setenv("example", "", 1);
           printf("Please reset your board now to begin the demonstration again from the start.\n");
       } else {
           setenv("example", "", 1);
       }
   }
   for (;;);
   return 0;

} </source>

Details

<source lang="c"> unsigned long baud = 115200;

char *env_val; /* we'll use this to save the value of environment variables */

NutRegisterDevice(&DEV_DEBUG, 0, 0);

freopen(DEV_DEBUG_NAME, "w", stdout); _ioctl(_fileno(stdout), UART_SETSPEED, &baud);

printf("\nEnvironment demonstration: getenv, setenv, putenv and unsetenv\n\n"); </source>

We set up our debug device, declare a char pointer for the value of our environment variable and output what this example is all about.

<source lang="c"> env_val = getenv("example"); </source>

Now we retrieve the value of the environment variable "example". getenv takes a char pointer as argument and returns the corresponding variable's value or NULL if the variable is not yet set.

<source lang="c"> if ((env_val == NULL) || (strcmp(env_val, "") == 0)) {

   printf("Environment variable 'example' doesn't exist yet and will be created.\n");
   putenv("example=created");
   printf("Please reset your board now to continue the demonstration.\n");

</source>

Now we check to see if our variable was not yet set. Technically a variable that was not yet set would return NULL. Due to a change in the newlib though unsetenv doesn't seem to work with Nut/OS right now so we "unset" variables by assigning an empty string to them. That's why we also have to check for an empty string here.

If the variable wasn't yet set, we set it to the value "created". putenv takes a single parameter, a key=value pair in the form of a string.

Since environment variables persist even after reset, we can now prompt the user to do exactly that. This time around though the variable exists which makes us branch into a different section of this example:

<source lang="c"> } else {

   if (strcmp(env_val, "created") == 0) {
       printf("Environment variable 'example' already exists and will be updated.\n");
       setenv("example", "updated", 1);
       printf("Please reset your board now to continue the demonstration.\n");

</source>

The variable is set so we can check it's value. If it says "created" we will update it to say "updated". This is achieved with setenv. The setenv function takes 3 arguments: A string containing the name of the variable, one with the new value and a number that specifies if we want to force updates. In this case we want to change the variable "example" to the value "updated" and force (1) updates. If we didn't force updates here, setenv would notice that "example" already exists, not change it's value at all and still return success.

Another reset is in order to let us branch into...

<source lang="c"> } else if (strcmp(env_val, "updated") == 0) {

   printf
      ("Environment variable 'example' already exists and has already been updated. It will now be cleared to allow this application to restart from it's initial state.\n");
   //unsetenv("example");
   setenv("example", "", 1);
   printf("Please reset your board now to begin the demonstration again from the start.\n");

</source>

If example has the value "updated" we want to unset it. As mentioned before unsetenv seems to be broken right now so I'll show you how to use it in the commented out line. It doesn't work though so we jus pretend it was unset if it has the value "" and set it to exactly that value using setenv. When unsetenv is fixed you can simply uncomment the unsetenv line, remove the one below it and the example will still work in the same way.

If we reset the application now, we're back at our initial state and the program will begin anew.

Output

<source lang="text"> Environment demonstration: getenv, setenv, putenv and unsetenv

Environment variable 'example' doesn't exist yet and will be created. Please reset your board now to continue the demonstration.

Environment demonstration: getenv, setenv, putenv and unsetenv

Environment variable 'example' already exists and will be updated. Please reset your board now to continue the demonstration.

Environment demonstration: getenv, setenv, putenv and unsetenv

Environment variable 'example' already exists and has already been updated. It w ill now be cleared to allow this application to restart from it's initial state. Please reset your board now to begin the demonstration again from the start. </source>

See also





Template:Languages