Nut/OS  4.10.3
API Reference
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 <stdint.h>
00036 #include <stdlib.h>
00037 #include <string.h>
00038 #include <memdebug.h>
00039 
00040 #include <sys/nutdebug.h>
00041 #include <sys/environ.h>
00042 
00043 NUTENVIRONMENT *nut_environ;
00044 
00045 /*
00046  * Read string from non-volatile memory.
00047  */
00048 static size_t read_string(int *addr, char *buf, size_t siz)
00049 {
00050     size_t rc = 0;
00051     char ch;
00052 
00053     while (rc <= siz) {
00054         if (NutNvMemLoad(*addr, &ch, 1)) {
00055             rc = 0;
00056             break;
00057         }
00058         (*addr)++;
00059         if (ch == 0) {
00060             break;
00061         }
00062         if (buf) {
00063             *buf++ = ch;
00064         }
00065         rc++;
00066     }
00067     if (buf) {
00068         *buf = 0;
00069     }
00070     return rc;
00071 }
00072 
00081 NUTENVIRONMENT *findenv(CONST char *name)
00082 {
00083     NUTENVIRONMENT *envp;
00084     size_t len;
00085     size_t max_len = 0;
00086     char *buf;
00087 
00088     NUTASSERT(name != NULL);
00089 
00090     if (nut_environ == NULL) {
00091         /* Load environment from non-volatile memory. */
00092         uint32_t magic;
00093         int addr = ENVIRON_EE_OFFSET;
00094         NUTENVIRONMENT *prvp = NULL;
00095 
00096         if (NutNvMemLoad(addr, &magic, sizeof(magic)) == 0 && magic == ENVIRON_MAGIC) {
00097             addr += 4;
00098             /* Determine the maximum string length. */
00099             while ((len = read_string(&addr, NULL, MAX_ENVIRON_ITEM_SIZE)) > 0) {
00100                 if (len > max_len) {
00101                     max_len = len;
00102                 }
00103                 len = read_string(&addr, NULL, MAX_ENVIRON_ITEM_SIZE);
00104                 if (len > max_len) {
00105                     max_len = len;
00106                 }
00107             }
00108             /* Read the environment. */
00109             addr = ENVIRON_EE_OFFSET + sizeof(magic);
00110             if (max_len && (buf = malloc(max_len + 1)) != NULL) {
00111                 while ((len = read_string(&addr, buf, max_len)) > 0) {
00112                     if ((envp = malloc(sizeof(NUTENVIRONMENT))) == NULL) {
00113                         break;
00114                     }
00115                     memset(envp, 0, sizeof(NUTENVIRONMENT));
00116                     if ((envp->env_name = malloc(len + 1)) == NULL) {
00117                         free(envp);
00118                         break;
00119                     }
00120                     strcpy(envp->env_name, buf);
00121                     len = read_string(&addr, buf, max_len);
00122                     if ((envp->env_value = malloc(len + 1)) == NULL) {
00123                         break;
00124                     }
00125                     strcpy(envp->env_value, buf);
00126                     if (prvp) {
00127                         prvp->env_next = envp;
00128                         envp->env_prev = prvp;
00129                     } else {
00130                         nut_environ = envp;
00131                     }
00132                     prvp = envp;
00133                 }
00134                 free(buf);
00135             }
00136         }
00137     }
00138     for (envp = nut_environ; envp; envp = envp->env_next) {
00139         if (strcmp(envp->env_name, name) == 0) {
00140             break;
00141         }
00142     }
00143     return envp;
00144 }
00145