-
Notifications
You must be signed in to change notification settings - Fork 1
/
settings.c
48 lines (36 loc) · 1005 Bytes
/
settings.c
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
#include "settings.h"
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
struct Settings settings;
#define SETTINGS_BUF_SIZE 4096
static char settings_buf[SETTINGS_BUF_SIZE];
static char *settings_buf_end = settings_buf;
static long read_str(const char *envvar, const char **ptr)
{
const char *value = getenv(envvar);
if (!value) {
fprintf(stderr, "Setting %s missing.\n", envvar);
return -1;
}
long len = strlen(value) + 1;
long remaining = SETTINGS_BUF_SIZE - (settings_buf_end - settings_buf);
if (len > remaining) {
fprintf(stderr, "Settings too long.\n");
return -2;
}
memcpy(settings_buf_end, value, len);
*ptr = settings_buf_end;
settings_buf_end += len;
return len;
}
int init_settings_from_env()
{
if (read_str("TMC_SPYWARE_DATA_DIR", &settings.data_dir) <= 0) {
return 0;
}
if (read_str("TMC_SPYWARE_AUTH_URL", &settings.auth_url) <= 0) {
return 0;
}
return 1;
}