1 /*****************************************************************************
2 * dirs.c: directories configuration
3 *****************************************************************************
4 * Copyright (C) 2001-2007 the VideoLAN team
5 * Copyright © 2007-2008 Rémi Denis-Courmont
7 * Authors: Gildas Bazin <gbazin@videolan.org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
28 #include <vlc_common.h>
31 # define _WIN32_IE IE5
40 #include "../libvlc.h"
41 #include "configuration.h"
42 #include <vlc_charset.h>
43 #include <vlc_configuration.h>
45 #include <errno.h> /* errno */
52 # define DIR_SHARE "share"
56 * config_GetDataDir: find directory where shared data is installed
58 * @return a string (always succeeds).
60 const char *config_GetDataDir( void )
62 #if defined (WIN32) || defined(__APPLE__) || defined (SYS_BEOS)
63 static char path[PATH_MAX] = "";
67 snprintf( path, sizeof( path ), "%s" DIR_SEP DIR_SHARE,
68 vlc_global()->psz_vlcpath );
69 path[sizeof( path ) - 1] = '\0';
77 static const char *GetDir( bool b_appdata, bool b_common_appdata )
79 /* FIXME: a full memory page here - quite a waste... */
80 static char homedir[PATH_MAX] = "";
83 wchar_t wdir[MAX_PATH];
85 # if defined (UNDER_CE)
86 if( SHGetSpecialFolderPath( NULL, wdir, CSIDL_APPDATA, 1 ) )
88 /* Get the "Application Data" folder for the current user */
89 if( S_OK == SHGetFolderPathW( NULL,
90 (b_appdata ? CSIDL_APPDATA :
91 (b_common_appdata ? CSIDL_PERSONAL : CSIDL_COMMON_APPDATA))
93 NULL, SHGFP_TYPE_CURRENT, wdir ) )
96 static char appdir[PATH_MAX] = "";
97 static char comappdir[PATH_MAX] = "";
98 WideCharToMultiByte (CP_UTF8, 0, wdir, -1,
100 (b_common_appdata ? comappdir :homedir),
101 PATH_MAX, NULL, NULL);
102 return b_appdata ? appdir : (b_common_appdata ? comappdir :homedir);
106 (void)b_common_appdata;
109 #ifdef LIBVLC_USE_PTHREAD
110 static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
111 pthread_mutex_lock (&lock);
116 const char *psz_localhome = getenv( "HOME" );
117 #if defined(HAVE_GETPWUID_R)
118 char buf[sysconf (_SC_GETPW_R_SIZE_MAX)];
119 if (psz_localhome == NULL)
121 struct passwd pw, *res;
123 if (!getpwuid_r (getuid (), &pw, buf, sizeof (buf), &res) && res)
124 psz_localhome = pw.pw_dir;
127 if (psz_localhome == NULL)
128 psz_localhome = getenv( "TMP" );
129 if (psz_localhome == NULL)
130 psz_localhome = "/tmp";
132 const char *uhomedir = FromLocale (psz_localhome);
133 strncpy (homedir, uhomedir, sizeof (homedir) - 1);
134 homedir[sizeof (homedir) - 1] = '\0';
135 LocaleFree (uhomedir);
137 #ifdef LIBVLC_USE_PTHREAD
138 pthread_mutex_unlock (&lock);
144 * Determines the system configuration directory.
146 * @return a string (always succeeds).
148 const char *config_GetConfDir( void )
151 return GetDir( false, true );
152 #elif defined(__APPLE__) || defined (SYS_BEOS)
153 static char path[PATH_MAX] = "";
157 snprintf( path, sizeof( path ), "%s"DIR_SEP DIR_SHARE, /* FIXME: Duh? */
158 vlc_global()->psz_vlcpath );
159 path[sizeof( path ) - 1] = '\0';
168 * Get the user's home directory
170 const char *config_GetHomeDir( void )
172 return GetDir (false, false);
175 static char *config_GetFooDir (const char *xdg_name, const char *xdg_default)
178 #if defined(WIN32) || defined(__APPLE__) || defined(SYS_BEOS)
179 const char *psz_parent = GetDir (true, false);
181 if( asprintf( &psz_dir, "%s" DIR_SEP CONFIG_DIR, psz_parent ) == -1 )
184 (void)xdg_name; (void)xdg_default;
186 char var[sizeof ("XDG__HOME") + strlen (xdg_name)];
187 /* XDG Base Directory Specification - Version 0.6 */
188 snprintf (var, sizeof (var), "XDG_%s_HOME", xdg_name);
190 const char *psz_home = getenv (var);
191 psz_home = psz_home ? FromLocale (psz_home) : NULL;
194 if( asprintf( &psz_dir, "%s/vlc", psz_home ) == -1 )
196 LocaleFree (psz_home);
200 /* Try HOME, then fallback to non-XDG dirs */
201 psz_home = config_GetHomeDir();
202 if( asprintf( &psz_dir, "%s/%s/vlc", psz_home, xdg_default ) == -1 )
209 * Get the user's VLC configuration directory
211 char *config_GetUserConfDir( void )
213 return config_GetFooDir ("CONFIG", ".config");
217 * Get the user's VLC data directory
218 * (used for stuff like the skins, custom lua modules, ...)
220 char *config_GetUserDataDir( void )
222 return config_GetFooDir ("DATA", ".local/share");
226 * Get the user's VLC cache directory
227 * (used for stuff like the modules cache, the album art cache, ...)
229 char *config_GetCacheDir( void )
231 return config_GetFooDir ("CACHE", ".cache");