1 /*****************************************************************************
2 * dirs.c: XDG directories configuration
3 *****************************************************************************
4 * Copyright (C) 2001-2007 VLC authors and VideoLAN
5 * Copyright © 2007-2009 Rémi Denis-Courmont
7 * Authors: Gildas Bazin <gbazin@videolan.org>
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU Lesser General Public License as published by
11 * the Free Software Foundation; either version 2.1 of the License, or
12 * (at your option) any later version.
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 Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with this program; if not, write to the Free Software Foundation,
21 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
28 #include <vlc_common.h>
30 #include "../libvlc.h"
31 #include "config/configuration.h"
38 #if !defined (__linux__)
40 * Determines the shared data directory
42 * @return a nul-terminated string or NULL. Use free() to release it.
44 char *config_GetDataDir (void)
46 const char *path = getenv ("VLC_DATA_PATH");
47 return strdup ((path != NULL) ? path : PKGDATADIR);
51 * Determines the architecture-dependent data directory
53 * @return a string (always succeeds).
55 char *config_GetLibDir (void)
57 return strdup (PKGLIBDIR);
62 * Determines the system configuration directory.
64 * @return a string (always succeeds).
66 const char *config_GetConfDir( void )
71 static char *config_GetHomeDir (void)
74 const char *home = getenv ("HOME");
75 #if defined(HAVE_GETPWUID_R)
76 /* 2/ Try /etc/passwd */
77 char buf[sysconf (_SC_GETPW_R_SIZE_MAX)];
80 struct passwd pw, *res;
82 if (!getpwuid_r (getuid (), &pw, buf, sizeof (buf), &res) && res)
93 static char *config_GetAppDir (const char *xdg_name, const char *xdg_default)
96 char var[sizeof ("XDG__HOME") + strlen (xdg_name)];
98 /* XDG Base Directory Specification - Version 0.6 */
99 snprintf (var, sizeof (var), "XDG_%s_HOME", xdg_name);
101 const char *home = getenv (var);
104 if (asprintf (&psz_dir, "%s/vlc", home) == -1)
109 char *psz_home = config_GetHomeDir ();
111 || asprintf( &psz_dir, "%s/%s/vlc", psz_home, xdg_default ) == -1 )
117 static char *config_GetTypeDir (const char *xdg_name)
119 const size_t namelen = strlen (xdg_name);
120 const char *home = getenv ("HOME");
121 const char *dir = getenv ("XDG_CONFIG_HOME");
122 const char *file = "user-dirs.dirs";
129 file = ".config/user-dirs.dirs";
133 if (asprintf (&path, "%s/%s", dir, file) == -1)
136 FILE *stream = fopen (path, "rt");
141 char *linebuf = NULL;
144 while (getline (&linebuf, &linelen, stream) != -1)
147 ptr += strspn (ptr, " \t"); /* Skip whites */
148 if (strncmp (ptr, "XDG_", 4))
150 ptr += 4; /* Skip XDG_ */
151 if (strncmp (ptr, xdg_name, namelen))
153 ptr += namelen; /* Skip XDG type name */
154 if (strncmp (ptr, "_DIR", 4))
156 ptr += 4; /* Skip _DIR */
157 ptr += strspn (ptr, " \t"); /* Skip whites */
160 ptr++; /* Skip equality sign */
161 ptr += strspn (ptr, " \t"); /* Skip whites */
164 ptr++; /* Skip quote */
165 linelen -= ptr - linebuf;
168 if (strncmp (ptr, "$HOME", 5))
170 path = malloc (linelen);
176 { /* Prefix with $HOME */
177 const size_t homelen = strlen (home);
179 path = malloc (homelen + linelen - 5);
182 memcpy (path, home, homelen);
183 out = path + homelen;
208 if (strcmp (xdg_name, "DESKTOP") == 0)
210 if (asprintf (&path, "%s/Desktop", home) == -1)
214 path = strdup (home);
221 char *config_GetUserDir (vlc_userdir_t type)
228 return config_GetAppDir ("CONFIG", ".config");
230 return config_GetAppDir ("DATA", ".local/share");
232 return config_GetAppDir ("CACHE", ".cache");
234 case VLC_DESKTOP_DIR:
235 return config_GetTypeDir ("DESKTOP");
236 case VLC_DOWNLOAD_DIR:
237 return config_GetTypeDir ("DOWNLOAD");
238 case VLC_TEMPLATES_DIR:
239 return config_GetTypeDir ("TEMPLATES");
240 case VLC_PUBLICSHARE_DIR:
241 return config_GetTypeDir ("PUBLICSHARE");
242 case VLC_DOCUMENTS_DIR:
243 return config_GetTypeDir ("DOCUMENTS");
245 return config_GetTypeDir ("MUSIC");
246 case VLC_PICTURES_DIR:
247 return config_GetTypeDir ("PICTURES");
249 return config_GetTypeDir ("VIDEOS");
251 return config_GetHomeDir ();