Git-Url: 
http://git.frugalware.org/gitweb/gitweb.cgi?p=yaxmenu.git;a=commitdiff;h=c87faf1eb324d5e9ef7fc2cc16c9d1addba98f6f

commit c87faf1eb324d5e9ef7fc2cc16c9d1addba98f6f
Author: James Buren <r...@frugalware.org>
Date:   Sun Dec 6 15:51:40 2009 -0600

xdg.c
* initial draft

diff --git a/src/xdg.c b/src/xdg.c
new file mode 100644
index 0000000..2875aa0
--- /dev/null
+++ b/src/xdg.c
@@ -0,0 +1,91 @@
+#include "globals.h"
+#include "utility.h"
+#include "xdg.h"
+
+static const char home_env[]            = "HOME";
+static const char xdg_delimiter[]       = ":";
+static const size_t xdg_base_elements   = 3;
+static const char xdg_config_home_env[] = "XDG_CONFIG_HOME";
+static const char xdg_config_home_def[] = ".config";
+static const char xdg_config_dirs_env[] = "XDG_CONFIG_DIRS";
+static const char xdg_config_dirs_def[] = "/etc/xdg";
+static const char xdg_data_home_env[]   = "XDG_DATA_HOME";
+static const char xdg_data_home_def[]   = ".local/share";
+static const char xdg_data_dirs_env[]   = "XDG_DATA_DIRS";
+static const char xdg_data_dirs_def[]   = "/usr/local/share:/usr/share";
+
+bool xdg_init(void) {
+  const char *env;
+  char home[PATH_MAX], user[PATH_MAX], sys[PATH_MAX], path[PATH_MAX];
+  char *str;
+  size_t num;
+
+  env = getenv(home_env);
+
+  if(!env) {
+    eprintf("Failed to retrieve %s environmental variable.\n",home_env);
+    return false;
+  }
+
+  xsnprintf(home,"%s",env);
+
+  env = getenv(xdg_config_home_env);
+
+  if(env && *env)
+    xsnprintf(user,"%s",env);
+  else
+    xsnprintf(user,"%s/%s",home,xdg_config_home_def);
+
+  env = getenv(xdg_config_dirs_env);
+
+  if(env && *env)
+    xsnprintf(sys,"%s",env);
+  else
+    xsnprintf(sys,"%s",xdg_config_dirs_def);
+
+  G->cfgdirs = xnew(char *,xstrclen(sys,':') + xdg_base_elements);
+
+  xsnprintf(path,"%s/%s",user,G->progname);
+
+  G->cfgdirs[num = 0] = xstrdup(path);
+
+  str = strtok(sys,xdg_delimiter);
+
+  do {
+    xsnprintf(path,"%s/%s",str,G->progname);
+    G->cfgdirs[++num] = xstrdup(path);
+  } while((str = strtok(NULL,xdg_delimiter)));
+
+  G->cfgdirs[num + 1] = NULL;
+
+  env = getenv(xdg_data_home_env);
+
+  if(env && *env)
+    xsnprintf(user,"%s",env);
+  else
+    xsnprintf(user,"%s/%s",home,xdg_data_home_def);
+
+  env = getenv(xdg_data_dirs_env);
+
+  if(env && *env)
+    xsnprintf(sys,"%s",env);
+  else
+    xsnprintf(sys,"%s",xdg_data_dirs_def);
+
+  G->datadirs = xnew(char *,xstrclen(sys,':') + xdg_base_elements);
+
+  xsnprintf(path,"%s/%s",user,G->progname);
+
+  G->datadirs[num = 0] = xstrdup(path);
+
+  str = strtok(sys,xdg_delimiter);
+
+  do {
+    xsnprintf(path,"%s/%s",str,G->progname);
+    G->datadirs[++num] = xstrdup(path);
+  } while((str = strtok(NULL,xdg_delimiter)));
+
+  G->datadirs[num + 1] = NULL;
+
+  return true;
+}
_______________________________________________
Frugalware-git mailing list
Frugalware-git@frugalware.org
http://frugalware.org/mailman/listinfo/frugalware-git

Reply via email to