/
platform_unix.c
296 lines (232 loc) · 7.57 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
#ifdef PHYSFS_HAVE_SYS_UCRED_H
# ifdef PHYSFS_HAVE_MNTENT_H
# undef PHYSFS_HAVE_MNTENT_H /* don't do both... */
# endif
28
# include <sys/mount.h>
29
# include <sys/ucred.h>
30
#endif
31
32
33
34
#ifdef PHYSFS_HAVE_MNTENT_H
#include <mntent.h>
#endif
35
36
37
38
39
#ifdef PHYSFS_HAVE_SYS_MNTTAB_H
#include <sys/mnttab.h>
#endif
40
41
#include "physfs_internal.h"
42
43
int __PHYSFS_platformInit(void)
{
44
return 1; /* always succeed. */
45
46
47
48
49
} /* __PHYSFS_platformInit */
int __PHYSFS_platformDeinit(void)
{
50
return 1; /* always succeed. */
51
52
53
} /* __PHYSFS_platformDeinit */
54
/* Stub version for platforms without CD-ROM support. */
55
void __PHYSFS_platformDetectAvailableCDs(PHYSFS_StringCallback cb, void *data)
56
{
57
58
#if (defined PHYSFS_NO_CDROM_SUPPORT)
/* no-op. */
59
60
#elif (defined PHYSFS_HAVE_SYS_UCRED_H)
61
int i;
62
63
struct statfs *mntbufp = NULL;
int mounts = getmntinfo(&mntbufp, MNT_WAIT);
64
65
66
for (i = 0; i < mounts; i++)
{
67
68
int add_it = 0;
69
if (strcmp(mntbufp[i].f_fstypename, "iso9660") == 0)
70
add_it = 1;
71
else if (strcmp( mntbufp[i].f_fstypename, "cd9660") == 0)
72
add_it = 1;
73
74
/* add other mount types here */
75
76
if (add_it)
77
cb(data, mntbufp[i].f_mntonname);
78
} /* for */
79
80
#elif (defined PHYSFS_HAVE_MNTENT_H)
81
82
83
84
FILE *mounts = NULL;
struct mntent *ent = NULL;
mounts = setmntent("/etc/mtab", "r");
85
BAIL_IF_MACRO(mounts == NULL, ERR_IO_ERROR, /*return void*/);
86
87
88
89
90
91
while ( (ent = getmntent(mounts)) != NULL )
{
int add_it = 0;
if (strcmp(ent->mnt_type, "iso9660") == 0)
add_it = 1;
92
93
94
95
96
97
98
99
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;
100
101
102
/* !!! FIXME: udf? automount? */
103
/* add other mount types here */
104
105
if (add_it)
106
cb(data, ent->mnt_dir);
107
108
109
} /* while */
endmntent(mounts);
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#elif (defined PHYSFS_HAVE_SYS_MNTTAB_H)
FILE *mounts = fopen(MNTTAB, "r");
struct mnttab ent;
BAIL_IF_MACRO(mounts == NULL, ERR_IO_ERROR, /*return void*/);
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.
132
#endif
133
} /* __PHYSFS_platformDetectAvailableCDs */
134
135
136
137
/* this is in posix.c ... */
extern char *__PHYSFS_platformCopyEnvironmentVariable(const char *varname);
138
139
140
141
142
143
144
145
/*
* 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.
*
146
* (envr) will be scribbled over, and you are expected to allocator.Free() the
147
148
149
* return value when you're done with it.
*/
static char *findBinaryInPath(const char *bin, char *envr)
150
{
151
152
153
size_t alloc_size = 0;
char *exe = NULL;
char *start = envr;
154
155
char *ptr;
156
157
BAIL_IF_MACRO(bin == NULL, ERR_INVALID_ARGUMENT, NULL);
BAIL_IF_MACRO(envr == NULL, ERR_INVALID_ARGUMENT, NULL);
158
159
160
do
{
161
162
size_t size;
ptr = strchr(start, ':'); /* find next $PATH separator. */
163
164
165
if (ptr)
*ptr = '\0';
166
167
size = strlen(start) + strlen(bin) + 2;
if (size > alloc_size)
168
{
169
char *x = (char *) allocator.Realloc(exe, size);
170
171
172
if (x == NULL)
{
if (exe != NULL)
173
allocator.Free(exe);
174
175
176
177
178
BAIL_MACRO(ERR_OUT_OF_MEMORY, NULL);
} /* if */
alloc_size = size;
exe = x;
179
} /* if */
180
181
/* build full binary path... */
182
strcpy(exe, start);
183
if ((exe[0] == '\0') || (exe[strlen(exe) - 1] != '/'))
184
strcat(exe, "/");
185
186
187
strcat(exe, bin);
if (access(exe, X_OK) == 0) /* Exists as executable? We're done. */
188
{
189
strcpy(exe, start); /* i'm lazy. piss off. */
190
return exe;
191
} /* if */
192
193
start = ptr + 1; /* start points to beginning of next element. */
194
195
} while (ptr != NULL);
196
if (exe != NULL)
197
allocator.Free(exe);
198
199
return NULL; /* doesn't exist in path. */
200
201
202
} /* findBinaryInPath */
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
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 */
235
236
char *__PHYSFS_platformCalcBaseDir(const char *argv0)
{
237
238
239
240
241
char *retval = NULL;
char *envr = NULL;
/* fast path: default behaviour can handle this. */
if ( (argv0 != NULL) && (strchr(argv0, '/') != NULL) )
242
return NULL; /* higher level will parse out real path from argv0. */
243
244
245
246
247
248
/*
* 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.
*/
249
retval = readSymLink("/proc/self/exe");
250
251
252
253
254
255
256
257
258
259
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 */
260
if (retval != NULL) /* chop off filename. */
261
{
262
263
264
char *ptr = strrchr(retval, '/');
if (ptr != NULL)
*ptr = '\0';
265
} /* if */
266
267
268
269
270
271
272
273
274
if ((retval == NULL) && (argv0 != NULL))
{
/* If there's no dirsep on argv0, then look through $PATH for it. */
envr = __PHYSFS_platformCopyEnvironmentVariable("PATH");
BAIL_IF_MACRO(!envr, NULL, NULL);
retval = findBinaryInPath(argv0, envr);
allocator.Free(envr);
} /* if */
275
276
277
278
279
280
281
282
283
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 */
284
return retval;
285
286
287
} /* __PHYSFS_platformCalcBaseDir */
288
289
int __PHYSFS_platformSetDefaultAllocator(PHYSFS_Allocator *a)
{
290
return 0; /* just use malloc() and friends. */
291
292
} /* __PHYSFS_platformSetDefaultAllocator */
293
#endif /* PHYSFS_PLATFORM_UNIX */
294
295
/* end of unix.c ... */