setenv(0,...) is no longer valid in posix-2013
[libc-test.git] / src / functional / env.c
blobcf8643941cbd715eb7f1b056e79662d02b7ff90d
1 #ifndef _GNU_SOURCE
2 #define _GNU_SOURCE 1
3 #endif
4 #include <stdlib.h>
5 #include <string.h>
6 #include <errno.h>
7 #include <unistd.h>
8 #include "test.h"
10 extern char **environ;
12 int main()
14 char *s;
15 int r;
17 if (!environ)
18 t_error("environ is NULL\n");
19 if (clearenv() || (environ && *environ))
20 t_error("clrearenv: %s\n", strerror(errno));
21 if (putenv("TEST=1"))
22 t_error("putenv: %s\n", strerror(errno));
23 if (strcmp(environ[0],"TEST=1") != 0)
24 t_error("putenv failed: environ[0]: %s, wanted \"TEST=1\"\n", environ[0]);
25 if ((s=environ[1]))
26 t_error("environ[1]: %p, wanted 0\n", s);
27 if (!(s=getenv("TEST")))
28 t_error("getenv(\"TEST\"): 0, wanted \"1\"\n");
29 if (strcmp(s,"1") != 0)
30 t_error("getenv(\"TEST\"): \"%s\", wanted \"1\"\n", s);
31 if (unsetenv("TEST"))
32 t_error("unsetenv: %s\n", strerror(errno));
33 if ((s=*environ))
34 t_error("*environ: %p != 0\n", s);
35 if ((s=getenv("TEST")))
36 t_error("getenv(\"TEST\"): %p, wanted 0\n", s);
37 errno = 0;
38 if (setenv("TEST", "2", 0))
39 t_error("setenv: %s\n", strerror(errno));
40 if (strcmp(s=getenv("TEST"),"2") != 0)
41 t_error("getenv(\"TEST\"): \"%s\", wanted \"2\"\n", s);
42 if (strcmp(environ[0], "TEST=2") != 0)
43 t_error("setenv failed: environ[0]: %s, wanted \"TEST=2\"\n", environ[0]);
44 errno = 0;
45 if (setenv("TEST", "3", 0))
46 t_error("setenv: %s\n", strerror(errno));
47 if (strcmp(s=getenv("TEST"),"2") != 0)
48 t_error("getenv(\"TEST\"): \"%s\", wanted \"2\"\n", s);
49 errno = 0;
50 if (setenv("TEST", "3", 1))
51 t_error("setenv: %s\n", strerror(errno));
52 if (strcmp(s=getenv("TEST"),"3") != 0)
53 t_error("getenv(\"TEST\"): \"%s\", wanted \"3\"\n", s);
54 /* test failures */
55 errno = 0;
56 if ((r=setenv("","",0)) != -1 || errno != EINVAL)
57 t_error("setenv(\"\",\"\",0): %d, errno: %d (%s), wanted -1, %d (EINVAL)\n", r, errno, strerror(errno), EINVAL);
58 return t_status;