patch [#28947] Patches for #28943 (Support for XDG Base Directory Specification)
[blender.git] / intern / ghost / intern / GHOST_SystemPathsX11.cpp
index 3153ae8..7261491 100644 (file)
@@ -1,6 +1,4 @@
-/**
- * $Id$
- *
+/*
  * ***** BEGIN GPL LICENSE BLOCK *****
  *
  * This program is free software; you can redistribute it and/or
  * ***** END GPL LICENSE BLOCK *****
  */
 
+/** \file ghost/intern/GHOST_SystemPathsX11.cpp
+ *  \ingroup GHOST
+ */
+
+
 #include "GHOST_SystemPathsX11.h"
 
 #include "GHOST_Debug.h"
 #include <stdio.h> // for fprintf only
 #include <cstdlib> // for exit
 
-#ifndef PREFIX
-#  define PREFIX "/usr/local"
+#ifdef WITH_XDG_USER_DIRS
+#  include <pwd.h> // for get home without use getenv()
+#  include <limits.h> // for PATH_MAX
 #endif
 
-
-using namespace std;
+#ifdef PREFIX
+static const char *static_path= PREFIX "/share" ;
+#else
+static const char *static_path= NULL;
+#endif
 
 GHOST_SystemPathsX11::GHOST_SystemPathsX11()
 {
@@ -55,17 +62,33 @@ GHOST_SystemPathsX11::~GHOST_SystemPathsX11()
 
 const GHOST_TUns8* GHOST_SystemPathsX11::getSystemDir() const
 {
-       return (GHOST_TUns8*) PREFIX "/share";
+       /* no prefix assumes a portable build which only uses bundled scripts */
+       return (const GHOST_TUns8 *)static_path;
 }
 
 const GHOST_TUns8* GHOST_SystemPathsX11::getUserDir() const
 {
-       char* env = getenv("HOME");
-       if(env) {
-               return (GHOST_TUns8*) env;
-       } else {
-               return NULL;
+#ifndef WITH_XDG_USER_DIRS
+       return (const GHOST_TUns8 *)getenv("HOME");
+#else /* WITH_XDG_USER_DIRS */
+       const char *home= getenv("XDG_CONFIG_HOME");
+
+       if (home) {
+               return (const GHOST_TUns8 *)home;
+       }
+       else {
+               static char user_path[PATH_MAX];
+
+               home= getenv("HOME");
+
+               if (home == NULL) {
+                       home= getpwuid(getuid())->pw_dir;
+               }
+
+               snprintf(user_path, sizeof(user_path), "%s/.config", home);
+               return (const GHOST_TUns8 *)user_path;
        }
+#endif /* WITH_XDG_USER_DIRS */
 }
 
 const GHOST_TUns8* GHOST_SystemPathsX11::getBinaryDir() const