author | unc0rr |
Sun, 14 Jan 2018 23:30:42 +0100 | |
branch | qmlfrontend |
changeset 12892 | 0088bcccd19a |
parent 12213 | bb5522e88ab2 |
permissions | -rw-r--r-- |
7768 | 1 |
/* |
2 |
* Unix support routines for PhysicsFS. |
|
3 |
* |
|
4 |
* Please see the file LICENSE.txt in the source's root directory. |
|
5 |
* |
|
6 |
* This file written by Ryan C. Gordon. |
|
7 |
*/ |
|
8 |
||
9 |
#define __PHYSICSFS_INTERNAL__ |
|
10 |
#include "physfs_platforms.h" |
|
11 |
||
12 |
#ifdef PHYSFS_PLATFORM_UNIX |
|
13 |
||
14 |
#include <ctype.h> |
|
15 |
#include <unistd.h> |
|
12213
bb5522e88ab2
bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents:
9799
diff
changeset
|
16 |
#include <stdlib.h> |
7768 | 17 |
#include <sys/types.h> |
18 |
#include <pwd.h> |
|
19 |
#include <sys/stat.h> |
|
20 |
#include <sys/param.h> |
|
21 |
#include <dirent.h> |
|
22 |
#include <time.h> |
|
23 |
#include <errno.h> |
|
12213
bb5522e88ab2
bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents:
9799
diff
changeset
|
24 |
#include <limits.h> |
7768 | 25 |
|
12892 | 26 |
#include <sys/types.h> |
27 |
#include <sys/sysctl.h> |
|
28 |
||
29 |
||
7768 | 30 |
#if PHYSFS_PLATFORM_LINUX && !defined(PHYSFS_HAVE_MNTENT_H) |
31 |
#define PHYSFS_HAVE_MNTENT_H 1 |
|
32 |
#elif PHYSFS_PLATFORM_SOLARIS && !defined(PHYSFS_HAVE_SYS_MNTTAB_H) |
|
33 |
#define PHYSFS_HAVE_SYS_MNTTAB_H 1 |
|
34 |
#elif PHYSFS_PLATFORM_BSD && !defined(PHYSFS_HAVE_SYS_UCRED_H) |
|
35 |
#define PHYSFS_HAVE_SYS_UCRED_H 1 |
|
36 |
#endif |
|
37 |
||
38 |
#ifdef PHYSFS_HAVE_SYS_UCRED_H |
|
39 |
# ifdef PHYSFS_HAVE_MNTENT_H |
|
40 |
# undef PHYSFS_HAVE_MNTENT_H /* don't do both... */ |
|
41 |
# endif |
|
42 |
# include <sys/mount.h> |
|
43 |
# include <sys/ucred.h> |
|
44 |
#endif |
|
45 |
||
46 |
#ifdef PHYSFS_HAVE_MNTENT_H |
|
47 |
#include <mntent.h> |
|
48 |
#endif |
|
49 |
||
50 |
#ifdef PHYSFS_HAVE_SYS_MNTTAB_H |
|
51 |
#include <sys/mnttab.h> |
|
52 |
#endif |
|
53 |
||
54 |
#include "physfs_internal.h" |
|
55 |
||
56 |
int __PHYSFS_platformInit(void) |
|
57 |
{ |
|
58 |
return 1; /* always succeed. */ |
|
59 |
} /* __PHYSFS_platformInit */ |
|
60 |
||
61 |
||
62 |
int __PHYSFS_platformDeinit(void) |
|
63 |
{ |
|
64 |
return 1; /* always succeed. */ |
|
65 |
} /* __PHYSFS_platformDeinit */ |
|
66 |
||
67 |
||
68 |
/* Stub version for platforms without CD-ROM support. */ |
|
69 |
void __PHYSFS_platformDetectAvailableCDs(PHYSFS_StringCallback cb, void *data) |
|
70 |
{ |
|
71 |
#if (defined PHYSFS_NO_CDROM_SUPPORT) |
|
72 |
/* no-op. */ |
|
73 |
||
74 |
#elif (defined PHYSFS_HAVE_SYS_UCRED_H) |
|
75 |
int i; |
|
76 |
struct statfs *mntbufp = NULL; |
|
77 |
int mounts = getmntinfo(&mntbufp, MNT_WAIT); |
|
78 |
||
79 |
for (i = 0; i < mounts; i++) |
|
80 |
{ |
|
81 |
int add_it = 0; |
|
82 |
||
83 |
if (strcmp(mntbufp[i].f_fstypename, "iso9660") == 0) |
|
84 |
add_it = 1; |
|
85 |
else if (strcmp( mntbufp[i].f_fstypename, "cd9660") == 0) |
|
86 |
add_it = 1; |
|
87 |
||
88 |
/* add other mount types here */ |
|
89 |
||
90 |
if (add_it) |
|
91 |
cb(data, mntbufp[i].f_mntonname); |
|
92 |
} /* for */ |
|
93 |
||
94 |
#elif (defined PHYSFS_HAVE_MNTENT_H) |
|
95 |
FILE *mounts = NULL; |
|
96 |
struct mntent *ent = NULL; |
|
97 |
||
98 |
mounts = setmntent("/etc/mtab", "r"); |
|
99 |
BAIL_IF_MACRO(mounts == NULL, PHYSFS_ERR_IO, /*return void*/); |
|
100 |
||
101 |
while ( (ent = getmntent(mounts)) != NULL ) |
|
102 |
{ |
|
103 |
int add_it = 0; |
|
104 |
if (strcmp(ent->mnt_type, "iso9660") == 0) |
|
105 |
add_it = 1; |
|
106 |
else if (strcmp(ent->mnt_type, "udf") == 0) |
|
107 |
add_it = 1; |
|
108 |
||
109 |
/* !!! FIXME: these might pick up floppy drives, right? */ |
|
110 |
else if (strcmp(ent->mnt_type, "auto") == 0) |
|
111 |
add_it = 1; |
|
112 |
else if (strcmp(ent->mnt_type, "supermount") == 0) |
|
113 |
add_it = 1; |
|
114 |
||
115 |
/* !!! FIXME: udf? automount? */ |
|
116 |
||
117 |
/* add other mount types here */ |
|
118 |
||
119 |
if (add_it) |
|
120 |
cb(data, ent->mnt_dir); |
|
121 |
} /* while */ |
|
122 |
||
123 |
endmntent(mounts); |
|
124 |
||
125 |
#elif (defined PHYSFS_HAVE_SYS_MNTTAB_H) |
|
126 |
FILE *mounts = fopen(MNTTAB, "r"); |
|
127 |
struct mnttab ent; |
|
128 |
||
129 |
BAIL_IF_MACRO(mounts == NULL, PHYSFS_ERR_IO, /*return void*/); |
|
130 |
while (getmntent(mounts, &ent) == 0) |
|
131 |
{ |
|
132 |
int add_it = 0; |
|
133 |
if (strcmp(ent.mnt_fstype, "hsfs") == 0) |
|
134 |
add_it = 1; |
|
135 |
||
136 |
/* add other mount types here */ |
|
137 |
||
138 |
if (add_it) |
|
139 |
cb(data, ent.mnt_mountp); |
|
140 |
} /* while */ |
|
141 |
||
142 |
fclose(mounts); |
|
143 |
||
144 |
#else |
|
145 |
#error Unknown platform. Should have defined PHYSFS_NO_CDROM_SUPPORT, perhaps. |
|
146 |
#endif |
|
147 |
} /* __PHYSFS_platformDetectAvailableCDs */ |
|
148 |
||
149 |
||
150 |
/* |
|
151 |
* See where program (bin) resides in the $PATH specified by (envr). |
|
152 |
* returns a copy of the first element in envr that contains it, or NULL |
|
153 |
* if it doesn't exist or there were other problems. PHYSFS_SetError() is |
|
154 |
* called if we have a problem. |
|
155 |
* |
|
156 |
* (envr) will be scribbled over, and you are expected to allocator.Free() the |
|
157 |
* return value when you're done with it. |
|
158 |
*/ |
|
159 |
static char *findBinaryInPath(const char *bin, char *envr) |
|
160 |
{ |
|
161 |
size_t alloc_size = 0; |
|
162 |
char *exe = NULL; |
|
163 |
char *start = envr; |
|
164 |
char *ptr; |
|
165 |
||
166 |
assert(bin != NULL); |
|
167 |
assert(envr != NULL); |
|
168 |
||
169 |
do |
|
170 |
{ |
|
171 |
size_t size; |
|
172 |
size_t binlen; |
|
173 |
||
174 |
ptr = strchr(start, ':'); /* find next $PATH separator. */ |
|
175 |
if (ptr) |
|
176 |
*ptr = '\0'; |
|
177 |
||
178 |
binlen = strlen(bin); |
|
179 |
size = strlen(start) + binlen + 2; |
|
12213
bb5522e88ab2
bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents:
9799
diff
changeset
|
180 |
if (size >= alloc_size) |
7768 | 181 |
{ |
182 |
char *x = (char *) allocator.Realloc(exe, size); |
|
183 |
if (!x) |
|
184 |
{ |
|
185 |
if (exe != NULL) |
|
186 |
allocator.Free(exe); |
|
187 |
BAIL_MACRO(PHYSFS_ERR_OUT_OF_MEMORY, NULL); |
|
188 |
} /* if */ |
|
189 |
||
190 |
alloc_size = size; |
|
191 |
exe = x; |
|
192 |
} /* if */ |
|
193 |
||
194 |
/* build full binary path... */ |
|
195 |
strcpy(exe, start); |
|
196 |
if ((exe[0] == '\0') || (exe[strlen(exe) - 1] != '/')) |
|
197 |
strcat(exe, "/"); |
|
198 |
strcat(exe, bin); |
|
199 |
||
200 |
if (access(exe, X_OK) == 0) /* Exists as executable? We're done. */ |
|
201 |
{ |
|
12213
bb5522e88ab2
bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents:
9799
diff
changeset
|
202 |
exe[(size - binlen) - 1] = '\0'; /* chop off filename, leave '/' */ |
7768 | 203 |
return exe; |
204 |
} /* if */ |
|
205 |
||
206 |
start = ptr + 1; /* start points to beginning of next element. */ |
|
207 |
} while (ptr != NULL); |
|
208 |
||
209 |
if (exe != NULL) |
|
210 |
allocator.Free(exe); |
|
211 |
||
212 |
return NULL; /* doesn't exist in path. */ |
|
213 |
} /* findBinaryInPath */ |
|
214 |
||
215 |
||
216 |
static char *readSymLink(const char *path) |
|
217 |
{ |
|
218 |
ssize_t len = 64; |
|
219 |
ssize_t rc = -1; |
|
220 |
char *retval = NULL; |
|
221 |
||
222 |
while (1) |
|
223 |
{ |
|
224 |
char *ptr = (char *) allocator.Realloc(retval, (size_t) len); |
|
225 |
if (ptr == NULL) |
|
226 |
break; /* out of memory. */ |
|
227 |
retval = ptr; |
|
228 |
||
229 |
rc = readlink(path, retval, len); |
|
230 |
if (rc == -1) |
|
231 |
break; /* not a symlink, i/o error, etc. */ |
|
232 |
||
233 |
else if (rc < len) |
|
234 |
{ |
|
235 |
retval[rc] = '\0'; /* readlink doesn't null-terminate. */ |
|
236 |
return retval; /* we're good to go. */ |
|
237 |
} /* else if */ |
|
238 |
||
239 |
len *= 2; /* grow buffer, try again. */ |
|
240 |
} /* while */ |
|
241 |
||
242 |
if (retval != NULL) |
|
243 |
allocator.Free(retval); |
|
244 |
return NULL; |
|
245 |
} /* readSymLink */ |
|
246 |
||
247 |
||
248 |
char *__PHYSFS_platformCalcBaseDir(const char *argv0) |
|
249 |
{ |
|
250 |
char *retval = NULL; |
|
251 |
const char *envr = NULL; |
|
252 |
||
12213
bb5522e88ab2
bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents:
9799
diff
changeset
|
253 |
/* Try to avoid using argv0 unless forced to. Try system-specific stuff. */ |
bb5522e88ab2
bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents:
9799
diff
changeset
|
254 |
|
bb5522e88ab2
bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents:
9799
diff
changeset
|
255 |
#if PHYSFS_PLATFORM_FREEBSD |
bb5522e88ab2
bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents:
9799
diff
changeset
|
256 |
{ |
bb5522e88ab2
bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents:
9799
diff
changeset
|
257 |
char fullpath[PATH_MAX]; |
12892 | 258 |
size_t buflen = sizeof (fullpath) - 1; |
12213
bb5522e88ab2
bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents:
9799
diff
changeset
|
259 |
int mib[4] = { CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1 }; |
12892 | 260 |
if (sysctl(mib, 4, fullpath, &buflen, NULL, 0) != -1) { |
261 |
int pathlen = strlen(fullpath); |
|
262 |
fullpath[pathlen] = '/'; |
|
263 |
fullpath[pathlen + 1] = 0; |
|
12213
bb5522e88ab2
bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents:
9799
diff
changeset
|
264 |
retval = __PHYSFS_strdup(fullpath); |
12892 | 265 |
} |
12213
bb5522e88ab2
bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents:
9799
diff
changeset
|
266 |
} |
bb5522e88ab2
bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents:
9799
diff
changeset
|
267 |
#elif PHYSFS_PLATFORM_SOLARIS |
bb5522e88ab2
bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents:
9799
diff
changeset
|
268 |
{ |
bb5522e88ab2
bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents:
9799
diff
changeset
|
269 |
const char *path = getexecname(); |
bb5522e88ab2
bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents:
9799
diff
changeset
|
270 |
if ((path != NULL) && (path[0] == '/')) /* must be absolute path... */ |
bb5522e88ab2
bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents:
9799
diff
changeset
|
271 |
retval = __PHYSFS_strdup(path); |
bb5522e88ab2
bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents:
9799
diff
changeset
|
272 |
} |
bb5522e88ab2
bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents:
9799
diff
changeset
|
273 |
#endif |
bb5522e88ab2
bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents:
9799
diff
changeset
|
274 |
|
bb5522e88ab2
bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents:
9799
diff
changeset
|
275 |
if (retval) |
bb5522e88ab2
bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents:
9799
diff
changeset
|
276 |
return retval; /* already got it. */ |
bb5522e88ab2
bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents:
9799
diff
changeset
|
277 |
|
bb5522e88ab2
bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents:
9799
diff
changeset
|
278 |
/* If there's a Linux-like /proc filesystem, you can get the full path to |
bb5522e88ab2
bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents:
9799
diff
changeset
|
279 |
* the current process from a symlink in there. |
7768 | 280 |
*/ |
12213
bb5522e88ab2
bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents:
9799
diff
changeset
|
281 |
|
bb5522e88ab2
bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents:
9799
diff
changeset
|
282 |
if (access("/proc", F_OK) == 0) |
7768 | 283 |
{ |
12213
bb5522e88ab2
bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents:
9799
diff
changeset
|
284 |
retval = readSymLink("/proc/self/exe"); |
bb5522e88ab2
bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents:
9799
diff
changeset
|
285 |
if (!retval) retval = readSymLink("/proc/curproc/file"); |
bb5522e88ab2
bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents:
9799
diff
changeset
|
286 |
if (!retval) retval = readSymLink("/proc/curproc/exe"); |
bb5522e88ab2
bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents:
9799
diff
changeset
|
287 |
if (retval == NULL) |
bb5522e88ab2
bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents:
9799
diff
changeset
|
288 |
{ |
bb5522e88ab2
bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents:
9799
diff
changeset
|
289 |
/* older kernels don't have /proc/self ... try PID version... */ |
bb5522e88ab2
bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents:
9799
diff
changeset
|
290 |
const unsigned long long pid = (unsigned long long) getpid(); |
bb5522e88ab2
bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents:
9799
diff
changeset
|
291 |
char path[64]; |
bb5522e88ab2
bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents:
9799
diff
changeset
|
292 |
const int rc = (int) snprintf(path,sizeof(path),"/proc/%llu/exe",pid); |
bb5522e88ab2
bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents:
9799
diff
changeset
|
293 |
if ( (rc > 0) && (rc < sizeof(path)) ) |
bb5522e88ab2
bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents:
9799
diff
changeset
|
294 |
retval = readSymLink(path); |
bb5522e88ab2
bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents:
9799
diff
changeset
|
295 |
} /* if */ |
7768 | 296 |
} /* if */ |
297 |
||
298 |
if (retval != NULL) /* chop off filename. */ |
|
299 |
{ |
|
300 |
char *ptr = strrchr(retval, '/'); |
|
301 |
if (ptr != NULL) |
|
302 |
*(ptr+1) = '\0'; |
|
303 |
else /* shouldn't happen, but just in case... */ |
|
304 |
{ |
|
305 |
allocator.Free(retval); |
|
306 |
retval = NULL; |
|
307 |
} /* else */ |
|
308 |
} /* if */ |
|
309 |
||
12213
bb5522e88ab2
bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents:
9799
diff
changeset
|
310 |
/* No /proc/self/exe, etc, but we have an argv[0] we can parse? */ |
7768 | 311 |
if ((retval == NULL) && (argv0 != NULL)) |
312 |
{ |
|
313 |
/* fast path: default behaviour can handle this. */ |
|
314 |
if (strchr(argv0, '/') != NULL) |
|
315 |
return NULL; /* higher level parses out real path from argv0. */ |
|
316 |
||
317 |
/* If there's no dirsep on argv0, then look through $PATH for it. */ |
|
318 |
envr = getenv("PATH"); |
|
319 |
if (envr != NULL) |
|
320 |
{ |
|
321 |
char *path = (char *) __PHYSFS_smallAlloc(strlen(envr) + 1); |
|
322 |
BAIL_IF_MACRO(!path, PHYSFS_ERR_OUT_OF_MEMORY, NULL); |
|
323 |
strcpy(path, envr); |
|
324 |
retval = findBinaryInPath(argv0, path); |
|
325 |
__PHYSFS_smallFree(path); |
|
326 |
} /* if */ |
|
327 |
} /* if */ |
|
328 |
||
329 |
if (retval != NULL) |
|
330 |
{ |
|
331 |
/* try to shrink buffer... */ |
|
332 |
char *ptr = (char *) allocator.Realloc(retval, strlen(retval) + 1); |
|
333 |
if (ptr != NULL) |
|
334 |
retval = ptr; /* oh well if it failed. */ |
|
335 |
} /* if */ |
|
336 |
||
337 |
return retval; |
|
338 |
} /* __PHYSFS_platformCalcBaseDir */ |
|
339 |
||
340 |
||
341 |
char *__PHYSFS_platformCalcPrefDir(const char *org, const char *app) |
|
342 |
{ |
|
343 |
/* |
|
344 |
* We use XDG's base directory spec, even if you're not on Linux. |
|
345 |
* This isn't strictly correct, but the results are relatively sane |
|
346 |
* in any case. |
|
347 |
* |
|
12213
bb5522e88ab2
bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents:
9799
diff
changeset
|
348 |
* https://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html |
7768 | 349 |
*/ |
350 |
const char *envr = getenv("XDG_DATA_HOME"); |
|
351 |
const char *append = "/"; |
|
352 |
char *retval = NULL; |
|
353 |
size_t len = 0; |
|
354 |
||
355 |
if (!envr) |
|
356 |
{ |
|
357 |
/* You end up with "$HOME/.local/share/Game Name 2" */ |
|
358 |
envr = __PHYSFS_getUserDir(); |
|
359 |
BAIL_IF_MACRO(!envr, ERRPASS, NULL); /* oh well. */ |
|
360 |
append = ".local/share/"; |
|
361 |
} /* if */ |
|
362 |
||
363 |
len = strlen(envr) + strlen(append) + strlen(app) + 2; |
|
364 |
retval = (char *) allocator.Malloc(len); |
|
365 |
BAIL_IF_MACRO(!retval, PHYSFS_ERR_OUT_OF_MEMORY, NULL); |
|
366 |
snprintf(retval, len, "%s%s%s/", envr, append, app); |
|
367 |
return retval; |
|
368 |
} /* __PHYSFS_platformCalcPrefDir */ |
|
369 |
||
370 |
||
371 |
int __PHYSFS_platformSetDefaultAllocator(PHYSFS_Allocator *a) |
|
372 |
{ |
|
373 |
return 0; /* just use malloc() and friends. */ |
|
374 |
} /* __PHYSFS_platformSetDefaultAllocator */ |
|
375 |
||
376 |
#endif /* PHYSFS_PLATFORM_UNIX */ |
|
377 |
||
378 |
/* end of unix.c ... */ |
|
379 |