summaryrefslogtreecommitdiff
path: root/klibc/klibc/putenv.c
blob: 8138c653b4920e5c9f3e2d35fe448dbcb9121de5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
/*
 * putenv.c
 */

#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

/* str should be a duplicated version of the input string;
   len is the length of the key including the = sign */
int __put_env(char *str, size_t len, int overwrite);

int putenv(const char *str)
{
  char *s;
  const char *e, *z;

  if ( !str ) {
    errno = EINVAL;
    return -1;
  }

  e = NULL;
  for ( z = str ; *z ; z++ ) {
    if ( *z == '=' )
      e = z;
  }

  if ( !e ) {
    errno = EINVAL;
    return -1;
  }

  s = strdup(str);
  if ( !s )
    return -1;

  return __put_env(s, e-str, 1);
}