setenv.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /*
  2. * Copyright 2008 by egnite Software GmbH
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 3. Neither the name of the copyright holders nor the names of
  14. * contributors may be used to endorse or promote products derived
  15. * from this software without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  18. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  19. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  20. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  21. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  22. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  23. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  24. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  25. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  26. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  27. * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  28. * SUCH DAMAGE.
  29. *
  30. * For additional information see http://www.ethernut.de/
  31. */
  32. #include <dev/nvmem.h>
  33. #include <stdint.h>
  34. #include <stdlib.h>
  35. #include <string.h>
  36. #include <errno.h>
  37. #include <memdebug.h>
  38. #include <sys/environ.h>
  39. #include <cfg/crt.h>
  40. /*
  41. * Save environment in non-volatile memory.
  42. */
  43. static int save_env(void)
  44. {
  45. int rc = 0;
  46. if (nut_environ) {
  47. NUTENVIRONMENT *envp;
  48. size_t len;
  49. uint32_t magic = ENVIRON_MAGIC;
  50. int addr = ENVIRON_EE_OFFSET;
  51. if (NutNvMemSave(addr, &magic, sizeof(magic)) == 0) {
  52. addr += sizeof(magic);
  53. for (envp = nut_environ; envp; envp = envp->env_next) {
  54. len = strlen(envp->env_name) + 1;
  55. if ((rc = NutNvMemSave(addr, envp->env_name, len)) != 0) {
  56. break;
  57. }
  58. addr += len;
  59. if (envp->env_value) {
  60. len = strlen(envp->env_value) + 1;
  61. if ((rc = NutNvMemSave(addr, envp->env_value, len)) != 0) {
  62. break;
  63. }
  64. } else {
  65. len = 1;
  66. if ((rc = NutNvMemSave(addr, "", len)) != 0) {
  67. break;
  68. }
  69. }
  70. addr += len;
  71. }
  72. if (rc == 0) {
  73. rc = NutNvMemSave(addr, "", 1);
  74. }
  75. }
  76. }
  77. return rc;
  78. }
  79. /*!
  80. * \brief Remove an environment variable.
  81. *
  82. * \param name Points to a string, which is the name of the variable.
  83. * \param value Points to a string, which is the value of the variable.
  84. * \param force If not zero, existing values will be updated.
  85. *
  86. * \return 0 upon successful completion. Otherwise, -1 is returned and
  87. * errno is set to indicate the error.
  88. */
  89. int setenv(const char *name, const char *value, int force)
  90. {
  91. NUTENVIRONMENT *envp;
  92. NUTENVIRONMENT *nxtp;
  93. NUTENVIRONMENT *prvp = NULL;
  94. if ((envp = findenv(name)) == NULL) {
  95. if ((envp = malloc(sizeof(NUTENVIRONMENT))) == NULL) {
  96. return -1;
  97. }
  98. memset(envp, 0, sizeof(NUTENVIRONMENT));
  99. if ((envp->env_name = strdup(name)) == NULL) {
  100. free(envp);
  101. return -1;
  102. }
  103. for (nxtp = nut_environ; nxtp; nxtp = nxtp->env_next) {
  104. if (strcmp(envp->env_name, nxtp->env_name) < 0) {
  105. if (nxtp->env_prev) {
  106. nxtp->env_prev->env_next = envp;
  107. } else {
  108. nut_environ = envp;
  109. }
  110. envp->env_next = nxtp;
  111. envp->env_prev = nxtp->env_prev;
  112. nxtp->env_prev = envp;
  113. break;
  114. }
  115. prvp = nxtp;
  116. }
  117. if (nxtp == NULL) {
  118. if (prvp) {
  119. prvp->env_next = envp;
  120. envp->env_prev = prvp;
  121. } else {
  122. nut_environ = envp;
  123. }
  124. }
  125. force = 1;
  126. }
  127. if (force) {
  128. if (envp->env_value) {
  129. if (strcmp(envp->env_value, value)) {
  130. free(envp->env_value);
  131. }
  132. else {
  133. force = 0;
  134. }
  135. }
  136. if (force) {
  137. if ((envp->env_value = strdup(value)) == NULL) {
  138. return -1;
  139. }
  140. return save_env();
  141. }
  142. }
  143. return 0;
  144. }
  145. /*!
  146. * \brief Remove an environment variable.
  147. *
  148. * \param name Points to a string, which is the name of the variable to
  149. * be removed.
  150. *
  151. * \return 0 upon successful completion. Otherwise, -1 is returned and
  152. * errno is set to indicate the error.
  153. */
  154. #ifdef CRT_UNSETENV_POSIX
  155. int unsetenv(const char *name)
  156. {
  157. NUTENVIRONMENT *envp;
  158. if ((envp = findenv(name)) == NULL) {
  159. errno = ENOENT;
  160. return -1;
  161. }
  162. if (envp->env_prev) {
  163. envp->env_prev->env_next = envp->env_next;
  164. }
  165. if (envp->env_next) {
  166. envp->env_next->env_prev = envp->env_prev;
  167. }
  168. if (nut_environ == envp) {
  169. nut_environ = envp->env_next;
  170. }
  171. free(envp->env_name);
  172. free(envp->env_value);
  173. free(envp);
  174. save_env();
  175. return 0;
  176. }
  177. #else
  178. void unsetenv(const char *name)
  179. {
  180. NUTENVIRONMENT *envp;
  181. if ((envp = findenv(name)) == NULL) {
  182. errno = ENOENT;
  183. return;
  184. }
  185. if (envp->env_prev) {
  186. envp->env_prev->env_next = envp->env_next;
  187. }
  188. if (envp->env_next) {
  189. envp->env_next->env_prev = envp->env_prev;
  190. }
  191. if (nut_environ == envp) {
  192. nut_environ = envp->env_next;
  193. }
  194. free(envp->env_name);
  195. free(envp->env_value);
  196. free(envp);
  197. save_env();
  198. }
  199. #endif