All of lore.kernel.org
 help / color / mirror / Atom feed
From: Xaver =?unknown-8bit?q?H=C3=B6rl?= <hoe.dom@gmx.de>
To: iwd@lists.01.org
Subject: [PATCH] client: Follow the xdg base directory specification
Date: Tue, 10 Dec 2019 14:06:09 +0100	[thread overview]
Message-ID: <20191210130609.5gnmcbb3cxthmfwi@blumerang> (raw)
In-Reply-To: <B4E8E87E-8EE1-4487-AEFA-59EC699A3FE9@holtmann.org>

[-- Attachment #1: Type: text/plain, Size: 2458 bytes --]

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 <stdio.h>
 #include <signal.h>
+#include <unistd.h>
+#include <sys/types.h>
+#include <sys/stat.h>

 #include <readline/history.h>
 #include <readline/readline.h>
@@ -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

  parent reply	other threads:[~2019-12-10 13:06 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-12-07 10:47 Suggestion: make iwctl follow the XDG Base Directory Specification hoe.dom
2019-12-07 15:47 ` Marcel Holtmann
2019-12-10 13:04   ` Xaver =?unknown-8bit?q?H=C3=B6rl?=
2019-12-10 13:06   ` Xaver =?unknown-8bit?q?H=C3=B6rl?= [this message]
2019-12-13  8:36     ` [PATCH] client: Follow the xdg base directory specification Marcel Holtmann

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20191210130609.5gnmcbb3cxthmfwi@blumerang \
    --to=hoe.dom@gmx.de \
    --cc=iwd@lists.01.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.