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 
00043 /*
00044  * Save environment in non-volatile memory.
00045  */
00046 static int save_env(void)
00047 {
00048     int rc = 0;
00049 
00050     if (nut_environ) {
00051         NUTENVIRONMENT *envp;
00052         size_t len;
00053         uint32_t magic = ENVIRON_MAGIC;
00054         int addr = ENVIRON_EE_OFFSET;
00055 
00056         if (NutNvMemSave(addr, &magic, sizeof(magic)) == 0) {
00057             addr += sizeof(magic);
00058             for (envp = nut_environ; envp; envp = envp->env_next) {
00059                 len = strlen(envp->env_name) + 1;
00060                 if ((rc = NutNvMemSave(addr, envp->env_name, len)) != 0) {
00061                     break;
00062                 }
00063                 addr += len;
00064                 if (envp->env_value) {
00065                     len = strlen(envp->env_value) + 1;
00066                     if ((rc = NutNvMemSave(addr, envp->env_value, len)) != 0) {
00067                         break;
00068                     }
00069                 } else {
00070                     len = 1;
00071                     if ((rc = NutNvMemSave(addr, "", len)) != 0) {
00072                         break;
00073                     }
00074                 }
00075                 addr += len;
00076             }
00077             if (rc == 0) {
00078                 rc = NutNvMemSave(addr, "", 1);
00079             }
00080         }
00081     }
00082     return rc;
00083 }
00084 
00095 int setenv(CONST char *name, CONST char *value, int force)
00096 {
00097     NUTENVIRONMENT *envp;
00098     NUTENVIRONMENT *nxtp;
00099     NUTENVIRONMENT *prvp = NULL;
00100 
00101     if ((envp = findenv(name)) == NULL) {
00102         if ((envp = malloc(sizeof(NUTENVIRONMENT))) == NULL) {
00103             return -1;
00104         }
00105         memset(envp, 0, sizeof(NUTENVIRONMENT));
00106         if ((envp->env_name = strdup(name)) == NULL) {
00107             free(envp);
00108             return -1;
00109         }
00110 
00111     for (nxtp = nut_environ; nxtp; nxtp = nxtp->env_next) {
00112         if (strcmp(envp->env_name, nxtp->env_name) < 0) {
00113         if (nxtp->env_prev) {
00114             nxtp->env_prev->env_next = envp;
00115         } else {
00116             nut_environ = envp;
00117         }
00118         envp->env_next = nxtp;
00119         envp->env_prev = nxtp->env_prev;
00120         nxtp->env_prev = envp;
00121         break;
00122         }
00123             prvp = nxtp;
00124     }
00125     if (nxtp == NULL) {
00126         if (prvp) {
00127         prvp->env_next = envp;
00128         envp->env_prev = prvp;
00129         } else {
00130         nut_environ = envp;
00131         }
00132     }
00133         force = 1;
00134     }
00135     if (force) {
00136         if (envp->env_value) {
00137             if (strcmp(envp->env_value, value)) {
00138                 free(envp->env_value);
00139             }
00140             else {
00141                 force = 0;
00142             }
00143         }
00144         if (force) {
00145             if ((envp->env_value = strdup(value)) == NULL) {
00146                 return -1;
00147             }
00148             return save_env();
00149         }
00150     }
00151     return 0;
00152 }
00153 
00163 void unsetenv(CONST char *name)
00164 {
00165     NUTENVIRONMENT *envp;
00166 
00167     if ((envp = findenv(name)) == NULL) {
00168         errno = ENOENT;
00169     return;
00170     }
00171     if (envp->env_prev) {
00172         envp->env_prev->env_next = envp->env_next;
00173     }
00174     if (envp->env_next) {
00175     envp->env_next->env_prev = envp->env_prev;
00176     }
00177     if (nut_environ == envp) {
00178     nut_environ = envp->env_next;
00179     }
00180     free(envp->env_name);
00181     free(envp->env_value);
00182     free(envp);
00183 
00184     save_env();
00185 }
00186 

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