]> git.sesse.net Git - vlc/blob - src/posix/linux_specific.c
Fix Metacube header handling with multiple header blocks.
[vlc] / src / posix / linux_specific.c
1 /*****************************************************************************
2  * linux_specific.c: Linux-specific initialization
3  *****************************************************************************
4  * Copyright © 2008-2012 Rémi Denis-Courmont
5  *
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.
10  *
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.
15  *
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  *****************************************************************************/
20
21 #ifdef HAVE_CONFIG_H
22 # include "config.h"
23 #endif
24
25 #include <stdio.h>
26 #include <string.h>
27
28 #include <vlc_common.h>
29 #include "libvlc.h"
30 #include "config/configuration.h"
31
32 char *config_GetLibDir (void)
33 {
34     char *path = NULL;
35
36     /* Find the path to libvlc (i.e. ourselves) */
37     FILE *maps = fopen ("/proc/self/maps", "rte");
38     if (maps == NULL)
39         goto error;
40
41     char *line = NULL;
42     size_t linelen = 0;
43     uintptr_t needle = (uintptr_t)config_GetLibDir;
44
45     for (;;)
46     {
47         ssize_t len = getline (&line, &linelen, maps);
48         if (len == -1)
49             break;
50
51         void *start, *end;
52         if (sscanf (line, "%p-%p", &start, &end) < 2)
53             continue;
54         /* This mapping contains the address of this function. */
55         if (needle < (uintptr_t)start || (uintptr_t)end <= needle)
56             continue;
57
58         char *dir = strchr (line, '/');
59         if (dir == NULL)
60             continue;
61
62         char *file = strrchr (line, '/');
63         if (end == NULL)
64             continue;
65         *file = '\0';
66
67         if (asprintf (&path, "%s/"PACKAGE, dir) == -1)
68             path = NULL;
69         break;
70     }
71
72     free (line);
73     fclose (maps);
74 error:
75     return (path != NULL) ? path : strdup (PKGLIBDIR);
76 }
77
78 char *config_GetDataDir (void)
79 {
80     const char *path = getenv ("VLC_DATA_PATH");
81     if (path != NULL)
82         return strdup (path);
83
84     char *libdir = config_GetLibDir ();
85     if (libdir == NULL)
86         return NULL; /* OOM */
87
88     char *datadir = NULL;
89
90     /* There are no clean ways to do this, are there?
91      * Due to multilibs, we cannot simply append ../share/. */
92     char *p = strstr (libdir, "/lib/");
93     if (p != NULL)
94     {
95         char *p2;
96         /* Deal with nested "lib" directories. Grmbl. */
97         while ((p2 = strstr (p + 4, "/lib/")) != NULL)
98             p = p2;
99         *p = '\0';
100
101         if (unlikely(asprintf (&datadir, "%s/share/"PACKAGE, libdir) == -1))
102             datadir = NULL;
103     }
104     free (libdir);
105     return (datadir != NULL) ? datadir : strdup (PKGDATADIR);
106 }