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 *****************************************************************************/
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 */
50 * config_GetDataDir: find directory where shared data is installed
52 * @return a string (always succeeds).
54 const char *config_GetDataDir( void )
56 #if defined (WIN32) || defined(__APPLE__) || defined (SYS_BEOS)
57 static char path[PATH_MAX] = "";
61 snprintf( path, sizeof( path ), "%s/share",
62 vlc_global()->psz_vlcpath );
63 path[sizeof( path ) - 1] = '\0';
72 * Determines the system configuration directory.
74 * @return a string (always succeeds).
76 const char *config_GetConfDir( void )
78 #if defined (WIN32) || defined(__APPLE__) || defined (SYS_BEOS)
79 static char path[PATH_MAX] = "";
83 snprintf( path, sizeof( path ), "%s/share", /* FIXME: Duh? */
84 vlc_global()->psz_vlcpath );
85 path[sizeof( path ) - 1] = '\0';
93 static const char *GetDir( bool b_appdata )
95 static char homedir[PATH_MAX] = "";
98 wchar_t wdir[MAX_PATH];
100 # if defined (UNDER_CE)
101 if( SHGetSpecialFolderPath( NULL, wdir, CSIDL_APPDATA, 1 ) )
103 /* Get the "Application Data" folder for the current user */
104 if( S_OK == SHGetFolderPathW( NULL,
105 (b_appdata ? CSIDL_APPDATA : CSIDL_PROFILE) | CSIDL_FLAG_CREATE,
106 NULL, SHGFP_TYPE_CURRENT, wdir ) )
109 static char appdir[PATH_MAX] = "";
110 WideCharToMultiByte (CP_UTF8, 0, wdir, -1,
111 b_appdata ? appdir : homedir, PATH_MAX,
113 return b_appdata ? appdir : homedir;
119 #ifdef LIBVLC_USE_PTHREAD
120 static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
121 pthread_mutex_lock (&lock);
126 const char *psz_localhome = getenv( "HOME" );
127 #if defined(HAVE_GETPWUID_R)
128 char buf[sysconf (_SC_GETPW_R_SIZE_MAX)];
129 if (psz_localhome == NULL)
131 struct passwd pw, *res;
133 if (!getpwuid_r (getuid (), &pw, buf, sizeof (buf), &res) && res)
134 psz_localhome = pw.pw_dir;
137 if (psz_localhome == NULL)
138 psz_localhome = getenv( "TMP" );
139 if (psz_localhome == NULL)
140 psz_localhome = "/tmp";
142 const char *uhomedir = FromLocale (psz_localhome);
143 strncpy (homedir, uhomedir, sizeof (homedir) - 1);
144 homedir[sizeof (homedir) - 1] = '\0';
145 LocaleFree (uhomedir);
147 #ifdef LIBVLC_USE_PTHREAD
148 pthread_mutex_unlock (&lock);
154 * Get the user's home directory
156 char *config_GetHomeDir( void )
158 return strdup (GetDir( false ));
161 static char *config_GetFooDir (const char *xdg_name, const char *xdg_default)
164 #if defined(WIN32) || defined(__APPLE__) || defined(SYS_BEOS)
165 char *psz_parent = strdup (GetDir (true));
167 if( asprintf( &psz_dir, "%s" DIR_SEP CONFIG_DIR, psz_parent ) == -1 )
171 (void)xdg_name; (void)xdg_default;
173 char var[sizeof ("XDG__HOME") + strlen (xdg_name)];
175 /* XDG Base Directory Specification - Version 0.6 */
176 snprintf (var, sizeof (var), "XDG_%s_HOME", xdg_name);
177 char *psz_home = getenv( var );
178 psz_home = psz_home ? FromLocaleDup( psz_home ) : NULL;
181 if( asprintf( &psz_dir, "%s/vlc", psz_home ) == -1 )
186 /* Try HOME, then fallback to non-XDG dirs */
187 psz_home = config_GetHomeDir();
188 if( asprintf( &psz_dir, "%s/%s/vlc", psz_home, xdg_default ) == -1 )
198 * Get the user's VLC configuration directory
200 char *config_GetUserConfDir( void )
202 return config_GetFooDir ("CONFIG", ".config");
206 * Get the user's VLC data directory
207 * (used for stuff like the skins, custom lua modules, ...)
209 char *config_GetUserDataDir( void )
211 return config_GetFooDir ("DATA", ".local/share");
215 * Get the user's VLC cache directory
216 * (used for stuff like the modules cache, the album art cache, ...)
218 char *config_GetCacheDir( void )
220 return config_GetFooDir ("CACHE", ".cache");