aboutsummaryrefslogtreecommitdiff
path: root/sandbox/envvar/envvar.c
blob: 8ef30130b1d7b21b0a1ba0611fd5e568f159d2e8 (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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char *argv[])
{
	char pg_dbname[128] = {0};
	char pg_host[128] = {0};
	char pg_password[128] = {0};
	char pg_port[8] = {0};
	char pg_user[128] = {0};

	const char* env;

	env = getenv("POSTGRES_DBNAME");
	if (env != NULL)
	{
		strncpy(pg_dbname, env, sizeof(pg_dbname));
	}

	env = getenv("POSTGRES_HOST");
	if (env != NULL)
	{
		strncpy(pg_host, env, sizeof(pg_host));
	}

	env = getenv("POSTGRES_PASSWORD");
	if (env != NULL)
	{
		strncpy(pg_password, env, sizeof(pg_password));
	}

	env = getenv("POSTGRES_PORT");
	if (env != NULL)
	{
		strncpy(pg_port, env, sizeof(pg_port));
	}

	env = getenv("POSTGRES_USER");
	if (env != NULL)
	{
		strncpy(pg_user, env, sizeof(pg_user));
	}

	char *config_fmt = "host=%s port=%s user=%s password=%s dbname=%s sslmode=disable";

	printf(config_fmt, pg_host, pg_port, pg_user, pg_password, pg_dbname);
	printf("\n");

	char sqladdr[2048];
	snprintf(
		sqladdr,
		sizeof(sqladdr),
		config_fmt,
		pg_host,
		pg_port,
		pg_user,
		pg_password,
		pg_dbname
	);

	printf("%s", sqladdr);
	printf("\n");

}