forked from videolan/vlc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdirs.c
242 lines (218 loc) · 6.7 KB
/
dirs.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/*****************************************************************************
* dirs.c: XDG directories configuration
*****************************************************************************
* Copyright (C) 2001-2007 VLC authors and VideoLAN
* Copyright © 2007-2009 Rémi Denis-Courmont
*
* Authors: Gildas Bazin <[email protected]>
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
*****************************************************************************/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include <vlc_common.h>
#include "../libvlc.h"
#include "config/configuration.h"
#include <unistd.h>
#include <pwd.h>
#include <assert.h>
#include <limits.h>
#if !defined (__linux__)
/**
* Determines the shared data directory
*
* @return a nul-terminated string or NULL. Use free() to release it.
*/
char *config_GetDataDir (void)
{
const char *path = getenv ("VLC_DATA_PATH");
return strdup ((path != NULL) ? path : PKGDATADIR);
}
/**
* Determines the architecture-dependent data directory
*
* @return a string (always succeeds).
*/
char *config_GetLibDir (void)
{
return strdup (PKGLIBDIR);
}
#endif
static char *config_GetHomeDir (void)
{
/* 1/ Try $HOME */
const char *home = getenv ("HOME");
if (home != NULL)
return strdup (home);
#if defined(HAVE_GETPWUID_R)
/* 2/ Try /etc/passwd */
long max = sysconf (_SC_GETPW_R_SIZE_MAX);
if (max != -1)
{
char buf[max];
struct passwd pwbuf, *pw;
if (getpwuid_r (getuid (), &pwbuf, buf, sizeof (buf), &pw) == 0
&& pw != NULL)
return strdup (pw->pw_dir);
}
#endif
return NULL;
}
static char *config_GetAppDir (const char *xdg_name, const char *xdg_default)
{
char *psz_dir;
char var[sizeof ("XDG__HOME") + strlen (xdg_name)];
/* XDG Base Directory Specification - Version 0.6 */
snprintf (var, sizeof (var), "XDG_%s_HOME", xdg_name);
const char *home = getenv (var);
if (home != NULL)
{
if (asprintf (&psz_dir, "%s/vlc", home) == -1)
psz_dir = NULL;
return psz_dir;
}
char *psz_home = config_GetHomeDir ();
if( psz_home == NULL
|| asprintf( &psz_dir, "%s/%s/vlc", psz_home, xdg_default ) == -1 )
psz_dir = NULL;
free (psz_home);
return psz_dir;
}
static char *config_GetTypeDir (const char *xdg_name)
{
const size_t namelen = strlen (xdg_name);
const char *home = getenv ("HOME");
const char *dir = getenv ("XDG_CONFIG_HOME");
const char *file = "user-dirs.dirs";
if (home == NULL)
return NULL;
if (dir == NULL)
{
dir = home;
file = ".config/user-dirs.dirs";
}
char *path;
if (asprintf (&path, "%s/%s", dir, file) == -1)
return NULL;
FILE *stream = fopen (path, "rt");
free (path);
path = NULL;
if (stream != NULL)
{
char *linebuf = NULL;
size_t linelen = 0;
while (getline (&linebuf, &linelen, stream) != -1)
{
char *ptr = linebuf;
ptr += strspn (ptr, " \t"); /* Skip whites */
if (strncmp (ptr, "XDG_", 4))
continue;
ptr += 4; /* Skip XDG_ */
if (strncmp (ptr, xdg_name, namelen))
continue;
ptr += namelen; /* Skip XDG type name */
if (strncmp (ptr, "_DIR", 4))
continue;
ptr += 4; /* Skip _DIR */
ptr += strspn (ptr, " \t"); /* Skip whites */
if (*ptr != '=')
continue;
ptr++; /* Skip equality sign */
ptr += strspn (ptr, " \t"); /* Skip whites */
if (*ptr != '"')
continue;
ptr++; /* Skip quote */
linelen -= ptr - linebuf;
char *out;
if (strncmp (ptr, "$HOME", 5))
{
path = malloc (linelen);
if (path == NULL)
continue;
out = path;
}
else
{ /* Prefix with $HOME */
const size_t homelen = strlen (home);
ptr += 5;
path = malloc (homelen + linelen - 5);
if (path == NULL)
continue;
memcpy (path, home, homelen);
out = path + homelen;
}
while (*ptr != '"')
{
if (*ptr == '\\')
ptr++;
if (*ptr == '\0')
{
free (path);
path = NULL;
continue;
}
*(out++) = *(ptr++);
}
*out = '\0';
break;
}
free (linebuf);
fclose (stream);
}
/* Default! */
if (path == NULL)
{
if (strcmp (xdg_name, "DESKTOP") == 0)
{
if (asprintf (&path, "%s/Desktop", home) == -1)
return NULL;
}
else
path = strdup (home);
}
return path;
}
char *config_GetUserDir (vlc_userdir_t type)
{
switch (type)
{
case VLC_HOME_DIR:
break;
case VLC_CONFIG_DIR:
return config_GetAppDir ("CONFIG", ".config");
case VLC_DATA_DIR:
return config_GetAppDir ("DATA", ".local/share");
case VLC_CACHE_DIR:
return config_GetAppDir ("CACHE", ".cache");
case VLC_DESKTOP_DIR:
return config_GetTypeDir ("DESKTOP");
case VLC_DOWNLOAD_DIR:
return config_GetTypeDir ("DOWNLOAD");
case VLC_TEMPLATES_DIR:
return config_GetTypeDir ("TEMPLATES");
case VLC_PUBLICSHARE_DIR:
return config_GetTypeDir ("PUBLICSHARE");
case VLC_DOCUMENTS_DIR:
return config_GetTypeDir ("DOCUMENTS");
case VLC_MUSIC_DIR:
return config_GetTypeDir ("MUSIC");
case VLC_PICTURES_DIR:
return config_GetTypeDir ("PICTURES");
case VLC_VIDEOS_DIR:
return config_GetTypeDir ("VIDEOS");
}
return config_GetHomeDir ();
}