aboutsummaryrefslogtreecommitdiff
path: root/tests/env/src.c
blob: 7544966fd5d0dc8d5f5d7066540031e879535033 (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
41
42
43
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int main() {
  printf("List:\n");
  for (char** str = environ; *str; str++) {
    printf("%s\n", *str);
  }
  printf("\n");

  printf("getenv(PATH): %s\n", getenv("PATH"));
  printf("getenv(NONEXISTENT): %s\n", getenv("NONEXISTENT"));

  printf("setenv/0(PATH) ret: %d\n", setenv("PATH", "test", 0));
  printf("getenv(PATH) after setenv/0: %s\n", getenv("PATH"));
  printf("setenv/1(PATH) ret: %d\n", setenv("PATH", "test2", 1));
  printf("getenv(PATH) after setenv/1: %s\n", getenv("PATH"));

  printf("setenv(SET_VALUE) ret: %d\n", setenv("SET_VALUE", "test3", 0));
  printf("setenv(SET_VALUE2) ret: %d\n", setenv("SET_VALUE2", "test4", 1));
  printf("getenv(SET_VALUE): %s\n", getenv("SET_VALUE"));
  printf("getenv(SET_VALUE2): %s\n", getenv("SET_VALUE2"));

  char buffer[] = "PUT_VALUE=test5";
  printf("putenv(PUT_VALUE) ret: %d\n", putenv(buffer));
  printf("getenv(PUT_VALUE): %s\n", getenv("PUT_VALUE"));
  // NOTE: This should change the value. In our implementation, it doesn't.
  buffer[10] = 'Q';
  printf("getenv(PUT_VALUE) after alteration: %s\n", getenv("PUT_VALUE"));

  printf("unsetenv(PUT_VALUE) ret: %d\n", unsetenv("PUT_VALUE"));
  printf("getenv(PUT_VALUE) after unsetenv: %s\n", getenv("PUT_VALUE"));

  printf("setenv(0) ret: %d\n", setenv(0, "foo", 1));
  printf("setenv('') ret: %d\n", setenv("", "foo", 1));
  printf("setenv(X=Y) ret: %d\n", setenv("X=Y", "foo", 1));
  printf("unsetenv(0) ret: %d\n", unsetenv(0));
  printf("unsetenv('') ret: %d\n", unsetenv(""));
  printf("unsetenv(X=Y) ret: %d\n", unsetenv("X=Y"));

  return 0;
}