From 9b44098817abefdc5df4627f751980181b410e9c Mon Sep 17 00:00:00 2001 From: Michael McVady Date: Fri, 13 May 2022 17:48:44 -0500 Subject: toy to load config from env vars --- sandbox/envvar/Makefile | 15 +++++++++++++++ sandbox/envvar/envvar.c | 29 +++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+) create mode 100644 sandbox/envvar/Makefile create mode 100644 sandbox/envvar/envvar.c (limited to 'sandbox/envvar') diff --git a/sandbox/envvar/Makefile b/sandbox/envvar/Makefile new file mode 100644 index 0000000..ae4c74e --- /dev/null +++ b/sandbox/envvar/Makefile @@ -0,0 +1,15 @@ +.PHONY: run clean + +CC = gcc +CFLAGS = -g -Wall -Wmissing-declarations -Wshadow -Wstrict-prototypes -Wmissing-prototypes -Wpointer-arith -Wcast-qual -Wsign-compare + +all: envvar + +envvar: envvar.c + $(CC) $(CFLAGS) -std=c99 -pedantic -o envvar envvar.c + +run: + ./envvar + +clean: + rm envvar diff --git a/sandbox/envvar/envvar.c b/sandbox/envvar/envvar.c new file mode 100644 index 0000000..bbcb1c9 --- /dev/null +++ b/sandbox/envvar/envvar.c @@ -0,0 +1,29 @@ +#include +#include + +int main(int argc, char *argv[]) +{ + const char* pg_dbname = getenv("POSTGRES_DBNAME"); + const char* pg_host = getenv("POSTGRES_HOST"); + const char* pg_password = getenv("POSTGRES_PASSWORD"); + const char* pg_port = getenv("POSTGRES_PORT"); + const char* pg_user = getenv("POSTGRES_USER"); + + printf("POSTGRES_DBNAME: %s\n", (pg_dbname != NULL) ? pg_dbname : "undefined"); + printf("POSTGRES_HOST: %s\n", (pg_host != NULL) ? pg_host : "undefined"); + printf("POSTGRES_PASSWORD: %s\n", (pg_password != NULL) ? pg_password : "undefined"); + printf("POSTGRES_PORT: %s\n", (pg_port != NULL) ? pg_port : "undefined"); + printf("POSTGRES_USER: %s\n", (pg_user != NULL) ? pg_user : "undefined"); + + char *config = "host=%s port=%s user=%s password=%s dbname=%s sslmode=disable\n"; + + sprintf( + config, + pg_host, + pg_port, + pg_user, + pg_password, + pg_dbname + ); + +} -- cgit v1.2.3