]> git.sesse.net Git - vlc/blobdiff - src/config/dirs.c
Eliminate some dead code
[vlc] / src / config / dirs.c
index 16cbf8134c85f4f26b506be6a3e741ff7da7d862..2ebd3c70243536c0040f4e484e2981353ff86c83 100644 (file)
 #include <vlc_common.h>
 
 #if defined( WIN32 )
-# define _WIN32_IE IE5
+# ifndef _WIN32_IE
+#  define _WIN32_IE 0x0501
+# endif
 # include <w32api.h>
+#ifndef UNDER_CE
 # include <direct.h>
+#endif
 # include <shlobj.h>
 #else
 # include <unistd.h>
@@ -52,6 +56,7 @@
 # define DIR_SHARE "share"
 #endif
 
+
 /**
  * config_GetDataDir: find directory where shared data is installed
  *
@@ -73,7 +78,8 @@ const char *config_GetDataDir( void )
 #endif
 }
 
-static const char *GetDir( bool b_appdata, bool b_common_appdata )
+#if defined (WIN32) || defined(__APPLE__) || defined (SYS_BEOS)
+static const char *GetDir( bool b_common )
 {
     /* FIXME: a full memory page here - quite a waste... */
     static char homedir[PATH_MAX] = "";
@@ -82,27 +88,26 @@ static const char *GetDir( bool b_appdata, bool b_common_appdata )
     wchar_t wdir[MAX_PATH];
 
 # if defined (UNDER_CE)
+    /*There are some errors in cegcc headers*/
+#undef SHGetSpecialFolderPath
+    BOOL WINAPI SHGetSpecialFolderPath(HWND,LPWSTR,int,BOOL);
     if( SHGetSpecialFolderPath( NULL, wdir, CSIDL_APPDATA, 1 ) )
 # else
     /* Get the "Application Data" folder for the current user */
-    if( S_OK == SHGetFolderPathW( NULL,
-              ( b_appdata ? CSIDL_APPDATA :
-               ( b_common_appdata ? CSIDL_COMMON_APPDATA: CSIDL_PERSONAL ) )
-              | CSIDL_FLAG_CREATE,
-                                  NULL, SHGFP_TYPE_CURRENT, wdir ) )
+    if( S_OK == SHGetFolderPathW( NULL, (b_common ? CSIDL_COMMON_APPDATA
+                                                  : CSIDL_APPDATA)
+              | CSIDL_FLAG_CREATE, NULL, SHGFP_TYPE_CURRENT, wdir ) )
 # endif
     {
         static char appdir[PATH_MAX] = "";
         static char comappdir[PATH_MAX] = "";
         WideCharToMultiByte (CP_UTF8, 0, wdir, -1,
-                             b_appdata ? appdir :
-                             (b_common_appdata ? comappdir :homedir),
-                              PATH_MAX, NULL, NULL);
-        return b_appdata ? appdir : (b_common_appdata ? comappdir :homedir);
+                             b_common ? comappdir : appdir,
+                             PATH_MAX, NULL, NULL);
+        return b_common ? comappdir : appdir;
     }
 #else
-    (void)b_appdata;
-    (void)b_common_appdata;
+    (void)b_common;
 #endif
 
 #ifdef LIBVLC_USE_PTHREAD
@@ -138,6 +143,7 @@ static const char *GetDir( bool b_appdata, bool b_common_appdata )
 #endif
     return homedir;
 }
+#endif
 
 /**
  * Determines the system configuration directory.
@@ -147,7 +153,7 @@ static const char *GetDir( bool b_appdata, bool b_common_appdata )
 const char *config_GetConfDir( void )
 {
 #if defined (WIN32)
-    return GetDir( false, true );
+    return GetDir( true );
 #elif defined(__APPLE__) || defined (SYS_BEOS)
     static char path[PATH_MAX] = "";
 
@@ -163,19 +169,53 @@ const char *config_GetConfDir( void )
 #endif
 }
 
-/**
- * Get the user's home directory
- */
-const char *config_GetHomeDir( void )
+static char *config_GetHomeDir (void)
 {
-    return GetDir (false, false);
+#ifndef WIN32
+    /* 1/ Try $HOME  */
+    const char *home = getenv ("HOME");
+#if defined(HAVE_GETPWUID_R)
+    /* 2/ Try /etc/passwd */
+    char buf[sysconf (_SC_GETPW_R_SIZE_MAX)];
+    if (home == NULL)
+    {
+        struct passwd pw, *res;
+
+        if (!getpwuid_r (getuid (), &pw, buf, sizeof (buf), &res) && res)
+            home = pw.pw_dir;
+    }
+#endif
+    /* 3/ Desperately try $TMP */
+    if (home == NULL)
+        home = getenv( "TMP" );
+    /* 4/ Beyond hope, hard-code /tmp */
+    if (home == NULL)
+        home = "/tmp";
+
+    return FromLocaleDup (home);
+
+#else /* WIN32 */
+    wchar_t wdir[MAX_PATH];
+
+# if defined (UNDER_CE)
+    /*There are some errors in cegcc headers*/
+#undef SHGetSpecialFolderPath
+    BOOL WINAPI SHGetSpecialFolderPath(HWND,LPWSTR,int,BOOL);
+    if (SHGetSpecialFolderPath (NULL, wdir, CSIDL_APPDATA, 1))
+# else
+    if (SHGetFolderPathW (NULL, CSIDL_PERSONAL | CSIDL_FLAG_CREATE,
+                          NULL, SHGFP_TYPE_CURRENT, wdir ) == S_OK)
+# endif
+        return FromWide (wdir);
+    return NULL;
+#endif
 }
 
-static char *config_GetFooDir (const char *xdg_name, const char *xdg_default)
+static char *config_GetAppDir (const char *xdg_name, const char *xdg_default)
 {
     char *psz_dir;
 #if defined(WIN32) || defined(__APPLE__) || defined(SYS_BEOS)
-    const char *psz_parent = GetDir (true, false);
+    const char *psz_parent = GetDir (false);
 
     if( asprintf( &psz_dir, "%s" DIR_SEP CONFIG_DIR, psz_parent ) == -1 )
         psz_dir = NULL;
@@ -186,8 +226,7 @@ static char *config_GetFooDir (const char *xdg_name, const char *xdg_default)
     /* XDG Base Directory Specification - Version 0.6 */
     snprintf (var, sizeof (var), "XDG_%s_HOME", xdg_name);
 
-    const char *psz_home = getenv (var);
-    psz_home = psz_home ? FromLocale (psz_home) : NULL;
+    char *psz_home = FromLocale (getenv (var));
     if( psz_home )
     {
         if( asprintf( &psz_dir, "%s/vlc", psz_home ) == -1 )
@@ -196,36 +235,44 @@ static char *config_GetFooDir (const char *xdg_name, const char *xdg_default)
         return psz_dir;
     }
 
-    /* Try HOME, then fallback to non-XDG dirs */
-    psz_home = config_GetHomeDir();
-    if( asprintf( &psz_dir, "%s/%s/vlc", psz_home, xdg_default ) == -1 )
+    psz_home = config_GetHomeDir ();
+    if( psz_home == NULL
+     || asprintf( &psz_dir, "%s/%s/vlc", psz_home, xdg_default ) == -1 )
         psz_dir = NULL;
+    free (psz_home);
 #endif
     return psz_dir;
 }
 
 /**
- * Get the user's VLC configuration directory
+ * Get the user's VLC cache directory
+ * (used for stuff like the modules cache, the album art cache, ...)
  */
-char *config_GetUserConfDir( void )
+char *config_GetCacheDir( void )
 {
-    return config_GetFooDir ("CONFIG", ".config");
-}
+#if defined(__APPLE__)
+    char *psz_dir;
+    const char *psz_parent = GetDir (false);
 
-/**
- * Get the user's VLC data directory
- * (used for stuff like the skins, custom lua modules, ...)
- */
-char *config_GetUserDataDir( void )
-{
-    return config_GetFooDir ("DATA", ".local/share");
+    if( asprintf( &psz_dir, "%s" DIR_SEP CACHES_DIR, psz_parent ) == -1 )
+        psz_dir = NULL;
+
+    return psz_dir;
+#else
+    return config_GetAppDir ("CACHE", ".cache");
+#endif
 }
 
-/**
- * Get the user's VLC cache directory
- * (used for stuff like the modules cache, the album art cache, ...)
- */
-char *config_GetCacheDir( void )
+char *config_GetUserDir (vlc_userdir_t type)
 {
-    return config_GetFooDir ("CACHE", ".cache");
+    switch (type)
+    {
+        case VLC_HOME_DIR:
+            return config_GetHomeDir ();
+        case VLC_CONFIG_DIR:
+            return config_GetAppDir ("CONFIG", ".config");
+        case VLC_DATA_DIR:
+            return config_GetAppDir ("DATA", ".local/share");
+    }
+    assert (0);
 }