Change the gmp download URL to https://gmplib.org/download
[vlc/gmpfix.git] / src / posix / linux_specific.c
blobef2426dbea79b3de5e57b0f7b18d1ac0deaed110
1 /*****************************************************************************
2 * linux_specific.c: Linux-specific initialization
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>
28 #include <vlc_common.h>
29 #include "libvlc.h"
30 #include "config/configuration.h"
32 char *config_GetLibDir (void)
34 char *path = NULL;
36 /* Find the path to libvlc (i.e. ourselves) */
37 FILE *maps = fopen ("/proc/self/maps", "rte");
38 if (maps == NULL)
39 goto error;
41 char *line = NULL;
42 size_t linelen = 0;
43 uintptr_t needle = (uintptr_t)config_GetLibDir;
45 for (;;)
47 ssize_t len = getline (&line, &linelen, maps);
48 if (len == -1)
49 break;
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;
58 char *dir = strchr (line, '/');
59 if (dir == NULL)
60 continue;
62 char *file = strrchr (line, '/');
63 if (end == NULL)
64 continue;
65 *file = '\0';
67 if (asprintf (&path, "%s/"PACKAGE, dir) == -1)
68 path = NULL;
69 break;
72 free (line);
73 fclose (maps);
74 error:
75 return (path != NULL) ? path : strdup (PKGLIBDIR);
78 char *config_GetDataDir (void)
80 const char *path = getenv ("VLC_DATA_PATH");
81 if (path != NULL)
82 return strdup (path);
84 char *libdir = config_GetLibDir ();
85 if (libdir == NULL)
86 return NULL; /* OOM */
88 char *datadir = NULL;
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)
95 char *p2;
96 /* Deal with nested "lib" directories. Grmbl. */
97 while ((p2 = strstr (p + 4, "/lib/")) != NULL)
98 p = p2;
99 *p = '\0';
101 if (unlikely(asprintf (&datadir, "%s/share/"PACKAGE, libdir) == -1))
102 datadir = NULL;
104 free (libdir);
105 return (datadir != NULL) ? datadir : strdup (PKGDATADIR);