commit a075d6225b0b37cfb35a3f8a20eedf554a627998
Author: Raniconduh <clagv.randomgames@gmail.com>
Date: Sun Jan 23 21:13:26 2022 +0000
diff --git a/include/opts.h b/include/opts.h
index d95dbfc..36dc151 100644
--- a/include/opts.h
+++ b/include/opts.h
@@ -112 +121 @@
#ifndef OPTS_H
#define OPTS_H
-#include <stdio.h>
+#include <stdbool.h>
+#define ICONS_VAR "icons"
extern bool show_icons;
extern bool show_dot_files;
+#define COLOR_VAR "color"
extern bool color;
+#define LONG_VAR "long"
extern bool p_long;
+
+bool check_config(void);
+void create_config(void);
+void read_config(void);
+void terminate_opts(void);
+
#endif
diff --git a/src/commands.c b/src/commands.c
index 51804ea..259242e 100644
--- a/src/commands.c
+++ b/src/commands.c
@@ -15914 +15914 @@ void set(char * v) {
void unset(char * v) {
- if (!strcmp(v, "color")) {
+ if (!strcmp(v, COLOR_VAR)) {
color = false;
set_color();
}
#if ICONS
- else if (!strcmp(v, "icons")) show_icons = false;
+ else if (!strcmp(v, ICONS_VAR)) show_icons = false;
#endif
- else if (!strcmp(v, "long")) p_long = false;
+ else if (!strcmp(v, LONG_VAR)) p_long = false;
else {
printw("Unknown variable (%s)", v);
refresh();
diff --git a/src/main.c b/src/main.c
index fc46f02..1b37104 100644
--- a/src/main.c
+++ b/src/main.c
@@ -167 +1611 @@
static size_t first_f, last_f, cursor;
-int main(int argc, char ** argv) {
+int main(int argc, char ** argv) {
+ if (!check_config()) create_config();
+ else read_config();
+ terminate_opts();
+
if (argc > 1) {
for (int i = 1; i < argc; i++) {
if (!strcmp(argv[i], "-p")) {
diff --git a/src/opts.c b/src/opts.c
index f73621a..a44af60 100644
--- a/src/opts.c
+++ b/src/opts.c
@@ -19 +1133 @@
+#include <sys/stat.h>
#include <stdbool.h>
+#include <string.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <ctype.h>
#include "opts.h"
+#define CFG_FNAME "config"
+
bool show_icons = true;
bool show_dot_files = false;
bool color = true;
bool p_long = false;
+
+static char * default_config_dir = NULL;
+static char * csc_config_path = NULL;
+static char * csc_config_file = NULL;
+
+
+bool check_config(void) {
+ char * csc_dir = "cscroll";
+ int csc_len = 7;
+
+ // find default config directory
+ char * xdg_config = getenv("XDG_CONFIG_HOME");
+ char * cfg_path = NULL;
+ if (!xdg_config) {
+ char * home = getenv("HOME");
+ cfg_path = malloc(strlen(home) + 9 + csc_len);
+ sprintf(cfg_path, "%s/.config", home);
+ } else {
+ cfg_path = malloc(strlen(xdg_config) + 1 + csc_len);
+ strcpy(cfg_path, xdg_config);
+ }
+
+ // set default config path variable
+ default_config_dir = malloc(strlen(cfg_path) + 1);
+ strcpy(default_config_dir, cfg_path);
+
+ // set cscroll config dir path
+ strcat(cfg_path, "/"); strcat(cfg_path, csc_dir);
+ csc_config_path = malloc(strlen(cfg_path) + 1);
+ strcpy(csc_config_path, cfg_path);
+
+ // set path to cscroll config file
+ csc_config_file = malloc(strlen(csc_config_path) + 8);
+ sprintf(csc_config_file, "%s/"CFG_FNAME, csc_config_path);
+
+ free(cfg_path);
+
+ struct stat st_buf;
+ // either cscroll config file or dir does not exist
+ if (stat(csc_config_path, &st_buf) < 0 ||
+ stat(csc_config_file, &st_buf) < 0) return false;
+ return true;
+}
+
+
+void create_config(void) {
+ struct stat st_buf;
+ // set with mode rwx------
+ if (stat(default_config_dir, &st_buf) < 0)
+ mkdir(default_config_dir, 0700);
+
+ // set with mode rw-rw-r-
+ if (stat(csc_config_path, &st_buf) < 0)
+ mkdir(csc_config_path, 0664);
+
+ // create config file if it doesnt exist
+ if (stat(csc_config_file, &st_buf) < 0) {
+ FILE * fp = fopen(csc_config_file, "w");
+ if (fp) fclose(fp);
+ }
+}
+
+
+void read_config(void) {
+ FILE * fp = fopen(csc_config_file, "r");
+
+ bool done = false;
+ // read by line
+ while (!done) {
+ char line[256] = {0}; // 0 initialize
+ char * lp = line;
+
+ // read line from file into buffer
+ int c;
+ while ((c = fgetc(fp)) != '\n') {
+ if (c == EOF) {
+ done = true;
+ break;
+ }
+ *lp++ = c;
+ }
+
+ if (!*line) break;
+
+
+ char * var = line;
+ // go past leading white space
+ while (*var && isspace(*var)) var++;
+
+ int n = 0;
+
+ // add null terminator to var
+ char * val = strchr(var, '=');
+ while (isspace(val[n - 1])) n--;
+ val[n] = 0;
+ val++;
+
+ // remove trailing white space after '='
+ while (*val && *val != '=' && isspace(*val)) val++;
+
+ bool bool_val = false;
+ if (!strcmp(val, "true")) bool_val = true;
+
+ if (!strcmp(var, ICONS_VAR)) show_icons = bool_val;
+ else if (!strcmp(var, COLOR_VAR)) color = bool_val;
+ else if (!strcmp(var, LONG_VAR)) p_long = bool_val;
+ }
+
+ fclose(fp);
+}
+
+
+void terminate_opts(void) {
+ free(default_config_dir);
+ free(csc_config_path);
+ free(csc_config_file);
+}