Back to index

glibc  2.9
tst-environ.c
Go to the documentation of this file.
00001 /* Copyright (C) 1999, 2004 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003 
00004    The GNU C Library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Lesser General Public
00006    License as published by the Free Software Foundation; either
00007    version 2.1 of the License, or (at your option) any later version.
00008 
00009    The GNU C Library is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Lesser General Public License for more details.
00013 
00014    You should have received a copy of the GNU Lesser General Public
00015    License along with the GNU C Library; if not, write to the Free
00016    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00017    02111-1307 USA.  */
00018 
00019 #include <errno.h>
00020 #include <stdio.h>
00021 #include <stdlib.h>
00022 #include <string.h>
00023 
00024 
00025 #define VAR "FOOBAR"
00026 
00027 char putenv_val[100] = VAR "=some longer value";
00028 
00029 int
00030 main (void)
00031 {
00032   int result = 0;
00033   const char *valp;
00034 
00035   /* First test: remove entry FOOBAR, whether it exists or not.  */
00036   unsetenv (VAR);
00037 
00038   /* Now getting the value should fail.  */
00039   if (getenv (VAR) != NULL)
00040     {
00041       printf ("There should be no `%s' value\n", VAR);
00042       result = 1;
00043     }
00044 
00045   /* Now add a value, with the replace flag cleared.  */
00046   if (setenv (VAR, "one", 0) != 0)
00047     {
00048       printf ("setenv #1 failed: %m\n");
00049       result = 1;
00050     }
00051 
00052   /* Getting this value should now be possible.  */
00053   valp = getenv (VAR);
00054   if (valp == NULL || strcmp (valp, "one") != 0)
00055     {
00056       puts ("getenv #2 failed");
00057       result = 1;
00058     }
00059 
00060   /* Try to replace without the replace flag set.  This should fail.  */
00061   if (setenv (VAR, "two", 0) != 0)
00062     {
00063       printf ("setenv #2 failed: %m\n");
00064       result = 1;
00065     }
00066 
00067   /* The value shouldn't have changed.  */
00068   valp = getenv (VAR);
00069   if (valp == NULL || strcmp (valp, "one") != 0)
00070     {
00071       puts ("getenv #3 failed");
00072       result = 1;
00073     }
00074 
00075   /* Now replace the value using putenv.  */
00076   if (putenv (putenv_val) != 0)
00077     {
00078       printf ("putenv #1 failed: %m\n");
00079       result = 1;
00080     }
00081 
00082   /* The value should have changed now.  */
00083   valp = getenv (VAR);
00084   if (valp == NULL || strcmp (valp, "some longer value") != 0)
00085     {
00086       printf ("getenv #4 failed (is \"%s\")\n", valp);
00087       result = 1;
00088     }
00089 
00090   /* Now one tricky check: changing the variable passed in putenv should
00091      change the environment.  */
00092   strcpy (&putenv_val[sizeof VAR], "a short one");
00093 
00094   /* The value should have changed again.  */
00095   valp = getenv (VAR);
00096   if (valp == NULL || strcmp (valp, "a short one") != 0)
00097     {
00098       puts ("getenv #5 failed");
00099       result = 1;
00100     }
00101 
00102   /* It should even be possible to rename the variable.  */
00103   strcpy (putenv_val, "XYZZY=some other value");
00104 
00105   /* Now a lookup using the old name should fail.  */
00106   if (getenv (VAR) != NULL)
00107     {
00108       puts ("getenv #6 failed");
00109       result = 1;
00110     }
00111 
00112   /* But using the new name it should work.  */
00113   valp = getenv ("XYZZY");
00114   if (valp == NULL || strcmp (valp, "some other value") != 0)
00115     {
00116       puts ("getenv #7 failed");
00117       result = 1;
00118     }
00119 
00120   /* Create a new variable with the old name.  */
00121   if (setenv (VAR, "a new value", 0) != 0)
00122     {
00123       printf ("setenv #3 failed: %m\n");
00124       result = 1;
00125     }
00126 
00127   /* At this point a getenv call must return the new value.  */
00128   valp = getenv (VAR);
00129   if (valp == NULL || strcmp (valp, "a new value") != 0)
00130     {
00131       puts ("getenv #8 failed");
00132       result = 1;
00133     }
00134 
00135   /* Black magic: rename the variable we added using putenv back.  */
00136   strcpy (putenv_val, VAR "=old name new value");
00137 
00138   /* This is interesting.  We have two variables with the same name.
00139      Getting a value should return one of them.  */
00140   valp = getenv (VAR);
00141   if (valp == NULL
00142       || (strcmp (valp, "a new value") != 0
00143          && strcmp (valp, "old name new value") != 0))
00144     {
00145       puts ("getenv #9 failed");
00146       result = 1;
00147     }
00148 
00149   /* More fun ahead: we are now removing the variable.  This should remove
00150      both values.  The cast is ok: this call should never put the string
00151      in the environment and it should never modify it.  */
00152   putenv ((char *) VAR);
00153 
00154   /* Getting the value should now fail.  */
00155   if (getenv (VAR) != NULL)
00156     {
00157       printf ("getenv #10 failed (\"%s\" found)\n", getenv (VAR));
00158       result = 1;
00159     }
00160 
00161   /* Now a test with an environment variable that's one character long.
00162      This is to test a special case in the getenv implementation.  */
00163   strcpy (putenv_val, "X=one character test");
00164   if (putenv (putenv_val) != 0)
00165     {
00166       printf ("putenv #2 failed: %m\n");
00167       result = 1;
00168     }
00169 
00170   valp = getenv ("X");
00171   if (valp == NULL || strcmp (valp, "one character test") != 0)
00172     {
00173       puts ("getenv #11 failed");
00174       result = 1;
00175     }
00176 
00177   /* Both setenv and unsetenv should return -1/EINVAL for NULL or "" name
00178      or if name contains '=' character.  */
00179   errno = 0;
00180   if (setenv (NULL, "val", 1) >= 0 || errno != EINVAL)
00181     {
00182       puts ("setenv #4 failed");
00183       result = 1;
00184     }
00185 
00186   errno = 0;
00187   if (setenv ("", "val", 0) >= 0 || errno != EINVAL)
00188     {
00189       puts ("setenv #5 failed");
00190       result = 1;
00191     }
00192 
00193   errno = 0;
00194   if (setenv ("var=val", "val", 1) >= 0 || errno != EINVAL)
00195     {
00196       puts ("setenv #6 failed");
00197       result = 1;
00198     }
00199 
00200   errno = 0;
00201   if (unsetenv (NULL) >= 0 || errno != EINVAL)
00202     {
00203       puts ("unsetenv #1 failed");
00204       result = 1;
00205     }
00206 
00207   errno = 0;
00208   if (unsetenv ("") >= 0 || errno != EINVAL)
00209     {
00210       puts ("unsetenv #2 failed");
00211       result = 1;
00212     }
00213 
00214   errno = 0;
00215   if (unsetenv ("x=y") >= 0 || errno != EINVAL)
00216     {
00217       puts ("unsetenv #3 failed");
00218       result = 1;
00219     }
00220 
00221   return result;
00222 }