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 <stdlib.h>
00036 #include <string.h>
00037 #include <errno.h>
00038 
00039 #include <sys/environ.h>
00040 
00041 /*
00042  * Save environment in non-volatile memory.
00043  */
00044 static int save_env(void)
00045 {
00046     int rc = 0;
00047 
00048     if (nut_environ) {
00049         NUTENVIRONMENT *envp;
00050         size_t len;
00051         u_long magic = ENVIRON_MAGIC;
00052         int addr = ENVIRON_EE_OFFSET;
00053 
00054         if (NutNvMemSave(addr, &magic, sizeof(magic)) == 0) {
00055             addr += sizeof(magic);
00056             for (envp = nut_environ; envp; envp = envp->env_next) {
00057                 len = strlen(envp->env_name) + 1;
00058                 if ((rc = NutNvMemSave(addr, envp->env_name, len)) != 0) {
00059                     break;
00060                 }
00061                 addr += len;
00062                 if (envp->env_value) {
00063                     len = strlen(envp->env_value) + 1;
00064                     if ((rc = NutNvMemSave(addr, envp->env_value, len)) != 0) {
00065                         break;
00066                     }
00067                 } else {
00068                     len = 1;
00069                     if ((rc = NutNvMemSave(addr, "", len)) != 0) {
00070                         break;
00071                     }
00072                 }
00073                 addr += len;
00074             }
00075             if (rc == 0) {
00076                 rc = NutNvMemSave(addr, "", 1);
00077             }
00078         }
00079     }
00080     return rc;
00081 }
00082 
00093 int setenv(CONST char *name, CONST char *value, int force)
00094 {
00095     NUTENVIRONMENT *envp;
00096     NUTENVIRONMENT *nxtp;
00097     NUTENVIRONMENT *prvp = NULL;
00098 
00099     if ((envp = findenv(name)) == NULL) {
00100         if ((envp = malloc(sizeof(NUTENVIRONMENT))) == NULL) {
00101             return -1;
00102         }
00103         memset(envp, 0, sizeof(NUTENVIRONMENT));
00104         if ((envp->env_name = malloc(strlen(name) + 1)) == NULL) {
00105             free(envp);
00106             return -1;
00107         }
00108     strcpy(envp->env_name, name);
00109 
00110     for (nxtp = nut_environ; nxtp; nxtp = nxtp->env_next) {
00111         if (strcmp(envp->env_name, nxtp->env_name) < 0) {
00112         if (nxtp->env_prev) {
00113             nxtp->env_prev->env_next = envp;
00114         } else {
00115             nut_environ = envp;
00116         }
00117         envp->env_next = nxtp;
00118         envp->env_prev = nxtp->env_prev;
00119         nxtp->env_prev = envp;
00120         break;
00121         }
00122             prvp = nxtp;
00123     }
00124     if (nxtp == NULL) {
00125         if (prvp) {
00126         prvp->env_next = envp;
00127         envp->env_prev = prvp;
00128         } else {
00129         nut_environ = envp;
00130         }
00131     }
00132         force = 1;
00133     }
00134     if (force) {
00135         if (envp->env_value) {
00136             if (strcmp(envp->env_value, value)) {
00137                 free(envp->env_value);
00138             }
00139             else {
00140                 force = 0;
00141             }
00142         }
00143         if (force) {
00144             if ((envp->env_value = malloc(strlen(value) + 1)) == NULL) {
00145                 return -1;
00146             }
00147             strcpy(envp->env_value, value);
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/