Initial commit
[resorg.git] / search-so.c
blob95b4d83d59e520d4e4db5c633c1ce4d137be98d8
1 /*
2 * This file is part of Resource Organizer.
4 * Copyright (C) 2014 Nikita Zlobin <nick87720z@gmail.com>
6 * Resource Organizer is free software: you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * Resource Organizer is distributed in the hope that it will be
12 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with Resource Organizer. If not, see
18 * <http://www.gnu.org/licenses/>.
21 #include <dirent.h>
22 #include <dlfcn.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <sys/types.h>
27 #include <unistd.h>
29 #include <search-so.h>
31 /* Search just the one directory. */
32 static void
33 dirIterate (
34 const char * dirname,
35 FilePathHandlerFunc cb_func
37 char * fileName = NULL;
38 DIR * dir;
39 long lDirLength;
40 long iNeedSlash;
41 struct dirent * dirEntry;
43 lDirLength = strlen (dirname);
44 if (!lDirLength) return;
45 if (dirname [lDirLength - 1] == '/')
46 iNeedSlash = 0;
47 else
48 iNeedSlash = 1;
50 dir = opendir (dirname);
51 if (! dir) return;
53 size_t size = 0, new_size;
54 while (1) {
55 dirEntry = readdir (dir);
56 if (! dirEntry) {
57 free (fileName);
58 closedir (dir);
59 return;
62 /* Skip special names */
63 if (strcmp (dirEntry->d_name, "..") == 0 || strcmp (dirEntry->d_name, ".") == 0)
64 continue;
66 /* Reallocation */
67 new_size = lDirLength
68 + strlen (dirEntry->d_name)
69 + 1 + iNeedSlash;
71 if (size < new_size)
72 fileName = realloc (fileName, (size = new_size));
74 strcpy (fileName, dirname);
75 if (iNeedSlash)
76 strcat (fileName, "/");
77 strcat (fileName, dirEntry->d_name);
79 /* Recursive search */
80 if (dirEntry->d_type == DT_DIR)
82 dirIterate (fileName, cb_func);
83 continue;
86 /* We've successfully found a ladspa_descriptor function. Pass
87 * it to the callback function. */
88 cb_func (fileName);
92 /*****************************************************************************/
94 void pathIterate (
95 const char * pathEnv,
96 FilePathHandlerFunc cb_func
98 char * buf = NULL;
99 const char * pcPath;
100 const char * pcStart;
101 const char * pcEnd;
103 pcPath = getenv (pathEnv);
104 if (!pcPath)
106 fprintf( stderr,
107 "Warning: You do not have a LADSPA_PATH "
108 "environment variable set.\n" );
109 return;
112 pcEnd = pcStart = pcPath;
113 while (1) {
114 while (*pcEnd != ':' && *pcEnd != '\0')
115 pcEnd++;
117 size_t size = 0, new_size, len;
118 len = pcEnd - pcStart;
119 new_size = 1 + len;
121 if (size < new_size)
122 buf = realloc (buf, (size = new_size));
124 if (pcEnd > pcStart)
125 strncpy (buf, pcStart, len);
126 buf [len] = '\0';
128 dirIterate (buf, cb_func);
130 if (* pcEnd == ':') pcStart = ++pcEnd;
131 else {
132 free (buf);
133 break;
138 /*****************************************************************************/
140 /* EOF */