environ.c

Go to the documentation of this file.
00001 /*
00002  * Copyright 2008 by egnite Software GmbH
00003  *
00004  * Redistribution and use in source and binary forms, with or without
00005  * modification, are permitted provided that the following conditions
00006  * are met:
00007  *
00008  * 1. Redistributions of source code must retain the above copyright
00009  *    notice, this list of conditions and the following disclaimer.
00010  * 2. Redistributions in binary form must reproduce the above copyright
00011  *    notice, this list of conditions and the following disclaimer in the
00012  *    documentation and/or other materials provided with the distribution.
00013  * 3. Neither the name of the copyright holders nor the names of
00014  *    contributors may be used to endorse or promote products derived
00015  *    from this software without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00018  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00019  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00020  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00021  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00022  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00023  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
00024  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
00025  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00026  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
00027  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00028  * SUCH DAMAGE.
00029  *
00030  * For additional information see http://www.ethernut.de/
00031  */
00032 
00033 #include <dev/nvmem.h>
00034 
00035 #include <stdlib.h>
00036 #include <string.h>
00037 
00038 #include <sys/environ.h>
00039 
00040 NUTENVIRONMENT *nut_environ;
00041 
00042 /*
00043  * Read string from non-volatile memory.
00044  */
00045 static size_t read_string(int *addr, char *buf, size_t siz)
00046 {
00047     size_t rc = 0;
00048     char ch;
00049 
00050     while (rc <= siz) {
00051         if (NutNvMemLoad(*addr, &ch, 1)) {
00052             rc = 0;
00053             break;
00054         }
00055         (*addr)++;
00056         if (ch == 0) {
00057             break;
00058         }
00059         if (buf) {
00060             *buf++ = ch;
00061         }
00062         rc++;
00063     }
00064     if (buf) {
00065         *buf = 0;
00066     }
00067     return rc;
00068 }
00069 
00078 NUTENVIRONMENT *findenv(CONST char *name)
00079 {
00080     NUTENVIRONMENT *envp;
00081     size_t len;
00082     size_t max_len = 0;
00083     char *buf;
00084 
00085     if (nut_environ == NULL) {
00086         /* Load environment from non-volatile memory. */
00087         u_long magic;
00088         int addr = ENVIRON_EE_OFFSET;
00089         NUTENVIRONMENT *prvp = NULL;
00090 
00091         if (NutNvMemLoad(addr, &magic, sizeof(magic)) == 0 && magic == ENVIRON_MAGIC) {
00092             addr += 4;
00093             /* Determine the maximum string length. */
00094             while ((len = read_string(&addr, NULL, MAX_ENVIRON_ITEM_SIZE)) > 0) {
00095                 if (len > max_len) {
00096                     max_len = len;
00097                 }
00098                 len = read_string(&addr, NULL, MAX_ENVIRON_ITEM_SIZE);
00099                 if (len > max_len) {
00100                     max_len = len;
00101                 }
00102             }
00103             /* Read the environment. */
00104             addr = ENVIRON_EE_OFFSET + sizeof(magic);
00105             if (max_len && (buf = malloc(max_len + 1)) != NULL) {
00106                 while ((len = read_string(&addr, buf, max_len)) > 0) {
00107                     if ((envp = malloc(sizeof(NUTENVIRONMENT))) == NULL) {
00108                         break;
00109                     }
00110                     memset(envp, 0, sizeof(NUTENVIRONMENT));
00111                     if ((envp->env_name = malloc(len + 1)) == NULL) {
00112                         free(envp);
00113                         break;
00114                     }
00115                 strcpy(envp->env_name, buf);
00116                     len = read_string(&addr, buf, max_len);
00117                     if ((envp->env_value = malloc(len + 1)) == NULL) {
00118                         break;
00119                     }
00120                     strcpy(envp->env_value, buf);
00121                 if (prvp) {
00122                     prvp->env_next = envp;
00123                         envp->env_prev = prvp;
00124                 } else {
00125                 nut_environ = envp;
00126                 }
00127                     prvp = envp;
00128                 }
00129                 free(buf);
00130             }
00131         }
00132     }
00133     for (envp = nut_environ; envp; envp = envp->env_next) {
00134         if (strcmp(envp->env_name, name) == 0) {
00135         break;
00136         }
00137     }
00138     return envp;
00139 }
00140 

© 2000-2007 by egnite Software GmbH - visit http://www.ethernut.de/