Python: Give goto_url_hook only one argument, like follow_url_hook.
[elinks.git] / src / util / env.c
blob6a948a1ca788d2c15fbd882ee914236ecb3bcc6a
1 /* Environment variables handling */
3 #ifdef HAVE_CONFIG_H
4 #include "config.h"
5 #endif
7 #include <ctype.h>
8 #include <errno.h>
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
13 #include "elinks.h"
15 #include "util/env.h"
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.
25 int
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;
40 if (substring) {
41 /* Copy the substring. */
42 value = memacpy(value, length);
43 if (!value) return -1;
44 allocated = 1;
47 ret = setenv(name, value, 1);
48 if (allocated) mem_free(value);
49 return ret;
52 #elif defined(HAVE_PUTENV)
55 int namelen = strlen(name);
56 char *s = malloc(namelen + length + 2);
58 if (!s) return -1;
59 memcpy(s, name, namelen);
60 s[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
66 * this function. */
67 return putenv(s);
70 #else
71 /* XXX: what to do ?? */
72 return -1;
73 #endif