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>
32 # define _WIN32_IE 0x0501
44 #include "../libvlc.h"
45 #include "configuration.h"
46 #include <vlc_charset.h>
47 #include <vlc_configuration.h>
49 #include <errno.h> /* errno */
56 # define DIR_SHARE "share"
61 * config_GetDataDir: find directory where shared data is installed
63 * @return a string (always succeeds).
65 const char *config_GetDataDir( void )
67 #if defined (WIN32) || defined(__APPLE__) || defined (SYS_BEOS)
68 static char path[PATH_MAX] = "";
72 snprintf( path, sizeof( path ), "%s" DIR_SEP DIR_SHARE, psz_vlcpath );
73 path[sizeof( path ) - 1] = '\0';
81 #if defined (WIN32) || defined(__APPLE__) || defined (SYS_BEOS)
82 static const char *GetDir( bool b_appdata, bool b_common_appdata )
84 /* FIXME: a full memory page here - quite a waste... */
85 static char homedir[PATH_MAX] = "";
88 wchar_t wdir[MAX_PATH];
90 # if defined (UNDER_CE)
91 /*There are some errors in cegcc headers*/
92 #undef SHGetSpecialFolderPath
93 BOOL WINAPI SHGetSpecialFolderPath(HWND,LPWSTR,int,BOOL);
94 if( SHGetSpecialFolderPath( NULL, wdir, CSIDL_APPDATA, 1 ) )
96 /* Get the "Application Data" folder for the current user */
97 if( S_OK == SHGetFolderPathW( NULL,
98 ( b_appdata ? CSIDL_APPDATA :
99 ( b_common_appdata ? CSIDL_COMMON_APPDATA: CSIDL_PERSONAL ) )
101 NULL, SHGFP_TYPE_CURRENT, wdir ) )
104 static char appdir[PATH_MAX] = "";
105 static char comappdir[PATH_MAX] = "";
106 WideCharToMultiByte (CP_UTF8, 0, wdir, -1,
108 (b_common_appdata ? comappdir :homedir),
109 PATH_MAX, NULL, NULL);
110 return b_appdata ? appdir : (b_common_appdata ? comappdir :homedir);
114 (void)b_common_appdata;
117 #ifdef LIBVLC_USE_PTHREAD
118 static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
119 pthread_mutex_lock (&lock);
124 const char *psz_localhome = getenv( "HOME" );
125 #if defined(HAVE_GETPWUID_R)
126 char buf[sysconf (_SC_GETPW_R_SIZE_MAX)];
127 if (psz_localhome == NULL)
129 struct passwd pw, *res;
131 if (!getpwuid_r (getuid (), &pw, buf, sizeof (buf), &res) && res)
132 psz_localhome = pw.pw_dir;
135 if (psz_localhome == NULL)
136 psz_localhome = getenv( "TMP" );
137 if (psz_localhome == NULL)
138 psz_localhome = "/tmp";
140 const char *uhomedir = FromLocale (psz_localhome);
141 strncpy (homedir, uhomedir, sizeof (homedir) - 1);
142 homedir[sizeof (homedir) - 1] = '\0';
143 LocaleFree (uhomedir);
145 #ifdef LIBVLC_USE_PTHREAD
146 pthread_mutex_unlock (&lock);
153 * Determines the system configuration directory.
155 * @return a string (always succeeds).
157 const char *config_GetConfDir( void )
160 return GetDir( false, true );
161 #elif defined(__APPLE__) || defined (SYS_BEOS)
162 static char path[PATH_MAX] = "";
166 snprintf( path, sizeof( path ), "%s"DIR_SEP DIR_SHARE, /* FIXME: Duh? */
168 path[sizeof( path ) - 1] = '\0';
176 static char *config_GetHomeDir (void)
180 const char *home = getenv ("HOME");
181 #if defined(HAVE_GETPWUID_R)
182 /* 2/ Try /etc/passwd */
183 char buf[sysconf (_SC_GETPW_R_SIZE_MAX)];
186 struct passwd pw, *res;
188 if (!getpwuid_r (getuid (), &pw, buf, sizeof (buf), &res) && res)
192 /* 3/ Desperately try $TMP */
194 home = getenv( "TMP" );
195 /* 4/ Beyond hope, hard-code /tmp */
199 return FromLocaleDup (home);
202 wchar_t wdir[MAX_PATH];
204 # if defined (UNDER_CE)
205 /*There are some errors in cegcc headers*/
206 #undef SHGetSpecialFolderPath
207 BOOL WINAPI SHGetSpecialFolderPath(HWND,LPWSTR,int,BOOL);
208 if (SHGetSpecialFolderPath (NULL, wdir, CSIDL_APPDATA, 1))
210 if (SHGetFolderPathW (NULL, CSIDL_PERSONAL | CSIDL_FLAG_CREATE,
211 NULL, SHGFP_TYPE_CURRENT, wdir ) == S_OK)
213 return FromWide (wdir);
218 char *config_GetUserDir (vlc_userdir_t type)
220 char *home = config_GetHomeDir ();
225 static char *config_GetFooDir (const char *xdg_name, const char *xdg_default)
228 #if defined(WIN32) || defined(__APPLE__) || defined(SYS_BEOS)
229 const char *psz_parent = GetDir (true, false);
231 if( asprintf( &psz_dir, "%s" DIR_SEP CONFIG_DIR, psz_parent ) == -1 )
234 (void)xdg_name; (void)xdg_default;
236 char var[sizeof ("XDG__HOME") + strlen (xdg_name)];
237 /* XDG Base Directory Specification - Version 0.6 */
238 snprintf (var, sizeof (var), "XDG_%s_HOME", xdg_name);
240 char *psz_home = FromLocale (getenv (var));
243 if( asprintf( &psz_dir, "%s/vlc", psz_home ) == -1 )
245 LocaleFree (psz_home);
249 psz_home = config_GetUserDir (VLC_HOME_DIR);
251 || asprintf( &psz_dir, "%s/%s/vlc", psz_home, xdg_default ) == -1 )
259 * Get the user's VLC configuration directory
261 char *config_GetUserConfDir( void )
263 return config_GetFooDir ("CONFIG", ".config");
267 * Get the user's VLC data directory
268 * (used for stuff like the skins, custom lua modules, ...)
270 char *config_GetUserDataDir( void )
272 return config_GetFooDir ("DATA", ".local/share");
276 * Get the user's VLC cache directory
277 * (used for stuff like the modules cache, the album art cache, ...)
279 char *config_GetCacheDir( void )
281 #if defined(__APPLE__)
283 const char *psz_parent = GetDir (true, false);
285 if( asprintf( &psz_dir, "%s" DIR_SEP CACHES_DIR, psz_parent ) == -1 )
290 return config_GetFooDir ("CACHE", ".cache");