[PATCH 2/6] macos.c: update the default path retriaval

Lubomir I. Ivanov neolit123 at gmail.com
Tue Oct 6 03:10:17 PDT 2015


From: "Lubomir I. Ivanov" <neolit123 at gmail.com>

- added system_default_directory()
- both system_default_directory() and system_default_filename()
now use the helper system_default_path_append()
- less safer compared to windows.c, assuming the OS
is stricter on which environment variables exist!

Signed-off-by: Lubomir I. Ivanov <neolit123 at gmail.com>
---

NOTE: only tested within a generic test.c file!
---
 macos.c | 49 +++++++++++++++++++++++++++++++++++++++----------
 1 file changed, 39 insertions(+), 10 deletions(-)

diff --git a/macos.c b/macos.c
index 36c1f60..ca5b1a8 100644
--- a/macos.c
+++ b/macos.c
@@ -43,20 +43,49 @@ bool subsurface_ignore_font(const char *font)
 	return false;
 }
 
-const char *system_default_filename(void)
+static const char *system_default_path_append(const char *append)
 {
-	const char *home, *user;
-	char *buffer;
-	int len;
-
-	home = getenv("HOME");
-	user = getenv("LOGNAME");
-	len = strlen(home) + strlen(user) + 45;
-	buffer = malloc(len);
-	snprintf(buffer, len, "%s/Library/Application Support/Subsurface/%s.xml", home, user);
+	const char *home = getenv("HOME");
+	const char *path = "/Library/Application Support/Subsurface";
+
+	int len = strlen(home) + strlen(path) + 1;
+	if (append)
+		len += strlen(append) + 1;
+
+	char *buffer = (char *)malloc(len);
+	memset(buffer, 0, len);
+	strcat(buffer, home);
+	strcat(buffer, path);
+	if (append) {
+		strcat(buffer, "/");
+		strcat(buffer, append);
+	}
+
 	return buffer;
 }
 
+const char *system_default_directory(void)
+{
+	static const char *path = NULL;
+	if (!path)
+		path = system_default_path_append(NULL);
+	return path;
+}
+
+const char *system_default_filename(void)
+{
+	char filename[128] = { 0 };
+	if (!*filename) {
+		const char *user = getenv("LOGNAME");
+		strcat(filename, user);
+		strcat(filename, ".xml");
+	}
+	static const char *path = NULL;
+	if (!path)
+		path = system_default_path_append(filename);
+	return path;
+}
+
 int enumerate_devices(device_callback_t callback, void *userdata, int dc_type)
 {
 	int index = -1, entries = 0;
-- 
1.7.11.msysgit.0



More information about the subsurface mailing list