You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

128 lines
3.0KB

  1. /* search.c
  2. Free software by Richard W.E. Furse. Do with as you will. No
  3. warranty. */
  4. /*****************************************************************************/
  5. #include <dirent.h>
  6. #include <dlfcn.h>
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #include <sys/types.h>
  11. #include <unistd.h>
  12. /*****************************************************************************/
  13. #include "ladspa.h"
  14. #include "utils.h"
  15. /*****************************************************************************/
  16. /* Search just the one directory. */
  17. static void
  18. LADSPADirectoryPluginSearch
  19. (const char * pcDirectory,
  20. LADSPAPluginSearchCallbackFunction fCallbackFunction) {
  21. char * pcFilename;
  22. DIR * psDirectory;
  23. LADSPA_Descriptor_Function fDescriptorFunction;
  24. long lDirLength;
  25. long iNeedSlash;
  26. struct dirent * psDirectoryEntry;
  27. void * pvPluginHandle;
  28. lDirLength = strlen(pcDirectory);
  29. if (!lDirLength)
  30. return;
  31. if (pcDirectory[lDirLength - 1] == '/')
  32. iNeedSlash = 0;
  33. else
  34. iNeedSlash = 1;
  35. psDirectory = opendir(pcDirectory);
  36. if (!psDirectory)
  37. return;
  38. while (1) {
  39. psDirectoryEntry = readdir(psDirectory);
  40. if (!psDirectoryEntry) {
  41. closedir(psDirectory);
  42. return;
  43. }
  44. pcFilename = malloc(lDirLength
  45. + strlen(psDirectoryEntry->d_name)
  46. + 1 + iNeedSlash);
  47. strcpy(pcFilename, pcDirectory);
  48. if (iNeedSlash)
  49. strcat(pcFilename, "/");
  50. strcat(pcFilename, psDirectoryEntry->d_name);
  51. pvPluginHandle = dlopen(pcFilename, RTLD_LAZY);
  52. if (pvPluginHandle) {
  53. /* This is a file and the file is a shared library! */
  54. dlerror();
  55. fDescriptorFunction
  56. = (LADSPA_Descriptor_Function)dlsym(pvPluginHandle,
  57. "ladspa_descriptor");
  58. if (dlerror() == NULL && fDescriptorFunction) {
  59. /* We've successfully found a ladspa_descriptor function. Pass
  60. it to the callback function. */
  61. fCallbackFunction(pcFilename,
  62. pvPluginHandle,
  63. fDescriptorFunction);
  64. }
  65. else {
  66. /* It was a library, but not a LADSPA one. Unload it. */
  67. dlclose(pcFilename);
  68. }
  69. }
  70. }
  71. }
  72. /*****************************************************************************/
  73. void
  74. LADSPAPluginSearch(LADSPAPluginSearchCallbackFunction fCallbackFunction) {
  75. char * pcBuffer;
  76. const char * pcEnd;
  77. const char * pcLADSPAPath;
  78. const char * pcStart;
  79. pcLADSPAPath = getenv("LADSPA_PATH");
  80. if (!pcLADSPAPath) {
  81. fprintf(stderr,
  82. "Warning: You do not have a LADSPA_PATH "
  83. "environment variable set.\n");
  84. return;
  85. }
  86. pcStart = pcLADSPAPath;
  87. while (*pcStart != '\0') {
  88. pcEnd = pcStart;
  89. while (*pcEnd != ':' && *pcEnd != '\0')
  90. pcEnd++;
  91. pcBuffer = malloc(1 + pcEnd - pcStart);
  92. if (pcEnd > pcStart)
  93. strncpy(pcBuffer, pcStart, pcEnd - pcStart);
  94. pcBuffer[pcEnd - pcStart] = '\0';
  95. LADSPADirectoryPluginSearch(pcBuffer, fCallbackFunction);
  96. pcStart = pcEnd;
  97. if (*pcStart == ':')
  98. pcStart++;
  99. }
  100. }
  101. /*****************************************************************************/
  102. /* EOF */