patch [#28947] Patches for #28943 (Support for XDG Base Directory Specification)
[blender.git] / intern / ghost / intern / GHOST_SystemPathsX11.cpp
index dd89357..7261491 100644 (file)
@@ -1,6 +1,4 @@
 /*
- * $Id$
- *
  * ***** BEGIN GPL LICENSE BLOCK *****
  *
  * This program is free software; you can redistribute it and/or
 #include <stdio.h> // for fprintf only
 #include <cstdlib> // for exit
 
-using namespace std;
+#ifdef WITH_XDG_USER_DIRS
+#  include <pwd.h> // for get home without use getenv()
+#  include <limits.h> // for PATH_MAX
+#endif
+
+#ifdef PREFIX
+static const char *static_path= PREFIX "/share" ;
+#else
+static const char *static_path= NULL;
+#endif
 
 GHOST_SystemPathsX11::GHOST_SystemPathsX11()
 {
@@ -56,21 +63,32 @@ GHOST_SystemPathsX11::~GHOST_SystemPathsX11()
 const GHOST_TUns8* GHOST_SystemPathsX11::getSystemDir() const
 {
        /* no prefix assumes a portable build which only uses bundled scripts */
-#ifdef PREFIX
-       return (GHOST_TUns8*) PREFIX "/share";
-#else
-       return NULL;
-#endif
+       return (const GHOST_TUns8 *)static_path;
 }
 
 const GHOST_TUns8* GHOST_SystemPathsX11::getUserDir() const
 {
-       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