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
42 #include "../libvlc.h"
43 #include "configuration.h"
44 #include <vlc_charset.h>
45 #include <vlc_configuration.h>
47 #include <errno.h> /* errno */
54 # define DIR_SHARE "share"
59 * config_GetDataDir: find directory where shared data is installed
61 * @return a string (always succeeds).
63 const char *config_GetDataDir( void )
65 #if defined (WIN32) || defined(__APPLE__) || defined (SYS_BEOS)
66 static char path[PATH_MAX] = "";
70 snprintf( path, sizeof( path ), "%s" DIR_SEP DIR_SHARE, psz_vlcpath );
71 path[sizeof( path ) - 1] = '\0';
79 static const char *GetDir( bool b_appdata, bool b_common_appdata )
81 /* FIXME: a full memory page here - quite a waste... */
82 static char homedir[PATH_MAX] = "";
85 wchar_t wdir[MAX_PATH];
87 # if defined (UNDER_CE)
88 /*There are some errors in cegcc headers*/
89 #undef SHGetSpecialFolderPath
90 BOOL WINAPI SHGetSpecialFolderPath(HWND,LPWSTR,int,BOOL);
91 if( SHGetSpecialFolderPath( NULL, wdir, CSIDL_APPDATA, 1 ) )
93 /* Get the "Application Data" folder for the current user */
94 if( S_OK == SHGetFolderPathW( NULL,
95 ( b_appdata ? CSIDL_APPDATA :
96 ( b_common_appdata ? CSIDL_COMMON_APPDATA: CSIDL_PERSONAL ) )
98 NULL, SHGFP_TYPE_CURRENT, wdir ) )
101 static char appdir[PATH_MAX] = "";
102 static char comappdir[PATH_MAX] = "";
103 WideCharToMultiByte (CP_UTF8, 0, wdir, -1,
105 (b_common_appdata ? comappdir :homedir),
106 PATH_MAX, NULL, NULL);
107 return b_appdata ? appdir : (b_common_appdata ? comappdir :homedir);
111 (void)b_common_appdata;
114 #ifdef LIBVLC_USE_PTHREAD
115 static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
116 pthread_mutex_lock (&lock);
121 const char *psz_localhome = getenv( "HOME" );
122 #if defined(HAVE_GETPWUID_R)
123 char buf[sysconf (_SC_GETPW_R_SIZE_MAX)];
124 if (psz_localhome == NULL)
126 struct passwd pw, *res;
128 if (!getpwuid_r (getuid (), &pw, buf, sizeof (buf), &res) && res)
129 psz_localhome = pw.pw_dir;
132 if (psz_localhome == NULL)
133 psz_localhome = getenv( "TMP" );
134 if (psz_localhome == NULL)
135 psz_localhome = "/tmp";
137 const char *uhomedir = FromLocale (psz_localhome);
138 strncpy (homedir, uhomedir, sizeof (homedir) - 1);
139 homedir[sizeof (homedir) - 1] = '\0';
140 LocaleFree (uhomedir);
142 #ifdef LIBVLC_USE_PTHREAD
143 pthread_mutex_unlock (&lock);
149 * Determines the system configuration directory.
151 * @return a string (always succeeds).
153 const char *config_GetConfDir( void )
156 return GetDir( false, true );
157 #elif defined(__APPLE__) || defined (SYS_BEOS)
158 static char path[PATH_MAX] = "";
162 snprintf( path, sizeof( path ), "%s"DIR_SEP DIR_SHARE, /* FIXME: Duh? */
164 path[sizeof( path ) - 1] = '\0';
173 * Get the user's home directory
175 const char *config_GetHomeDir( void )
177 return GetDir (false, false);
180 static char *config_GetFooDir (const char *xdg_name, const char *xdg_default)
183 #if defined(WIN32) || defined(__APPLE__) || defined(SYS_BEOS)
184 const char *psz_parent = GetDir (true, false);
186 if( asprintf( &psz_dir, "%s" DIR_SEP CONFIG_DIR, psz_parent ) == -1 )
189 (void)xdg_name; (void)xdg_default;
191 char var[sizeof ("XDG__HOME") + strlen (xdg_name)];
192 /* XDG Base Directory Specification - Version 0.6 */
193 snprintf (var, sizeof (var), "XDG_%s_HOME", xdg_name);
195 const char *psz_home = getenv (var);
196 psz_home = psz_home ? FromLocale (psz_home) : NULL;
199 if( asprintf( &psz_dir, "%s/vlc", psz_home ) == -1 )
201 LocaleFree (psz_home);
205 /* Try HOME, then fallback to non-XDG dirs */
206 psz_home = config_GetHomeDir();
207 if( asprintf( &psz_dir, "%s/%s/vlc", psz_home, xdg_default ) == -1 )
214 * Get the user's VLC configuration directory
216 char *config_GetUserConfDir( void )
218 return config_GetFooDir ("CONFIG", ".config");
222 * Get the user's VLC data directory
223 * (used for stuff like the skins, custom lua modules, ...)
225 char *config_GetUserDataDir( void )
227 return config_GetFooDir ("DATA", ".local/share");
231 * Get the user's VLC cache directory
232 * (used for stuff like the modules cache, the album art cache, ...)
234 char *config_GetCacheDir( void )
236 #if defined(__APPLE__)
238 const char *psz_parent = GetDir (true, false);
240 if( asprintf( &psz_dir, "%s" DIR_SEP CACHES_DIR, psz_parent ) == -1 )
245 return config_GetFooDir ("CACHE", ".cache");