chiark / gitweb /
volume_id: provide libvolume_id.a file
[elogind.git] / klibc / klibc / setenv.c
1 /*
2  * setenv.c
3  */
4
5 #include <errno.h>
6 #include <stdlib.h>
7 #include <string.h>
8 #include <unistd.h>
9
10 /* str should be a duplicated version of the input string;
11    len is the length of the key including the = sign */
12 int __put_env(char *str, size_t len, int overwrite);
13
14 int setenv(const char *name, const char *val, int overwrite)
15 {
16   const char *z;
17   char *s;
18   size_t l1, l2;
19
20   if ( !name || !name[0] ) {
21     errno = EINVAL;
22     return -1;
23   }
24
25   l1 = 0;
26   for ( z = name ; *z ; z++ ) {
27     l1++;
28     if ( *z == '=' ) {
29       errno = EINVAL;
30       return -1;
31     }
32   }
33
34   l2 = strlen(val);
35
36   s = malloc(l1+l2+2);
37   if ( !s )
38     return -1;
39
40   memcpy(s, name, l1);
41   s[l1] = '=';
42   memcpy(s+l1+1, val, l2+1);
43
44   return __put_env(s, l1+1, overwrite);
45 }