Skip to content

Latest commit

 

History

History
1535 lines (1261 loc) · 39.4 KB

platform_unix.c

File metadata and controls

1535 lines (1261 loc) · 39.4 KB
 
May 12, 2007
May 12, 2007
1
2
3
4
5
6
7
8
/**
* MojoSetup; a portable, flexible installation application.
*
* Please see the file LICENSE.txt in the source's root directory.
*
* This file written by Ryan C. Gordon.
*/
May 8, 2007
May 8, 2007
9
#if PLATFORM_UNIX
May 2, 2007
May 2, 2007
10
Dec 4, 2006
Dec 4, 2006
11
12
#if PLATFORM_MACOSX
#include <Carbon/Carbon.h>
Dec 9, 2006
Dec 9, 2006
13
14
#undef true
#undef false
Dec 4, 2006
Dec 4, 2006
15
16
#endif
Dec 8, 2006
Dec 8, 2006
17
#include <sys/time.h>
Mar 25, 2006
Mar 25, 2006
18
19
20
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/param.h>
Dec 13, 2006
Dec 13, 2006
21
#include <sys/utsname.h>
Apr 22, 2007
Apr 22, 2007
22
#include <sys/mount.h>
Apr 13, 2009
Apr 13, 2009
23
#include <stdio.h>
Dec 9, 2006
Dec 9, 2006
24
25
#include <time.h>
#include <unistd.h>
May 7, 2007
May 7, 2007
26
#include <signal.h>
May 9, 2007
May 9, 2007
27
#include <syslog.h>
Sep 20, 2007
Sep 20, 2007
28
#include <dirent.h>
Sep 20, 2007
Sep 20, 2007
29
#include <fcntl.h>
Feb 13, 2008
Feb 13, 2008
30
#include <sys/wait.h>
Apr 13, 2009
Apr 13, 2009
31
#include <limits.h>
Jun 9, 2010
Jun 9, 2010
32
#include <errno.h>
Apr 13, 2009
Apr 13, 2009
33
Apr 22, 2007
Apr 22, 2007
34
35
36
37
38
39
40
41
42
43
44
#if MOJOSETUP_HAVE_SYS_UCRED_H
# ifdef MOJOSETUP_HAVE_MNTENT_H
# undef MOJOSETUP_HAVE_MNTENT_H /* don't do both... */
# endif
# include <sys/ucred.h>
#endif
#if MOJOSETUP_HAVE_MNTENT_H
# include <mntent.h>
#endif
Apr 13, 2009
Apr 13, 2009
45
46
47
48
#if MOJOSETUP_HAVE_SYS_MNTTAB_H
# include <sys/mnttab.h>
#endif
Dec 13, 2006
Dec 13, 2006
49
#if PLATFORM_BEOS
Dec 13, 2006
Dec 13, 2006
50
51
52
53
#define DLOPEN_ARGS 0
void *beos_dlopen(const char *fname, int unused);
void *beos_dlsym(void *lib, const char *sym);
void beos_dlclose(void *lib);
May 7, 2007
May 7, 2007
54
void beos_usleep(unsigned long ticks);
Dec 13, 2006
Dec 13, 2006
55
56
57
#define dlopen beos_dlopen
#define dlsym beos_dlsym
#define dlclose beos_dlclose
May 7, 2007
May 7, 2007
58
#define usleep beos_usleep
Dec 13, 2006
Dec 13, 2006
59
60
61
62
#else
#include <dlfcn.h>
#define DLOPEN_ARGS (RTLD_NOW | RTLD_GLOBAL)
#endif
Dec 13, 2006
Dec 13, 2006
63
May 8, 2007
May 8, 2007
64
#include "platform.h"
May 17, 2007
May 17, 2007
65
#include "gui.h"
Jan 25, 2008
Jan 25, 2008
66
#include "fileio.h"
Mar 25, 2006
Mar 25, 2006
67
Dec 4, 2006
Dec 4, 2006
68
69
static struct timeval startup_time;
Nov 21, 2007
Nov 21, 2007
70
71
72
73
74
75
76
77
78
79
80
81
82
83
boolean MojoPlatform_istty(void)
{
static boolean already_checked = false; // this never changes in a run.
static boolean retval = false;
if (!already_checked)
{
retval = isatty(0) && isatty(1) ? true : false;
already_checked = true;
} // if
return retval;
} // MojoPlatform_istty
May 20, 2007
May 20, 2007
84
char *MojoPlatform_currentWorkingDir(void)
Dec 14, 2006
Dec 14, 2006
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
{
char *retval = NULL;
size_t len;
// loop a few times in case we don't have a large enough buffer.
for (len = 128; len <= (16*1024); len *= 2)
{
retval = (char *) xrealloc(retval, len);
if (getcwd(retval, len-1) != NULL)
{
size_t slen = strlen(retval);
if (retval[slen-1] != '/') // make sure this ends with '/' ...
{
retval[slen] = '/';
retval[slen+1] = '\0';
} // if
return retval;
} // if
} // for
free(retval);
return NULL;
May 20, 2007
May 20, 2007
107
} // MojoPlatform_currentWorkingDir
Dec 14, 2006
Dec 14, 2006
108
109
Jul 2, 2007
Jul 2, 2007
110
111
112
113
114
char *MojoPlatform_readlink(const char *linkname)
{
size_t alloclen = 16;
char *retval = NULL;
char *buf = NULL;
Apr 13, 2009
Apr 13, 2009
115
ssize_t len = -1;
Jul 2, 2007
Jul 2, 2007
116
117
118
119
120
121
122
123
124
do
{
alloclen *= 2;
buf = xrealloc(buf, alloclen);
len = readlink(linkname, buf, alloclen-1);
if ( (len != -1) && (len < (alloclen-1)) ) // !error && !overflow
{
buf[len] = '\0'; // readlink() doesn't null-terminate!
Apr 13, 2009
Apr 13, 2009
125
retval = xrealloc(buf, (size_t) (len+1)); // shrink it down.
Jul 2, 2007
Jul 2, 2007
126
} // if
Dec 10, 2011
Dec 10, 2011
127
} while (len >= (((ssize_t)alloclen)-1)); // loop if we need bigger buf.
Jul 2, 2007
Jul 2, 2007
128
129
130
131
132
return retval; // caller must free() this.
} // MojoPlatform_readlink
Dec 14, 2006
Dec 14, 2006
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
static void *guaranteeAllocation(void *ptr, size_t len, size_t *_alloclen)
{
void *retval = NULL;
size_t alloclen = *_alloclen;
if (alloclen > len)
return ptr;
if (!alloclen)
alloclen = 1;
while (alloclen <= len)
alloclen *= 2;
retval = xrealloc(ptr, alloclen);
if (retval != NULL)
*_alloclen = alloclen;
return retval;
} // guaranteeAllocation
// This is a mess, but I'm not sure it can be done more cleanly.
static char *realpathInternal(char *path, const char *cwd, int linkloop)
{
char *linkname = NULL;
char *retval = NULL;
size_t len = 0;
size_t alloclen = 0;
if (*path == '/') // absolute path.
{
retval = xstrdup("/");
path++;
len = 1;
} // if
else // relative path.
{
if (cwd != NULL)
retval = xstrdup(cwd);
else
{
May 20, 2007
May 20, 2007
174
if ((retval = MojoPlatform_currentWorkingDir()) == NULL)
Dec 14, 2006
Dec 14, 2006
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
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
return NULL;
} // else
len = strlen(retval);
} // else
while (true)
{
struct stat statbuf;
size_t newlen;
char *nextpath = strchr(path, '/');
if (nextpath != NULL)
*nextpath = '\0';
newlen = strlen(path);
retval = guaranteeAllocation(retval, len + newlen + 2, &alloclen);
strcpy(retval + len, path);
if (*path == '\0')
retval[--len] = '\0'; // chop ending "/" bit, it gets readded later.
else if (strcmp(path, ".") == 0)
{
retval[--len] = '\0'; // chop ending "/." bit
} // else if
else if (strcmp(path, "..") == 0)
{
char *ptr;
retval[--len] = '\0'; // chop ending "/.." bit
ptr = strrchr(retval, '/');
if ((ptr == NULL) || (ptr == retval))
{
strcpy(retval, "/");
len = 0;
} // if
else
{
*ptr = '\0';
len -= (size_t) ((retval+len)-ptr);
} // else
} // else if
// it may be a symlink...check it.
else if (lstat(retval, &statbuf) == -1)
goto realpathInternal_failed;
else if (S_ISLNK(statbuf.st_mode))
{
char *newresolve = NULL;
if (linkloop > 255)
goto realpathInternal_failed;
Jul 2, 2007
Jul 2, 2007
228
229
linkname = MojoPlatform_readlink(retval);
if (linkname == NULL)
Dec 14, 2006
Dec 14, 2006
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
goto realpathInternal_failed;
// chop off symlink name for its cwd.
retval[len] = '\0';
// resolve the link...
newresolve = realpathInternal(linkname, retval, linkloop + 1);
if (newresolve == NULL)
goto realpathInternal_failed;
len = strlen(newresolve);
retval = guaranteeAllocation(retval, len + 2, &alloclen);
strcpy(retval, newresolve);
free(newresolve);
free(linkname);
Jul 2, 2007
Jul 2, 2007
245
linkname = NULL;
Dec 14, 2006
Dec 14, 2006
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
276
277
278
279
280
281
282
283
284
285
286
287
288
} // else if
else
{
len += newlen;
} // else
if (nextpath == NULL)
break; // holy crap we're done!
else // append a '/' before the next path element.
{
path = nextpath + 1;
retval[len++] = '/';
retval[len] = '\0';
} // else
} // while
// Shrink string if we're using more memory than necessary...
if (alloclen > len+1)
retval = (char *) xrealloc(retval, len+1);
return retval;
realpathInternal_failed:
free(linkname);
free(retval);
return NULL;
} // realpathInternal
// Rolling my own realpath, even if the runtime has one, since apparently
// the spec is a little flakey, and it can overflow PATH_MAX. On BeOS <= 5,
// we'd have to resort to BPath to do this, too, and I'd rather avoid the C++
// dependencies and headers.
char *MojoPlatform_realpath(const char *_path)
{
char *path = xstrdup(_path);
char *retval = realpathInternal(path, NULL, 0);
free(path);
return retval;
} // MojoPlatform_realpath
Dec 2, 2006
Dec 2, 2006
289
290
291
292
293
294
295
296
297
298
// (Stolen from physicsfs: http://icculus.org/physfs/ ...)
/*
* See where program (bin) resides in the $PATH. Returns a copy of the first
* element in $PATH that contains it, or NULL if it doesn't exist or there
* were other problems.
*
* You are expected to free() the return value when you're done with it.
*/
static char *findBinaryInPath(const char *bin)
{
Dec 2, 2006
Dec 2, 2006
299
const char *_envr = getenv("PATH");
Oct 24, 2011
Oct 24, 2011
300
size_t alloc_size = 64;
Dec 2, 2006
Dec 2, 2006
301
char *envr = NULL;
Dec 2, 2006
Dec 2, 2006
302
char *exe = NULL;
Dec 2, 2006
Dec 2, 2006
303
char *start = NULL;
Dec 2, 2006
Dec 2, 2006
304
305
char *ptr = NULL;
Dec 2, 2006
Dec 2, 2006
306
if ((_envr == NULL) || (bin == NULL))
Dec 2, 2006
Dec 2, 2006
307
308
return NULL;
Apr 24, 2009
Apr 24, 2009
309
envr = xstrdup(_envr);
Oct 24, 2011
Oct 24, 2011
310
exe = (char *) xmalloc(alloc_size);
Dec 2, 2006
Dec 2, 2006
311
312
start = envr;
Dec 2, 2006
Dec 2, 2006
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
do
{
size_t size;
ptr = strchr(start, ':'); // find next $PATH separator.
if (ptr)
*ptr = '\0';
size = strlen(start) + strlen(bin) + 2;
if (size > alloc_size)
{
char *x = (char *) xrealloc(exe, size);
alloc_size = size;
exe = x;
} // if
// build full binary path...
strcpy(exe, start);
if ((exe[0] == '\0') || (exe[strlen(exe) - 1] != '/'))
strcat(exe, "/");
strcat(exe, bin);
if (access(exe, X_OK) == 0) // Exists as executable? We're done.
{
strcpy(exe, start); // i'm lazy. piss off.
Apr 25, 2009
Apr 25, 2009
337
free(envr);
Dec 2, 2006
Dec 2, 2006
338
339
340
341
342
343
return(exe);
} // if
start = ptr + 1; // start points to beginning of next element.
} while (ptr != NULL);
Oct 24, 2011
Oct 24, 2011
344
free(exe);
Apr 24, 2009
Apr 24, 2009
345
346
free(envr);
Oct 24, 2011
Oct 24, 2011
347
return NULL; // doesn't exist in path.
Dec 2, 2006
Dec 2, 2006
348
349
350
} // findBinaryInPath
Dec 9, 2006
Dec 9, 2006
351
char *MojoPlatform_appBinaryPath(void)
Mar 25, 2006
Mar 25, 2006
352
{
Dec 2, 2006
Dec 2, 2006
353
const char *argv0 = GArgv[0];
Dec 9, 2006
Dec 9, 2006
354
char *retval = NULL;
Mar 25, 2006
Mar 25, 2006
355
Jan 12, 2011
Jan 12, 2011
356
357
358
// !!! FIXME: try /proc/$PID/exe here?
if (strchr(argv0, '/') != NULL)
Dec 14, 2006
Dec 14, 2006
359
retval = MojoPlatform_realpath(argv0); // argv[0] contains a path?
Dec 2, 2006
Dec 2, 2006
360
361
else // slow path...have to search the whole $PATH for this one...
{
Dec 2, 2006
Dec 2, 2006
362
char *found = findBinaryInPath(argv0);
Dec 14, 2006
Dec 14, 2006
363
364
if (found)
retval = MojoPlatform_realpath(found);
Dec 2, 2006
Dec 2, 2006
365
366
free(found);
} // else
Mar 25, 2006
Mar 25, 2006
367
368
369
370
return retval;
} // MojoPlatform_appBinaryPath
Dec 3, 2006
Dec 3, 2006
371
May 10, 2007
May 10, 2007
372
373
374
375
376
377
378
char *MojoPlatform_homedir(void)
{
const char *envr = getenv("HOME");
return xstrdup(envr ? envr : "/");
} // MojoPlatform_homedir
Dec 3, 2006
Dec 3, 2006
379
// This implementation is a bit naive.
Jan 20, 2008
Jan 20, 2008
380
char *MojoPlatform_locale(void)
Dec 3, 2006
Dec 3, 2006
381
{
Jan 20, 2008
Jan 20, 2008
382
char *retval = NULL;
Jan 20, 2008
Jan 20, 2008
383
char *ptr = NULL;
Dec 3, 2006
Dec 3, 2006
384
385
386
const char *envr = getenv("LANG");
if (envr != NULL)
{
Jan 20, 2008
Jan 20, 2008
387
388
retval = xstrdup(envr);
ptr = strchr(retval, '.'); // chop off encoding if explicitly listed.
Dec 3, 2006
Dec 3, 2006
389
390
if (ptr != NULL)
*ptr = '\0';
Feb 29, 2008
Feb 29, 2008
391
392
393
ptr = strchr(retval, '@'); // chop off extra bits if explicitly listed.
if (ptr != NULL)
*ptr = '\0';
Dec 3, 2006
Dec 3, 2006
394
395
} // if
Feb 16, 2007
Feb 16, 2007
396
397
#if PLATFORM_MACOSX
else if (CFLocaleCreateCanonicalLocaleIdentifierFromString == NULL)
Jan 20, 2008
Jan 20, 2008
398
retval = NULL; // !!! FIXME: 10.2 compatibility?
Feb 16, 2007
Feb 16, 2007
399
400
401
402
403
404
405
406
407
408
409
410
411
else if (CFLocaleCreateCanonicalLocaleIdentifierFromString != NULL)
{
CFPropertyListRef languages = CFPreferencesCopyAppValue(
CFSTR("AppleLanguages"),
kCFPreferencesCurrentApplication);
if (languages != NULL)
{
CFStringRef primary = CFArrayGetValueAtIndex(languages, 0);
if (primary != NULL)
{
CFStringRef locale =
CFLocaleCreateCanonicalLocaleIdentifierFromString(
Feb 29, 2008
Feb 29, 2008
412
kCFAllocatorDefault, primary);
Feb 16, 2007
Feb 16, 2007
413
414
if (locale != NULL)
{
Jan 20, 2008
Jan 20, 2008
415
const CFIndex len = (CFStringGetLength(locale) + 1) * 6;
Jan 20, 2008
Jan 20, 2008
416
ptr = (char*) xmalloc(len);
Jan 20, 2008
Jan 20, 2008
417
CFStringGetCString(locale, ptr, len, kCFStringEncodingUTF8);
Feb 16, 2007
Feb 16, 2007
418
CFRelease(locale);
Jan 20, 2008
Jan 20, 2008
419
retval = xrealloc(ptr, strlen(ptr) + 1);
May 12, 2009
May 12, 2009
420
421
422
423
424
425
// !!! FIXME: this may not be 100% right, but change
// !!! FIXME: xx-YY to xx_YY (lang_country).
if (retval[2] == '-')
retval[2] = '_';
if (retval[3] == '-')
retval[3] = '_';
Feb 16, 2007
Feb 16, 2007
426
427
428
429
430
431
432
} // if
} // if
CFRelease(languages);
} // if
} // else if
#endif
Dec 3, 2006
Dec 3, 2006
433
434
435
return retval;
} // MojoPlatform_locale
Dec 4, 2006
Dec 4, 2006
436
Jan 20, 2008
Jan 20, 2008
437
char *MojoPlatform_osType(void)
Dec 4, 2006
Dec 4, 2006
438
439
{
#if PLATFORM_MACOSX
Jan 20, 2008
Jan 20, 2008
440
return xstrdup("macosx");
Dec 13, 2006
Dec 13, 2006
441
#elif PLATFORM_BEOS
Jan 20, 2008
Jan 20, 2008
442
return xstrdup("beos"); // !!! FIXME: zeta? haiku?
Dec 4, 2006
Dec 4, 2006
443
#elif defined(linux) || defined(__linux) || defined(__linux__)
Jan 20, 2008
Jan 20, 2008
444
return xstrdup("linux");
Dec 4, 2006
Dec 4, 2006
445
#elif defined(__FreeBSD__) || defined(__DragonFly__)
Jan 20, 2008
Jan 20, 2008
446
return xstrdup("freebsd");
Dec 4, 2006
Dec 4, 2006
447
#elif defined(__NetBSD__)
Jan 20, 2008
Jan 20, 2008
448
return xstrdup("netbsd");
Dec 4, 2006
Dec 4, 2006
449
#elif defined(__OpenBSD__)
Jan 20, 2008
Jan 20, 2008
450
return xstrdup("openbsd");
Dec 4, 2006
Dec 4, 2006
451
#elif defined(bsdi) || defined(__bsdi) || defined(__bsdi__)
Jan 20, 2008
Jan 20, 2008
452
return xstrdup("bsdi");
Dec 4, 2006
Dec 4, 2006
453
#elif defined(_AIX)
Jan 20, 2008
Jan 20, 2008
454
return xstrdup("aix");
Dec 4, 2006
Dec 4, 2006
455
#elif defined(hpux) || defined(__hpux) || defined(__hpux__)
Jan 20, 2008
Jan 20, 2008
456
return xstrdup("hpux");
Dec 4, 2006
Dec 4, 2006
457
#elif defined(sgi) || defined(__sgi) || defined(__sgi__) || defined(_SGI_SOURCE)
Jan 20, 2008
Jan 20, 2008
458
return xstrdup("irix");
Apr 13, 2009
Apr 13, 2009
459
460
#elif defined(sun)
return xstrdup("solaris");
Dec 4, 2006
Dec 4, 2006
461
462
#else
# error Please define your platform.
Jan 20, 2008
Jan 20, 2008
463
return NULL;
Apr 13, 2009
Apr 13, 2009
464
#endif
Dec 4, 2006
Dec 4, 2006
465
466
467
} // MojoPlatform_ostype
Dec 18, 2010
Dec 18, 2010
468
char *MojoPlatform_osVersion(void)
Dec 4, 2006
Dec 4, 2006
469
470
{
#if PLATFORM_MACOSX
Feb 28, 2010
Feb 28, 2010
471
SInt32 ver, major, minor, patch;
Aug 11, 2008
Aug 11, 2008
472
473
474
475
476
477
478
479
480
481
482
boolean convert = false;
char *buf = NULL;
char dummy = 0;
int len = 0;
if (Gestalt(gestaltSystemVersion, &ver) != noErr)
return NULL;
if (ver < 0x1030)
convert = true; // split (ver) into (major),(minor),(patch).
else
Dec 4, 2006
Dec 4, 2006
483
{
Aug 11, 2008
Aug 11, 2008
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
// presumably this won't fail. But if it does, we'll just use the
// original version value. This might cut the value--10.12.11 will
// come out to 10.9.9, for example--but it's better than nothing.
if (Gestalt(gestaltSystemVersionMajor, &major) != noErr)
convert = true;
if (Gestalt(gestaltSystemVersionMinor, &minor) != noErr)
convert = true;
if (Gestalt(gestaltSystemVersionBugFix, &patch) != noErr)
convert = true;
} /* else */
if (convert)
{
major = ((ver & 0xFF00) >> 8);
major = (((major / 16) * 10) + (major % 16));
minor = ((ver & 0xF0) >> 4);
patch = (ver & 0xF);
} /* if */
Feb 28, 2010
Feb 28, 2010
503
504
len = snprintf(&dummy, sizeof (dummy), "%d.%d.%d",
(int) major, (int) minor, (int) patch);
Aug 11, 2008
Aug 11, 2008
505
buf = (char *) xmalloc(len+1);
Feb 28, 2010
Feb 28, 2010
506
snprintf(buf, len+1, "%d.%d.%d", (int) major, (int) minor, (int) patch);
Aug 11, 2008
Aug 11, 2008
507
508
return buf;
Dec 13, 2006
Dec 13, 2006
509
510
511
512
513
514
#else
// This information may or may not actually MEAN anything. On BeOS, it's
// useful, but on other things, like Linux, it'll give you the kernel
// version, which doesn't necessarily help.
struct utsname un;
if (uname(&un) == 0)
Jan 20, 2008
Jan 20, 2008
515
return xstrdup(un.release);
Dec 4, 2006
Dec 4, 2006
516
517
#endif
Jan 20, 2008
Jan 20, 2008
518
return NULL;
Dec 4, 2006
Dec 4, 2006
519
520
} // MojoPlatform_osversion
Dec 4, 2006
Dec 4, 2006
521
Dec 18, 2010
Dec 18, 2010
522
523
524
525
526
527
528
529
530
char *MojoPlatform_osMachine(void)
{
struct utsname un;
if (uname(&un) == 0)
return xstrdup(un.machine);
return NULL;
} // MojoPlatform_osMachine
May 7, 2007
May 7, 2007
531
532
533
534
535
536
void MojoPlatform_sleep(uint32 ticks)
{
usleep(ticks * 1000);
} // MojoPlatform_sleep
Dec 4, 2006
Dec 4, 2006
537
538
539
540
541
542
543
544
545
546
547
uint32 MojoPlatform_ticks(void)
{
uint64 then_ms, now_ms;
struct timeval now;
gettimeofday(&now, NULL);
then_ms = (((uint64) startup_time.tv_sec) * 1000) +
(((uint64) startup_time.tv_usec) / 1000);
now_ms = (((uint64) now.tv_sec) * 1000) + (((uint64) now.tv_usec) / 1000);
return ((uint32) (now_ms - then_ms));
} // MojoPlatform_ticks
Dec 9, 2006
Dec 9, 2006
548
549
550
551
552
553
void MojoPlatform_die(void)
{
_exit(86);
} // MojoPlatform_die
Dec 9, 2006
Dec 9, 2006
554
555
556
boolean MojoPlatform_unlink(const char *fname)
{
May 7, 2007
May 7, 2007
557
558
boolean retval = false;
struct stat statbuf;
May 31, 2008
May 31, 2008
559
if (lstat(fname, &statbuf) != -1)
May 7, 2007
May 7, 2007
560
561
562
563
564
565
566
{
if (S_ISDIR(statbuf.st_mode))
retval = (rmdir(fname) == 0);
else
retval = (unlink(fname) == 0);
} // if
return retval;
Dec 9, 2006
Dec 9, 2006
567
568
569
} // MojoPlatform_unlink
May 1, 2007
May 1, 2007
570
571
572
573
574
575
boolean MojoPlatform_symlink(const char *src, const char *dst)
{
return (symlink(dst, src) == 0);
} // MojoPlatform_symlink
May 18, 2007
May 18, 2007
576
boolean MojoPlatform_mkdir(const char *path, uint16 perms)
May 1, 2007
May 1, 2007
577
578
{
// !!! FIXME: error if already exists?
May 18, 2007
May 18, 2007
579
return (mkdir(path, perms) == 0);
May 1, 2007
May 1, 2007
580
581
582
583
584
585
586
587
588
} // MojoPlatform_mkdir
boolean MojoPlatform_rename(const char *src, const char *dst)
{
return (rename(src, dst) == 0);
} // MojoPlatform_rename
Apr 22, 2007
Apr 22, 2007
589
590
591
592
593
594
595
596
boolean MojoPlatform_exists(const char *dir, const char *fname)
{
boolean retval = false;
if (fname == NULL)
retval = (access(dir, F_OK) != -1);
else
{
const size_t len = strlen(dir) + strlen(fname) + 2;
Jul 30, 2009
Jul 30, 2009
597
char *buf = (char *) xmalloc(len);
Apr 22, 2007
Apr 22, 2007
598
599
600
601
602
603
604
605
snprintf(buf, len, "%s/%s", dir, fname);
retval = (access(buf, F_OK) != -1);
free(buf);
} // else
return retval;
} // MojoPlatform_exists
May 10, 2007
May 10, 2007
606
607
608
609
610
611
612
613
614
615
boolean MojoPlatform_writable(const char *fname)
{
return (access(fname, W_OK) == 0);
} // MojoPlatform_writable
boolean MojoPlatform_isdir(const char *dir)
{
boolean retval = false;
struct stat statbuf;
Sep 20, 2007
Sep 20, 2007
616
if (lstat(dir, &statbuf) != -1)
May 10, 2007
May 10, 2007
617
618
619
620
621
622
623
624
{
if (S_ISDIR(statbuf.st_mode))
retval = true;
} // if
return retval;
} // MojoPlatform_isdir
Sep 20, 2007
Sep 20, 2007
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
boolean MojoPlatform_issymlink(const char *dir)
{
boolean retval = false;
struct stat statbuf;
if (lstat(dir, &statbuf) != -1)
{
if (S_ISLNK(statbuf.st_mode))
retval = true;
} // if
return retval;
} // MojoPlatform_issymlink
boolean MojoPlatform_isfile(const char *dir)
{
boolean retval = false;
struct stat statbuf;
if (lstat(dir, &statbuf) != -1)
{
if (S_ISREG(statbuf.st_mode))
retval = true;
} // if
return retval;
Sep 26, 2007
Sep 26, 2007
648
} // MojoPlatform_isfile
Sep 20, 2007
Sep 20, 2007
649
650
Jan 14, 2008
Jan 14, 2008
651
652
653
654
655
656
657
658
void *MojoPlatform_stdout(void)
{
int *retval = (int *) xmalloc(sizeof (int));
*retval = 1; // stdout.
return retval;
} // MojoPlatform_stdout
Sep 20, 2007
Sep 20, 2007
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
void *MojoPlatform_open(const char *fname, uint32 flags, uint16 mode)
{
void *retval = NULL;
int fd = -1;
int unixflags = 0;
if ((flags & MOJOFILE_READ) && (flags & MOJOFILE_WRITE))
unixflags |= O_RDWR;
else if (flags & MOJOFILE_READ)
unixflags |= O_RDONLY;
else if (flags & MOJOFILE_WRITE)
unixflags |= O_WRONLY;
else
return NULL; // have to specify SOMETHING.
if (flags & MOJOFILE_APPEND)
unixflags |= O_APPEND;
if (flags & MOJOFILE_TRUNCATE)
unixflags |= O_TRUNC;
if (flags & MOJOFILE_CREATE)
unixflags |= O_CREAT;
if (flags & MOJOFILE_EXCLUSIVE)
unixflags |= O_EXCL;
fd = open(fname, unixflags, (mode_t) mode);
if (fd != -1)
{
int *intptr = (int *) xmalloc(sizeof (int));
*intptr = fd;
retval = intptr;
} // if
return retval;
} // MojoPlatform_open
int64 MojoPlatform_read(void *fd, void *buf, uint32 bytes)
{
return (int64) read(*((int *) fd), buf, bytes);
} // MojoPlatform_read
int64 MojoPlatform_write(void *fd, const void *buf, uint32 bytes)
{
return (int64) write(*((int *) fd), buf, bytes);
} // MojoPlatform_write
int64 MojoPlatform_tell(void *fd)
{
return (int64) lseek(*((int *) fd), 0, SEEK_CUR);
} // MojoPlatform_tell
int64 MojoPlatform_seek(void *fd, int64 offset, MojoFileSeek whence)
{
int unixwhence;
switch (whence)
{
case MOJOSEEK_SET: unixwhence = SEEK_SET; break;
case MOJOSEEK_CURRENT: unixwhence = SEEK_CUR; break;
case MOJOSEEK_END: unixwhence = SEEK_END; break;
default: return -1; // !!! FIXME: maybe just abort?
} // switch
return (int64) lseek(*((int *) fd), offset, unixwhence);
} // MojoPlatform_seek
int64 MojoPlatform_flen(void *fd)
{
struct stat statbuf;
if (fstat(*((int *) fd), &statbuf) == -1)
return -1;
return((int64) statbuf.st_size);
} // MojoPlatform_flen
boolean MojoPlatform_flush(void *fd)
{
return (fsync(*((int *) fd)) == 0);
} // MojoPlatform_flush
boolean MojoPlatform_close(void *fd)
{
boolean retval = false;
Jan 14, 2008
Jan 14, 2008
746
747
748
749
750
751
752
753
754
755
int handle = *((int *) fd);
// don't close stdin, stdout, or stderr.
if ((handle == 0) || (handle == 1) || (handle == 2))
{
free(fd);
return true;
} // if
if (close(handle) == 0)
Sep 20, 2007
Sep 20, 2007
756
757
758
759
760
free(fd);
return retval;
} // MojoPlatform_close
Sep 20, 2007
Sep 20, 2007
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
void *MojoPlatform_opendir(const char *dirname)
{
return opendir(dirname);
} // MojoPlatform_opendir
char *MojoPlatform_readdir(void *_dirhandle)
{
DIR *dirhandle = (DIR *) _dirhandle;
struct dirent *dent = NULL;
while ((dent = readdir(dirhandle)) != NULL)
{
if (strcmp(dent->d_name, ".") == 0)
continue; // skip these.
else if (strcmp(dent->d_name, "..") == 0)
continue; // skip these, too.
else
break; // found a valid entry, go on.
} // while
return ((dent) ? xstrdup(dent->d_name) : NULL);
} // MojoPlatform_readdir
void MojoPlatform_closedir(void *dirhandle)
{
closedir((DIR *) dirhandle);
} // MojoPlatform_closedir
Sep 20, 2007
Sep 20, 2007
794
795
796
797
int64 MojoPlatform_filesize(const char *fname)
{
int retval = -1;
struct stat statbuf;
Sep 25, 2007
Sep 25, 2007
798
if ( (lstat(fname, &statbuf) != -1) && (S_ISREG(statbuf.st_mode)) )
Sep 20, 2007
Sep 20, 2007
799
800
801
802
803
retval = (int64) statbuf.st_size;
return retval;
} // MojoPlatform_filesize
May 3, 2007
May 3, 2007
804
805
806
807
808
809
810
811
812
813
814
815
816
boolean MojoPlatform_perms(const char *fname, uint16 *p)
{
boolean retval = false;
struct stat statbuf;
if (stat(fname, &statbuf) != -1)
{
*p = statbuf.st_mode;
retval = true;
} // if
return retval;
} // MojoPlatform_perms
May 18, 2007
May 18, 2007
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
uint16 MojoPlatform_defaultFilePerms(void)
{
return 0644;
} // MojoPlatform_defaultFilePerms
uint16 MojoPlatform_defaultDirPerms(void)
{
return 0755;
} // MojoPlatform_defaultDirPerms
uint16 MojoPlatform_makePermissions(const char *str, boolean *_valid)
{
uint16 retval = 0644;
boolean valid = true;
if (str != NULL)
{
char *endptr = NULL;
long strval = strtol(str, &endptr, 8);
// complete string was a valid number?
valid = ((*endptr == '\0') && (strval >= 0) && (strval <= 0xFFFF));
if (valid)
retval = (uint16) strval;
} // if
*_valid = valid;
return retval;
} // MojoPlatform_makePermissions
May 3, 2007
May 3, 2007
848
849
850
851
852
853
boolean MojoPlatform_chmod(const char *fname, uint16 p)
{
return (chmod(fname, p) != -1);
} // MojoPlatform_chmod
Apr 22, 2007
Apr 22, 2007
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
char *MojoPlatform_findMedia(const char *uniquefile)
{
#if MOJOSETUP_HAVE_SYS_UCRED_H
int i = 0;
struct statfs *mntbufp = NULL;
int mounts = getmntinfo(&mntbufp, MNT_WAIT);
for (i = 0; i < mounts; i++)
{
const char *mnt = mntbufp[i].f_mntonname;
if (MojoPlatform_exists(mnt, uniquefile))
return xstrdup(mnt);
} // for
#elif MOJOSETUP_HAVE_MNTENT_H
FILE *mounts = setmntent("/etc/mtab", "r");
if (mounts != NULL)
{
struct mntent *ent = NULL;
while ((ent = getmntent(mounts)) != NULL)
{
const char *mnt = ent->mnt_dir;
if (MojoPlatform_exists(mnt, uniquefile))
{
endmntent(mounts);
return xstrdup(mnt);
} // if
} // while
endmntent(mounts);
} // if
Apr 13, 2009
Apr 13, 2009
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
#elif MOJOSETUP_HAVE_SYS_MNTTAB_H
FILE *mounts = fopen(MNTTAB, "r");
if (mounts != NULL)
{
struct mnttab ent;
while (getmntent(mounts, &ent) == 0)
{
const char *mnt = ent.mnt_mountp;
if (MojoPlatform_exists(mnt, uniquefile))
{
fclose(mounts);
return xstrdup(mnt);
} // if
} // while
fclose(mounts);
} // if
Apr 22, 2007
Apr 22, 2007
901
902
903
904
905
906
907
908
#else
# warning No mountpoint detection on this platform...
#endif
return NULL;
} // MojoPlatform_findMedia
Dec 10, 2006
Dec 10, 2006
909
910
void MojoPlatform_log(const char *str)
{
May 9, 2007
May 9, 2007
911
syslog(LOG_USER | LOG_INFO, "%s", str);
May 17, 2007
May 17, 2007
912
913
914
915
916
917
918
#if PLATFORM_MACOSX
// put to stdout too, if this isn't the stdio UI.
// This will let the info show up in /Applications/Utilities/Console.app
if ((GGui != NULL) && (strcmp(GGui->name(), "stdio") != 0))
printf("%s\n", str);
#endif
Dec 10, 2006
Dec 10, 2006
919
920
} // MojoPlatform_log
Dec 9, 2006
Dec 9, 2006
921
Dec 9, 2006
Dec 9, 2006
922
923
static boolean testTmpDir(const char *dname, char *buf,
size_t len, const char *tmpl)
Dec 9, 2006
Dec 9, 2006
924
{
Dec 9, 2006
Dec 9, 2006
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
boolean retval = false;
if ( (dname != NULL) && (access(dname, R_OK | W_OK | X_OK) == 0) )
{
struct stat statbuf;
if ( (stat(dname, &statbuf) == 0) && (S_ISDIR(statbuf.st_mode)) )
{
const size_t rc = snprintf(buf, len, "%s/%s", dname, tmpl);
if (rc < len)
retval = true;
} // if
} // if
return retval;
} // testTmpDir
void *MojoPlatform_dlopen(const uint8 *img, size_t len)
{
// Write the image to a temporary file, dlopen() it, and delete it
// immediately. The inode will be kept around by the Unix kernel until
// we either dlclose() it or the process terminates, but we don't have
// to worry about polluting the /tmp directory or cleaning this up later.
Jan 14, 2007
Jan 14, 2007
947
948
949
// We'll try every reasonable temp directory location until we find one
// that works, in case (say) one lets us write a file, but there
// isn't enough space for the data.
Dec 9, 2006
Dec 9, 2006
950
Jan 14, 2007
Jan 14, 2007
951
952
// /dev/shm may be able to avoid writing to physical media...try it first.
const char *dirs[] = { "/dev/shm", getenv("TMPDIR"), P_tmpdir, "/tmp" };
Sep 26, 2007
Sep 26, 2007
953
const char *tmpl = "mojosetup-plugin-XXXXXX";
Dec 9, 2006
Dec 9, 2006
954
char fname[PATH_MAX];
Jan 14, 2007
Jan 14, 2007
955
956
void *retval = NULL;
int i = 0;
Dec 13, 2006
Dec 13, 2006
957
Jun 1, 2012
Jun 1, 2012
958
959
960
#if PLATFORM_MACOSX
if (dlopen == NULL) return NULL; // weak symbol on older Mac OS X
#endif
Dec 13, 2006
Dec 13, 2006
961
Jan 14, 2007
Jan 14, 2007
962
963
964
965
966
#ifndef P_tmpdir // glibc defines this, maybe others.
#define P_tmpdir NULL
#endif
for (i = 0; (i < STATICARRAYLEN(dirs)) && (retval == NULL); i++)
Dec 9, 2006
Dec 9, 2006
967
{
Jul 30, 2009
Jul 30, 2009
968
if (testTmpDir(dirs[i], fname, sizeof (fname), tmpl))
Dec 9, 2006
Dec 9, 2006
969
{
Jan 14, 2007
Jan 14, 2007
970
971
972
973
974
975
976
977
978
const int fd = mkstemp(fname);
if (fd != -1)
{
const size_t bw = write(fd, img, len);
const int rc = close(fd);
if ((bw == len) && (rc != -1))
retval = dlopen(fname, DLOPEN_ARGS);
unlink(fname);
} // if
Dec 9, 2006
Dec 9, 2006
979
} // if
Jan 14, 2007
Jan 14, 2007
980
} // for
Dec 9, 2006
Dec 9, 2006
981
982
return retval;
Dec 9, 2006
Dec 9, 2006
983
984
985
986
987
} // MojoPlatform_dlopen
void *MojoPlatform_dlsym(void *lib, const char *sym)
{
Apr 21, 2007
Apr 21, 2007
988
989
990
991
#if PLATFORM_MACOSX
if (dlsym == NULL) return NULL; // weak symbol on older Mac OS X
#endif
Dec 9, 2006
Dec 9, 2006
992
993
994
995
996
997
return dlsym(lib, sym);
} // MojoPlatform_dlsym
void MojoPlatform_dlclose(void *lib)
{
Apr 21, 2007
Apr 21, 2007
998
999
1000
#if PLATFORM_MACOSX
if (dlclose == NULL) return; // weak symbol on older Mac OS X
#endif