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_common )
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, (b_common ? CSIDL_COMMON_APPDATA
99 | CSIDL_FLAG_CREATE, NULL, SHGFP_TYPE_CURRENT, wdir ) )
102 static char appdir[PATH_MAX] = "";
103 static char comappdir[PATH_MAX] = "";
104 WideCharToMultiByte (CP_UTF8, 0, wdir, -1,
105 b_common ? comappdir : appdir,
106 PATH_MAX, NULL, NULL);
107 return b_common ? comappdir : appdir;
113 #ifdef LIBVLC_USE_PTHREAD
114 static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
115 pthread_mutex_lock (&lock);
120 const char *psz_localhome = getenv( "HOME" );
121 #if defined(HAVE_GETPWUID_R)
122 char buf[sysconf (_SC_GETPW_R_SIZE_MAX)];
123 if (psz_localhome == NULL)
125 struct passwd pw, *res;
127 if (!getpwuid_r (getuid (), &pw, buf, sizeof (buf), &res) && res)
128 psz_localhome = pw.pw_dir;
131 if (psz_localhome == NULL)
132 psz_localhome = getenv( "TMP" );
133 if (psz_localhome == NULL)
134 psz_localhome = "/tmp";
136 const char *uhomedir = FromLocale (psz_localhome);
137 strncpy (homedir, uhomedir, sizeof (homedir) - 1);
138 homedir[sizeof (homedir) - 1] = '\0';
139 LocaleFree (uhomedir);
141 #ifdef LIBVLC_USE_PTHREAD
142 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( 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';
172 static char *config_GetHomeDir (void)
176 const char *home = getenv ("HOME");
177 #if defined(HAVE_GETPWUID_R)
178 /* 2/ Try /etc/passwd */
179 char buf[sysconf (_SC_GETPW_R_SIZE_MAX)];
182 struct passwd pw, *res;
184 if (!getpwuid_r (getuid (), &pw, buf, sizeof (buf), &res) && res)
188 /* 3/ Desperately try $TMP */
190 home = getenv( "TMP" );
191 /* 4/ Beyond hope, hard-code /tmp */
195 return FromLocaleDup (home);
198 wchar_t wdir[MAX_PATH];
200 # if defined (UNDER_CE)
201 /*There are some errors in cegcc headers*/
202 #undef SHGetSpecialFolderPath
203 BOOL WINAPI SHGetSpecialFolderPath(HWND,LPWSTR,int,BOOL);
204 if (SHGetSpecialFolderPath (NULL, wdir, CSIDL_APPDATA, 1))
206 if (SHGetFolderPathW (NULL, CSIDL_PERSONAL | CSIDL_FLAG_CREATE,
207 NULL, SHGFP_TYPE_CURRENT, wdir ) == S_OK)
209 return FromWide (wdir);
214 static char *config_GetAppDir (const char *xdg_name, const char *xdg_default)
217 #if defined(WIN32) || defined(__APPLE__) || defined(SYS_BEOS)
218 const char *psz_parent = GetDir (false);
220 if( asprintf( &psz_dir, "%s" DIR_SEP CONFIG_DIR, psz_parent ) == -1 )
223 (void)xdg_name; (void)xdg_default;
225 char var[sizeof ("XDG__HOME") + strlen (xdg_name)];
226 /* XDG Base Directory Specification - Version 0.6 */
227 snprintf (var, sizeof (var), "XDG_%s_HOME", xdg_name);
229 char *psz_home = FromLocale (getenv (var));
232 if( asprintf( &psz_dir, "%s/vlc", psz_home ) == -1 )
234 LocaleFree (psz_home);
238 psz_home = config_GetHomeDir ();
240 || asprintf( &psz_dir, "%s/%s/vlc", psz_home, xdg_default ) == -1 )
248 * Get the user's VLC cache directory
249 * (used for stuff like the modules cache, the album art cache, ...)
251 char *config_GetCacheDir( void )
253 #if defined(__APPLE__)
255 const char *psz_parent = GetDir (false);
257 if( asprintf( &psz_dir, "%s" DIR_SEP CACHES_DIR, psz_parent ) == -1 )
262 return config_GetAppDir ("CACHE", ".cache");
266 char *config_GetUserDir (vlc_userdir_t type)
271 return config_GetHomeDir ();
273 return config_GetAppDir ("CONFIG", ".config");
275 return config_GetAppDir ("DATA", ".local/share");