]> git.sesse.net Git - vlc/blob - src/config/dirs.c
Eliminate some dead code
[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_common )
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, (b_common ? CSIDL_COMMON_APPDATA
98                                                   : CSIDL_APPDATA)
99               | CSIDL_FLAG_CREATE, NULL, SHGFP_TYPE_CURRENT, wdir ) )
100 # endif
101     {
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;
108     }
109 #else
110     (void)b_common;
111 #endif
112
113 #ifdef LIBVLC_USE_PTHREAD
114     static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
115     pthread_mutex_lock (&lock);
116 #endif
117
118     if (!*homedir)
119     {
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)
124         {
125             struct passwd pw, *res;
126
127             if (!getpwuid_r (getuid (), &pw, buf, sizeof (buf), &res) && res)
128                 psz_localhome = pw.pw_dir;
129         }
130 #endif
131         if (psz_localhome == NULL)
132             psz_localhome = getenv( "TMP" );
133         if (psz_localhome == NULL)
134             psz_localhome = "/tmp";
135
136         const char *uhomedir = FromLocale (psz_localhome);
137         strncpy (homedir, uhomedir, sizeof (homedir) - 1);
138         homedir[sizeof (homedir) - 1] = '\0';
139         LocaleFree (uhomedir);
140     }
141 #ifdef LIBVLC_USE_PTHREAD
142     pthread_mutex_unlock (&lock);
143 #endif
144     return homedir;
145 }
146 #endif
147
148 /**
149  * Determines the system configuration directory.
150  *
151  * @return a string (always succeeds).
152  */
153 const char *config_GetConfDir( void )
154 {
155 #if defined (WIN32)
156     return GetDir( true );
157 #elif defined(__APPLE__) || defined (SYS_BEOS)
158     static char path[PATH_MAX] = "";
159
160     if( *path == '\0' )
161     {
162         snprintf( path, sizeof( path ), "%s"DIR_SEP DIR_SHARE, /* FIXME: Duh? */
163                   psz_vlcpath );
164         path[sizeof( path ) - 1] = '\0';
165     }
166     return path;
167 #else
168     return SYSCONFDIR;
169 #endif
170 }
171
172 static char *config_GetHomeDir (void)
173 {
174 #ifndef WIN32
175     /* 1/ Try $HOME  */
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)];
180     if (home == NULL)
181     {
182         struct passwd pw, *res;
183
184         if (!getpwuid_r (getuid (), &pw, buf, sizeof (buf), &res) && res)
185             home = pw.pw_dir;
186     }
187 #endif
188     /* 3/ Desperately try $TMP */
189     if (home == NULL)
190         home = getenv( "TMP" );
191     /* 4/ Beyond hope, hard-code /tmp */
192     if (home == NULL)
193         home = "/tmp";
194
195     return FromLocaleDup (home);
196
197 #else /* WIN32 */
198     wchar_t wdir[MAX_PATH];
199
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))
205 # else
206     if (SHGetFolderPathW (NULL, CSIDL_PERSONAL | CSIDL_FLAG_CREATE,
207                           NULL, SHGFP_TYPE_CURRENT, wdir ) == S_OK)
208 # endif
209         return FromWide (wdir);
210     return NULL;
211 #endif
212 }
213
214 static char *config_GetAppDir (const char *xdg_name, const char *xdg_default)
215 {
216     char *psz_dir;
217 #if defined(WIN32) || defined(__APPLE__) || defined(SYS_BEOS)
218     const char *psz_parent = GetDir (false);
219
220     if( asprintf( &psz_dir, "%s" DIR_SEP CONFIG_DIR, psz_parent ) == -1 )
221         psz_dir = NULL;
222
223     (void)xdg_name; (void)xdg_default;
224 #else
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);
228
229     char *psz_home = FromLocale (getenv (var));
230     if( psz_home )
231     {
232         if( asprintf( &psz_dir, "%s/vlc", psz_home ) == -1 )
233             psz_dir = NULL;
234         LocaleFree (psz_home);
235         return psz_dir;
236     }
237
238     psz_home = config_GetHomeDir ();
239     if( psz_home == NULL
240      || asprintf( &psz_dir, "%s/%s/vlc", psz_home, xdg_default ) == -1 )
241         psz_dir = NULL;
242     free (psz_home);
243 #endif
244     return psz_dir;
245 }
246
247 /**
248  * Get the user's VLC cache directory
249  * (used for stuff like the modules cache, the album art cache, ...)
250  */
251 char *config_GetCacheDir( void )
252 {
253 #if defined(__APPLE__)
254     char *psz_dir;
255     const char *psz_parent = GetDir (false);
256
257     if( asprintf( &psz_dir, "%s" DIR_SEP CACHES_DIR, psz_parent ) == -1 )
258         psz_dir = NULL;
259
260     return psz_dir;
261 #else
262     return config_GetAppDir ("CACHE", ".cache");
263 #endif
264 }
265
266 char *config_GetUserDir (vlc_userdir_t type)
267 {
268     switch (type)
269     {
270         case VLC_HOME_DIR:
271             return config_GetHomeDir ();
272         case VLC_CONFIG_DIR:
273             return config_GetAppDir ("CONFIG", ".config");
274         case VLC_DATA_DIR:
275             return config_GetAppDir ("DATA", ".local/share");
276     }
277     assert (0);
278 }