DSL
WhereIs.cpp
1 //@AUTOHEADER@BEGIN@
2 /***********************************************************************\
3 | Drift Standard Libraries v1.01 |
4 | Copyright 2010-2023 Drift Solutions / Indy Sams |
5 | Docs and more information available at https://www.driftsolutions.dev |
6 | This file released under the 3-clause BSD license, |
7 | see included DSL.LICENSE.TXT file for details. |
8 \***********************************************************************/
9 //@AUTOHEADER@END@
10 
11 #include <drift/dslcore.h>
12 #include <drift/WhereIs.h>
13 #include <drift/Directory.h>
14 #include <drift/GenLib.h>
15 
16 void WhereIs_Search(WHEREIS_RESULTS * ret, const char * sDir, const char * fn) {
17  char buf[MAX_PATH],buf2[MAX_PATH];
18  memset(&buf, 0, sizeof(buf));
19  memset(&buf2, 0, sizeof(buf2));
20  bool is_dir = false;
21  Directory dir(sDir);
22  while (dir.Read(buf, sizeof(buf), &is_dir)) {
23  if (!is_dir) {
24  if (wildicmp(fn, buf)) {
25  sprintf(buf2, "%s%s", sDir, buf);
26  ret->sResults[ret->nCount] = dsl_strdup(buf2);
27  ret->nCount++;
28  }
29  }
30  }
31 }
32 
33 WHEREIS_RESULTS * DSL_CC WhereIs(const char * fn) {
34  WHEREIS_RESULTS * ret = (WHEREIS_RESULTS *)dsl_malloc(sizeof(WHEREIS_RESULTS));
35  memset(ret, 0, sizeof(WHEREIS_RESULTS));
36  char dir[MAX_PATH];
37  memset(&dir, 0, sizeof(dir));
38  if (getcwd(dir, sizeof(dir))) {
39  if (dir[strlen(dir) - 1] != PATH_SEP) {
40  strcat(dir, PATH_SEPS);
41  }
42  WhereIs_Search(ret, dir, fn);
43  }
44 #ifdef WIN32
45  if (SHGetSpecialFolderPathA(NULL, dir, CSIDL_SYSTEM, FALSE)) {
46  if (dir[strlen(dir) - 1] != PATH_SEP) {
47  strcat(dir, PATH_SEPS);
48  }
49  WhereIs_Search(ret, dir, fn);
50  }
51  if (SHGetSpecialFolderPathA(NULL, dir, CSIDL_WINDOWS, FALSE)) {
52  if (dir[strlen(dir) - 1] != PATH_SEP) {
53  strcat(dir, PATH_SEPS);
54  }
55  WhereIs_Search(ret, dir, fn);
56  }
57 #else
58  #if defined(__x86_64__)
59  WhereIs_Search(ret, "/usr/lib64/", fn);
60  WhereIs_Search(ret, "/usr/local/lib64/", fn);
61  WhereIs_Search(ret, "/lib64/", fn);
62  WhereIs_Search(ret, "/usr/lib/x86_64-linux-gnu/", fn);
63  #endif
64  WhereIs_Search(ret, "/usr/lib/", fn);
65  WhereIs_Search(ret, "/usr/local/lib/", fn);
66  WhereIs_Search(ret, "/lib/", fn);
67  WhereIs_Search(ret, "/usr/lib/i386-linux-gnu/", fn);
68 
69  const char * ld = getenv("LD_LIBRARY_PATH");
70  if (ld && strlen(ld)) {
71  char * tmp = dsl_strdup(ld);
72  char * p2 = NULL;
73  char * p = strtok_r(tmp, ":", &p2);
74  while (p) {
75  strcpy(dir, p);
76  if (dir[strlen(dir) - 1] != PATH_SEP) {
77  strcat(dir, PATH_SEPS);
78  }
79  WhereIs_Search(ret, dir, fn);
80  p = strtok_r(NULL, ":", &p2);
81  }
82  dsl_free(tmp);
83  }
84 #endif
85  return ret;
86 }
87 
88 void DSL_CC WhereIs_FreeResults(WHEREIS_RESULTS * ptr) {
89  if (ptr == NULL) { return; }
90 
91  for (int i=0; i < ptr->nCount; i++) {
92  dsl_free(ptr->sResults[i]);
93  }
94  dsl_free(ptr);
95 }
DSL_API void DSL_CC dsl_free(void *ptr)
Definition: dsl.cpp:345
DSL_API int DSL_CC wildicmp(const char *wild, const char *string)
Definition: GenLib.cpp:469