Skip to content

Latest commit

 

History

History
385 lines (305 loc) · 9.79 KB

platform_unix.c

File metadata and controls

385 lines (305 loc) · 9.79 KB
 
Jul 7, 2001
Jul 7, 2001
1
2
3
/*
* Unix support routines for PhysicsFS.
*
Mar 11, 2007
Mar 11, 2007
4
* Please see the file LICENSE.txt in the source's root directory.
Jul 7, 2001
Jul 7, 2001
5
6
7
8
*
* This file written by Ryan C. Gordon.
*/
Mar 11, 2007
Mar 11, 2007
9
10
11
12
#define __PHYSICSFS_INTERNAL__
#include "physfs_platforms.h"
#ifdef PHYSFS_PLATFORM_UNIX
Jun 29, 2002
Jun 29, 2002
13
Jul 7, 2001
Jul 7, 2001
14
15
#include <stdio.h>
#include <stdlib.h>
Jul 8, 2001
Jul 8, 2001
16
17
18
19
20
21
#include <string.h>
#include <ctype.h>
#include <unistd.h>
#include <sys/types.h>
#include <pwd.h>
#include <sys/stat.h>
Jul 16, 2001
Jul 16, 2001
22
#include <sys/param.h>
Jul 8, 2001
Jul 8, 2001
23
24
25
#include <dirent.h>
#include <time.h>
#include <errno.h>
Jul 20, 2002
Jul 20, 2002
26
#include <sys/mount.h>
Mar 5, 2002
Mar 5, 2002
27
Jul 25, 2002
Jul 25, 2002
28
29
30
31
#if (!defined PHYSFS_NO_PTHREADS_SUPPORT)
#include <pthread.h>
#endif
Jul 20, 2002
Jul 20, 2002
32
33
34
35
36
#ifdef PHYSFS_HAVE_SYS_UCRED_H
# ifdef PHYSFS_HAVE_MNTENT_H
# undef PHYSFS_HAVE_MNTENT_H /* don't do both... */
# endif
# include <sys/ucred.h>
Mar 5, 2002
Mar 5, 2002
37
#endif
Jul 7, 2001
Jul 7, 2001
38
Jul 20, 2002
Jul 20, 2002
39
40
41
#ifdef PHYSFS_HAVE_MNTENT_H
#include <mntent.h>
#endif
Apr 6, 2002
Apr 6, 2002
42
Jul 7, 2001
Jul 7, 2001
43
44
#include "physfs_internal.h"
Mar 5, 2002
Mar 5, 2002
45
Mar 24, 2002
Mar 24, 2002
46
47
48
49
50
51
52
53
54
55
56
57
int __PHYSFS_platformInit(void)
{
return(1); /* always succeed. */
} /* __PHYSFS_platformInit */
int __PHYSFS_platformDeinit(void)
{
return(1); /* always succeed. */
} /* __PHYSFS_platformDeinit */
Jul 25, 2002
Jul 25, 2002
58
/* Stub version for platforms without CD-ROM support. */
Sep 29, 2004
Sep 29, 2004
59
void __PHYSFS_platformDetectAvailableCDs(PHYSFS_StringCallback cb, void *data)
Jul 25, 2002
Jul 25, 2002
60
{
Apr 13, 2009
Apr 13, 2009
61
62
#if (defined PHYSFS_NO_CDROM_SUPPORT)
/* no-op. */
Jul 25, 2002
Jul 25, 2002
63
May 24, 2003
May 24, 2003
64
#elif (defined PHYSFS_HAVE_SYS_UCRED_H)
Jul 25, 2002
Jul 25, 2002
65
int i;
Sep 29, 2004
Sep 29, 2004
66
67
struct statfs *mntbufp = NULL;
int mounts = getmntinfo(&mntbufp, MNT_WAIT);
Mar 5, 2002
Mar 5, 2002
68
Jul 25, 2002
Jul 25, 2002
69
70
for (i = 0; i < mounts; i++)
{
Mar 5, 2002
Mar 5, 2002
71
72
int add_it = 0;
Jul 25, 2002
Jul 25, 2002
73
if (strcmp(mntbufp[i].f_fstypename, "iso9660") == 0)
Mar 5, 2002
Mar 5, 2002
74
add_it = 1;
Jul 25, 2002
Jul 25, 2002
75
else if (strcmp( mntbufp[i].f_fstypename, "cd9660") == 0)
Apr 6, 2002
Apr 6, 2002
76
add_it = 1;
May 21, 2002
May 21, 2002
77
78
/* add other mount types here */
Mar 5, 2002
Mar 5, 2002
79
80
if (add_it)
Sep 29, 2004
Sep 29, 2004
81
cb(data, mntbufp[i].f_mntonname);
Jul 25, 2002
Jul 25, 2002
82
} /* for */
Mar 5, 2002
Mar 5, 2002
83
May 24, 2003
May 24, 2003
84
#elif (defined PHYSFS_HAVE_MNTENT_H)
Aug 7, 2001
Aug 7, 2001
85
86
87
88
FILE *mounts = NULL;
struct mntent *ent = NULL;
mounts = setmntent("/etc/mtab", "r");
Mar 13, 2005
Mar 13, 2005
89
BAIL_IF_MACRO(mounts == NULL, ERR_IO_ERROR, /*return void*/);
Aug 7, 2001
Aug 7, 2001
90
91
92
93
94
95
while ( (ent = getmntent(mounts)) != NULL )
{
int add_it = 0;
if (strcmp(ent->mnt_type, "iso9660") == 0)
add_it = 1;
May 21, 2002
May 21, 2002
96
97
/* add other mount types here */
Aug 7, 2001
Aug 7, 2001
98
99
if (add_it)
Sep 29, 2004
Sep 29, 2004
100
cb(data, ent->mnt_dir);
Aug 7, 2001
Aug 7, 2001
101
102
103
} /* while */
endmntent(mounts);
Mar 5, 2002
Mar 5, 2002
104
#endif
Apr 13, 2009
Apr 13, 2009
105
} /* __PHYSFS_platformDetectAvailableCDs */
Jul 7, 2001
Jul 7, 2001
106
107
May 24, 2002
May 24, 2002
108
109
/* this is in posix.c ... */
extern char *__PHYSFS_platformCopyEnvironmentVariable(const char *varname);
Jul 8, 2001
Jul 8, 2001
110
111
May 21, 2002
May 21, 2002
112
113
114
115
116
117
/*
* 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.
*
Mar 14, 2005
Mar 14, 2005
118
* (envr) will be scribbled over, and you are expected to allocator.Free() the
May 21, 2002
May 21, 2002
119
120
121
* return value when you're done with it.
*/
static char *findBinaryInPath(const char *bin, char *envr)
Jul 8, 2001
Jul 8, 2001
122
{
May 21, 2002
May 21, 2002
123
124
125
size_t alloc_size = 0;
char *exe = NULL;
char *start = envr;
Jul 8, 2001
Jul 8, 2001
126
127
char *ptr;
May 21, 2002
May 21, 2002
128
129
BAIL_IF_MACRO(bin == NULL, ERR_INVALID_ARGUMENT, NULL);
BAIL_IF_MACRO(envr == NULL, ERR_INVALID_ARGUMENT, NULL);
Jul 8, 2001
Jul 8, 2001
130
131
132
do
{
May 21, 2002
May 21, 2002
133
134
size_t size;
ptr = strchr(start, ':'); /* find next $PATH separator. */
Jul 8, 2001
Jul 8, 2001
135
136
137
if (ptr)
*ptr = '\0';
May 21, 2002
May 21, 2002
138
139
size = strlen(start) + strlen(bin) + 2;
if (size > alloc_size)
Jul 8, 2001
Jul 8, 2001
140
{
Mar 14, 2005
Mar 14, 2005
141
char *x = (char *) allocator.Realloc(exe, size);
May 21, 2002
May 21, 2002
142
143
144
if (x == NULL)
{
if (exe != NULL)
Mar 14, 2005
Mar 14, 2005
145
allocator.Free(exe);
May 21, 2002
May 21, 2002
146
147
148
149
150
BAIL_MACRO(ERR_OUT_OF_MEMORY, NULL);
} /* if */
alloc_size = size;
exe = x;
Jul 8, 2001
Jul 8, 2001
151
} /* if */
May 21, 2002
May 21, 2002
152
153
/* build full binary path... */
Jul 8, 2001
Jul 8, 2001
154
strcpy(exe, start);
Nov 30, 2002
Nov 30, 2002
155
if ((exe[0] == '\0') || (exe[strlen(exe) - 1] != '/'))
Aug 29, 2001
Aug 29, 2001
156
strcat(exe, "/");
May 21, 2002
May 21, 2002
157
158
159
strcat(exe, bin);
if (access(exe, X_OK) == 0) /* Exists as executable? We're done. */
Jul 8, 2001
Jul 8, 2001
160
{
May 21, 2002
May 21, 2002
161
162
163
strcpy(exe, start); /* i'm lazy. piss off. */
return(exe);
} /* if */
Jul 8, 2001
Jul 8, 2001
164
May 21, 2002
May 21, 2002
165
start = ptr + 1; /* start points to beginning of next element. */
Jul 8, 2001
Jul 8, 2001
166
167
} while (ptr != NULL);
May 21, 2002
May 21, 2002
168
if (exe != NULL)
Mar 14, 2005
Mar 14, 2005
169
allocator.Free(exe);
May 21, 2002
May 21, 2002
170
171
172
173
174
175
176
return(NULL); /* doesn't exist in path. */
} /* findBinaryInPath */
char *__PHYSFS_platformCalcBaseDir(const char *argv0)
{
Mar 19, 2007
Mar 19, 2007
177
178
179
180
181
182
183
184
const char *PROC_SELF_EXE = "/proc/self/exe";
char *retval = NULL;
char *envr = NULL;
struct stat stbuf;
/* fast path: default behaviour can handle this. */
if ( (argv0 != NULL) && (strchr(argv0, '/') != NULL) )
return(NULL); /* higher level will parse out real path from argv0. */
May 21, 2002
May 21, 2002
185
Mar 19, 2007
Mar 19, 2007
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/*
* 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.
*/
if ((lstat(PROC_SELF_EXE, &stbuf) != -1) && (S_ISLNK(stbuf.st_mode)))
{
const size_t len = stbuf.st_size;
char *buf = (char *) allocator.Malloc(len+1);
if (buf != NULL) /* if NULL, maybe you'll get lucky later. */
{
if (readlink(PROC_SELF_EXE, buf, len) != len)
allocator.Free(buf);
else
{
buf[len] = '\0'; /* readlink doesn't null-terminate. */
retval = buf; /* we're good to go. */
} /* else */
} /* if */
} /* if */
May 21, 2002
May 21, 2002
206
Mar 19, 2007
Mar 19, 2007
207
208
209
210
211
212
213
214
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 */
May 21, 2002
May 21, 2002
215
Jul 8, 2001
Jul 8, 2001
216
217
218
219
return(retval);
} /* __PHYSFS_platformCalcBaseDir */
Jul 16, 2001
Jul 16, 2001
220
221
222
223
224
225
226
char *__PHYSFS_platformRealPath(const char *path)
{
char resolved_path[MAXPATHLEN];
char *retval = NULL;
errno = 0;
BAIL_IF_MACRO(!realpath(path, resolved_path), strerror(errno), NULL);
Mar 14, 2005
Mar 14, 2005
227
retval = (char *) allocator.Malloc(strlen(resolved_path) + 1);
Jul 16, 2001
Jul 16, 2001
228
229
BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
strcpy(retval, resolved_path);
Jan 4, 2003
Jan 4, 2003
230
Jul 16, 2001
Jul 16, 2001
231
232
233
return(retval);
} /* __PHYSFS_platformRealPath */
Aug 23, 2001
Aug 23, 2001
234
Mar 21, 2007
Mar 21, 2007
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
char *__PHYSFS_platformCurrentDir(void)
{
/*
* This can't just do platformRealPath("."), since that would eventually
* just end up calling back into here.
*/
int allocSize = 0;
char *retval = NULL;
char *ptr;
do
{
allocSize += 100;
ptr = (char *) allocator.Realloc(retval, allocSize);
if (ptr == NULL)
{
if (retval != NULL)
allocator.Free(retval);
BAIL_MACRO(ERR_OUT_OF_MEMORY, NULL);
} /* if */
retval = ptr;
ptr = getcwd(retval, allocSize);
} while (ptr == NULL && errno == ERANGE);
if (ptr == NULL && errno)
{
/*
* getcwd() failed for some reason, for example current
* directory not existing.
*/
if (retval != NULL)
allocator.Free(retval);
BAIL_MACRO(ERR_NO_SUCH_FILE, NULL);
} /* if */
return(retval);
} /* __PHYSFS_platformCurrentDir */
Mar 20, 2007
Mar 20, 2007
276
277
278
279
280
281
int __PHYSFS_platformSetDefaultAllocator(PHYSFS_Allocator *a)
{
return(0); /* just use malloc() and friends. */
} /* __PHYSFS_platformSetDefaultAllocator */
Jul 25, 2002
Jul 25, 2002
282
#if (defined PHYSFS_NO_PTHREADS_SUPPORT)
Jul 25, 2002
Jul 25, 2002
283
284
285
286
287
288
289
290
291
PHYSFS_uint64 __PHYSFS_platformGetThreadID(void) { return(0x0001); }
void *__PHYSFS_platformCreateMutex(void) { return((void *) 0x0001); }
void __PHYSFS_platformDestroyMutex(void *mutex) {}
int __PHYSFS_platformGrabMutex(void *mutex) { return(1); }
void __PHYSFS_platformReleaseMutex(void *mutex) {}
#else
Apr 3, 2007
Apr 3, 2007
292
293
294
295
296
297
298
typedef struct
{
pthread_mutex_t mutex;
pthread_t owner;
PHYSFS_uint32 count;
} PthreadMutex;
Aug 9, 2002
Aug 9, 2002
299
300
301
302
303
304
305
306
307
308
309
310
311
312
/* Just in case; this is a panic value. */
#if ((!defined SIZEOF_INT) || (SIZEOF_INT <= 0))
# define SIZEOF_INT 4
#endif
#if (SIZEOF_INT == 4)
# define PHTREAD_TO_UI64(thr) ( (PHYSFS_uint64) ((PHYSFS_uint32) (thr)) )
#elif (SIZEOF_INT == 2)
# define PHTREAD_TO_UI64(thr) ( (PHYSFS_uint64) ((PHYSFS_uint16) (thr)) )
#elif (SIZEOF_INT == 1)
# define PHTREAD_TO_UI64(thr) ( (PHYSFS_uint64) ((PHYSFS_uint8) (thr)) )
#else
# define PHTREAD_TO_UI64(thr) ((PHYSFS_uint64) (thr))
#endif
Jul 25, 2002
Jul 25, 2002
313
Jul 25, 2002
Jul 25, 2002
314
315
PHYSFS_uint64 __PHYSFS_platformGetThreadID(void)
{
Jul 25, 2002
Jul 25, 2002
316
return(PHTREAD_TO_UI64(pthread_self()));
Jul 25, 2002
Jul 25, 2002
317
318
319
} /* __PHYSFS_platformGetThreadID */
Mar 30, 2002
Mar 30, 2002
320
321
322
void *__PHYSFS_platformCreateMutex(void)
{
int rc;
Apr 3, 2007
Apr 3, 2007
323
324
325
PthreadMutex *m = (PthreadMutex *) allocator.Malloc(sizeof (PthreadMutex));
BAIL_IF_MACRO(m == NULL, ERR_OUT_OF_MEMORY, NULL);
rc = pthread_mutex_init(&m->mutex, NULL);
Mar 30, 2002
Mar 30, 2002
326
327
if (rc != 0)
{
Mar 14, 2005
Mar 14, 2005
328
allocator.Free(m);
Mar 30, 2002
Mar 30, 2002
329
330
331
BAIL_MACRO(strerror(rc), NULL);
} /* if */
Apr 3, 2007
Apr 3, 2007
332
333
m->count = 0;
m->owner = (pthread_t) 0xDEADBEEF;
Mar 30, 2002
Mar 30, 2002
334
335
336
337
338
339
return((void *) m);
} /* __PHYSFS_platformCreateMutex */
void __PHYSFS_platformDestroyMutex(void *mutex)
{
Apr 3, 2007
Apr 3, 2007
340
341
342
343
344
345
346
PthreadMutex *m = (PthreadMutex *) mutex;
/* Destroying a locked mutex is a bug, but we'll try to be helpful. */
if ((m->owner == pthread_self()) && (m->count > 0))
pthread_mutex_unlock(&m->mutex);
pthread_mutex_destroy(&m->mutex);
Sep 20, 2005
Sep 20, 2005
347
allocator.Free(m);
Mar 30, 2002
Mar 30, 2002
348
349
350
351
352
} /* __PHYSFS_platformDestroyMutex */
int __PHYSFS_platformGrabMutex(void *mutex)
{
Apr 3, 2007
Apr 3, 2007
353
354
355
356
357
358
359
360
361
362
PthreadMutex *m = (PthreadMutex *) mutex;
pthread_t tid = pthread_self();
if (m->owner != tid)
{
if (pthread_mutex_lock(&m->mutex) != 0)
return(0);
m->owner = tid;
} /* if */
m->count++;
Sep 20, 2005
Sep 20, 2005
363
return(1);
Mar 30, 2002
Mar 30, 2002
364
365
366
367
368
} /* __PHYSFS_platformGrabMutex */
void __PHYSFS_platformReleaseMutex(void *mutex)
{
Apr 3, 2007
Apr 3, 2007
369
370
371
372
373
374
375
376
377
PthreadMutex *m = (PthreadMutex *) mutex;
if (m->owner == pthread_self())
{
if (--m->count == 0)
{
m->owner = (pthread_t) 0xDEADBEEF;
pthread_mutex_unlock(&m->mutex);
} /* if */
} /* if */
Mar 30, 2002
Mar 30, 2002
378
379
} /* __PHYSFS_platformReleaseMutex */
Jul 25, 2002
Jul 25, 2002
380
381
#endif /* !PHYSFS_NO_PTHREADS_SUPPORT */
Mar 11, 2007
Mar 11, 2007
382
#endif /* PHYSFS_PLATFORM_UNIX */
May 24, 2002
May 24, 2002
383
Jul 7, 2001
Jul 7, 2001
384
/* end of unix.c ... */