fehQlibs 26
Qlibs
Loading...
Searching...
No Matches
env.c
Go to the documentation of this file.
1#include "str.h"
2#include "alloc.h"
3#include "env.h"
4
12extern /*@null@*/char *env_get(char *s)
13{
14 int i;
15 unsigned int len;
16
17 if (!s) return 0;
18 len = str_len(s);
19 for (i = 0; environ[i]; ++i)
20 if (str_start(environ[i],s) && (environ[i][len] == '='))
21 return environ[i] + len + 1;
22 return 0;
23}
24
25extern char *env_findeq(char *s)
26{
27 for (; *s; ++s)
28 if (*s == '=') return s;
29 return 0;
30}
31
32int env_isinit = 0; /* if env_isinit: */
33static int ea; /* environ is a pointer to ea+1 char*'s. */
34static int en; /* the first en of those are ALLOCATED. environ[en] is 0. */
35
36static void env_del(int i) {
38 environ[i] = environ[--en];
39 environ[en] = 0;
40}
41
42static void env_unsetlen(char *s,int len)
43{
44 int i;
45
46 for (i = en - 1; i >= 0; --i)
47 if (!str_diffn(s,environ[i],len))
48 if (environ[i][len] == '=')
49 env_del(i);
50}
51
52int env_unset(char *s)
53{
54 if (!env_isinit)
55 if (!env_init()) return 0;
56 env_unsetlen(s,str_len(s));
57 return 1;
58}
59
60int env_set(char *s)
61{
62 char *t;
63
64 t = env_findeq(s);
65 if (t) env_unsetlen(s,t - s);
66 if (en == ea) {
67 ea += 30;
68 if (!alloc_re((void **)&environ,(en + 1) * sizeof(char *),(ea + 1) * sizeof(char *)))
69 { ea = en; return 0; }
70 }
71 environ[en++] = s;
72 environ[en] = 0;
73 return 1;
74}
75
76int env_puts(char *s)
77{
78 char *u;
79
80 if (!env_isinit)
81 if (!env_init()) return 0;
82 u = alloc(str_len(s) + 1);
83 if (!u) return 0;
84 str_copy(u,s);
85 if (!env_set(u)) { alloc_free(u); return 0; }
86 return 1;
87}
88
89int env_put(char *name,char *value) {
90 char *ln;
91 int len;
92
93 if (!env_isinit)
94 if (!env_init()) return 0;
95 len = str_len(name);
96 ln = alloc(len + str_len(value) + 2);
97 if (!ln) return 0;
98 str_copy(ln,name);
99 ln[len] = '=';
100 str_copy(ln + len + 1,value);
101 if (!env_set(ln)) { alloc_free(ln); return 0; }
102 return 1;
103}
104
105int env_init() {
106 char **newenviron;
107 int i;
108
109 for (en = 0; environ[en]; ++en)
110 ;
111 ea = en + 10;
112 newenviron = (char **) alloc((ea + 1) * sizeof(char *));
113 if (!newenviron) return 0;
114
115 for (en = 0; environ[en]; ++en) {
116 newenviron[en] = alloc(str_len(environ[en]) + 1);
117 if (!newenviron[en]) {
118 for (i = 0; i < en; ++i) alloc_free(newenviron[i]);
119 alloc_free(newenviron);
120 return 0;
121 }
122 str_copy(newenviron[en],environ[en]);
123 }
124
125 newenviron[en] = 0;
126 environ = newenviron;
127 env_isinit = 1;
128 return 1;
129}
130
131static char *null = 0;
132
133void env_clear() {
134 if (env_isinit)
135 while (en) env_del(0);
136 else environ = &null;
137}
138extern char *env_pick()
139{
140 return environ[0];
141}
void env_clear()
Definition: env.c:133
char * env_findeq(char *s)
Definition: env.c:25
int env_isinit
Definition: env.c:32
char * env_get(char *s)
Definition: env.c:12
int env_unset(char *s)
Definition: env.c:52
int env_set(char *s)
Definition: env.c:60
char * env_pick()
Definition: env.c:138
int env_init()
Definition: env.c:105
int env_puts(char *s)
Definition: env.c:76
int env_put(char *name, char *value)
Definition: env.c:89
void alloc_free(void *)
Definition: alloc.c:45
void * alloc(unsigned int)
Definition: alloc.c:27
int alloc_re(void **, unsigned int, unsigned int)
Definition: alloc.c:53
char ** environ
int str_start(const char *, const char *)
Definition: str.c:116
int str_diffn(const char *, const char *, unsigned int)
Definition: str.c:54
unsigned int str_len(const char *)
Definition: str.c:68
unsigned int str_copy(char *, const char *)
Definition: str.c:11