Debian Codegraph @

module/module.cpp

raw symbols SCIP
1 /*
2 * Descent 3
3 * Copyright (C) 2024 Parallax Software
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18 --- HISTORICAL COMMENTS FOLLOW ---
19
20 * $Logfile: /DescentIII/Main/module/module.cpp $
21 * $Revision: 20 $
22 * $Date: 10/21/99 2:43p $
23 * $Author: Kevin $
24 *
25 * Source for Dynamic Loadable Module functions
26 *
27 * $Log: /DescentIII/Main/module/module.cpp $
28 *
29 * 20 10/21/99 2:43p Kevin
30 * Mac Merge
31 *
32 * 19 8/24/99 4:33p Jeff
33 * fixed bug with finding alternate file names in Linux
34 *
35 * 18 8/22/99 7:12p Jeff
36 * when open a module on a Linux system, if the original open fails, then
37 * check for alternate files with different case.
38 *
39 * 17 8/16/99 11:48a Nate
40 * (JEFF) Use calls to dd_ instead of ddio_ (this library cannot be
41 * dependant on any other libs)
42 *
43 * 16 7/28/99 5:22p Kevin
44 * Mac Merge fixes
45 *
46 * 15 7/28/99 2:17p Kevin
47 * Macintosh Stuff!
48 *
49 * 14 5/13/99 5:07p Ardussi
50 * changes for compiling on the Mac
51 *
52 * 13 4/19/99 4:01a Jeff
53 * fixed splitpath
54 *
55 * 12 4/18/99 3:14p Jeff
56 * fixed splitpath for linux
57 *
58 * 11 4/16/99 1:06a Jeff
59 *
60 * 10 1/19/99 1:08p Jason
61 * added dynamically loadable dlls
62 *
63 * 9 1/14/99 10:36a Jeff
64 * removed ddio_ dependency (requires a #ifdef around functions taken out
65 * of ddio_)
66 *
67 * 8 1/13/99 6:50a Jeff
68 * made linux friendly (#include case-sensitivity)
69 *
70 * 7 1/11/99 1:01p Jeff
71 * code will convert an .so->.dll for win32 and .dll->.so for Linux
72 *
73 * 6 1/11/99 12:53p Jeff
74 * added a function that given a module name it will make sure it has an
75 * extension. Made Osiris friendly with modules with no extension
76 *
77 * 5 1/11/99 12:29p Jeff
78 * tack on an automatic extension to the module name if one isn't given
79 * (system dependent)
80 *
81 * 4 1/10/99 6:47a Jeff
82 * Changes made to get linux version to compile
83 *
84 * 3 7/06/98 10:45a Jeff
85 * Made Linux friendly
86 *
87 * 2 6/05/98 2:15p Jeff
88 * Initial creation
89 *
90 * 1 6/05/98 2:14p Jeff
91 *
92 * $NoKeywords: $
93 */
94 #include "module.h"cxx . . $ `<file>/lib/module.h`/
95 #include "pstypes.h"cxx . . $ `<file>/lib/pstypes.h`/
96 #include "pserror.h"cxx . . $ `<file>/misc/pserror.h`/
97 #include "ddio.h"cxx . . $ `<file>/ddio/ddio.h`/
98
99 #if defined(POSIXcxx . . $ `<built-in>:477:9`!)
100 #include <dlfcn.h>
101
102 static bool mod_FindRealFileNameCaseInsenstivecxx . . $ mod_FindRealFileNameCaseInsenstive(dfb34736e99ce5d).(const char *directory, const char *filename, char *new_filename);
103 #endif
104
105 #include "module.h"cxx . . $ `<file>/lib/module.h`/
106 #include "pstypes.h"cxx . . $ `<file>/lib/pstypes.h`/
107 #include "pserror.h"cxx . . $ `<file>/misc/pserror.h`/
108 #include <stdio.h>
109 #include <stdarg.h>
110 #include <string.h>
111 #if defined(POSIXcxx . . $ `<built-in>:477:9`!)
112 #include "linux_fix.h"cxx . . $ `<file>/linux/linux_fix.h`/
113 #endif
114 #if defined(WIN32) // INSTEAD OF MAKING MODULE HAVE DEPENDENCIES, PUT THE 2 DDIO FUNCTIONS I NEED HERE
115 // Split a pathname into its component parts
116 static void dd_SplitPath(const char *srcPath, char *path, char *filename, char *ext) {
117 char drivename[_MAX_DRIVE], dirname[_MAX_DIR];
118 _splitpath(srcPath, drivename, dirname, filename, ext);
119 if (path)
120 sprintf(path, "%s%s", drivename, dirname);
121 }
122 // Constructs a path in the local file system's syntax
123 // newPath: stores the constructed path
124 // absolutePathHeader: absolute path on which the sub directories will be appended
125 // (specified in local file system syntax)
126 // takes a variable number of subdirectories which will be concatenated on to the path
127 // the last argument in the list of sub dirs *MUST* be NULL to terminate the list
128 static void dd_MakePath(char *newPath, const char *absolutePathHeader, const char *subDir, ...) {
129 const char delimiter = '\\';
130 va_list args;
131 char *currentDir = NULL;
132 int pathLength = 0;
133
134 assert(newPath);
135 assert(absolutePathHeader);
136 assert(subDir);
137
138 if (newPath != absolutePathHeader) {
139 strcpy(newPath, absolutePathHeader);
140 }
141 // Add the first sub directory
142 pathLength = strlen(newPath);
143 if (newPath[pathLength - 1] != delimiter) {
144 newPath[pathLength] = delimiter; // add the delimiter
145 newPath[pathLength + 1] = 0; // terminate the string
146 }
147 strcat(newPath, subDir);
148
149 // Add the additional subdirectories
150 va_start(args, subDir);
151 while ((currentDir = va_arg(args, char *)) != NULL) {
152 pathLength = strlen(newPath);
153 if (newPath[pathLength - 1] != delimiter) {
154 newPath[pathLength] = delimiter; // add the delimiter
155 newPath[pathLength + 1] = 0; // terminate the string
156 }
157 strcat(newPath, currentDir);
158 }
159 va_end(args);
160 }
161 #elif defined(POSIXcxx . . $ `<built-in>:477:9`!)
162 // Split a pathname into its component parts
163 static void dd_SplitPathcxx . . $ dd_SplitPath(b05ba7ab99603c07).(const char *srcPath, char *path, char *filename, char *ext) {
164 int pathStart = -1;
165 int pathEnd = -1;
166 int fileStart = -1;
167 int fileEnd = -1;
168 int extStart = -1;
169 int extEnd = -1;
170 int totalLen = strlen(srcPath);
171 // Check for an extension
172 ///////////////////////////////////////
173 int t = totalLen - 1;
174 while ((srcPath[t] != '.') && (srcPath[t] != '/') && (t >= 0))
175 t--;
176 // see if we are at an extension
177 if ((t >= 0) && (srcPath[t] == '.')) {
178 // we have an extension
179 extStart = t;
180 extEnd = totalLen - 1;
181 if (ext) {
182 strncpy(ext, &(srcPath[extStart]), extEnd - extStart + 1);
183 ext[extEnd - extStart + 1] = '\0';
184 }
185 } else {
186 // no extension
187 if (ext)
188 ext[0] = '\0';
189 }
190
191 // Check for file name
192 ////////////////////////////////////
193 int temp = (extStart != -1) ? (extStart) : (totalLen - 1);
194 while ((srcPath[temp] != '/') && (temp >= 0))
195 temp--;
196 if (temp < 0)
197 temp = 0;
198 if (srcPath[temp] == '/') {
199 // we have a file
200 fileStart = temp + 1;
201 if (extStart != -1)
202 fileEnd = extStart - 1;
203 else
204 fileEnd = totalLen - 1;
205 if (filename) {
206 strncpy(filename, &(srcPath[fileStart]), fileEnd - fileStart + 1);
207 filename[fileEnd - fileStart + 1] = '\0';
208 }
209 pathStart = 0;
210 pathEnd = fileStart - 2;
211 // Copy the rest into the path name
212 if (path) {
213 strncpy(path, &(srcPath[pathStart]), pathEnd - pathStart + 1);
214 path[pathEnd - pathStart + 1] = 0;
215 }
216 } else {
217 // only file, no path
218 fileStart = 0;
219 if (extStart != -1)
220 fileEnd = extStart - 1;
221 else
222 fileEnd = totalLen - 1;
223 if (filename) {
224 strncpy(filename, &(srcPath[fileStart]), fileEnd - fileStart + 1);
225 filename[fileEnd - fileStart + 1] = 0;
226 }
227
228 // Only file no path
229 if (path) {
230 path[0] = 0;
231 }
232 }
233 }
234 // Constructs a path in the local file system's syntax
235 // newPath: stores the constructed path
236 // absolutePathHeader: absolute path on which the sub directories will be appended
237 // (specified in local file system syntax)
238 // takes a variable number of subdirectories which will be concatenated on to the path
239 // the last argument in the list of sub dirs *MUST* be NULL to terminate the list
240 static void dd_MakePathcxx . . $ dd_MakePath(9ad4b0815420a85).(char *newPath, const char *absolutePathHeader, const char *subDir, ...) {
241 const char delimiter = '/';
242 va_list args;
243 char *currentDir = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!;
244 int pathLength = 0;
245
246 ASSERTcxx . . $ `misc/pserror.h:246:9`!(newPath);
247 ASSERTcxx . . $ `misc/pserror.h:246:9`!(absolutePathHeader);
248 ASSERTcxx . . $ `misc/pserror.h:246:9`!(subDir);
249
250 if (newPath != absolutePathHeader) {
251 strcpy(newPath, absolutePathHeader);
252 }
253 // Add the first sub directory
254 pathLength = strlen(newPath);
255 if (newPath[pathLength - 1] != delimiter) {
256 newPath[pathLength] = delimiter; // add the delimiter
257 newPath[pathLength + 1] = 0; // terminate the string
258 }
259 strcat(newPath, subDir);
260
261 // Add the additional subdirectories
262 va_startcxx . . $ `<external>/8105065126723003039/stdarg.h:50:9`!(args, subDir);
263 while ((currentDir = va_argcxx . . $ `<external>/8105065126723003039/stdarg.h:53:9`!(args, char *)) != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!) {
264 pathLength = strlen(newPath);
265 if (newPath[pathLength - 1] != delimiter) {
266 newPath[pathLength] = delimiter; // add the delimiter
267 newPath[pathLength + 1] = 0; // terminate the string
268 }
269 strcat(newPath, currentDir);
270 }
271 va_endcxx . . $ `<external>/8105065126723003039/stdarg.h:52:9`!(args);
272 }
273 #endif
274 int ModLastErrorcxx . . $ ModLastError. = MODERR_NOERRORcxx . . $ `lib/module.h:123:9`!;
275 // Returns the real name of the module. If a given file has an extension, it will
276 // just return that filename. If the given file has no given extension, the
277 // system specific extension is concatted and returned.
278 void mod_GetRealModuleNamecxx . . $ mod_GetRealModuleName(611124a97efc52dd).(const char *modfilename, char *realmodfilename) {
279 char pathname[_MAX_PATHcxx . . $ `linux/linux_fix.h:34:9`!], filename[_MAX_FNAMEcxx . . $ `linux/linux_fix.h:35:9`!], extension[_MAX_EXTcxx . . $ `linux/linux_fix.h:36:9`!];
280 dd_SplitPathcxx . . $ dd_SplitPath(b05ba7ab99603c07).(modfilename, pathname, filename, extension);
281 if (*extension == '\0')
282 #if defined(WIN32)
283 strcat(filename, ".dll");
284 #elif defined(__LINUX__cxx . . $ `<external>/8627488274992530813/SDL_platform.h:61:9`!)
285 strcat(filename, ".so");
286 #elif defined(MACOSX)
287 strcat(filename, ".dylib");
288 #else
289 #error Unsupported platform!
290 #endif
291 else {
292 #if defined(WIN32)
293 if (!stricmp(extension, ".so") || !stricmp(extension, "msl") || !stricmp(extension, "dylib"))
294 strcat(filename, ".dll");
295 else
296 strcat(filename, extension);
297 #elif defined(__LINUX__cxx . . $ `<external>/8627488274992530813/SDL_platform.h:61:9`!)
298 if (!stricmpcxx . . $ `linux/linux_fix.h:43:9`!(extension, ".dll") || !stricmpcxx . . $ `linux/linux_fix.h:43:9`!(extension, "msl") || !stricmpcxx . . $ `linux/linux_fix.h:43:9`!(extension, "dylib"))
299 strcat(filename, ".so");
300 else
301 strcat(filename, extension);
302 #elif defined(MACOSX)
303 if (!stricmp(extension, ".dll") || !stricmp(extension, "msl") || !stricmp(extension, "so"))
304 strcat(filename, ".dylib");
305 else
306 strcat(filename, extension);
307 #else
308 #error Unsupported platform!
309 #endif
310 }
311 if (*pathname != '\0')
312 dd_MakePathcxx . . $ dd_MakePath(9ad4b0815420a85).(realmodfilename, pathname, filename, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!);
313 else
314 strcpy(realmodfilename, filename);
315 }
316 // Loads a dynamic module into memory for use.
317 // Returns true on success, false otherwise
318 // modfilename is the name of the module (without an extension such as DLL, or so)
319 bool mod_LoadModulecxx . . $ mod_LoadModule(2b2b4995973ac6b0).(modulecxx . . $ module# *handle, const char *imodfilename, int flags) {
320 if (!imodfilename) {
321 ModLastErrorcxx . . $ ModLastError. = MODERR_OTHERcxx . . $ `lib/module.h:129:9`!;
322 return false;
323 }
324 if (!handle) {
325 ModLastErrorcxx . . $ ModLastError. = MODERR_INVALIDHANDLEcxx . . $ `lib/module.h:128:9`!;
326 return false;
327 }
328 handle->handlecxx . . $ module#handle. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!;
329 char modfilename[_MAX_PATHcxx . . $ `linux/linux_fix.h:34:9`!];
330 mod_GetRealModuleNamecxx . . $ mod_GetRealModuleName(611124a97efc52dd).(imodfilename, modfilename);
331 #if defined(WIN32)
332 handle->handle = LoadLibrary(modfilename);
333 if (!handle->handle) {
334 // There was an error loading the module
335 DWORD err = GetLastError();
336 switch (err) {
337 case ERROR_DLL_INIT_FAILED:
338 ModLastError = MODERR_MODINITFAIL;
339 break;
340 case ERROR_DLL_NOT_FOUND:
341 ModLastError = MODERR_MODNOTFOUND;
342 break;
343 case ERROR_INVALID_DLL:
344 ModLastError = MODERR_INVALIDMOD;
345 break;
346 default:
347 ModLastError = MODERR_OTHER;
348 break;
349 }
350 return false;
351 }
352 #elif defined(POSIXcxx . . $ `<built-in>:477:9`!)
353 int f = 0;
354 if (flags & MODF_LAZYcxx . . $ `lib/module.h:132:9`!)
355 f |= RTLD_LAZYcxx . . $ `<external>/10632388298615623587/dlfcn.h:24:9`!;
356 if (flags & MODF_NOWcxx . . $ `lib/module.h:133:9`!)
357 f |= RTLD_NOWcxx . . $ `<external>/10632388298615623587/dlfcn.h:25:9`!;
358 if (flags & MODF_GLOBALcxx . . $ `lib/module.h:134:9`!)
359 f |= RTLD_GLOBALcxx . . $ `<external>/10632388298615623587/dlfcn.h:33:9`!;
360 handle->handlecxx . . $ module#handle. = dlopen(modfilename, f);
361 if (!handle->handlecxx . . $ module#handle.) {
362 // ok we couldn't find the given name...try other ways
363 char dir[_MAX_PATHcxx . . $ `linux/linux_fix.h:34:9`!], fname[_MAX_PATHcxx . . $ `linux/linux_fix.h:34:9`!], nname[_MAX_PATHcxx . . $ `linux/linux_fix.h:34:9`!], ext[64];
364 dd_SplitPathcxx . . $ dd_SplitPath(b05ba7ab99603c07).(modfilename, dir, fname, ext);
365 strcat(fname, ext);
366
367 if (!mod_FindRealFileNameCaseInsenstivecxx . . $ mod_FindRealFileNameCaseInsenstive(dfb34736e99ce5d).(dir, fname, nname)) {
368 mprintfcxx . . $ `ddebug/mono.h:82:9`!(0, "Module Load Err: %s\n", dlerror());
369 ModLastErrorcxx . . $ ModLastError. = MODERR_MODNOTFOUNDcxx . . $ `lib/module.h:125:9`!;
370 return false;
371 } else {
372 // ok we have a different filename
373 dd_MakePathcxx . . $ dd_MakePath(9ad4b0815420a85).(modfilename, dir, nname, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!);
374 mprintfcxx . . $ `ddebug/mono.h:82:9`!(0, "MOD: Attempting to open %s instead of %s\n", modfilename, fname);
375 handle->handlecxx . . $ module#handle. = dlopen(modfilename, f);
376 if (!handle->handlecxx . . $ module#handle.) {
377 mprintfcxx . . $ `ddebug/mono.h:82:9`!(0, "Module Load Err: %s\n", dlerror());
378 ModLastErrorcxx . . $ ModLastError. = MODERR_MODNOTFOUNDcxx . . $ `lib/module.h:125:9`!;
379 return false;
380 }
381 }
382 }
383 #endif
384 // Success
385 return true;
386 }
387 // Frees a previously loaded module from memory, it can no longer be used
388 // Returns true on success, false otherwise
389 bool mod_FreeModulecxx . . $ mod_FreeModule(f63d3fb9c9f41dd6).(modulecxx . . $ module# *handle) {
390 bool ret = true;
391
392 if (!handle) {
393 ModLastErrorcxx . . $ ModLastError. = MODERR_INVALIDHANDLEcxx . . $ `lib/module.h:128:9`!;
394 return false;
395 }
396 if (!handle->handlecxx . . $ module#handle.) {
397 ModLastErrorcxx . . $ ModLastError. = MODERR_OTHERcxx . . $ `lib/module.h:129:9`!;
398 return false;
399 }
400 #if defined(WIN32)
401 ret = (FreeLibrary(handle->handle) != 0);
402 #elif defined(POSIXcxx . . $ `<built-in>:477:9`!)
403 dlclose(handle->handlecxx . . $ module#handle.); // dlclose() returns an int, but no docs say what values!!!
404 #endif
405 handle->handlecxx . . $ module#handle. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!;
406 return ret;
407 }
408 // Returns a pointer to a function within a loaded module. If it returns NULL there was an error. Check
409 // mod_GetLastError to see if there was an error symstr is the name of the function you want to get the symbol for (Do
410 // NOT give any pre/suffix to this name) parmbytes is the size (in bytes) of the parameter list the function should have
411 MODPROCADDRESScxx . . $ `lib/module.h:111:9`! mod_GetSymbolcxx . . $ mod_GetSymbol(434bf6e9929b4f62).(modulecxx . . $ module# *handle, const char *symstr, uint8_tcxx . . $ uint8_t# parmbytes) {
412 char buffer[256];
413 MODPROCADDRESScxx . . $ `lib/module.h:111:9`! sym;
414 if (!handle) {
415 ModLastErrorcxx . . $ ModLastError. = MODERR_INVALIDHANDLEcxx . . $ `lib/module.h:128:9`!;
416 return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!;
417 }
418 if (!symstr) {
419 ModLastErrorcxx . . $ ModLastError. = MODERR_OTHERcxx . . $ `lib/module.h:129:9`!;
420 return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!;
421 }
422 if (!handle->handlecxx . . $ module#handle.) {
423 ModLastErrorcxx . . $ ModLastError. = MODERR_NOMODcxx . . $ `lib/module.h:127:9`!;
424 return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!;
425 }
426 #if defined(WIN32)
427 // We need to first form the correct symbol name (for Windows)
428 if (parmbytes == 255)
429 sprintf(buffer, "%s", symstr);
430 else
431 sprintf(buffer, "_%s@%d", symstr, parmbytes);
432 DWORD err;
433 sym = GetProcAddress(handle->handle, buffer);
434 if (!sym) {
435 err = GetLastError();
436
437 // Try again using no byte ordinals
438 if (parmbytes != 255) {
439 sprintf(buffer, "%s", symstr);
440 sym = GetProcAddress(handle->handle, buffer);
441 if (!sym)
442 err = GetLastError();
443 }
444 }
445 if (!sym) {
446 // there was an error
447
448 switch (err) {
449 case ERROR_DLL_INIT_FAILED:
450 ModLastError = MODERR_MODINITFAIL;
451 break;
452 case ERROR_DLL_NOT_FOUND:
453 ModLastError = MODERR_MODNOTFOUND;
454 break;
455 case ERROR_INVALID_DLL:
456 ModLastError = MODERR_INVALIDMOD;
457 break;
458 default:
459 ModLastError = MODERR_OTHER;
460 break;
461 }
462 return NULL;
463 }
464 #elif defined(POSIXcxx . . $ `<built-in>:477:9`!)
465 sym = dlsym(handle->handlecxx . . $ module#handle., symstr);
466 if (!sym) {
467 ModLastErrorcxx . . $ ModLastError. = MODERR_OTHERcxx . . $ `lib/module.h:129:9`!;
468 return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!;
469 }
470 #endif
471 return sym;
472 }
473 // Returns an error code to what the last error was. When this function is called the last error is cleared, so by
474 // calling this function it not only returns the last error, but it removes it, so if you were to call this function
475 // again, it would return no error
476 int mod_GetLastErrorcxx . . $ mod_GetLastError(b126dc7c1de90089).(void) {
477 // Clear out the errors
478 #if defined(WIN32)
479 SetLastError(0);
480 #elif defined(POSIXcxx . . $ `<built-in>:477:9`!)
481 dlerror();
482 #endif
483 int ret = ModLastErrorcxx . . $ ModLastError.;
484 ModLastErrorcxx . . $ ModLastError. = MODERR_NOERRORcxx . . $ `lib/module.h:123:9`!;
485 return ret;
486 }
487
488 #if defined(POSIXcxx . . $ `<built-in>:477:9`!)
489 #include <assert.h>
490 #include <stdarg.h>
491 #include <sys/stat.h>
492 #include <sys/types.h>
493 #include <fcntl.h>
494 #include <unistd.h>
495 #include <utime.h>
496 #include <glob.h>
497 #include <string.h>
498 #include <errno.h>
499 #include <ctype.h>
500
501 void dd_GetWorkingDircxx . . $ dd_GetWorkingDir(a07d8cccb7673f5a).(char *path, int len);
502 bool dd_SetWorkingDircxx . . $ dd_SetWorkingDir(c5f7da30cbc0f4e9).(const char *path);
503 bool dd_FindFileStartcxx . . $ dd_FindFileStart(84e74a5521f49206).(const char *wildcard, char *namebuf);
504 bool dd_FindNextFilecxx . . $ dd_FindNextFile(c6b7e35ff6de5671).(char *namebuf);
505 void dd_FindFileClosecxx . . $ dd_FindFileClose(49f6e7a06ebc5aa8).();
506
507 // retrieve the current working folder where file operation will occur.
508 void dd_GetWorkingDircxx . . $ dd_GetWorkingDir(a07d8cccb7673f5a).(char *path, int len) { getcwd(path, len); }
509
510 bool dd_SetWorkingDircxx . . $ dd_SetWorkingDir(c5f7da30cbc0f4e9).(const char *path) { return (chdir(path)) ? false : true; }
511
512 // These functions allow one to find a file
513 static int globerrfncxx . . $ globerrfn(cb7559238bb26814).(const char *path, int err) {
514 mprintfcxx . . $ `ddebug/mono.h:82:9`!(0, "Error accessing %s: %s .... \n", path, strerror(err));
515 return 0;
516 }
517
518 class CModFindFilescxx . . $ CModFindFiles# {
519 public:
520 CModFindFilescxx . . $ CModFindFiles#CModFindFiles(49f6e7a06ebc5aa8).() { globindexcxx . . $ CModFindFiles#globindex. = -1; }
521
522 bool Startcxx . . $ CModFindFiles#Start(84e74a5521f49206).(const char *wildcard, char *namebuf);
523 bool Nextcxx . . $ CModFindFiles#Next(c6b7e35ff6de5671).(char *namebuf);
524 void Closecxx . . $ CModFindFiles#Close(49f6e7a06ebc5aa8).(void);
525
526 private:
527 int globindexcxx . . $ CModFindFiles#globindex.;
528 glob_t ffrescxx . . $ CModFindFiles#ffres.;
529 };
530
531 bool CModFindFilescxx . . $ CModFindFiles#::Startcxx . . $ CModFindFiles#Start(84e74a5521f49206).(const char *wildcard, char *namebuf) {
532 ASSERTcxx . . $ `misc/pserror.h:246:9`!(wildcard);
533 ASSERTcxx . . $ `misc/pserror.h:246:9`!(namebuf);
534
535 if (globindexcxx . . $ CModFindFiles#globindex. != -1)
536 Closecxx . . $ CModFindFiles#Close(49f6e7a06ebc5aa8).();
537
538 int rc, flags;
539 flags = GLOB_MARKcxx . . $ `<external>/16386592658489186903/glob.h:40:9`!;
540 rc = glob(wildcard, flags, globerrfncxx . . $ globerrfn(cb7559238bb26814)., &ffrescxx . . $ CModFindFiles#ffres.);
541 if (rc == GLOB_NOSPACEcxx . . $ `<external>/16386592658489186903/glob.h:68:9`!) {
542 mprintfcxx . . $ `ddebug/mono.h:82:9`!(0, "Out of space during glob\n");
543 globindexcxx . . $ CModFindFiles#globindex. = -1;
544 return false;
545 }
546 if (!ffrescxx . . $ CModFindFiles#ffres..gl_pathc) {
547 globindexcxx . . $ CModFindFiles#globindex. = -1;
548 return false;
549 }
550
551 globindexcxx . . $ CModFindFiles#globindex. = 0;
552 char ext[256];
553 dd_SplitPathcxx . . $ dd_SplitPath(b05ba7ab99603c07).(ffrescxx . . $ CModFindFiles#ffres..gl_pathv[0], NULLcxx . . $ `<external>/15298297234472979974/stddef.h:416:9`!, namebuf, ext);
554 strcat(namebuf, ext);
555 return true;
556 }
557
558 bool CModFindFilescxx . . $ CModFindFiles#::Nextcxx . . $ CModFindFiles#Next(c6b7e35ff6de5671).(char *namebuf) {
559 ASSERTcxx . . $ `misc/pserror.h:246:9`!(namebuf);
560 if (globindexcxx . . $ CModFindFiles#globindex. == -1)
561 return false;
562 globindexcxx . . $ CModFindFiles#globindex.++;
563 if (globindexcxx . . $ CModFindFiles#globindex. >= ffrescxx . . $ CModFindFiles#ffres..gl_pathc)
564 return false;
565
566 char ext[256];
567 dd_SplitPathcxx . . $ dd_SplitPath(b05ba7ab99603c07).(ffrescxx . . $ CModFindFiles#ffres..gl_pathv[globindexcxx . . $ CModFindFiles#globindex.], NULLcxx . . $ `<external>/15298297234472979974/stddef.h:416:9`!, namebuf, ext);
568 strcat(namebuf, ext);
569 return true;
570 }
571
572 void CModFindFilescxx . . $ CModFindFiles#::Closecxx . . $ CModFindFiles#Close(49f6e7a06ebc5aa8).(void) {
573 if (globindexcxx . . $ CModFindFiles#globindex. == -1)
574 return;
575 globindexcxx . . $ CModFindFiles#globindex. = -1;
576 globfree(&ffrescxx . . $ CModFindFiles#ffres.);
577 }
578
579 bool mod_FindRealFileNameCaseInsenstivecxx . . $ mod_FindRealFileNameCaseInsenstive(dfb34736e99ce5d).(const char *directory, const char *fname, char *new_filename) {
580 bool use_dir = false;
581 char dir_to_use[_MAX_PATHcxx . . $ `linux/linux_fix.h:34:9`!];
582 char file_to_use[_MAX_PATHcxx . . $ `linux/linux_fix.h:34:9`!];
583
584 char *real_dir, *real_file;
585
586 if (directory) {
587 // there is a directory for this path
588 use_dir = true;
589 real_dir = (char *)directory;
590 real_file = (char *)fname;
591 } else {
592 // there may be a directory in the path (*sigh*)
593 char t_ext[256];
594 char t_dir[_MAX_PATHcxx . . $ `linux/linux_fix.h:34:9`!];
595 char t_filename[_MAX_PATHcxx . . $ `linux/linux_fix.h:34:9`!];
596
597 dd_SplitPathcxx . . $ dd_SplitPath(b05ba7ab99603c07).(fname, t_dir, t_filename, t_ext);
598 if (strlen(t_dir) > 0) {
599 use_dir = true;
600 strcpy(dir_to_use, t_dir);
601 real_dir = (char *)dir_to_use;
602 strcpy(file_to_use, t_filename);
603 strcat(file_to_use, t_ext);
604 real_file = (char *)file_to_use;
605
606 mprintfcxx . . $ `ddebug/mono.h:82:9`!(1, "MOD: Found directory \"%s\" in filename, new filename is \"%s\"\n", real_dir, real_file);
607 } else {
608 use_dir = false;
609 real_dir = NULLcxx . . $ `<external>/15298297234472979974/stddef.h:416:9`!;
610 real_file = (char *)fname;
611 }
612 }
613
614 // build up a list of filenames in the current directory that begin with the lowercase and
615 // upper case first letter of the filename
616
617 // do the case of the first letter to start
618 int case_val;
619 char wildcard_pattern[_MAX_PATHcxx . . $ `linux/linux_fix.h:34:9`!];
620 int iterations = 1;
621 bool found_match = false;
622
623 if ((real_file[0] >= 'a' && real_file[0] <= 'z') || (real_file[0] >= 'A' && real_file[0] <= 'Z')) {
624 // alpha first letter...we need to do 2 iterations
625 iterations = 2;
626 }
627
628 for (case_val = 0; case_val < iterations; case_val++) {
629 if (case_val) {
630 // do the opposite case of the first letter
631 char first_letter;
632 first_letter = real_file[0];
633 if (first_letter >= 'a' && first_letter <= 'z') {
634 // we need to uppercase the letter
635 first_letter = toupper(first_letter);
636 } else {
637 // we need to lowercase the letter
638 first_letter = tolower(first_letter);
639 }
640
641 // create a wildcard patter full of ? replacing letters (except the first one)
642 char *wptr = wildcard_pattern;
643 char *fptr = &real_file[1];
644 *wptr = first_letter;
645 wptr++;
646 while (*fptr) {
647 if (isalpha(*fptr)) {
648 *wptr = '?';
649 } else {
650 *wptr = *fptr;
651 }
652
653 fptr++;
654 wptr++;
655 }
656 *wptr = '\0';
657 } else {
658 // use the case of the first letter
659 // create a wildcard patter full of ? replacing letters (except the first one)
660 char *wptr = wildcard_pattern;
661 char *fptr = &real_file[1];
662 *wptr = real_file[0];
663 wptr++;
664 while (*fptr) {
665 if (isalpha(*fptr)) {
666 *wptr = '?';
667 } else {
668 *wptr = *fptr;
669 }
670
671 fptr++;
672 wptr++;
673 }
674 *wptr = '\0';
675 }
676
677 // now tack on a directory if we are to use a directory
678 char *wpattern;
679 char fullpath[_MAX_PATHcxx . . $ `linux/linux_fix.h:34:9`!];
680 if (use_dir) {
681 dd_MakePathcxx . . $ dd_MakePath(9ad4b0815420a85).(fullpath, real_dir, wildcard_pattern, NULLcxx . . $ `<external>/15298297234472979974/stddef.h:416:9`!);
682 wpattern = fullpath;
683 } else {
684 wpattern = wildcard_pattern;
685 }
686
687 // ok, we have our wildcard pattern, get all the files that match it
688 // and search them looking for a match (case insensitive)
689 char namebuffer[_MAX_PATHcxx . . $ `linux/linux_fix.h:34:9`!];
690 bool gotfile;
691 CModFindFilescxx . . $ CModFindFiles# ffcxx . . $ CModFindFiles#CModFindFiles(49f6e7a06ebc5aa8).;
692 for (gotfile = ff.Startcxx . . $ CModFindFiles#Start(84e74a5521f49206).(wpattern, namebuffer); gotfile; gotfile = ff.Nextcxx . . $ CModFindFiles#Next(c6b7e35ff6de5671).(namebuffer)) {
693 if (!stricmpcxx . . $ `linux/linux_fix.h:43:9`!(namebuffer, real_file)) {
694 // we found a match!
695 found_match = true;
696 break;
697 }
698 }
699 ff.Closecxx . . $ CModFindFiles#Close(49f6e7a06ebc5aa8).();
700
701 if (found_match) {
702 strcpy(new_filename, namebuffer);
703 mprintfcxx . . $ `ddebug/mono.h:82:9`!(1, "MOD: Using \"%s\" instead of \"%s\"\n", new_filename, real_file);
704 break;
705 }
706 }
707
708 return found_match;
709 }
710 #endif