demux:mkv: don't read further than our parent or its parent
[vlc.git] / src / linux / dirs.c
blob701f1027fc869a1a5cc32fcbece58e0dafca3116
1 /*****************************************************************************
2 * linux/dirs.c: Linux-specific directories
3 *****************************************************************************
4 * Copyright © 2008-2012 Rémi Denis-Courmont
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU Lesser General Public License as published by
8 * the Free Software Foundation; either version 2.1 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
19 *****************************************************************************/
21 #ifdef HAVE_CONFIG_H
22 # include "config.h"
23 #endif
25 #include <stdio.h>
26 #include <string.h>
27 #include <linux/limits.h>
29 #include <vlc_common.h>
30 #include "libvlc.h"
31 #include "config/configuration.h"
33 char *config_GetLibDir (void)
35 static struct
37 vlc_mutex_t lock;
38 char path[PATH_MAX];
39 } cache = {
40 VLC_STATIC_MUTEX, "",
43 /* Reading and parsing /proc/self/maps is slow, so cache the value since
44 * it's guaranteed not to change during the life-time of the process. */
45 vlc_mutex_lock(&cache.lock);
46 if (cache.path[0] != '\0')
48 char *ret = strdup(cache.path);
49 vlc_mutex_unlock(&cache.lock);
50 return ret;
52 char *path = NULL;
54 /* Find the path to libvlc (i.e. ourselves) */
55 FILE *maps = fopen ("/proc/self/maps", "rte");
56 if (maps == NULL)
57 goto error;
59 char *line = NULL;
60 size_t linelen = 0;
61 uintptr_t needle = (uintptr_t)config_GetLibDir;
63 for (;;)
65 ssize_t len = getline (&line, &linelen, maps);
66 if (len == -1)
67 break;
69 void *start, *end;
70 if (sscanf (line, "%p-%p", &start, &end) < 2)
71 continue;
72 /* This mapping contains the address of this function. */
73 if (needle < (uintptr_t)start || (uintptr_t)end <= needle)
74 continue;
76 char *dir = strchr (line, '/');
77 if (dir == NULL)
78 continue;
80 char *file = strrchr (line, '/');
81 if (end == NULL)
82 continue;
83 *file = '\0';
85 if (asprintf (&path, "%s/"PACKAGE, dir) == -1)
86 path = NULL;
87 break;
90 free (line);
91 fclose (maps);
92 error:
93 if (path == NULL)
94 path = strdup(PKGLIBDIR);
95 if (likely(path != NULL && sizeof(cache.path) > strlen(path)))
96 strcpy(cache.path, path);
97 vlc_mutex_unlock(&cache.lock);
98 return path;
101 char *config_GetDataDir (void)
103 const char *path = getenv ("VLC_DATA_PATH");
104 if (path != NULL)
105 return strdup (path);
107 char *libdir = config_GetLibDir ();
108 if (libdir == NULL)
109 return NULL; /* OOM */
111 char *datadir = NULL;
113 /* There are no clean ways to do this, are there?
114 * Due to multilibs, we cannot simply append ../share/. */
115 char *p = strstr (libdir, "/lib/");
116 if (p != NULL)
118 char *p2;
119 /* Deal with nested "lib" directories. Grmbl. */
120 while ((p2 = strstr (p + 4, "/lib/")) != NULL)
121 p = p2;
122 *p = '\0';
124 if (unlikely(asprintf (&datadir, "%s/share/"PACKAGE, libdir) == -1))
125 datadir = NULL;
127 free (libdir);
128 return (datadir != NULL) ? datadir : strdup (PKGDATADIR);