1 /* Environment variables handling */
16 #include "util/memory.h"
17 #include "util/string.h"
19 /* Set @name environment variable to @value or a substring of it:
20 * On success, it returns 0.
21 * If @value is NULL and on error, it returns -1.
22 * If @length >= 0 and smaller than true @value length, it will
23 * set @name to specified substring of @value.
26 env_set(unsigned char *name
, unsigned char *value
, int length
)
28 int true_length
, substring
= 0;
30 if (!value
|| !name
|| !*name
) return -1;
32 true_length
= strlen(value
);
33 substring
= (length
>= 0 && length
< true_length
);
34 if (!substring
) length
= true_length
;
36 #if defined(HAVE_SETENV)
38 int ret
, allocated
= 0;
41 /* Copy the substring. */
42 value
= memacpy(value
, length
);
43 if (!value
) return -1;
47 ret
= setenv(name
, value
, 1);
48 if (allocated
) mem_free(value
);
52 #elif defined(HAVE_PUTENV)
55 int namelen
= strlen(name
);
56 char *s
= malloc(namelen
+ length
+ 2);
59 memcpy(s
, name
, namelen
);
61 memcpy(&s
[namelen
+ 1], value
, length
);
62 s
[namelen
+ 1 + length
] = '\0';
64 /* @s is never freed by us, this is intentional.
65 * --> Possible memleaks on repeated use of
71 /* XXX: what to do ?? */