1570af302Sopenharmony_ci#ifndef _GNU_SOURCE
2570af302Sopenharmony_ci#define _GNU_SOURCE 1
3570af302Sopenharmony_ci#endif
4570af302Sopenharmony_ci#include <stdlib.h>
5570af302Sopenharmony_ci#include <string.h>
6570af302Sopenharmony_ci#include <errno.h>
7570af302Sopenharmony_ci#include <unistd.h>
8570af302Sopenharmony_ci#include "test.h"
9570af302Sopenharmony_ci
10570af302Sopenharmony_ciextern char **environ;
11570af302Sopenharmony_ci
12570af302Sopenharmony_ciint main()
13570af302Sopenharmony_ci{
14570af302Sopenharmony_ci	char *s;
15570af302Sopenharmony_ci	int r;
16570af302Sopenharmony_ci
17570af302Sopenharmony_ci	if (!environ)
18570af302Sopenharmony_ci		t_error("environ is NULL\n");
19570af302Sopenharmony_ci	if (clearenv() || (environ && *environ))
20570af302Sopenharmony_ci		t_error("clrearenv: %s\n", strerror(errno));
21570af302Sopenharmony_ci	if (putenv("TEST=1"))
22570af302Sopenharmony_ci		t_error("putenv: %s\n", strerror(errno));
23570af302Sopenharmony_ci	if (strcmp(environ[0],"TEST=1") != 0)
24570af302Sopenharmony_ci		t_error("putenv failed: environ[0]: %s, wanted \"TEST=1\"\n", environ[0]);
25570af302Sopenharmony_ci	if ((s=environ[1]))
26570af302Sopenharmony_ci		t_error("environ[1]: %p, wanted 0\n", s);
27570af302Sopenharmony_ci	if (!(s=getenv("TEST")))
28570af302Sopenharmony_ci		t_error("getenv(\"TEST\"): 0, wanted \"1\"\n");
29570af302Sopenharmony_ci	if (strcmp(s,"1") != 0)
30570af302Sopenharmony_ci		t_error("getenv(\"TEST\"): \"%s\", wanted \"1\"\n", s);
31570af302Sopenharmony_ci	if (unsetenv("TEST"))
32570af302Sopenharmony_ci		t_error("unsetenv: %s\n", strerror(errno));
33570af302Sopenharmony_ci	if ((s=*environ))
34570af302Sopenharmony_ci		t_error("*environ: %p != 0\n", s);
35570af302Sopenharmony_ci	if ((s=getenv("TEST")))
36570af302Sopenharmony_ci		t_error("getenv(\"TEST\"): %p, wanted 0\n", s);
37570af302Sopenharmony_ci	errno = 0;
38570af302Sopenharmony_ci	if (setenv("TEST", "2", 0))
39570af302Sopenharmony_ci		t_error("setenv: %s\n", strerror(errno));
40570af302Sopenharmony_ci	if (strcmp(s=getenv("TEST"),"2") != 0)
41570af302Sopenharmony_ci		t_error("getenv(\"TEST\"): \"%s\", wanted \"2\"\n", s);
42570af302Sopenharmony_ci	if (strcmp(environ[0], "TEST=2") != 0)
43570af302Sopenharmony_ci		t_error("setenv failed: environ[0]: %s, wanted \"TEST=2\"\n", environ[0]);
44570af302Sopenharmony_ci	errno = 0;
45570af302Sopenharmony_ci	if (setenv("TEST", "3", 0))
46570af302Sopenharmony_ci		t_error("setenv: %s\n", strerror(errno));
47570af302Sopenharmony_ci	if (strcmp(s=getenv("TEST"),"2") != 0)
48570af302Sopenharmony_ci		t_error("getenv(\"TEST\"): \"%s\", wanted \"2\"\n", s);
49570af302Sopenharmony_ci	errno = 0;
50570af302Sopenharmony_ci	if (setenv("TEST", "3", 1))
51570af302Sopenharmony_ci		t_error("setenv: %s\n", strerror(errno));
52570af302Sopenharmony_ci	if (strcmp(s=getenv("TEST"),"3") != 0)
53570af302Sopenharmony_ci		t_error("getenv(\"TEST\"): \"%s\", wanted \"3\"\n", s);
54570af302Sopenharmony_ci	/* test failures */
55570af302Sopenharmony_ci	errno = 0;
56570af302Sopenharmony_ci	if ((r=setenv("","",0)) != -1 || errno != EINVAL)
57570af302Sopenharmony_ci		t_error("setenv(\"\",\"\",0): %d, errno: %d (%s), wanted -1, %d (EINVAL)\n", r, errno, strerror(errno), EINVAL);
58570af302Sopenharmony_ci	return t_status;
59570af302Sopenharmony_ci}
60