Prex Home / Browse Source - Prex Version: 0.9.0

root/usr/test/environ/environ.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. oops
  2. show_env
  3. main

   1 /*-
   2  * Copyright (c) 2009, Kohsuke Ohtani
   3  * All rights reserved.
   4  *
   5  * Redistribution and use in source and binary forms, with or without
   6  * modification, are permitted provided that the following conditions
   7  * are met:
   8  * 1. Redistributions of source code must retain the above copyright
   9  *    notice, this list of conditions and the following disclaimer.
  10  * 2. Redistributions in binary form must reproduce the above copyright
  11  *    notice, this list of conditions and the following disclaimer in the
  12  *    documentation and/or other materials provided with the distribution.
  13  * 3. Neither the name of the author nor the names of any co-contributors
  14  *    may be used to endorse or promote products derived from this software
  15  *    without specific prior written permission.
  16  *
  17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  27  * SUCH DAMAGE.
  28  */
  29 
  30 /*
  31  * environ.c - test POSIX environment variable
  32  */
  33 
  34 #include <stdio.h>
  35 #include <stdlib.h>
  36 #include <unistd.h>
  37 
  38 extern char **environ;
  39 
  40 static void
  41 oops(void)
  42 {
  43         printf("oops!\n");
  44         exit(1);
  45 }
  46 
  47 static void
  48 show_env(void)
  49 {
  50         char **envp = environ;
  51         int i;
  52 
  53         for (i = 0; envp[i] != NULL; i++) {
  54                 printf("envp[%d]: %s\n", i, envp[i]);
  55         }
  56 }
  57 
  58 int
  59 main(int argc, char *argv[])
  60 {
  61         char *val;
  62 
  63         printf("test environment variables\n");
  64 
  65         /* Show environment passed by shell */
  66         printf("show env\n");
  67         show_env();
  68 
  69         /* Set new viriable */
  70         printf("setenv-1\n");
  71         setenv("PATH", "/boot", 1);
  72         show_env();
  73 
  74         /* Overwirte existing viriable */
  75         printf("setenv-2\n");
  76         setenv("PATH", "/boot:/bin", 1);
  77         show_env();
  78 
  79         /* No overwirte please... */
  80         printf("setenv-3\n");
  81         setenv("PATH", "/boot", 0);
  82         show_env();
  83 
  84         /* No overwirte please... */
  85         printf("setenv-4\n");
  86         setenv("PATH", "/boot", 0);
  87         show_env();
  88 
  89         /* Get non existing variable */
  90         printf("getenv-1\n");
  91         val = getenv("TMP");
  92         if (val != NULL)
  93                 oops();
  94 
  95         /* Get existing variable */
  96         printf("getenv-2\n");
  97         val = getenv("PATH");
  98         if (val == NULL)
  99                 oops();
 100         printf("PATH=%s\n", val);
 101 
 102         /* putenv */
 103         printf("putenv-1\n");
 104         putenv("TMP=/tmp");
 105         show_env();
 106 
 107         printf("putenv-2\n");
 108         putenv("ABC=/abc");
 109         show_env();
 110 
 111         exit(0);
 112 }

/* [<][>][^][v][top][bottom][index][help] */