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