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