3 * - Environment Handler
10 char **environ = NULL;
11 static char **expected_environ = NULL;
12 static int num_allocated_slots;
15 char *getenv(const char *name)
20 if(!environ) return NULL;
21 if(!name) return NULL;
23 len = strlen((char*)name);
25 for( char **env = environ; *env; env ++ )
28 if(strncmp(name, env_str, len) == 0 && env_str[len] == '=') {
37 int putenv(char *string)
39 char *eqpos = strchr(string, '=');
44 size_t namelen = eqpos - string;
46 static const int alloc_step = 10;
47 if( expected_environ == NULL || expected_environ != environ )
49 if( expected_environ )
50 free(expected_environ);
54 for( char **env = environ; *env; env ++ )
56 envc ++; // NULL termination
57 envc ++; // assume we're adding a new value
59 num_allocated_slots = (envc + alloc_step-1) / alloc_step * alloc_step;
61 expected_environ = malloc(num_allocated_slots*sizeof(char*));
66 for( char **env = environ; *env; env ++ )
67 expected_environ[idx++] = *env;
68 expected_environ[idx++] = NULL;
70 environ = expected_environ;
74 for( char **env = environ; *env; env ++, envc++ )
76 if( strncmp(*env, string, namelen) != 0 )
78 if( *env[namelen] != '=' )
83 if( num_allocated_slots >= envc+1 )
85 num_allocated_slots += alloc_step;
86 expected_environ = realloc(expected_environ, num_allocated_slots*sizeof(char*));
89 environ = expected_environ;
91 environ[envc] = string;
92 environ[envc+1] = NULL;