5 static GKeyFile
*config
;
6 static int config_descr
;
8 static const gchar
*CONFIG_KEYS
[] = {
14 /* NULL if key doesn't exist */
15 gchar
*get_option(gchar
*key
, gchar
*group
) {
20 return g_key_file_get_string(config
, group
, key
, NULL
);
23 /* false if key isn't already defined or value is invalid */
24 gboolean
set_option(gchar
*key
, gchar
*value
, gchar
*group
) {
29 if(!g_key_file_get_string(config
, group
, key
, NULL
))
32 //TODO safteycheck value
33 g_key_file_set_string(config
, group
, key
, value
);
37 /* initial load/check */
38 gboolean
config_init() {
41 return TRUE
; //already init'd
43 config
= g_key_file_new();
45 const gchar
*config_path
;
46 GStatBuf
*config_lstat
;
48 config_path
= "/etc/systemd_compat.conf";
50 /* does conf exist? */
51 if(g_lstat(config_path
, config_lstat
)) {
53 /* if not, can we write it */
54 if(g_access("/etc/", W_OK
)) {
55 g_printf("%s\n", "no write permissions for /etc/! exiting..");
59 config_descr
= g_open(config_path
, O_CREAT
, 644);
61 gchar
*posix_hostname
;
62 posix_hostname
= g_malloc(255);
64 gethostname(posix_hostname
, 255);
66 g_key_file_set_string(config
, "hostnamed", "Hostname", posix_hostname
);
67 g_key_file_set_string(config
, "hostnamed", "PrettyHostname", "");
68 g_key_file_set_string(config
, "hostnamed", "IconName", "Computer");
69 g_key_file_set_string(config
, "hostnamed", "ChassisType", "laptop"); //TODO set these correctly
71 if(!g_key_file_save_to_file(config
, config_path
, NULL
)) {
72 g_printf("failed to write config to %s!\n", config_path
);
73 g_free(posix_hostname
);
77 g_printf("wrote config to %s\n", config_path
);
79 g_free(posix_hostname
);
83 /* it does exist, read it */
86 if(!g_access(config_path
, W_OK
)) {
87 g_printf("%s\n", "no write permissions for /etc/! exiting..");
89 } else if(g_key_file_load_from_file(config
, config_path
, G_KEY_FILE_KEEP_COMMENTS
, NULL
)) {
90 config_descr
= g_open(config_path
, O_RDWR
, 644);
94 g_printf("could not read config at %s! exiting..", config_path
);
105 g_close(config_descr
, NULL
);