©2004 by Marc J. Rochkind. All rights reserved. Portions marked "Open Source" may be copied under license.

 

Main Page   Modules   Namespace List   Class Hierarchy   Compound List   File List   Namespace Members   Compound Members   File Members  

c5/setenv.c

Go to the documentation of this file.
00001 /*
00002     setenv, getenv, and unsetenv
00003     AUP2, Sec. 5.02
00004 
00005     Copyright 2003 by Marc J. Rochkind. All rights reserved.
00006     May be copied only for purposes and under conditions described
00007     on the Web page www.basepath.com/aup/copyright.htm.
00008 
00009     The Example Files are provided "as is," without any warranty;
00010     without even the implied warranty of merchantability or fitness
00011     for a particular purpose. The author and his publisher are not
00012     responsible for any damages, direct or incidental, resulting
00013     from the use or non-use of these Example Files.
00014 
00015     The Example Files may contain defects, and some contain deliberate
00016     coding mistakes that were included for educational reasons.
00017     You are responsible for determining if and how the Example Files
00018     are to be used.
00019 
00020 */
00021 #include <stdlib.h>
00022 #include <string.h>
00023 #include <errno.h>
00024 #include "setenv.h"
00025 
00026 extern char **environ;
00027 
00028 /*[setnew]*/
00029 static int setnew(int i, const char *var, const char *val)
00030 {
00031     char *s;
00032 
00033     if ((s = malloc(strlen(var) + 1 + strlen(val) + 1)) == NULL)
00034         return -1;
00035     strcpy(s, var);
00036     strcat(s, "=");
00037     strcat(s, val);
00038     /* possible memory leak with old value of environ[i] */
00039     environ[i] = s;
00040     return 0;
00041 }
00042 /*[setenv]*/
00043 int setenv(const char *var, const char *val, int overwrite)
00044 {
00045     int i;
00046     size_t varlen;
00047     char **e;
00048 
00049     if (var == NULL || val == NULL || var[0] == '\0' ||
00050       strchr(var, '=') != NULL) {
00051         errno = EINVAL;
00052         return -1;
00053     }
00054     varlen = strlen(var);
00055     for (i = 0; environ[i] != NULL; i++)
00056         if (strncmp(environ[i], var, varlen) == 0 &&
00057           environ[i][varlen] == '=')
00058             break;
00059     if (environ[i] == NULL) {
00060         if ((e = malloc((i + 2) * sizeof(char *))) == NULL)
00061             return -1;
00062         memcpy(e, environ, i * sizeof(char *));
00063         /* possible memory leaks with old pointer array */
00064         environ = e;
00065         environ[i + 1] = NULL;
00066         return setnew(i, var, val);
00067     }
00068     else {
00069         if (overwrite) {
00070             if (strlen(&environ[i][varlen + 1]) >= strlen(val)) {
00071                 strcpy(&environ[i][varlen + 1], val);
00072                 return 0;
00073             }
00074             return setnew(i, var, val);
00075         }
00076         return 0;
00077     }
00078 }
00079 /*[unsetenv]*/
00080 int unsetenv(const char *var)
00081 {
00082     int i, found = -1;
00083     size_t varlen;
00084 
00085     if (var == NULL || var[0] == '\0' || strchr(var, '=') != NULL) {
00086         errno = EINVAL;
00087         return -1;
00088     }
00089     varlen = strlen(var);
00090     for (i = 0; environ[i] != NULL; i++)
00091         if (strncmp(environ[i], var, varlen) == 0 &&
00092           environ[i][varlen] == '=')
00093             found = i;
00094     if (found != -1)
00095         /* possible memory leak with old value of environ[found] */
00096         memmove(&environ[found], &environ[found + 1],
00097           (i - found) * sizeof(char *));
00098     return 0;
00099 }
00100 /*[]*/

Generated on Fri Apr 23 10:56:56 2004 for AUP2 Example Source by doxygen 1.3.1