mlib update: new isnan()/isnanf() implementation
[tangerine.git] / compiler / clib / __env.c
blob0402acda58a7f29fac56821ade7a80ce364eb380
1 /*
2 Copyright © 1995-2003, The AROS Development Team. All rights reserved.
3 $Id$
5 Internal functions for environment variables handling.
6 */
8 #include "__arosc_privdata.h"
10 #include <stdlib.h>
11 #include <strings.h>
12 #include <proto/exec.h>
14 #include "__env.h"
16 static __env_item *__env_newvar(const char *name, int valuesize)
18 __env_item *item;
20 item = malloc(sizeof(__env_item));
21 if (!item) goto err1;
23 item->name = strdup(name);
24 if (!item->name) goto err2;
26 item->value = malloc(valuesize);
27 if (!item->value) goto err3;
29 item->next = NULL;
31 return item;
33 err3:
34 free(item->name);
35 err2:
36 free(item);
37 err1:
38 return NULL;
41 static __env_item **internal_findvar(register const char *name)
44 __env_item **curr;
46 for (
47 curr = &__env_list;
48 *curr && strcmp((*curr)->name, name);
49 curr = &((*curr)->next)
52 return curr;
56 Allocates space for a variable with name 'name' returning a pointer to it.
57 If a variable with this name already exists then returns a pointer to that
58 variable.
60 Returns NULL on error.
62 __env_item *__env_getvar(const char *name, int valuesize)
64 register __env_item **curr;
66 curr = internal_findvar(name);
68 if (*curr)
70 if (strlen((*curr)->value) < valuesize)
72 free((*curr)->value);
73 (*curr)->value = malloc(valuesize);
75 if (!(*curr)->value)
77 __env_item *tmp = (*curr)->next;
79 free(*curr);
80 *curr = tmp;
84 else
86 *curr = __env_newvar(name, valuesize);
89 return *curr;
92 void __env_delvar(const char *name)
94 register __env_item **curr;
96 curr = internal_findvar(name);
98 if (*curr)
100 register __env_item *tmp = *curr;
102 *curr = (*curr)->next;
104 free(tmp->name);
105 free(tmp->value);
106 free(tmp);