/
platform_unix.c
338 lines (268 loc) · 8.85 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
16
17
18
#include <ctype.h>
#include <unistd.h>
#include <sys/types.h>
#include <pwd.h>
#include <sys/stat.h>
19
#include <sys/param.h>
20
21
22
#include <dirent.h>
#include <time.h>
#include <errno.h>
23
24
25
26
27
28
29
30
31
#if PHYSFS_PLATFORM_LINUX && !defined(PHYSFS_HAVE_MNTENT_H)
#define PHYSFS_HAVE_MNTENT_H 1
#elif PHYSFS_PLATFORM_SOLARIS && !defined(PHYSFS_HAVE_SYS_MNTTAB_H)
#define PHYSFS_HAVE_SYS_MNTTAB_H 1
#elif PHYSFS_PLATFORM_BSD && !defined(PHYSFS_HAVE_SYS_UCRED_H)
#define PHYSFS_HAVE_SYS_UCRED_H 1
#endif
32
33
34
35
#ifdef PHYSFS_HAVE_SYS_UCRED_H
# ifdef PHYSFS_HAVE_MNTENT_H
# undef PHYSFS_HAVE_MNTENT_H /* don't do both... */
# endif
36
# include <sys/mount.h>
37
# include <sys/ucred.h>
38
#endif
39
40
41
42
#ifdef PHYSFS_HAVE_MNTENT_H
#include <mntent.h>
#endif
43
44
45
46
47
#ifdef PHYSFS_HAVE_SYS_MNTTAB_H
#include <sys/mnttab.h>
#endif
48
49
#include "physfs_internal.h"
50
51
int __PHYSFS_platformInit(void)
{
52
return 1; /* always succeed. */
53
54
55
56
57
} /* __PHYSFS_platformInit */
int __PHYSFS_platformDeinit(void)
{
58
return 1; /* always succeed. */
59
60
61
} /* __PHYSFS_platformDeinit */
62
/* Stub version for platforms without CD-ROM support. */
63
void __PHYSFS_platformDetectAvailableCDs(PHYSFS_StringCallback cb, void *data)
64
{
65
66
#if (defined PHYSFS_NO_CDROM_SUPPORT)
/* no-op. */
67
68
#elif (defined PHYSFS_HAVE_SYS_UCRED_H)
69
int i;
70
71
struct statfs *mntbufp = NULL;
int mounts = getmntinfo(&mntbufp, MNT_WAIT);
72
73
74
for (i = 0; i < mounts; i++)
{
75
76
int add_it = 0;
77
if (strcmp(mntbufp[i].f_fstypename, "iso9660") == 0)
78
add_it = 1;
79
else if (strcmp( mntbufp[i].f_fstypename, "cd9660") == 0)
80
add_it = 1;
81
82
/* add other mount types here */
83
84
if (add_it)
85
cb(data, mntbufp[i].f_mntonname);
86
} /* for */
87
88
#elif (defined PHYSFS_HAVE_MNTENT_H)
89
90
91
92
FILE *mounts = NULL;
struct mntent *ent = NULL;
mounts = setmntent("/etc/mtab", "r");
93
BAIL_IF_MACRO(mounts == NULL, PHYSFS_ERR_IO, /*return void*/);
94
95
96
97
98
99
while ( (ent = getmntent(mounts)) != NULL )
{
int add_it = 0;
if (strcmp(ent->mnt_type, "iso9660") == 0)
add_it = 1;
100
101
102
103
104
105
106
107
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;
108
109
110
/* !!! FIXME: udf? automount? */
111
/* add other mount types here */
112
113
if (add_it)
114
cb(data, ent->mnt_dir);
115
116
117
} /* while */
endmntent(mounts);
118
119
120
121
122
#elif (defined PHYSFS_HAVE_SYS_MNTTAB_H)
FILE *mounts = fopen(MNTTAB, "r");
struct mnttab ent;
123
BAIL_IF_MACRO(mounts == NULL, PHYSFS_ERR_IO, /*return void*/);
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
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);
#else
#error Unknown platform. Should have defined PHYSFS_NO_CDROM_SUPPORT, perhaps.
140
#endif
141
} /* __PHYSFS_platformDetectAvailableCDs */
142
143
144
145
/* this is in posix.c ... */
extern char *__PHYSFS_platformCopyEnvironmentVariable(const char *varname);
146
147
148
149
150
151
152
153
/*
* 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.
*
154
* (envr) will be scribbled over, and you are expected to allocator.Free() the
155
156
157
* return value when you're done with it.
*/
static char *findBinaryInPath(const char *bin, char *envr)
158
{
159
160
161
size_t alloc_size = 0;
char *exe = NULL;
char *start = envr;
162
163
char *ptr;
164
165
assert(bin != NULL);
assert(envr != NULL);
166
167
168
do
{
169
size_t size;
170
171
size_t binlen;
172
ptr = strchr(start, ':'); /* find next $PATH separator. */
173
174
175
if (ptr)
*ptr = '\0';
176
177
binlen = strlen(bin);
size = strlen(start) + binlen + 2;
178
if (size > alloc_size)
179
{
180
char *x = (char *) allocator.Realloc(exe, size);
181
if (!x)
182
183
{
if (exe != NULL)
184
allocator.Free(exe);
185
BAIL_MACRO(PHYSFS_ERR_OUT_OF_MEMORY, NULL);
186
187
188
189
} /* if */
alloc_size = size;
exe = x;
190
} /* if */
191
192
/* build full binary path... */
193
strcpy(exe, start);
194
if ((exe[0] == '\0') || (exe[strlen(exe) - 1] != '/'))
195
strcat(exe, "/");
196
197
198
strcat(exe, bin);
if (access(exe, X_OK) == 0) /* Exists as executable? We're done. */
199
{
200
exe[size - binlen] = '\0'; /* chop off filename, leave '/' */
201
return exe;
202
} /* if */
203
204
start = ptr + 1; /* start points to beginning of next element. */
205
206
} while (ptr != NULL);
207
if (exe != NULL)
208
allocator.Free(exe);
209
210
return NULL; /* doesn't exist in path. */
211
212
213
} /* findBinaryInPath */
214
215
216
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
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 */
246
247
char *__PHYSFS_platformCalcBaseDir(const char *argv0)
{
248
249
250
251
252
char *retval = NULL;
char *envr = NULL;
/* fast path: default behaviour can handle this. */
if ( (argv0 != NULL) && (strchr(argv0, '/') != NULL) )
253
return NULL; /* higher level will parse out real path from argv0. */
254
255
256
257
258
259
/*
* Try to avoid using argv0 unless forced to. If there's a Linux-like
* /proc filesystem, you can get the full path to the current process from
* the /proc/self/exe symlink.
*/
260
retval = readSymLink("/proc/self/exe");
261
262
263
264
265
266
267
268
269
270
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 */
271
if (retval != NULL) /* chop off filename. */
272
{
273
274
char *ptr = strrchr(retval, '/');
if (ptr != NULL)
275
*(ptr+1) = '\0';
276
} /* if */
277
278
279
280
if ((retval == NULL) && (argv0 != NULL))
{
/* If there's no dirsep on argv0, then look through $PATH for it. */
281
/* !!! FIXME: smallAlloc? */
282
envr = __PHYSFS_platformCopyEnvironmentVariable("PATH");
283
BAIL_IF_MACRO(!envr, ERRPASS, NULL);
284
285
286
retval = findBinaryInPath(argv0, envr);
allocator.Free(envr);
} /* if */
287
288
289
290
291
292
293
294
295
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 */
296
return retval;
297
298
299
} /* __PHYSFS_platformCalcBaseDir */
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
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.
*
* http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html
*/
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();
BAIL_IF_MACRO(!envr, ERRPASS, NULL); /* oh well. */
append = ".local/share/";
} /* if */
322
len = strlen(envr) + strlen(append) + strlen(app) + 2;
323
324
retval = (char *) allocator.Malloc(len);
BAIL_IF_MACRO(!retval, PHYSFS_ERR_OUT_OF_MEMORY, NULL);
325
snprintf(retval, len, "%s%s%s/", envr, append, app);
326
327
328
329
return retval;
} /* __PHYSFS_platformCalcPrefDir */
330
331
int __PHYSFS_platformSetDefaultAllocator(PHYSFS_Allocator *a)
{
332
return 0; /* just use malloc() and friends. */
333
334
} /* __PHYSFS_platformSetDefaultAllocator */
335
#endif /* PHYSFS_PLATFORM_UNIX */
336
337
/* end of unix.c ... */