]> git.sesse.net Git - vlc/blob - src/config/dirs.c
Move psz_vlcpath out of p_root
[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     if( SHGetSpecialFolderPath( NULL, wdir, CSIDL_APPDATA, 1 ) )
86 # else
87     /* Get the "Application Data" folder for the current user */
88     if( S_OK == SHGetFolderPathW( NULL,
89               ( b_appdata ? CSIDL_APPDATA :
90                ( b_common_appdata ? CSIDL_COMMON_APPDATA: CSIDL_PERSONAL ) )
91               | CSIDL_FLAG_CREATE,
92                                   NULL, SHGFP_TYPE_CURRENT, wdir ) )
93 # endif
94     {
95         static char appdir[PATH_MAX] = "";
96         static char comappdir[PATH_MAX] = "";
97         WideCharToMultiByte (CP_UTF8, 0, wdir, -1,
98                              b_appdata ? appdir :
99                              (b_common_appdata ? comappdir :homedir),
100                               PATH_MAX, NULL, NULL);
101         return b_appdata ? appdir : (b_common_appdata ? comappdir :homedir);
102     }
103 #else
104     (void)b_appdata;
105     (void)b_common_appdata;
106 #endif
107
108 #ifdef LIBVLC_USE_PTHREAD
109     static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
110     pthread_mutex_lock (&lock);
111 #endif
112
113     if (!*homedir)
114     {
115         const char *psz_localhome = getenv( "HOME" );
116 #if defined(HAVE_GETPWUID_R)
117         char buf[sysconf (_SC_GETPW_R_SIZE_MAX)];
118         if (psz_localhome == NULL)
119         {
120             struct passwd pw, *res;
121
122             if (!getpwuid_r (getuid (), &pw, buf, sizeof (buf), &res) && res)
123                 psz_localhome = pw.pw_dir;
124         }
125 #endif
126         if (psz_localhome == NULL)
127             psz_localhome = getenv( "TMP" );
128         if (psz_localhome == NULL)
129             psz_localhome = "/tmp";
130
131         const char *uhomedir = FromLocale (psz_localhome);
132         strncpy (homedir, uhomedir, sizeof (homedir) - 1);
133         homedir[sizeof (homedir) - 1] = '\0';
134         LocaleFree (uhomedir);
135     }
136 #ifdef LIBVLC_USE_PTHREAD
137     pthread_mutex_unlock (&lock);
138 #endif
139     return homedir;
140 }
141
142 /**
143  * Determines the system configuration directory.
144  *
145  * @return a string (always succeeds).
146  */
147 const char *config_GetConfDir( void )
148 {
149 #if defined (WIN32)
150     return GetDir( false, true );
151 #elif defined(__APPLE__) || defined (SYS_BEOS)
152     static char path[PATH_MAX] = "";
153
154     if( *path == '\0' )
155     {
156         snprintf( path, sizeof( path ), "%s"DIR_SEP DIR_SHARE, /* FIXME: Duh? */
157                   psz_vlcpath );
158         path[sizeof( path ) - 1] = '\0';
159     }
160     return path;
161 #else
162     return SYSCONFDIR;
163 #endif
164 }
165
166 /**
167  * Get the user's home directory
168  */
169 const char *config_GetHomeDir( void )
170 {
171     return GetDir (false, false);
172 }
173
174 static char *config_GetFooDir (const char *xdg_name, const char *xdg_default)
175 {
176     char *psz_dir;
177 #if defined(WIN32) || defined(__APPLE__) || defined(SYS_BEOS)
178     const char *psz_parent = GetDir (true, false);
179
180     if( asprintf( &psz_dir, "%s" DIR_SEP CONFIG_DIR, psz_parent ) == -1 )
181         psz_dir = NULL;
182
183     (void)xdg_name; (void)xdg_default;
184 #else
185     char var[sizeof ("XDG__HOME") + strlen (xdg_name)];
186     /* XDG Base Directory Specification - Version 0.6 */
187     snprintf (var, sizeof (var), "XDG_%s_HOME", xdg_name);
188
189     const char *psz_home = getenv (var);
190     psz_home = psz_home ? FromLocale (psz_home) : NULL;
191     if( psz_home )
192     {
193         if( asprintf( &psz_dir, "%s/vlc", psz_home ) == -1 )
194             psz_dir = NULL;
195         LocaleFree (psz_home);
196         return psz_dir;
197     }
198
199     /* Try HOME, then fallback to non-XDG dirs */
200     psz_home = config_GetHomeDir();
201     if( asprintf( &psz_dir, "%s/%s/vlc", psz_home, xdg_default ) == -1 )
202         psz_dir = NULL;
203 #endif
204     return psz_dir;
205 }
206
207 /**
208  * Get the user's VLC configuration directory
209  */
210 char *config_GetUserConfDir( void )
211 {
212     return config_GetFooDir ("CONFIG", ".config");
213 }
214
215 /**
216  * Get the user's VLC data directory
217  * (used for stuff like the skins, custom lua modules, ...)
218  */
219 char *config_GetUserDataDir( void )
220 {
221     return config_GetFooDir ("DATA", ".local/share");
222 }
223
224 /**
225  * Get the user's VLC cache directory
226  * (used for stuff like the modules cache, the album art cache, ...)
227  */
228 char *config_GetCacheDir( void )
229 {
230     return config_GetFooDir ("CACHE", ".cache");
231 }