]> git.sesse.net Git - vlc/blob - src/config/dirs.c
Cosmetics
[vlc] / src / config / dirs.c
1 /*****************************************************************************
2  * dirs.c: directories configuration
3  *****************************************************************************
4  * Copyright (C) 2001-2007 the VideoLAN team
5  * Copyright © 2007-2008 Rémi Denis-Courmont
6  *
7  * Authors: Gildas Bazin <gbazin@videolan.org>
8  *
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.
13  *
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.
18  *
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  *****************************************************************************/
23
24 #ifdef HAVE_CONFIG_H
25 # include "config.h"
26 #endif
27
28 #include <vlc_common.h>
29
30 #if defined( WIN32 )
31 # ifndef _WIN32_IE
32 #  define _WIN32_IE 0x0501
33 # endif
34 # include <w32api.h>
35 #ifndef UNDER_CE
36 # include <direct.h>
37 #endif
38 # include <shlobj.h>
39 #else
40 # include <unistd.h>
41 # include <pwd.h>
42 #endif
43
44 #include "../libvlc.h"
45 #include "configuration.h"
46 #include <vlc_charset.h>
47 #include <vlc_configuration.h>
48
49 #include <errno.h>                                                  /* errno */
50 #include <assert.h>
51 #include <limits.h>
52
53 #if defined( WIN32 )
54 # define DIR_SHARE ""
55 #else
56 # define DIR_SHARE "share"
57 #endif
58
59
60 /**
61  * config_GetDataDir: find directory where shared data is installed
62  *
63  * @return a string (always succeeds).
64  */
65 const char *config_GetDataDir( void )
66 {
67 #if defined (WIN32) || defined(__APPLE__) || defined (SYS_BEOS)
68     static char path[PATH_MAX] = "";
69
70     if( *path == '\0' )
71     {
72         snprintf( path, sizeof( path ), "%s" DIR_SEP DIR_SHARE, psz_vlcpath );
73         path[sizeof( path ) - 1] = '\0';
74     }
75     return path;
76 #else
77     return DATA_PATH;
78 #endif
79 }
80
81 #if defined (WIN32) || defined(__APPLE__) || defined (SYS_BEOS)
82 static const char *GetDir( bool b_appdata, bool b_common_appdata )
83 {
84     /* FIXME: a full memory page here - quite a waste... */
85     static char homedir[PATH_MAX] = "";
86
87 #if defined (WIN32)
88     wchar_t wdir[MAX_PATH];
89
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 ) )
95 # else
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 ) )
100               | CSIDL_FLAG_CREATE,
101                                   NULL, SHGFP_TYPE_CURRENT, wdir ) )
102 # endif
103     {
104         static char appdir[PATH_MAX] = "";
105         static char comappdir[PATH_MAX] = "";
106         WideCharToMultiByte (CP_UTF8, 0, wdir, -1,
107                              b_appdata ? appdir :
108                              (b_common_appdata ? comappdir :homedir),
109                               PATH_MAX, NULL, NULL);
110         return b_appdata ? appdir : (b_common_appdata ? comappdir :homedir);
111     }
112 #else
113     (void)b_appdata;
114     (void)b_common_appdata;
115 #endif
116
117 #ifdef LIBVLC_USE_PTHREAD
118     static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
119     pthread_mutex_lock (&lock);
120 #endif
121
122     if (!*homedir)
123     {
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)
128         {
129             struct passwd pw, *res;
130
131             if (!getpwuid_r (getuid (), &pw, buf, sizeof (buf), &res) && res)
132                 psz_localhome = pw.pw_dir;
133         }
134 #endif
135         if (psz_localhome == NULL)
136             psz_localhome = getenv( "TMP" );
137         if (psz_localhome == NULL)
138             psz_localhome = "/tmp";
139
140         const char *uhomedir = FromLocale (psz_localhome);
141         strncpy (homedir, uhomedir, sizeof (homedir) - 1);
142         homedir[sizeof (homedir) - 1] = '\0';
143         LocaleFree (uhomedir);
144     }
145 #ifdef LIBVLC_USE_PTHREAD
146     pthread_mutex_unlock (&lock);
147 #endif
148     return homedir;
149 }
150 #endif
151
152 /**
153  * Determines the system configuration directory.
154  *
155  * @return a string (always succeeds).
156  */
157 const char *config_GetConfDir( void )
158 {
159 #if defined (WIN32)
160     return GetDir( false, true );
161 #elif defined(__APPLE__) || defined (SYS_BEOS)
162     static char path[PATH_MAX] = "";
163
164     if( *path == '\0' )
165     {
166         snprintf( path, sizeof( path ), "%s"DIR_SEP DIR_SHARE, /* FIXME: Duh? */
167                   psz_vlcpath );
168         path[sizeof( path ) - 1] = '\0';
169     }
170     return path;
171 #else
172     return SYSCONFDIR;
173 #endif
174 }
175
176 static char *config_GetHomeDir (void)
177 {
178 #ifndef WIN32
179     /* 1/ Try $HOME  */
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)];
184     if (home == NULL)
185     {
186         struct passwd pw, *res;
187
188         if (!getpwuid_r (getuid (), &pw, buf, sizeof (buf), &res) && res)
189             home = pw.pw_dir;
190     }
191 #endif
192     /* 3/ Desperately try $TMP */
193     if (home == NULL)
194         home = getenv( "TMP" );
195     /* 4/ Beyond hope, hard-code /tmp */
196     if (home == NULL)
197         home = "/tmp";
198
199     return FromLocaleDup (home);
200
201 #else /* WIN32 */
202     wchar_t wdir[MAX_PATH];
203
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))
209 # else
210     if (SHGetFolderPathW (NULL, CSIDL_PERSONAL | CSIDL_FLAG_CREATE,
211                           NULL, SHGFP_TYPE_CURRENT, wdir ) == S_OK)
212 # endif
213         return FromWide (wdir);
214     return NULL;
215 #endif
216 }
217
218 static char *config_GetAppDir (const char *xdg_name, const char *xdg_default)
219 {
220     char *psz_dir;
221 #if defined(WIN32) || defined(__APPLE__) || defined(SYS_BEOS)
222     const char *psz_parent = GetDir (true, false);
223
224     if( asprintf( &psz_dir, "%s" DIR_SEP CONFIG_DIR, psz_parent ) == -1 )
225         psz_dir = NULL;
226
227     (void)xdg_name; (void)xdg_default;
228 #else
229     char var[sizeof ("XDG__HOME") + strlen (xdg_name)];
230     /* XDG Base Directory Specification - Version 0.6 */
231     snprintf (var, sizeof (var), "XDG_%s_HOME", xdg_name);
232
233     char *psz_home = FromLocale (getenv (var));
234     if( psz_home )
235     {
236         if( asprintf( &psz_dir, "%s/vlc", psz_home ) == -1 )
237             psz_dir = NULL;
238         LocaleFree (psz_home);
239         return psz_dir;
240     }
241
242     psz_home = config_GetHomeDir ();
243     if( psz_home == NULL
244      || asprintf( &psz_dir, "%s/%s/vlc", psz_home, xdg_default ) == -1 )
245         psz_dir = NULL;
246     free (psz_home);
247 #endif
248     return psz_dir;
249 }
250
251 /**
252  * Get the user's VLC configuration directory
253  */
254 char *config_GetUserConfDir( void )
255 {
256     return config_GetAppDir ("CONFIG", ".config");
257 }
258
259 /**
260  * Get the user's VLC data directory
261  * (used for stuff like the skins, custom lua modules, ...)
262  */
263 char *config_GetUserDataDir( void )
264 {
265     return config_GetAppDir ("DATA", ".local/share");
266 }
267
268 /**
269  * Get the user's VLC cache directory
270  * (used for stuff like the modules cache, the album art cache, ...)
271  */
272 char *config_GetCacheDir( void )
273 {
274 #if defined(__APPLE__)
275     char *psz_dir;
276     const char *psz_parent = GetDir (true, false);
277
278     if( asprintf( &psz_dir, "%s" DIR_SEP CACHES_DIR, psz_parent ) == -1 )
279         psz_dir = NULL;
280
281     return psz_dir;
282 #else
283     return config_GetAppDir ("CACHE", ".cache");
284 #endif
285 }
286
287 char *config_GetUserDir (vlc_userdir_t type)
288 {
289     char *home = config_GetHomeDir ();
290     (void)type;
291     return home;
292 }