Debian Codegraph @

ddio/lnxfile.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/ddio_lnx/lnxfile.cpp $
21 * $Revision: 1.5 $
22 * $Date: 2004/12/05 09:02:45 $
23 * $Author: ryan $
24 *
25 * Linux file routines
26 *
27 * $Log: lnxfile.cpp,v $
28 * Revision 1.5 2004/12/05 09:02:45 ryan
29 * Valgrind fix.
30 *
31 * Revision 1.4 2004/02/25 00:04:06 ryan
32 * Removed loki_utils dependency and ported to MacOS X (runs, but incomplete).
33 *
34 * Revision 1.3 2001/01/11 11:41:57 icculus
35 * Allow .dotfiles to be shown in file dialogs.
36 *
37 * Revision 1.2 2000/06/24 01:15:15 icculus
38 * patched to compile.
39 *
40 * Revision 1.1.1.1 2000/04/18 00:00:33 icculus
41 * initial checkin
42 *
43 *
44 * 13 10/02/99 2:59p Jeff
45 * moved ddio_GetCDDrive to lnxcdrom.cpp
46 *
47 * 12 9/07/99 4:36p Jeff
48 * fixed ddio_FindFileFirst/Next when a directory is returned
49 *
50 * 11 8/22/99 7:10p Jeff
51 * fix directory lock function if the pid check returns an error, now
52 * assume the pid is invalid
53 *
54 * 10 7/14/99 9:06p Jeff
55 * added comment header
56 *
57 * $NoKeywords: $
58 */
59
60 #include <cerrno>
61 #include <cstdarg>
62 #include <cstdio>
63 #include <cstdlib>
64 #include <cstring>
65 #include <csignal>
66 #include <dirent.h>
67 #include <fcntl.h>
68 #include <glob.h>
69 #include <sys/stat.h>
70 #include <unistd.h>
71 #include <utime.h>
72
73 #if MACOSX
74 #include <ctype.h>
75 #endif
76
77 #include "ddio.h"cxx . . $ `<file>/ddio/ddio.h`/
78 #include "pserror.h"cxx . . $ `<file>/misc/pserror.h`/
79 #include "mem.h"cxx . . $ `<file>/mem/mem.h`/
80
81 #define _MAX_DIRcxx . . $ `ddio/lnxfile.cpp:81:9`! 256
82
83 // ---------------------------------------------------------------------------
84 // File operations
85
86 // retrieve the current working folder where file operation will occur.
87 void ddio_GetWorkingDircxx . . $ ddio_GetWorkingDir(a07d8cccb7673f5a).(char *path, int len) { getcwd(path, len); }
88
89 bool ddio_SetWorkingDircxx . . $ ddio_SetWorkingDir(c5f7da30cbc0f4e9).(const char *path) { return (chdir(path)) ? false : true; }
90
91 bool ddio_FileDiffcxx . . $ ddio_FileDiff(1b1fb50d0437e4a5).(const std::filesystemcxx . . $ std/filesystem/::pathcxx . . $ std/filesystem/__cxx11/path# &path1, const std::filesystemcxx . . $ std/filesystem/::pathcxx . . $ std/filesystem/__cxx11/path# &path2) {
92 struct stat abuf{}, bbuf{};
93
94 if (stat(path1.u8stringcxx . . $ std/filesystem/__cxx11/path#u8string(219584694fe2b248).().c_strcxx . . $ std/__cxx11/basic_string#c_str(8e614c7de0b1573e).(), &abuf))
95 Int3cxx . . $ `misc/pserror.h:247:9`!(); // error getting stat info
96
97 if (stat(path2.u8stringcxx . . $ std/filesystem/__cxx11/path#u8string(219584694fe2b248).().c_strcxx . . $ std/__cxx11/basic_string#c_str(8e614c7de0b1573e).(), &bbuf))
98 Int3cxx . . $ `misc/pserror.h:247:9`!(); // error getting stat info
99
100 if ((abuf.st_size != bbuf.st_size) || (abuf.st_mtimecxx . . $ `<external>/16045971866008360577/struct_stat.h:78:11`! != bbuf.st_mtimecxx . . $ `<external>/16045971866008360577/struct_stat.h:78:11`!))
101 return true;
102 return false;
103 }
104
105 // get a file's length
106 int ddio_GetFileLengthcxx . . $ ddio_GetFileLength(89bc1efcdac0ca22).(FILE *filePtr) {
107 int size = -1;
108 if (filePtr) {
109 int filedes = fileno(filePtr);
110 struct stat info;
111
112 fstat(filedes, &info);
113 size = info.st_size;
114 } else {
115 mprintfcxx . . $ `ddebug/mono.h:82:9`!(0, "Tried getting length of NULL fileptr!\n");
116 Int3cxx . . $ `misc/pserror.h:247:9`!();
117 }
118 return size;
119 }
120
121 // Split a pathname into its component parts
122 void ddio_SplitPathcxx . . $ ddio_SplitPath(b05ba7ab99603c07).(const char *srcPath, char *path, char *filename, char *ext) {
123 int pathStart = -1;
124 int pathEnd = -1;
125 int fileStart = -1;
126 int fileEnd = -1;
127 int extStart = -1;
128 int extEnd = -1;
129
130 int totalLen = strlen(srcPath);
131
132 // Check for an extension
133 ///////////////////////////////////////
134 int t = totalLen - 1;
135 while ((srcPath[t] != '.') && (srcPath[t] != '/') && (t >= 0))
136 t--;
137 // see if we are at an extension
138 if ((t >= 0) && (srcPath[t] == '.')) {
139 // we have an extension
140 extStart = t;
141 extEnd = totalLen - 1;
142 if (ext) {
143 strncpy(ext, &(srcPath[extStart]), extEnd - extStart + 1);
144 ext[extEnd - extStart + 1] = '\0';
145 }
146 } else {
147 // no extension
148 if (ext)
149 ext[0] = '\0';
150 }
151
152 // Check for file name
153 ////////////////////////////////////
154 int temp = (extStart != -1) ? (extStart) : (totalLen - 1);
155 while ((temp >= 0) && (srcPath[temp] != '/'))
156 temp--;
157 if (temp < 0)
158 temp = 0;
159 if (srcPath[temp] == '/') {
160 // we have a file
161 fileStart = temp + 1;
162 if (extStart != -1)
163 fileEnd = extStart - 1;
164 else
165 fileEnd = totalLen - 1;
166 if (filename) {
167 strncpy(filename, &(srcPath[fileStart]), fileEnd - fileStart + 1);
168 filename[fileEnd - fileStart + 1] = '\0';
169 }
170 pathStart = 0;
171 pathEnd = fileStart - 2;
172 // Copy the rest into the path name
173 if (path) {
174 strncpy(path, &(srcPath[pathStart]), pathEnd - pathStart + 1);
175 path[pathEnd - pathStart + 1] = 0;
176 }
177 } else {
178 // only file, no path
179 fileStart = 0;
180 if (extStart != -1)
181 fileEnd = extStart - 1;
182 else
183 fileEnd = totalLen - 1;
184
185 if (filename) {
186 strncpy(filename, &(srcPath[fileStart]), fileEnd - fileStart + 1);
187 filename[fileEnd - fileStart + 1] = 0;
188 }
189
190 // Only file no path
191 if (path) {
192 path[0] = 0;
193 }
194 }
195 }
196
197 void ddio_CopyFileTimecxx . . $ ddio_CopyFileTime(df5f3ffd01de66e0).(const std::filesystemcxx . . $ std/filesystem/::pathcxx . . $ std/filesystem/__cxx11/path# &dest, const std::filesystemcxx . . $ std/filesystem/::pathcxx . . $ std/filesystem/__cxx11/path# &src) {
198 struct stat abuf{};
199
200 if (stat(src.u8stringcxx . . $ std/filesystem/__cxx11/path#u8string(219584694fe2b248).().c_strcxx . . $ std/__cxx11/basic_string#c_str(8e614c7de0b1573e).(), &abuf))
201 Int3cxx . . $ `misc/pserror.h:247:9`!();
202
203 struct utimbuf bbuf{};
204 bbuf.actime = abuf.st_atimecxx . . $ `<external>/16045971866008360577/struct_stat.h:77:11`!;
205 bbuf.modtime = abuf.st_mtimecxx . . $ `<external>/16045971866008360577/struct_stat.h:78:11`!;
206
207 if (utime(dest.u8stringcxx . . $ std/filesystem/__cxx11/path#u8string(219584694fe2b248).().c_strcxx . . $ std/__cxx11/basic_string#c_str(8e614c7de0b1573e).(), &bbuf))
208 Int3cxx . . $ `misc/pserror.h:247:9`!();
209 }
210
211 // deletes a file. Returns 1 if successful, 0 on failure
212 int ddio_DeleteFilecxx . . $ ddio_DeleteFile(5c4306d8553b3696).(const char *name) { return (!unlink(name)); }
213
214 // rcg06192000 extern "C" is my add, so nettest would link.
215 // extern "C"
216 //{
217 // Constructs a path in the local file system's syntax
218 // newPath: stores the constructed path
219 // absolutePathHeader: absolute path on which the sub directories will be appended
220 // (specified in local file system syntax)
221 // takes a variable number of subdirectories which will be concatenated on to the path
222 // the last argument in the list of sub dirs *MUST* be NULL to terminate the list
223 void ddio_MakePathcxx . . $ ddio_MakePath(9ad4b0815420a85).(char *newPath, const char *absolutePathHeader, const char *subDir, ...) {
224 const char delimiter = '/';
225 va_listcxx . . $ va_list# args;
226 char *currentDir = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!;
227 int pathLength = 0;
228
229 ASSERTcxx . . $ `misc/pserror.h:246:9`!(newPath);
230 ASSERTcxx . . $ `misc/pserror.h:246:9`!(absolutePathHeader);
231 ASSERTcxx . . $ `misc/pserror.h:246:9`!(subDir);
232
233 if (newPath != absolutePathHeader) {
234 strcpy(newPath, absolutePathHeader);
235 }
236
237 // Add the first sub directory
238 pathLength = strlen(newPath);
239 if (pathLength > 0 && newPath[pathLength - 1] != delimiter) {
240 newPath[pathLength] = delimiter; // add the delimiter
241 newPath[pathLength + 1] = 0; // terminate the string
242 }
243 strcat(newPath, subDir);
244
245 // Add the additional subdirectories
246 va_startcxx . . $ `<external>/8105065126723003039/stdarg.h:50:9`!(args, subDir);
247 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`!) {
248 pathLength = strlen(newPath);
249 if (newPath[pathLength - 1] != delimiter) {
250 newPath[pathLength] = delimiter; // add the delimiter
251 newPath[pathLength + 1] = 0; // terminate the string
252 }
253 strcat(newPath, currentDir);
254 }
255 va_endcxx . . $ `<external>/8105065126723003039/stdarg.h:52:9`!(args);
256 }
257
258 //}
259
260 // These functions allow one to find a file
261 // You use FindFileStart by giving it a wildcard (like *.*, *.txt, u??.*, whatever). It returns
262 // a filename in namebuf.
263 // Use FindNextFile to get the next file found with the wildcard given in FindFileStart.
264 // Use FindFileClose to end your search.
265 glob_t ffrescxx . . $ ffres. = {0, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!, 0};
266 int globindexcxx . . $ globindex. = -1;
267 static int globerrfncxx . . $ globerrfn(cb7559238bb26814).(const char *path, int err) {
268 mprintfcxx . . $ `ddebug/mono.h:82:9`!(0, "Error accessing %s: %s .... \n", path, strerror(err));
269 return 0;
270 }
271
272 #if MACOSX
273 int noglob_findnext(struct find_t *f);
274 struct find_t {
275 DIR *dir;
276 char pattern[_MAX_PATH];
277 char name[_MAX_PATH];
278 };
279
280 static find_t finder;
281
282 int noglob_findfirst(char *filename, int x, struct find_t *f) {
283 char *ptr;
284
285 if (strlen(filename) >= sizeof(f->pattern))
286 return (1);
287
288 strcpy(f->pattern, filename);
289 // FixFilePath(f->pattern);
290 ptr = strrchr(f->pattern, '/');
291
292 if (ptr == NULL) {
293 ptr = filename;
294 f->dir = opendir(".");
295 } else {
296 *ptr = '\0';
297 f->dir = opendir(f->pattern);
298 memmove(f->pattern, ptr + 1, strlen(ptr + 1) + 1);
299 }
300
301 return (noglob_findnext(f));
302 }
303
304 static int check_pattern_nocase(const char *x, const char *y) {
305 if ((x == NULL) || (y == NULL))
306 return (0); /* not a match. */
307
308 while ((*x) && (*y)) {
309 if (*x == '*') {
310 x++;
311 while (*y != '\0') {
312 if (toupper((int)*x) == toupper((int)*y))
313 break;
314 y++;
315 }
316 }
317
318 else if (*x == '?') {
319 if (*y == '\0')
320 return (0); /* anything but EOS is okay. */
321 }
322
323 else {
324 if (toupper((int)*x) != toupper((int)*y))
325 return (0); /* not a match. */
326 }
327
328 x++;
329 y++;
330 }
331
332 return (*x == *y); /* it's a match (both should be EOS). */
333 }
334
335 int noglob_findnext(struct find_t *f) {
336 struct dirent *dent;
337
338 if (f->dir == NULL)
339 return (1); /* no such dir or we're just done searching. */
340
341 while ((dent = readdir(f->dir)) != NULL) {
342 if (check_pattern_nocase(f->pattern, dent->d_name)) {
343 if (strlen(dent->d_name) < sizeof(f->name)) {
344 strcpy(f->name, dent->d_name);
345 return (0); /* match. */
346 }
347 }
348 }
349
350 // closedir(f->dir);
351 // f->dir = NULL;
352 return (1); /* no match in whole directory. */
353 }
354 #endif
355
356 bool ddio_FindFileStartcxx . . $ ddio_FindFileStart(84e74a5521f49206).(const char *wildcard, char *namebuf) {
357 ASSERTcxx . . $ `misc/pserror.h:246:9`!(wildcard);
358 ASSERTcxx . . $ `misc/pserror.h:246:9`!(namebuf);
359 if (globindexcxx . . $ globindex. != -1)
360 ddio_FindFileClosecxx . . $ ddio_FindFileClose(49f6e7a06ebc5aa8).();
361
362 #if MACOSX
363 if (noglob_findfirst((char *)wildcard, 0, &finder) == 0) {
364 strcpy(namebuf, finder.name);
365 return true;
366 }
367 return false;
368 #else
369 int rc, flags;
370 flags = GLOB_MARKcxx . . $ `<external>/16386592658489186903/glob.h:40:9`! | GLOB_TILDEcxx . . $ `<external>/16386592658489186903/glob.h:53:10`!;
371 rc = glob(wildcard, flags, globerrfncxx . . $ globerrfn(cb7559238bb26814)., &ffrescxx . . $ ffres.);
372 if (rc == GLOB_NOSPACEcxx . . $ `<external>/16386592658489186903/glob.h:68:9`!) {
373 mprintfcxx . . $ `ddebug/mono.h:82:9`!(0, "Out of space during glob\n");
374 globindexcxx . . $ globindex. = -1;
375 return false;
376 }
377 if (!ffrescxx . . $ ffres..gl_pathc) {
378 globindexcxx . . $ globindex. = -1;
379 return false;
380 }
381
382 globindexcxx . . $ globindex. = 0;
383
384 // if the last character is a / then we are to return that last section (it is a directory and
385 // ddio_splitpath can't handle that).
386 int gllen = strlen(ffrescxx . . $ ffres..gl_pathv[0]);
387 if (ffrescxx . . $ ffres..gl_pathv[0][gllen - 1] == '/') {
388 // directory to return!
389 char *ptr = &ffrescxx . . $ ffres..gl_pathv[0][gllen - 2];
390 char *start = &ffrescxx . . $ ffres..gl_pathv[0][0];
391 char *end = ptr;
392 while (ptr >= start) {
393 if (*ptr == '/') {
394 // stop! it's the end of the directory
395 break;
396 }
397 ptr--;
398 }
399 if (ptr < start)
400 ptr = start;
401 if (*ptr == '/')
402 ptr++;
403
404 start = namebuf;
405 while (ptr <= end) {
406 *start = *ptr;
407 start++;
408 ptr++;
409 }
410 *start = '\0';
411 } else {
412 // filename to return
413 char ext[256];
414 ddio_SplitPathcxx . . $ ddio_SplitPath(b05ba7ab99603c07).(ffrescxx . . $ ffres..gl_pathv[0], NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!, namebuf, ext);
415 strcat(namebuf, ext);
416 }
417 return true;
418 #endif
419 }
420
421 bool ddio_FindNextFilecxx . . $ ddio_FindNextFile(c6b7e35ff6de5671).(char *namebuf) {
422 ASSERTcxx . . $ `misc/pserror.h:246:9`!(namebuf);
423 #if MACOSX
424 if (noglob_findnext(&finder) == 0) {
425 strcpy(namebuf, finder.name);
426 return true;
427 }
428 return false;
429 #else
430 if (globindexcxx . . $ globindex. == -1)
431 return false;
432 globindexcxx . . $ globindex.++;
433 if (globindexcxx . . $ globindex. >= ffrescxx . . $ ffres..gl_pathc)
434 return false;
435
436 // if the last character is a / then we are to return that last section (it is a directory and
437 // ddio_splitpath can't handle that).
438 int gllen = strlen(ffrescxx . . $ ffres..gl_pathv[globindexcxx . . $ globindex.]);
439 if (ffrescxx . . $ ffres..gl_pathv[globindexcxx . . $ globindex.][gllen - 1] == '/') {
440 // directory to return!
441 char *ptr = &ffrescxx . . $ ffres..gl_pathv[globindexcxx . . $ globindex.][gllen - 2];
442 char *start = &ffrescxx . . $ ffres..gl_pathv[globindexcxx . . $ globindex.][0];
443 char *end = ptr;
444 while (ptr >= start) {
445 if (*ptr == '/') {
446 // stop! it's the end of the directory
447 break;
448 }
449 ptr--;
450 }
451 if (ptr < start)
452 ptr = start;
453 if (*ptr == '/')
454 ptr++;
455
456 start = namebuf;
457 while (ptr <= end) {
458 *start = *ptr;
459 start++;
460 ptr++;
461 }
462 *start = '\0';
463 } else {
464 // filename to return
465 char ext[256];
466 ddio_SplitPathcxx . . $ ddio_SplitPath(b05ba7ab99603c07).(ffrescxx . . $ ffres..gl_pathv[globindexcxx . . $ globindex.], NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!, namebuf, ext);
467 strcat(namebuf, ext);
468 }
469
470 return true;
471 #endif
472 }
473
474 void ddio_FindFileClosecxx . . $ ddio_FindFileClose(49f6e7a06ebc5aa8).() {
475 #if MACOSX
476 if (finder.dir != NULL) {
477 closedir(finder.dir);
478 finder.dir = NULL;
479 }
480 #else
481 if (globindexcxx . . $ globindex. == -1)
482 return;
483 globindexcxx . . $ globindex. = -1;
484 globfree(&ffrescxx . . $ ffres.);
485 #endif
486 }
487
488 // pass in a pathname (could be from ddio_SplitPath), root_path will have the drive name.
489 void ddio_GetRootFromPathcxx . . $ ddio_GetRootFromPath(611124a97efc52dd).(const char *srcPath, char *root_path) {
490 assertcxx . . $ `<external>/4398592474888995393/assert.h:102:11`!(root_path);
491 assertcxx . . $ `<external>/4398592474888995393/assert.h:102:11`!(srcPath);
492
493 strcpy(root_path, "/");
494 }
495
496 // retrieve root names, free up roots array (allocated with malloc) after use
497 int ddio_GetFileSysRootscxx . . $ ddio_GetFileSysRoots(9654734fd31b2328).(char **roots, int max_roots) {
498 if (max_roots < 1)
499 return 0;
500
501 roots[0] = mem_strdupcxx . . $ `mem/mem.h:81:9`!("/");
502 return 1;
503 }
504
505 // given a path, it cleans it up (if the path is /usr/lib/../src it would make it /usr/src)
506 // srcPath is the original path
507 // dest is the finished cleaned path.
508 // dest should be at least _MAX_PATH in size
509 void ddio_CleanPathcxx . . $ ddio_CleanPath(72c91e5e80d20bae).(char *dest, const char *srcPath) {
510 // NOTE: we may want to use getcwd() here if we don't want symbolic links
511 // but I think we do
512 ////////////////////////////////////////////////////////////////////////
513
514 strcpy(dest, srcPath);
515
516 // break the path into directories
517 char **directories;
518 int dirs;
519 int path_length;
520
521 // make sure the path ends with a / for sanity
522 path_length = strlen(dest);
523
524 if (dest[path_length - 1] != '/') {
525 dest[path_length] = '/';
526 dest[path_length + 1] = '\0';
527 path_length++;
528 }
529
530 // now divide the full path into seperate NULL terminated strings,counting the number
531 // of directories in the process
532 dirs = 0;
533 char *strptr;
534 if (dest[0] == '/')
535 strptr = dest + 1; // skip first / of root dir
536 else
537 strptr = dest;
538
539 while (*strptr != '\0') {
540 if (*strptr == '/') {
541 *strptr = '\0';
542 dirs++;
543 }
544 strptr++;
545 }
546
547 // check to make sure we have a directory, if we don't then return the original path given
548 if (dirs == 0) {
549 strcpy(dest, srcPath);
550 return;
551 }
552
553 // allocate the memory needed for the seperate strings of each directory
554 directories = (char **)mem_malloccxx . . $ `mem/mem.h:79:9`!(sizeof(char *) * dirs);
555 if (!directories) {
556 strcpy(dest, srcPath);
557 return;
558 }
559
560 // now get all the directories, and place into the individual strings
561 strptr = dest;
562 int count = 0;
563 while (count < dirs) {
564 directories[count] = mem_strdupcxx . . $ `mem/mem.h:81:9`!(strptr);
565 strptr += strlen(strptr) + 1;
566 count++;
567 }
568
569 // now the fun part, figure out the correct order of the directories
570 int *dir_order;
571
572 dir_order = (int *)mem_malloccxx . . $ `mem/mem.h:79:9`!(sizeof(int) * dirs);
573 if (!dir_order) {
574 strcpy(dest, srcPath);
575 return;
576 }
577
578 for (count = 0; count < dirs; count++)
579 dir_order[count] = -1; // a -1 means the end of the sequence
580
581 // now build the order based on the indicies
582 int curr_index = 0;
583 for (count = 0; count < dirs; count++) {
584
585 if (!stricmpcxx . . $ `linux/linux_fix.h:43:9`!(directories[count], "..")) {
586 // we have to back up a directory
587 curr_index--; // back up
588 if (curr_index < 0)
589 curr_index = 0; // can't go further than root
590 dir_order[curr_index] = -1; // invalidate current slot
591 } else if (stricmpcxx . . $ `linux/linux_fix.h:43:9`!(directories[count], ".")) {
592 // we have a normal directory, add it's index
593 dir_order[curr_index] = count;
594 curr_index++;
595 }
596 }
597
598 // now rebuild the correct path for use, when we hit -1, we're done
599 dest[0] = '\0';
600 for (count = 0; count < dirs; count++) {
601 if (dir_order[count] == -1)
602 break;
603 else {
604 strcat(dest, directories[dir_order[count]]);
605 strcat(dest, "/");
606 }
607 }
608
609 // now remove trailing / char
610 path_length = strlen(dest);
611 if ((path_length > 0) && (dest[path_length - 1] == '/'))
612 dest[path_length - 1] = '\0';
613
614 // free up all the allocated memory and we're done
615 for (count = 0; count < dirs; count++) {
616 if (directories[count])
617 mem_freecxx . . $ `mem/mem.h:80:9`!(directories[count]);
618 }
619 if (directories)
620 mem_freecxx . . $ `mem/mem.h:80:9`!(directories);
621 if (dir_order)
622 mem_freecxx . . $ `mem/mem.h:80:9`!(dir_order);
623 }
624
625 // given a path (with no filename), it will return the parent path
626 // srcPath is the source given path
627 // dest is where the parent path will be placed
628 // returns true on success
629 // dest should be at least _MAX_PATH in length
630 bool ddio_GetParentPathcxx . . $ ddio_GetParentPath(9efbdc942cde3d3d).(char *dest, const char *srcPath) {
631 assertcxx . . $ `<external>/4398592474888995393/assert.h:102:11`!(srcPath);
632 assertcxx . . $ `<external>/4398592474888995393/assert.h:102:11`!(dest);
633
634 #define PARENT_DELIMcxx . . $ `ddio/lnxfile.cpp:634:9`! ".."
635 int spath_len = strlen(srcPath);
636 char *temp;
637
638 temp = (char *)mem_malloccxx . . $ `mem/mem.h:79:9`!(spath_len + strlen(PARENT_DELIMcxx . . $ `ddio/lnxfile.cpp:634:9`!) + 3);
639 if (!temp) {
640 return false;
641 }
642
643 ddio_MakePathcxx . . $ ddio_MakePath(9ad4b0815420a85).(temp, srcPath, PARENT_DELIMcxx . . $ `ddio/lnxfile.cpp:634:9`!, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!);
644 ddio_CleanPathcxx . . $ ddio_CleanPath(72c91e5e80d20bae).(dest, temp);
645 mem_freecxx . . $ `mem/mem.h:80:9`!(temp);
646 return true;
647 }
648
649 // Finds a full path from a relative path
650 // Parameters: full_path - filled in with the fully-specified path. Buffer must be at least _MAX_PATH bytes long
651 // rel_path - a path specification, either relative or absolute
652 // Returns TRUE if successful, FALSE if an error
653 bool ddio_GetFullPathcxx . . $ ddio_GetFullPath(9efbdc942cde3d3d).(char *full_path, const char *rel_path) {
654 char old_path[_MAX_PATHcxx . . $ `linux/linux_fix.h:34:9`!];
655
656 ddio_GetWorkingDircxx . . $ ddio_GetWorkingDir(a07d8cccb7673f5a).(old_path, sizeof(old_path)); // save old directory
657
658 if (!ddio_SetWorkingDircxx . . $ ddio_SetWorkingDir(c5f7da30cbc0f4e9).(rel_path)) // try switching to new directory
659 return 0; // couldn't switch, so return error
660
661 ddio_GetWorkingDircxx . . $ ddio_GetWorkingDir(a07d8cccb7673f5a).(full_path, _MAX_PATHcxx . . $ `linux/linux_fix.h:34:9`!); // get path from the OS
662
663 ddio_SetWorkingDircxx . . $ ddio_SetWorkingDir(c5f7da30cbc0f4e9).(old_path); // now restore old path
664
665 return 1;
666 }
667
668 // Generates a temporary filename based on the prefix, and basedir
669 // Parameters:
670 // basedir - directory to put the files
671 // prefix - prefix for the temp filename
672 // filename - buffer to hold generated filename (must be at least _MAX_PATH in length)
673 //
674 // Returns TRUE if successful, FALSE if an error
675 bool ddio_GetTempFileNamecxx . . $ ddio_GetTempFileName(dfb34736e99ce5d).(const char *basedir, const char *prefix, char *filename) {
676 char old_workdir[_MAX_PATHcxx . . $ `linux/linux_fix.h:34:9`!];
677 bool success = false;
678
679 if (strlen(prefix) > 64)
680 return false;
681
682 ddio_GetWorkingDircxx . . $ ddio_GetWorkingDir(a07d8cccb7673f5a).(old_workdir, _MAX_PATHcxx . . $ `linux/linux_fix.h:34:9`!);
683
684 if (!ddio_SetWorkingDircxx . . $ ddio_SetWorkingDir(c5f7da30cbc0f4e9).(basedir)) {
685 return false; // invalid base directory
686 }
687
688 char randname[10];
689 int index;
690 int tries = 0;
691 char rc;
692
693 bool created = false;
694
695 index = 0;
696
697 while (!success && tries < 20) {
698 // generate a bunch of random characters
699 rc = (randcxx . . $ rand(9efc7249d839d30e).() % 128);
700 if ((rc >= 'a' && rc <= 'z') || (rc >= 'A' && rc <= 'Z') || (rc >= '0' && rc <= '9')) {
701 // valid character
702 randname[index] = rc;
703 index++;
704
705 if (index == 10) {
706 // we hit the size of our max, see if we generated a unique filename
707 char t[_MAX_PATHcxx . . $ `linux/linux_fix.h:34:9`!];
708 randname[9] = '\0';
709 snprintf(t, sizeof(t), "%s%s.tmp", prefix, randname);
710
711 // see if we can find this file
712 FILE *fd = fopen(t, "rb");
713 if (!fd) {
714 // we found a good file!
715 ddio_MakePathcxx . . $ ddio_MakePath(9ad4b0815420a85).(filename, basedir, t, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!);
716 success = true;
717 created = true;
718 } else {
719 // already taken
720 fclose(fd);
721 tries++;
722 index = 0;
723 }
724 }
725 } else {
726 continue; // try again
727 }
728 }
729
730 ddio_SetWorkingDircxx . . $ ddio_SetWorkingDir(c5f7da30cbc0f4e9).(old_workdir);
731 return created;
732 }
733
734 bool ddio_CheckProcesscxx . . $ ddio_CheckProcess(83c267b20bac841a).(int pid) {
735 if (kill(pid, 0) == -1) {
736 if (errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! != ESRCHcxx . . $ `<external>/8987895939074593727/errno-base.h:7:9`!) {
737 /* some other error, log it */
738 mprintfcxx . . $ `ddebug/mono.h:82:9`!(0, "Error sending signal to PID for lock check (%d)\n", pid);
739 }
740 return false;
741 } else {
742 /* process exists */
743 return true;
744 }
745 }
746
747 int ddio_GetPIDcxx . . $ ddio_GetPID(b126dc7c1de90089).() {
748 return getpid();
749 }