Use XDG_DATA_HOME as location for the history file. Also reads and migrates the history from the old location in the users home directory if present. --- client/display.c | 68 +++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 62 insertions(+), 6 deletions(-) diff --git a/client/display.c b/client/display.c index 10c87b2a..d1a1a7f7 100644 --- a/client/display.c +++ b/client/display.c @@ -27,6 +27,9 @@ #define _GNU_SOURCE #include #include +#include +#include +#include #include #include @@ -632,22 +635,75 @@ static void signal_handler(void *user_data) display_refresh_reset(); } +char * get_data_dir() +{ + const char *xdg_data_home; + const char *home_path; + char *data_dir; + + xdg_data_home = getenv("XDG_DATA_HOME"); + if (!xdg_data_home || *xdg_data_home != '/') { + home_path = getenv("HOME"); + if (home_path) + data_dir = l_strdup_printf("%s/%s", home_path, + ".local/share/iwctl"); + else + return NULL; + } else { + data_dir = l_strdup_printf("%s/%s", xdg_data_home, + "iwctl"); + } + return data_dir; +} + +char * get_old_history_file() +{ + const char *home_path; + char *old_history_path; + struct stat st; + int err; + + home_path = getenv("HOME"); + if (home_path) { + old_history_path = l_strdup_printf("%s/%s", home_path, + ".iwctl_history"); + + err = stat(old_history_path, &st); + if (err || !S_ISREG(st.st_mode)) { + l_free(old_history_path); + return NULL; + } + } + return old_history_path; +} + static char *history_path; void display_init(void) { - const char *home_path; + char *data_dir; + char *old_history_file; display_refresh.redo_entries = l_queue_new(); stifle_history(24); - home_path = getenv("HOME"); - if (home_path) - history_path = l_strdup_printf("%s/%s", home_path, - ".iwctl_history"); + data_dir = get_data_dir(); + if (data_dir) { + mkdir(data_dir,0700); + + history_path = l_strdup_printf("%s/%s", data_dir, + "history"); + l_free(data_dir); + } - read_history(history_path); + old_history_file = get_old_history_file(); + if (old_history_file) { + read_history(old_history_file); + unlink(old_history_file); + l_free(old_history_file); + } else + read_history(history_path); setlinebuf(stdout); -- 2.24.0