]> git.sesse.net Git - vlc/blob - src/config/dirs.c
WinCE: more missing functions fixes
[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 # define _WIN32_IE IE5
32 # include <w32api.h>
33 # include <direct.h>
34 # include <shlobj.h>
35 #else
36 # include <unistd.h>
37 # include <pwd.h>
38 #endif
39
40 #include "../libvlc.h"
41 #include "configuration.h"
42 #include <vlc_charset.h>
43 #include <vlc_configuration.h>
44
45 #include <errno.h>                                                  /* errno */
46 #include <assert.h>
47 #include <limits.h>
48
49 #if defined( WIN32 )
50 # define DIR_SHARE ""
51 #else
52 # define DIR_SHARE "share"
53 #endif
54
55 /**
56  * config_GetDataDir: find directory where shared data is installed
57  *
58  * @return a string (always succeeds).
59  */
60 const char *config_GetDataDir( void )
61 {
62 #if defined (WIN32) || defined(__APPLE__) || defined (SYS_BEOS)
63     static char path[PATH_MAX] = "";
64
65     if( *path == '\0' )
66     {
67         snprintf( path, sizeof( path ), "%s" DIR_SEP DIR_SHARE, psz_vlcpath );
68         path[sizeof( path ) - 1] = '\0';
69     }
70     return path;
71 #else
72     return DATA_PATH;
73 #endif
74 }
75
76 static const char *GetDir( bool b_appdata, bool b_common_appdata )
77 {
78     /* FIXME: a full memory page here - quite a waste... */
79     static char homedir[PATH_MAX] = "";
80
81 #if defined (WIN32)
82     wchar_t wdir[MAX_PATH];
83
84 # if defined (UNDER_CE)
85     /*There are some errors in cegcc headers*/
86 #undef SHGetSpecialFolderPath
87     BOOL WINAPI SHGetSpecialFolderPath(HWND,LPWSTR,int,BOOL);
88     if( SHGetSpecialFolderPath( NULL, wdir, CSIDL_APPDATA, 1 ) )
89 # else
90     /* Get the "Application Data" folder for the current user */
91     if( S_OK == SHGetFolderPathW( NULL,
92               ( b_appdata ? CSIDL_APPDATA :
93                ( b_common_appdata ? CSIDL_COMMON_APPDATA: CSIDL_PERSONAL ) )
94               | CSIDL_FLAG_CREATE,
95                                   NULL, SHGFP_TYPE_CURRENT, wdir ) )
96 # endif
97     {
98         static char appdir[PATH_MAX] = "";
99         static char comappdir[PATH_MAX] = "";
100         WideCharToMultiByte (CP_UTF8, 0, wdir, -1,
101                              b_appdata ? appdir :
102                              (b_common_appdata ? comappdir :homedir),
103                               PATH_MAX, NULL, NULL);
104         return b_appdata ? appdir : (b_common_appdata ? comappdir :homedir);
105     }
106 #else
107     (void)b_appdata;
108     (void)b_common_appdata;
109 #endif
110
111 #ifdef LIBVLC_USE_PTHREAD
112     static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
113     pthread_mutex_lock (&lock);
114 #endif
115
116     if (!*homedir)
117     {
118         const char *psz_localhome = getenv( "HOME" );
119 #if defined(HAVE_GETPWUID_R)
120         char buf[sysconf (_SC_GETPW_R_SIZE_MAX)];
121         if (psz_localhome == NULL)
122         {
123             struct passwd pw, *res;
124
125             if (!getpwuid_r (getuid (), &pw, buf, sizeof (buf), &res) && res)
126                 psz_localhome = pw.pw_dir;
127         }
128 #endif
129         if (psz_localhome == NULL)
130             psz_localhome = getenv( "TMP" );
131         if (psz_localhome == NULL)
132             psz_localhome = "/tmp";
133
134         const char *uhomedir = FromLocale (psz_localhome);
135         strncpy (homedir, uhomedir, sizeof (homedir) - 1);
136         homedir[sizeof (homedir) - 1] = '\0';
137         LocaleFree (uhomedir);
138     }
139 #ifdef LIBVLC_USE_PTHREAD
140     pthread_mutex_unlock (&lock);
141 #endif
142     return homedir;
143 }
144
145 /**
146  * Determines the system configuration directory.
147  *
148  * @return a string (always succeeds).
149  */
150 const char *config_GetConfDir( void )
151 {
152 #if defined (WIN32)
153     return GetDir( false, true );
154 #elif defined(__APPLE__) || defined (SYS_BEOS)
155     static char path[PATH_MAX] = "";
156
157     if( *path == '\0' )
158     {
159         snprintf( path, sizeof( path ), "%s"DIR_SEP DIR_SHARE, /* FIXME: Duh? */
160                   psz_vlcpath );
161         path[sizeof( path ) - 1] = '\0';
162     }
163     return path;
164 #else
165     return SYSCONFDIR;
166 #endif
167 }
168
169 /**
170  * Get the user's home directory
171  */
172 const char *config_GetHomeDir( void )
173 {
174     return GetDir (false, false);
175 }
176
177 static char *config_GetFooDir (const char *xdg_name, const char *xdg_default)
178 {
179     char *psz_dir;
180 #if defined(WIN32) || defined(__APPLE__) || defined(SYS_BEOS)
181     const char *psz_parent = GetDir (true, false);
182
183     if( asprintf( &psz_dir, "%s" DIR_SEP CONFIG_DIR, psz_parent ) == -1 )
184         psz_dir = NULL;
185
186     (void)xdg_name; (void)xdg_default;
187 #else
188     char var[sizeof ("XDG__HOME") + strlen (xdg_name)];
189     /* XDG Base Directory Specification - Version 0.6 */
190     snprintf (var, sizeof (var), "XDG_%s_HOME", xdg_name);
191
192     const char *psz_home = getenv (var);
193     psz_home = psz_home ? FromLocale (psz_home) : NULL;
194     if( psz_home )
195     {
196         if( asprintf( &psz_dir, "%s/vlc", psz_home ) == -1 )
197             psz_dir = NULL;
198         LocaleFree (psz_home);
199         return psz_dir;
200     }
201
202     /* Try HOME, then fallback to non-XDG dirs */
203     psz_home = config_GetHomeDir();
204     if( asprintf( &psz_dir, "%s/%s/vlc", psz_home, xdg_default ) == -1 )
205         psz_dir = NULL;
206 #endif
207     return psz_dir;
208 }
209
210 /**
211  * Get the user's VLC configuration directory
212  */
213 char *config_GetUserConfDir( void )
214 {
215     return config_GetFooDir ("CONFIG", ".config");
216 }
217
218 /**
219  * Get the user's VLC data directory
220  * (used for stuff like the skins, custom lua modules, ...)
221  */
222 char *config_GetUserDataDir( void )
223 {
224     return config_GetFooDir ("DATA", ".local/share");
225 }
226
227 /**
228  * Get the user's VLC cache directory
229  * (used for stuff like the modules cache, the album art cache, ...)
230  */
231 char *config_GetCacheDir( void )
232 {
233     return config_GetFooDir ("CACHE", ".cache");
234 }