Nut/OS  4.10.3
API Reference
setenv.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 <errno.h>
00039 #include <memdebug.h>
00040 
00041 #include <sys/environ.h>
00042 #include <cfg/crt.h>
00043 
00044 /*
00045  * Save environment in non-volatile memory.
00046  */
00047 static int save_env(void)
00048 {
00049     int rc = 0;
00050 
00051     if (nut_environ) {
00052         NUTENVIRONMENT *envp;
00053         size_t len;
00054         uint32_t magic = ENVIRON_MAGIC;
00055         int addr = ENVIRON_EE_OFFSET;
00056 
00057         if (NutNvMemSave(addr, &magic, sizeof(magic)) == 0) {
00058             addr += sizeof(magic);
00059             for (envp = nut_environ; envp; envp = envp->env_next) {
00060                 len = strlen(envp->env_name) + 1;
00061                 if ((rc = NutNvMemSave(addr, envp->env_name, len)) != 0) {
00062                     break;
00063                 }
00064                 addr += len;
00065                 if (envp->env_value) {
00066                     len = strlen(envp->env_value) + 1;
00067                     if ((rc = NutNvMemSave(addr, envp->env_value, len)) != 0) {
00068                         break;
00069                     }
00070                 } else {
00071                     len = 1;
00072                     if ((rc = NutNvMemSave(addr, "", len)) != 0) {
00073                         break;
00074                     }
00075                 }
00076                 addr += len;
00077             }
00078             if (rc == 0) {
00079                 rc = NutNvMemSave(addr, "", 1);
00080             }
00081         }
00082     }
00083     return rc;
00084 }
00085 
00096 int setenv(CONST char *name, CONST char *value, int force)
00097 {
00098     NUTENVIRONMENT *envp;
00099     NUTENVIRONMENT *nxtp;
00100     NUTENVIRONMENT *prvp = NULL;
00101 
00102     if ((envp = findenv(name)) == NULL) {
00103         if ((envp = malloc(sizeof(NUTENVIRONMENT))) == NULL) {
00104             return -1;
00105         }
00106         memset(envp, 0, sizeof(NUTENVIRONMENT));
00107         if ((envp->env_name = strdup(name)) == NULL) {
00108             free(envp);
00109             return -1;
00110         }
00111 
00112         for (nxtp = nut_environ; nxtp; nxtp = nxtp->env_next) {
00113             if (strcmp(envp->env_name, nxtp->env_name) < 0) {
00114                 if (nxtp->env_prev) {
00115                     nxtp->env_prev->env_next = envp;
00116                 } else {
00117                     nut_environ = envp;
00118                 }
00119                 envp->env_next = nxtp;
00120                 envp->env_prev = nxtp->env_prev;
00121                 nxtp->env_prev = envp;
00122                 break;
00123             }
00124             prvp = nxtp;
00125         }
00126         if (nxtp == NULL) {
00127             if (prvp) {
00128                 prvp->env_next = envp;
00129                 envp->env_prev = prvp;
00130             } else {
00131                 nut_environ = envp;
00132             }
00133         }
00134         force = 1;
00135     }
00136     if (force) {
00137         if (envp->env_value) {
00138             if (strcmp(envp->env_value, value)) {
00139                 free(envp->env_value);
00140             }
00141             else {
00142                 force = 0;
00143             }
00144         }
00145         if (force) {
00146             if ((envp->env_value = strdup(value)) == NULL) {
00147                 return -1;
00148             }
00149             return save_env();
00150         }
00151     }
00152     return 0;
00153 }
00154 
00155 
00156 
00166 #ifdef CRT_UNSETENV_POSIX
00167 int unsetenv(CONST char *name)
00168 {
00169     NUTENVIRONMENT *envp;
00170 
00171     if ((envp = findenv(name)) == NULL) {
00172         errno = ENOENT;
00173         return -1;
00174     }
00175     if (envp->env_prev) {
00176         envp->env_prev->env_next = envp->env_next;
00177     }
00178     if (envp->env_next) {
00179         envp->env_next->env_prev = envp->env_prev;
00180     }
00181     if (nut_environ == envp) {
00182         nut_environ = envp->env_next;
00183     }
00184     free(envp->env_name);
00185     free(envp->env_value);
00186     free(envp);
00187 
00188     save_env();
00189     return 0;
00190 }
00191 
00192 
00193 #else 
00194 
00195 void unsetenv(CONST char *name)
00196 {
00197     NUTENVIRONMENT *envp;
00198 
00199     if ((envp = findenv(name)) == NULL) {
00200         errno = ENOENT;
00201         return;
00202     }
00203     if (envp->env_prev) {
00204         envp->env_prev->env_next = envp->env_next;
00205     }
00206     if (envp->env_next) {
00207         envp->env_next->env_prev = envp->env_prev;
00208     }
00209     if (nut_environ == envp) {
00210         nut_environ = envp->env_next;
00211     }
00212     free(envp->env_name);
00213     free(envp->env_value);
00214     free(envp);
00215 
00216     save_env();
00217 }
00218 
00219 #endif