autocreate paths to config and db if required
This commit is contained in:
parent
f220724d42
commit
95e0ebc073
3 changed files with 58 additions and 12 deletions
|
@ -3,6 +3,7 @@
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include <sys/stat.h>
|
||||||
|
|
||||||
char *klist_config_key_to_string(KLIST_CONFIG_KEY key) {
|
char *klist_config_key_to_string(KLIST_CONFIG_KEY key) {
|
||||||
switch (key) {
|
switch (key) {
|
||||||
|
@ -20,6 +21,38 @@ KLIST_CONFIG_KEY klist_config_string_to_key(char *key) {
|
||||||
return _CONFIG_KEY_COUNT;
|
return _CONFIG_KEY_COUNT;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
char *klist_config_name(char *name) {
|
||||||
|
char *config_home = getenv("XDG_CONFIG_HOME");
|
||||||
|
bool config_home_a = false;
|
||||||
|
if (!config_home) {
|
||||||
|
config_home_a = true;
|
||||||
|
char *home = getenv("HOME");
|
||||||
|
const char *config_home_format = "%s/.config";
|
||||||
|
int config_home_len = snprintf(name, 0, config_home_format, home);
|
||||||
|
config_home = malloc(++config_home_len * sizeof(char));
|
||||||
|
snprintf(config_home, config_home_len, config_home_format, home, name);
|
||||||
|
}
|
||||||
|
|
||||||
|
int config_dir_len = snprintf(NULL, 0, "%s/%s", config_home, name);
|
||||||
|
char *config_dir = malloc(++config_dir_len * sizeof(char));
|
||||||
|
snprintf(config_dir, config_dir_len, "%s/%s", config_home, name);
|
||||||
|
|
||||||
|
struct stat st;
|
||||||
|
if (stat(config_dir, &st) == -1)
|
||||||
|
mkdir(config_dir, 0755);
|
||||||
|
|
||||||
|
int config_file_len = snprintf(NULL, 0, "%s/%s/config", config_home, name);
|
||||||
|
char *config_file_name = malloc(++config_file_len * sizeof(char));
|
||||||
|
snprintf(config_file_name, config_file_len, "%s/%s/config", config_home,
|
||||||
|
name);
|
||||||
|
|
||||||
|
if (config_home_a)
|
||||||
|
free(config_home);
|
||||||
|
free(config_dir);
|
||||||
|
|
||||||
|
return config_file_name;
|
||||||
|
}
|
||||||
|
|
||||||
void klist_config_parse(klist_config *ctx, FILE *config) {
|
void klist_config_parse(klist_config *ctx, FILE *config) {
|
||||||
fseek(config, 0, SEEK_END);
|
fseek(config, 0, SEEK_END);
|
||||||
int c;
|
int c;
|
||||||
|
@ -81,14 +114,30 @@ void klist_config_parse(klist_config *ctx, FILE *config) {
|
||||||
|
|
||||||
void klist_config_setup(char *name, FILE *config) {
|
void klist_config_setup(char *name, FILE *config) {
|
||||||
char *data_home = getenv("XDG_DATA_HOME");
|
char *data_home = getenv("XDG_DATA_HOME");
|
||||||
|
bool data_home_a = false;
|
||||||
|
if (!data_home) {
|
||||||
|
data_home_a = true;
|
||||||
|
char *home = getenv("HOME");
|
||||||
|
const char *data_home_format = "%s/.local/state";
|
||||||
|
int data_home_len = snprintf(name, 0, data_home_format, home);
|
||||||
|
data_home = malloc(++data_home_len * sizeof(char));
|
||||||
|
snprintf(data_home, data_home_len, data_home_format, home, name);
|
||||||
|
}
|
||||||
|
|
||||||
int db_file_len = snprintf(NULL, 0, "%s/%s", data_home, name);
|
int db_file_len = snprintf(NULL, 0, "%s/%s", data_home, name);
|
||||||
char *db_file = malloc(db_file_len * sizeof(char));
|
char *db_file = malloc(++db_file_len * sizeof(char));
|
||||||
snprintf(db_file, db_file_len, "%s/%s", data_home, name);
|
snprintf(db_file, db_file_len, "%s/%s", data_home, name);
|
||||||
|
|
||||||
fseek(config, 0, SEEK_END);
|
struct stat st;
|
||||||
fprintf(config, "%s=%s\n", klist_config_key_to_string(CONFIG_DATABASE_NAME),
|
if (stat(db_file, &st) == -1)
|
||||||
db_file);
|
mkdir(db_file, 0755);
|
||||||
|
|
||||||
|
if (data_home_a)
|
||||||
|
free(data_home);
|
||||||
|
|
||||||
|
// fseek(config, 0, SEEK_END);
|
||||||
|
fprintf(config, "%s=%s/tasks.db\n",
|
||||||
|
klist_config_key_to_string(CONFIG_DATABASE_NAME), db_file);
|
||||||
fclose(config);
|
fclose(config);
|
||||||
|
|
||||||
if (db_file)
|
if (db_file)
|
||||||
|
|
|
@ -9,5 +9,6 @@ struct klist_config {
|
||||||
};
|
};
|
||||||
typedef struct klist_config klist_config;
|
typedef struct klist_config klist_config;
|
||||||
|
|
||||||
|
char *klist_config_name(char *);
|
||||||
void klist_config_parse(klist_config *, FILE *);
|
void klist_config_parse(klist_config *, FILE *);
|
||||||
void klist_config_setup(char *, FILE *);
|
void klist_config_setup(char *, FILE *);
|
|
@ -11,19 +11,15 @@ void print_help(klist *ctx, char **argv);
|
||||||
void setup(klist *ctx, int argc, char **argv);
|
void setup(klist *ctx, int argc, char **argv);
|
||||||
|
|
||||||
int main(int argc, char **argv) {
|
int main(int argc, char **argv) {
|
||||||
char *name = argv[0];
|
char *name = "klist";
|
||||||
char *config_home = getenv("XDG_CONFIG_HOME");
|
|
||||||
|
|
||||||
int config_file_len = snprintf(NULL, 0, "%s/%s", config_home, name);
|
|
||||||
char *config_file_name = malloc(config_file_len * sizeof(char));
|
|
||||||
snprintf(config_file_name, config_file_len, "%s/%s", config_home, name);
|
|
||||||
|
|
||||||
klist_config *config = malloc(sizeof(klist_config));
|
klist_config *config = malloc(sizeof(klist_config));
|
||||||
|
|
||||||
|
char *config_file_name = klist_config_name(name);
|
||||||
|
|
||||||
FILE *config_file = NULL;
|
FILE *config_file = NULL;
|
||||||
if (access(config_file_name, F_OK) == -1) {
|
if (access(config_file_name, F_OK) == -1) {
|
||||||
config_file = fopen(config_file_name, "w");
|
config_file = fopen(config_file_name, "w");
|
||||||
klist_config_setup(argv[0], config_file);
|
klist_config_setup(name, config_file);
|
||||||
} else
|
} else
|
||||||
config_file = fopen(config_file_name, "a+");
|
config_file = fopen(config_file_name, "a+");
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue