1 /* ladspa.c - helper functions related to LADSPA
2 * based on files from the LADSPA SDK
3 * Copyright © 2013 Géraud Meyer <graud@gmx.com>
5 * This file is part of ng-jackspa.
7 * ng-jackspa is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License version 2 as published by the
9 * Free Software Foundation.
11 * This program is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
16 * You should have received a copy of the GNU General Public License along
17 * with ng-jackspa. If not, see <http://www.gnu.org/licenses/>.
20 /* dlopen() wrapper. When the filename is not an absolute path, search the
23 static void *ladspa_dlopen(const char *file
, int flag
)
26 const char *LADSPA_PATH
, *start
, *end
;
28 void *handle
; /* returned handle */
30 filenlength
= strlen(file
);
34 /* The filename is absolute */
35 handle
= dlopen(file
, flag
);
39 else if ((LADSPA_PATH
= getenv("LADSPA_PATH")) ) {
40 /* Check along the LADSPA_PATH path to see if we can find the file
41 * there. We do NOT call dlopen() directly as this would find plugins
42 * on the LD_LIBRARY_PATH */
43 for (start
= LADSPA_PATH
; *start
; start
= *end
== ':' ? ++end
: end
) {
44 for (end
= start
; *end
!= ':' && *end
; end
++);
46 if (!(strbuf
= malloc(filenlength
+ 2 + (end
- start
)))) {
47 fputs("memory allocation error\n", stderr
);
50 strncpy(strbuf
, start
, end
- start
);
51 if (end
> start
&& *(end
- 1) != '/') {
52 strbuf
[end
- start
] = '/';
53 strcpy(strbuf
+ 1 + (end
- start
), file
);
58 handle
= dlopen(strbuf
, flag
);
66 /* As a last effort, try to add the suffix ".so" and recurse */
67 if (filenlength
<= 3 || strcmp(file
+ filenlength
- 3, ".so")) {
68 if (!(strbuf
= malloc(filenlength
+ 4)))
69 fputs("memory allocation error\n", stderr
);
72 strcat(strbuf
, ".so");
74 handle
= ladspa_dlopen(strbuf
, flag
);
82 /* If nothing has worked, then at least we can make sure we set the correct
83 * error message - and this should correspond to a call to dlopen() with
84 * the actual filename requested.
85 * The dlopen() manual page does not specify whether the first or last
86 * error message will be kept when multiple calls are made to dlopen().
87 * We've covered the former case - now we can handle the latter by calling
88 * dlopen() again here.
90 return dlopen(file
, flag
);