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 
00040 #include <sys/environ.h>
00041 
00042 /*
00043  * Save environment in non-volatile memory.
00044  */
00045 static int save_env(void)
00046 {
00047     int rc = 0;
00048 
00049     if (nut_environ) {
00050         NUTENVIRONMENT *envp;
00051         size_t len;
00052         uint32_t magic = ENVIRON_MAGIC;
00053         int addr = ENVIRON_EE_OFFSET;
00054 
00055         if (NutNvMemSave(addr, &magic, sizeof(magic)) == 0) {
00056             addr += sizeof(magic);
00057             for (envp = nut_environ; envp; envp = envp->env_next) {
00058                 len = strlen(envp->env_name) + 1;
00059                 if ((rc = NutNvMemSave(addr, envp->env_name, len)) != 0) {
00060                     break;
00061                 }
00062                 addr += len;
00063                 if (envp->env_value) {
00064                     len = strlen(envp->env_value) + 1;
00065                     if ((rc = NutNvMemSave(addr, envp->env_value, len)) != 0) {
00066                         break;
00067                     }
00068                 } else {
00069                     len = 1;
00070                     if ((rc = NutNvMemSave(addr, "", len)) != 0) {
00071                         break;
00072                     }
00073                 }
00074                 addr += len;
00075             }
00076             if (rc == 0) {
00077                 rc = NutNvMemSave(addr, "", 1);
00078             }
00079         }
00080     }
00081     return rc;
00082 }
00083 
00094 int setenv(CONST char *name, CONST char *value, int force)
00095 {
00096     NUTENVIRONMENT *envp;
00097     NUTENVIRONMENT *nxtp;
00098     NUTENVIRONMENT *prvp = NULL;
00099 
00100     if ((envp = findenv(name)) == NULL) {
00101         if ((envp = malloc(sizeof(NUTENVIRONMENT))) == NULL) {
00102             return -1;
00103         }
00104         memset(envp, 0, sizeof(NUTENVIRONMENT));
00105         if ((envp->env_name = malloc(strlen(name) + 1)) == NULL) {
00106             free(envp);
00107             return -1;
00108         }
00109     strcpy(envp->env_name, name);
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 = malloc(strlen(value) + 1)) == NULL) {
00146                 return -1;
00147             }
00148             strcpy(envp->env_value, value);
00149             return save_env();
00150         }
00151     }
00152     return 0;
00153 }
00154 
00164 void unsetenv(CONST char *name)
00165 {
00166     NUTENVIRONMENT *envp;
00167 
00168     if ((envp = findenv(name)) == NULL) {
00169         errno = ENOENT;
00170     return;
00171     }
00172     if (envp->env_prev) {
00173         envp->env_prev->env_next = envp->env_next;
00174     }
00175     if (envp->env_next) {
00176     envp->env_next->env_prev = envp->env_prev;
00177     }
00178     if (nut_environ == envp) {
00179     nut_environ = envp->env_next;
00180     }
00181     free(envp->env_name);
00182     free(envp->env_value);
00183     free(envp);
00184 
00185     save_env();
00186 }
00187 

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