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 )
247 static char *config_GetTypeDir (const char *xdg_name)
249 #if defined(WIN32) || defined(__APPLE__) || defined(SYS_BEOS)
251 return config_GetAppDir (NULL, NULL);
253 const size_t namelen = strlen (xdg_name);
254 const char *home = getenv ("HOME");
255 const size_t homelen = strlen (home);
256 const char *dir = getenv ("XDG_CONFIG_HOME");
257 const char *file = "user-dirs.dirs";
264 file = ".config/user-dirs.dirs";
268 if (asprintf (&path, "%s/%s", dir, file) == -1)
271 FILE *stream = fopen (path, "rt");
276 char *linebuf = NULL;
279 while (getline (&linebuf, &linelen, stream) != -1)
282 ptr += strspn (ptr, " \t"); /* Skip whites */
283 if (strncmp (ptr, "XDG_", 4))
285 ptr += 4; /* Skip XDG_ */
286 if (strncmp (ptr, xdg_name, namelen))
288 ptr += namelen; /* Skip XDG type name */
289 if (strncmp (ptr, "_DIR", 4))
291 ptr += 4; /* Skip _DIR */
292 ptr += strspn (ptr, " \t"); /* Skip whites */
295 ptr++; /* Skip equality sign */
296 ptr += strspn (ptr, " \t"); /* Skip whites */
299 ptr++; /* Skip quote */
300 linelen -= ptr - linebuf;
303 if (strncmp (ptr, "$HOME", 5))
305 path = malloc (linelen);
311 { /* Prefix with $HOME */
313 path = malloc (homelen + linelen - 5);
316 memcpy (path, home, homelen);
317 out = path + homelen;
335 if (strcmp (xdg_name, "DESKTOP") == 0)
337 if (asprintf (&path, "%s/Desktop", home) == -1)
341 path = strdup (home);
345 char *ret = FromLocaleDup (path);
353 * Get the user's VLC cache directory
354 * (used for stuff like the modules cache, the album art cache, ...)
356 char *config_GetCacheDir( void )
358 #if defined(__APPLE__)
360 const char *psz_parent = GetDir (false);
362 if( asprintf( &psz_dir, "%s" DIR_SEP CACHES_DIR, psz_parent ) == -1 )
367 return config_GetAppDir ("CACHE", ".cache");
371 char *config_GetUserDir (vlc_userdir_t type)
376 return config_GetHomeDir ();
378 return config_GetAppDir ("CONFIG", ".config");
380 return config_GetAppDir ("DATA", ".local/share");
381 case VLC_DESKTOP_DIR:
382 return config_GetTypeDir ("DESKTOP");
383 case VLC_DOWNLOAD_DIR:
384 return config_GetTypeDir ("DOWNLOAD");
385 case VLC_TEMPLATES_DIR:
386 return config_GetTypeDir ("TEMPLATES");
387 case VLC_PUBLICSHARE_DIR:
388 return config_GetTypeDir ("PUBLICSHARE");
389 case VLC_DOCUMENTS_DIR:
390 return config_GetTypeDir ("DOCUMENTS");
392 return config_GetTypeDir ("MUSIC");
393 case VLC_PICTURES_DIR:
394 return config_GetTypeDir ("PICTURES");
396 return config_GetTypeDir ("VIDEOS");