Skip to content

Latest commit

 

History

History
456 lines (367 loc) · 12.6 KB

unix.c

File metadata and controls

456 lines (367 loc) · 12.6 KB
 
Jul 7, 2001
Jul 7, 2001
1
2
3
4
5
6
7
8
/*
* Unix support routines for PhysicsFS.
*
* Please see the file LICENSE in the source's root directory.
*
* This file written by Ryan C. Gordon.
*/
May 10, 2002
May 10, 2002
9
10
11
12
#if HAVE_CONFIG_H
# include <config.h>
#endif
Jul 10, 2002
Jul 10, 2002
13
14
/* BeOS uses beos.cpp and posix.c ... Cygwin and such use win32.c ... */
#if ((!defined __BEOS__) && (!defined WIN32))
Jun 29, 2002
Jun 29, 2002
15
Jul 7, 2001
Jul 7, 2001
16
17
#include <stdio.h>
#include <stdlib.h>
Jul 8, 2001
Jul 8, 2001
18
19
20
21
22
23
#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
24
#include <sys/param.h>
Jul 8, 2001
Jul 8, 2001
25
26
27
#include <dirent.h>
#include <time.h>
#include <errno.h>
Jul 20, 2002
Jul 20, 2002
28
#include <sys/mount.h>
Mar 5, 2002
Mar 5, 2002
29
Jul 25, 2002
Jul 25, 2002
30
31
32
33
#if (!defined PHYSFS_NO_PTHREADS_SUPPORT)
#include <pthread.h>
#endif
Jul 20, 2002
Jul 20, 2002
34
35
36
37
38
#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
39
#endif
Jul 7, 2001
Jul 7, 2001
40
Jul 20, 2002
Jul 20, 2002
41
42
43
#ifdef PHYSFS_HAVE_MNTENT_H
#include <mntent.h>
#endif
Apr 6, 2002
Apr 6, 2002
44
Jul 7, 2001
Jul 7, 2001
45
46
47
48
49
50
#define __PHYSICSFS_INTERNAL__
#include "physfs_internal.h"
const char *__PHYSFS_platformDirSeparator = "/";
Mar 5, 2002
Mar 5, 2002
51
Mar 24, 2002
Mar 24, 2002
52
53
54
55
56
57
58
59
60
61
62
63
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
64
65
66
67
68
69
70
71
72
73
74
75
76
#ifdef PHYSFS_NO_CDROM_SUPPORT
/* Stub version for platforms without CD-ROM support. */
char **__PHYSFS_platformDetectAvailableCDs(void)
{
char **retval = (char **) malloc(sizeof (char *));
BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
*retval = NULL;
return(retval);
} /* __PHYSFS_platformDetectAvailableCDs */
#else
Mar 24, 2002
Mar 24, 2002
77
Jul 20, 2002
Jul 20, 2002
78
#ifdef PHYSFS_HAVE_SYS_UCRED_H
Mar 5, 2002
Mar 5, 2002
79
80
81
82
83
char **__PHYSFS_platformDetectAvailableCDs(void)
{
char **retval = (char **) malloc(sizeof (char *));
int cd_count = 1; /* We count the NULL entry. */
Jul 25, 2002
Jul 25, 2002
84
struct statfs *mntbufp = NULL;
Mar 5, 2002
Mar 5, 2002
85
int mounts;
Jul 25, 2002
Jul 25, 2002
86
int i;
Mar 5, 2002
Mar 5, 2002
87
Apr 5, 2002
Apr 5, 2002
88
89
BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
Jul 25, 2002
Jul 25, 2002
90
mounts = getmntinfo(&mntbufp, MNT_WAIT);
Mar 5, 2002
Mar 5, 2002
91
Jul 25, 2002
Jul 25, 2002
92
93
for (i = 0; i < mounts; i++)
{
Mar 5, 2002
Mar 5, 2002
94
95
int add_it = 0;
Jul 25, 2002
Jul 25, 2002
96
if (strcmp(mntbufp[i].f_fstypename, "iso9660") == 0)
Mar 5, 2002
Mar 5, 2002
97
add_it = 1;
Jul 25, 2002
Jul 25, 2002
98
else if (strcmp( mntbufp[i].f_fstypename, "cd9660") == 0)
Apr 6, 2002
Apr 6, 2002
99
add_it = 1;
May 21, 2002
May 21, 2002
100
101
/* add other mount types here */
Mar 5, 2002
Mar 5, 2002
102
103
104
if (add_it)
{
Jun 29, 2002
Jun 29, 2002
105
char **tmp = realloc(retval, sizeof (char *) * (cd_count + 1));
Mar 5, 2002
Mar 5, 2002
106
107
108
if (tmp)
{
retval = tmp;
Jun 29, 2002
Jun 29, 2002
109
retval[cd_count - 1] = (char *)
Jul 25, 2002
Jul 25, 2002
110
malloc(strlen(mntbufp[i].f_mntonname) + 1);
Jun 29, 2002
Jun 29, 2002
111
if (retval[cd_count - 1])
Mar 5, 2002
Mar 5, 2002
112
{
Jul 25, 2002
Jul 25, 2002
113
strcpy(retval[cd_count - 1], mntbufp[i].f_mntonname);
Mar 5, 2002
Mar 5, 2002
114
115
116
117
cd_count++;
} /* if */
} /* if */
} /* if */
Jul 25, 2002
Jul 25, 2002
118
} /* for */
Mar 5, 2002
Mar 5, 2002
119
120
121
122
123
retval[cd_count - 1] = NULL;
return(retval);
} /* __PHYSFS_platformDetectAvailableCDs */
Jul 20, 2002
Jul 20, 2002
124
#endif
Mar 5, 2002
Mar 5, 2002
125
126
Jul 20, 2002
Jul 20, 2002
127
#ifdef PHYSFS_HAVE_MNTENT_H
Mar 5, 2002
Mar 5, 2002
128
Jul 7, 2001
Jul 7, 2001
129
130
char **__PHYSFS_platformDetectAvailableCDs(void)
{
Aug 7, 2001
Aug 7, 2001
131
132
133
134
135
char **retval = (char **) malloc(sizeof (char *));
int cd_count = 1; /* We count the NULL entry. */
FILE *mounts = NULL;
struct mntent *ent = NULL;
Apr 5, 2002
Apr 5, 2002
136
137
BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
Aug 7, 2001
Aug 7, 2001
138
139
140
141
142
143
144
145
146
*retval = NULL;
mounts = setmntent("/etc/mtab", "r");
BAIL_IF_MACRO(mounts == NULL, ERR_IO_ERROR, retval);
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
147
148
/* add other mount types here */
Aug 7, 2001
Aug 7, 2001
149
150
151
if (add_it)
{
Jun 29, 2002
Jun 29, 2002
152
char **tmp = realloc(retval, sizeof (char *) * (cd_count + 1));
Aug 7, 2001
Aug 7, 2001
153
154
155
156
if (tmp)
{
retval = tmp;
retval[cd_count-1] = (char *) malloc(strlen(ent->mnt_dir) + 1);
Jun 29, 2002
Jun 29, 2002
157
if (retval[cd_count - 1])
Aug 7, 2001
Aug 7, 2001
158
{
Jun 29, 2002
Jun 29, 2002
159
strcpy(retval[cd_count - 1], ent->mnt_dir);
Aug 7, 2001
Aug 7, 2001
160
161
162
163
164
165
166
cd_count++;
} /* if */
} /* if */
} /* if */
} /* while */
endmntent(mounts);
Jul 8, 2001
Jul 8, 2001
167
Aug 7, 2001
Aug 7, 2001
168
retval[cd_count - 1] = NULL;
Jul 8, 2001
Jul 8, 2001
169
return(retval);
Mar 5, 2002
Mar 5, 2002
170
171
172
} /* __PHYSFS_platformDetectAvailableCDs */
#endif
Jul 7, 2001
Jul 7, 2001
173
Jul 25, 2002
Jul 25, 2002
174
175
#endif /* !PHYSFS_NO_CDROM_SUPPORT */
Jul 7, 2001
Jul 7, 2001
176
May 24, 2002
May 24, 2002
177
178
/* this is in posix.c ... */
extern char *__PHYSFS_platformCopyEnvironmentVariable(const char *varname);
Jul 8, 2001
Jul 8, 2001
179
180
May 21, 2002
May 21, 2002
181
182
183
184
185
186
187
188
189
190
/*
* 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.
*
* (envr) will be scribbled over, and you are expected to free() the
* return value when you're done with it.
*/
static char *findBinaryInPath(const char *bin, char *envr)
Jul 8, 2001
Jul 8, 2001
191
{
May 21, 2002
May 21, 2002
192
193
194
size_t alloc_size = 0;
char *exe = NULL;
char *start = envr;
Jul 8, 2001
Jul 8, 2001
195
196
char *ptr;
May 21, 2002
May 21, 2002
197
198
BAIL_IF_MACRO(bin == NULL, ERR_INVALID_ARGUMENT, NULL);
BAIL_IF_MACRO(envr == NULL, ERR_INVALID_ARGUMENT, NULL);
Jul 8, 2001
Jul 8, 2001
199
200
201
do
{
May 21, 2002
May 21, 2002
202
203
size_t size;
ptr = strchr(start, ':'); /* find next $PATH separator. */
Jul 8, 2001
Jul 8, 2001
204
205
206
if (ptr)
*ptr = '\0';
May 21, 2002
May 21, 2002
207
208
size = strlen(start) + strlen(bin) + 2;
if (size > alloc_size)
Jul 8, 2001
Jul 8, 2001
209
{
May 21, 2002
May 21, 2002
210
211
212
213
214
215
216
217
218
219
char *x = (char *) realloc(exe, size);
if (x == NULL)
{
if (exe != NULL)
free(exe);
BAIL_MACRO(ERR_OUT_OF_MEMORY, NULL);
} /* if */
alloc_size = size;
exe = x;
Jul 8, 2001
Jul 8, 2001
220
} /* if */
May 21, 2002
May 21, 2002
221
222
/* build full binary path... */
Jul 8, 2001
Jul 8, 2001
223
strcpy(exe, start);
Nov 30, 2002
Nov 30, 2002
224
if ((exe[0] == '\0') || (exe[strlen(exe) - 1] != '/'))
Aug 29, 2001
Aug 29, 2001
225
strcat(exe, "/");
May 21, 2002
May 21, 2002
226
227
228
strcat(exe, bin);
if (access(exe, X_OK) == 0) /* Exists as executable? We're done. */
Jul 8, 2001
Jul 8, 2001
229
{
May 21, 2002
May 21, 2002
230
231
232
strcpy(exe, start); /* i'm lazy. piss off. */
return(exe);
} /* if */
Jul 8, 2001
Jul 8, 2001
233
May 21, 2002
May 21, 2002
234
start = ptr + 1; /* start points to beginning of next element. */
Jul 8, 2001
Jul 8, 2001
235
236
} while (ptr != NULL);
May 21, 2002
May 21, 2002
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
if (exe != NULL)
free(exe);
return(NULL); /* doesn't exist in path. */
} /* findBinaryInPath */
char *__PHYSFS_platformCalcBaseDir(const char *argv0)
{
/* If there isn't a path on argv0, then look through the $PATH for it. */
char *retval;
char *envr;
if (strchr(argv0, '/') != NULL) /* default behaviour can handle this. */
return(NULL);
May 24, 2002
May 24, 2002
254
envr = __PHYSFS_platformCopyEnvironmentVariable("PATH");
May 21, 2002
May 21, 2002
255
256
BAIL_IF_MACRO(!envr, NULL, NULL);
retval = findBinaryInPath(argv0, envr);
Jul 8, 2001
Jul 8, 2001
257
258
259
260
261
free(envr);
return(retval);
} /* __PHYSFS_platformCalcBaseDir */
Jul 8, 2001
Jul 8, 2001
262
263
264
/* Much like my college days, try to sleep for 10 milliseconds at a time... */
void __PHYSFS_platformTimeslice(void)
{
Mar 5, 2002
Mar 5, 2002
265
usleep( 10 * 1000 ); /* don't care if it fails. */
Jul 8, 2001
Jul 8, 2001
266
267
268
} /* __PHYSFS_platformTimeslice */
Jan 4, 2003
Jan 4, 2003
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
#if defined(__MACH__) && defined(__APPLE__)
/*
* This function is only for OSX. The problem is that Apple's applications
* can actually be directory structures with the actual executable nested
* several levels down. PhysFS computes the base directory from the Unix
* executable, but this may not be the correct directory. Apple tries to
* hide everything from the user, so from Finder, the user never sees the
* Unix executable, and the directory package (bundle) is considered the
* "executable". This means that the correct base directory is at the
* level where the directory structure starts.
* A typical bundle seems to look like this:
* MyApp.app/ <-- top level...this is what the user sees in Finder
* Contents/
* MacOS/
* MyApp <-- the actual executable
*
* Since anything below the app folder is considered hidden, most
* application files need to be at the top level if you intend to
* write portable software. Thus if the application resides in:
* /Applications/MyProgram
* and the executable is the bundle MyApp.app,
* PhysFS computes the following as the base directory:
* /Applications/MyProgram/MyApp.app/Contents/MacOS/
* We need to strip off the MyApp.app/Contents/MacOS/
*
* However, there are corner cases. OSX applications can be traditional
* Unix executables without the bundle. Also, it is not entirely clear
* to me what kinds of permutations bundle structures can have.
*
* For now, this is a temporary hack until a better solution
* can be made. This function will try to find a "/Contents/MacOS"
* inside the path. If it succeeds, then the path will be truncated
* to correct the directory. If it is not found, the path will be
* left alone and will presume it is a traditional Unix execuatable.
* Most programs also include the .app extention in the top level
* folder, but it doesn't seem to be a requirement (Acrobat doesn't
* have it). MacOS looks like it can also be MacOSClassic.
* This function will test for MacOS and hope it captures any
* other permutations.
*/
static void stripAppleBundle(char *path)
{
char *sub_str = "/contents/macos";
char *found_ptr = NULL;
char *tempbuf = NULL;
int i;
/* Calloc will place the \0 character in the proper place for us */
tempbuf = (char*)calloc( (strlen(path)+1), sizeof(char) );
/* Unlike other Unix filesystems, HFS is case insensitive
* It wouldn be nice to use strcasestr, but it doesn't seem
* to be available in the OSX gcc library right now.
* So we should make a lower case copy of the path to
* compare against
*/
for(i=0; i<strlen(path); i++)
{
/* convert to lower case */
tempbuf[i] = tolower(path[i]);
}
/* See if we can find "/contents/macos" in the path */
found_ptr = strstr(tempbuf, sub_str);
if(NULL == found_ptr)
{
/* It doesn't look like a bundle so we can keep the
* original path. Just return */
free(tempbuf);
return;
}
/* We have a bundle, so let's backstep character by character
* to erase the extra parts of the path. Quit when we hit
* the preceding '/' character.
*/
for(i=strlen(path)-strlen(found_ptr)-1; i>=0; i--)
{
if('/' == path[i])
{
break;
}
}
/* Safety check */
if(i<1)
{
/* This probably shouldn't happen. */
path[0] = '\0';
}
else
{
/* Back up one more to remove trailing '/' and set the '\0' */
path[i] = '\0';
}
free(tempbuf);
return;
}
#endif /* defined __MACH__ && defined __APPLE__ */
Jul 16, 2001
Jul 16, 2001
366
367
368
369
370
371
372
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);
May 24, 2002
May 24, 2002
373
retval = (char *) malloc(strlen(resolved_path) + 1);
Jul 16, 2001
Jul 16, 2001
374
375
BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
strcpy(retval, resolved_path);
Jan 4, 2003
Jan 4, 2003
376
377
378
379
380
#if defined(__MACH__) && defined(__APPLE__)
stripAppleBundle(retval);
#endif /* defined __MACH__ && defined __APPLE__ */
Jul 16, 2001
Jul 16, 2001
381
382
383
return(retval);
} /* __PHYSFS_platformRealPath */
Aug 23, 2001
Aug 23, 2001
384
Jul 25, 2002
Jul 25, 2002
385
#if (defined PHYSFS_NO_PTHREADS_SUPPORT)
Jul 25, 2002
Jul 25, 2002
386
387
388
389
390
391
392
393
394
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
Aug 9, 2002
Aug 9, 2002
395
396
397
398
399
400
401
402
403
404
405
406
407
408
/* 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
409
Jul 25, 2002
Jul 25, 2002
410
411
PHYSFS_uint64 __PHYSFS_platformGetThreadID(void)
{
Jul 25, 2002
Jul 25, 2002
412
return(PHTREAD_TO_UI64(pthread_self()));
Jul 25, 2002
Jul 25, 2002
413
414
415
} /* __PHYSFS_platformGetThreadID */
Mar 30, 2002
Mar 30, 2002
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
void *__PHYSFS_platformCreateMutex(void)
{
int rc;
pthread_mutex_t *m = (pthread_mutex_t *) malloc(sizeof (pthread_mutex_t));
BAIL_IF_MACRO(m == NULL, ERR_OUT_OF_MEMORY, NULL);
rc = pthread_mutex_init(m, NULL);
if (rc != 0)
{
free(m);
BAIL_MACRO(strerror(rc), NULL);
} /* if */
return((void *) m);
} /* __PHYSFS_platformCreateMutex */
void __PHYSFS_platformDestroyMutex(void *mutex)
{
pthread_mutex_destroy((pthread_mutex_t *) mutex);
free(mutex);
} /* __PHYSFS_platformDestroyMutex */
int __PHYSFS_platformGrabMutex(void *mutex)
{
return(pthread_mutex_lock((pthread_mutex_t *) mutex) == 0);
} /* __PHYSFS_platformGrabMutex */
void __PHYSFS_platformReleaseMutex(void *mutex)
{
pthread_mutex_unlock((pthread_mutex_t *) mutex);
} /* __PHYSFS_platformReleaseMutex */
Jul 25, 2002
Jul 25, 2002
450
451
452
#endif /* !PHYSFS_NO_PTHREADS_SUPPORT */
Jul 11, 2002
Jul 11, 2002
453
#endif /* !defined __BEOS__ && !defined WIN32 */
May 24, 2002
May 24, 2002
454
Jul 7, 2001
Jul 7, 2001
455
/* end of unix.c ... */