This repository has been archived by the owner on Jul 6, 2021. It is now read-only.
forked from nevali/opencflite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCFPlatform.c
558 lines (509 loc) · 19.1 KB
/
CFPlatform.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
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
174
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
228
229
230
231
232
233
234
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
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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
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
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
/*
* Copyright (c) 2008-2009 Brent Fulgham <[email protected]>. All rights reserved.
* Copyright (c) 2009 Grant Erickson <[email protected]>. All rights reserved.
*
* This source code is a modified version of the CoreFoundation sources released by Apple Inc. under
* the terms of the APSL version 2.0 (see below).
*
* For information about changes from the original Apple source release can be found by reviewing the
* source control system for the project at https://sourceforge.net/svn/?group_id=246198.
*
* The original license information is as follows:
*
* Copyright (c) 2008 Apple Inc. All rights reserved.
*
* @APPLE_LICENSE_HEADER_START@
*
* This file contains Original Code and/or Modifications of Original Code
* as defined in and that are subject to the Apple Public Source License
* Version 2.0 (the 'License'). You may not use this file except in
* compliance with the License. Please obtain a copy of the License at
* http://www.opensource.apple.com/apsl/ and read it before using this
* file.
*
* The Original Code and all software distributed under the License are
* distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
* EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
* INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
* Please see the License for the specific language governing rights and
* limitations under the License.
*
* @APPLE_LICENSE_HEADER_END@
*/
/* CFPlatform.c
Copyright 1999-2002, Apple, Inc. All rights reserved.
Responsibility: Christopher Kane
*/
#include "CFInternal.h"
#include "CFPriv.h"
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#if DEPLOYMENT_TARGET_WINDOWS
/* In typical fragile fashion, order of include on Windows is important */
#include <io.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <windows.h>
#include <tchar.h>
#ifndef _WIN32_IE
#define _WIN32_IE 0x0600
#endif
#define strdup _strdup
#include <shlobj.h>
extern size_t strlcpy(char *dst, const char *src, size_t siz);
extern size_t strlcat(char *dst, const char *src, size_t siz);
#ifndef SHGFP_TYPE_CURRENT
#define SHGFP_TYPE_CURRENT 0
#endif
#else
#include <sys/stat.h>
#include <unistd.h>
#include <pwd.h>
#include <stdio.h>
#endif
#if DEPLOYMENT_TARGET_MACOSX
#include <mach-o/dyld.h>
#include <crt_externs.h>
#endif
#if DEPLOYMENT_TARGET_MACOSX
#define kCFPlatformInterfaceStringEncoding kCFStringEncodingUTF8
#else
#define kCFPlatformInterfaceStringEncoding CFStringGetSystemEncoding()
#endif
static CFStringRef _CFUserName(void);
#if DEPLOYMENT_TARGET_MACOSX
// CoreGraphics and LaunchServices are only projects (1 Dec 2006) that use these
char **_CFArgv(void) { return *_NSGetArgv(); }
int _CFArgc(void) { return *_NSGetArgc(); }
#endif
__private_extern__ Boolean _CFGetCurrentDirectory(char *path, int maxlen) {
#if DEPLOYMENT_TARGET_WINDOWS || 0
DWORD len = GetCurrentDirectoryA(maxlen, path);
return ((0 != len) && (maxlen > 0) && (len + 1 <= (DWORD)maxlen));
#else
return getcwd(path, maxlen) != NULL;
#endif
}
static Boolean __CFIsCFM = false;
// If called super early, we just return false
__private_extern__ Boolean _CFIsCFM(void) {
return __CFIsCFM;
}
#if DEPLOYMENT_TARGET_WINDOWS || 0
#define PATH_SEP '\\'
#else
#define PATH_SEP '/'
#endif
#if DEPLOYMENT_TARGET_WINDOWS
#define PATH_LIST_SEP ';'
#else
#define PATH_LIST_SEP ':'
#endif
static char *_CFSearchForNameInPath(const char *name, char *path) {
struct stat statbuf;
#if DEPLOYMENT_TARGET_WINDOWS && !defined(__GNUC__)
char nname[MAX_PATH + 1];
#else
char nname[strlen(name) + strlen(path) + 2];
#endif
#if !DEPLOYMENT_TARGET_WINDOWS
int no_hang_fd = open("/dev/autofs_nowait", 0);
#endif
for (;;) {
char *p = (char *)strchr(path, PATH_LIST_SEP);
if (NULL != p) {
*p = '\0';
}
nname[0] = '\0';
strlcat(nname, path, sizeof(nname));
strlcat(nname, "/", sizeof(nname));
strlcat(nname, name, sizeof(nname));
// Could also do access(us, X_OK) == 0 in next condition,
// for executable-only searching
if (0 == stat(nname, &statbuf) && (statbuf.st_mode & S_IFMT) == S_IFREG) {
if (p != NULL) {
*p = PATH_LIST_SEP;
}
#if !DEPLOYMENT_TARGET_WINDOWS
close(no_hang_fd);
#endif
return strdup(nname);
}
if (NULL == p) {
break;
}
*p = PATH_LIST_SEP;
path = p + 1;
}
#if !DEPLOYMENT_TARGET_WINDOWS
close(no_hang_fd);
#endif
return NULL;
}
#if DEPLOYMENT_TARGET_WINDOWS
// Returns the path to the CF DLL, which we can then use to find resources like char sets
__private_extern__ const char* _CFDLLPath(void) {
static char cachedPath[MAX_PATH+1] = "";
if ('\0' == cachedPath[0]) {
#if defined(DEBUG)
char* DLLFileName = "CoreFoundation_debug";
#elif defined(PROFILE)
char* DLLFileName = "CoreFoundation_profile";
#else
char* DLLFileName = "CoreFoundation";
#endif
HMODULE ourModule = GetModuleHandleA(DLLFileName);
CFAssert(ourModule, __kCFLogAssertion, "GetModuleHandle failed");
DWORD wResult = GetModuleFileNameA(ourModule, cachedPath, MAX_PATH+1);
CFAssert1(wResult > 0, __kCFLogAssertion, "GetModuleFileName failed: %d", GetLastError());
CFAssert1(wResult < MAX_PATH+1, __kCFLogAssertion, "GetModuleFileName result truncated: %s", cachedPath);
// strip off last component, the DLL name
CFIndex idx;
for (idx = wResult - 1; idx; idx--) {
if ('\\' == cachedPath[idx]) {
cachedPath[idx] = '\0';
break;
}
}
}
return cachedPath;
}
#endif
static const char *__CFProcessPath = NULL;
static const char *__CFprogname = NULL;
const char **_CFGetProgname(void) {
if (!__CFprogname)
_CFProcessPath(); // sets up __CFprogname as a side-effect
return &__CFprogname;
}
const char **_CFGetProcessPath(void) {
if (!__CFProcessPath)
_CFProcessPath(); // sets up __CFProcessPath as a side-effect
return &__CFProcessPath;
}
const char *_CFProcessPath(void) {
if (__CFProcessPath) return __CFProcessPath;
char *thePath = NULL;
#if DEPLOYMENT_TARGET_MACOSX || DEPLOYMENT_TARGET_LINUX
if (!_CFIsSetUgid()) {
#else
if (!__CFProcessPath) {
#endif
thePath = getenv("CFProcessPath");
if (thePath) {
int len = (int)strlen(thePath);
__CFProcessPath = (const char *)CFAllocatorAllocate(kCFAllocatorSystemDefault, len+1, 0);
if (__CFOASafe) __CFSetLastAllocationEventName((void *)__CFProcessPath, "CFUtilities (process-path)");
memmove((char *)__CFProcessPath, thePath, len + 1);
}
}
#if DEPLOYMENT_TARGET_WINDOWS
if (!__CFProcessPath) {
char buf[CFMaxPathSize] = {0};
HINSTANCE hinst = GetModuleHandle(NULL);
DWORD rlen = hinst ? GetModuleFileNameA(hinst, buf, CFMaxPathSize) : 0;
thePath = rlen ? buf : NULL;
#elif DEPLOYMENT_TARGET_MACOSX
int execIndex = 0;
if (!__CFProcessPath && NULL != (*_NSGetArgv())[execIndex]) {
int no_hang_fd = open("/dev/autofs_nowait", 0);
char buf[CFMaxPathSize] = {0};
struct stat statbuf;
const char *arg0 = (*_NSGetArgv())[execIndex];
if (arg0[0] == '/') {
// We've got an absolute path; look no further;
thePath = (char *)arg0;
} else {
char *theList = getenv("PATH");
if (NULL != theList && NULL == strrchr(arg0, '/')) {
thePath = _CFSearchForNameInPath(arg0, theList);
if (thePath) {
// User could have "." or "../bin" or other relative path in $PATH
if (('/' != thePath[0]) && _CFGetCurrentDirectory(buf, CFMaxPathSize)) {
strlcat(buf, "/", sizeof(buf));
strlcat(buf, thePath, sizeof(buf));
if (0 == stat(buf, &statbuf)) {
free(thePath);
thePath = buf;
}
}
if (thePath != buf) {
strlcpy(buf, thePath, sizeof(buf));
free((void *)thePath);
thePath = buf;
}
}
}
}
// After attempting a search through $PATH, if existant,
// try prepending the current directory to argv[0].
if (!thePath && _CFGetCurrentDirectory(buf, CFMaxPathSize)) {
if (buf[strlen(buf)-1] != '/') {
strlcat(buf, "/", sizeof(buf));
}
strlcat(buf, arg0, CFMaxPathSize);
if (0 == stat(buf, &statbuf)) {
thePath = buf;
}
}
if (thePath) {
// We are going to process the buffer replacing all "/./" and "//" with "/"
CFIndex srcIndex = 0, dstIndex = 0;
CFIndex len = strlen(thePath);
for (srcIndex=0; srcIndex<len; srcIndex++) {
thePath[dstIndex] = thePath[srcIndex];
dstIndex++;
while (srcIndex < len-1 && thePath[srcIndex] == '/' && (thePath[srcIndex+1] == '/' || (thePath[srcIndex+1] == '.' && srcIndex < len-2 && thePath[srcIndex+2] == '/'))) srcIndex += (thePath[srcIndex+1] == '/' ? 1 : 2);
}
thePath[dstIndex] = 0;
}
if (!thePath) {
thePath = (*_NSGetArgv())[execIndex];
}
#elif DEPLOYMENT_TARGET_LINUX
if (!__CFProcessPath) {
pid_t pid = getpid();
char buf[2][CFMaxPathSize] = {{0}, {0}};
int n;
n = snprintf(buf[0], CFMaxPathSize, "/proc/%u/exe", pid);
if (n > 0) {
n = readlink(buf[0], buf[1], CFMaxPathLength);
if (n > 0 && n <= CFMaxPathLength) {
buf[1][n] = '\0';
thePath = buf[1];
}
}
#else
#error "Don't know how to compute the process path for this platform."
#endif
if (thePath) {
int len = (int)strlen(thePath);
__CFProcessPath = (const char *)CFAllocatorAllocate(kCFAllocatorSystemDefault, len + 1, 0);
if (__CFOASafe) __CFSetLastAllocationEventName((void *)__CFProcessPath, "CFUtilities (process-path)");
memmove((char *)__CFProcessPath, thePath, len + 1);
}
if (__CFProcessPath) {
const char *p = 0;
int i;
for (i = 0; __CFProcessPath[i] != 0; i++){
if (__CFProcessPath[i] == PATH_SEP)
p = __CFProcessPath + i;
}
if (p != 0)
__CFprogname = p + 1;
else
__CFprogname = __CFProcessPath;
}
#if DEPLOYMENT_TARGET_MACOSX
close(no_hang_fd);
#endif
}
if (!__CFProcessPath) {
__CFProcessPath = "";
__CFprogname = __CFProcessPath;
} else {
const char *p = 0;
int i;
for (i = 0; __CFProcessPath[i] != 0; i++){
if (__CFProcessPath[i] == PATH_SEP)
p = __CFProcessPath + i;
}
if (p != 0)
__CFprogname = p + 1;
else
__CFprogname = __CFProcessPath;
}
return __CFProcessPath;
}
__private_extern__ CFStringRef _CFProcessNameString(void) {
static CFStringRef __CFProcessNameString = NULL;
if (!__CFProcessNameString) {
const char *processName = *_CFGetProgname();
if (!processName) processName = "";
__CFProcessNameString = CFStringCreateWithCString(__CFGetDefaultAllocator(), processName, kCFPlatformInterfaceStringEncoding);
}
return __CFProcessNameString;
}
static CFStringRef __CFUserName = NULL;
#if (DEPLOYMENT_TARGET_MACOSX) || defined(__svr4__) || defined(__hpux__) || DEPLOYMENT_TARGET_LINUX || DEPLOYMENT_TARGET_FREEBSD
static CFURLRef __CFHomeDirectory = NULL;
static uint32_t __CFEUID = -1;
static uint32_t __CFUID = -1;
static CFURLRef _CFCopyHomeDirURLForUser(struct passwd *upwd) {
CFURLRef home = NULL;
if (!_CFIsSetUgid()) {
const char *path = getenv("CFFIXED_USER_HOME");
if (path) {
home = CFURLCreateFromFileSystemRepresentation(kCFAllocatorSystemDefault, (uint8_t *)path, strlen(path), true);
}
}
if (!home) {
if (upwd && upwd->pw_dir) {
home = CFURLCreateFromFileSystemRepresentation(kCFAllocatorSystemDefault, (uint8_t *)upwd->pw_dir, strlen(upwd->pw_dir), true);
}
}
return home;
}
static void _CFUpdateUserInfo(void) {
struct passwd *upwd;
__CFEUID = geteuid();
__CFUID = getuid();
if (__CFHomeDirectory) CFRelease(__CFHomeDirectory);
__CFHomeDirectory = NULL;
if (__CFUserName) CFRelease(__CFUserName);
__CFUserName = NULL;
upwd = getpwuid(__CFEUID ? __CFEUID : __CFUID);
__CFHomeDirectory = _CFCopyHomeDirURLForUser(upwd);
if (!__CFHomeDirectory) {
const char *cpath = getenv("HOME");
if (cpath) {
__CFHomeDirectory = CFURLCreateFromFileSystemRepresentation(kCFAllocatorSystemDefault, (uint8_t *)cpath, strlen(cpath), true);
}
}
// This implies that UserManager stores directory info in CString
// rather than FileSystemRep. Perhaps this is wrong & we should
// expect NeXTSTEP encodings. A great test of our localized system would
// be to have a user "O-umlat z e r". XXX
if (upwd && upwd->pw_name) {
__CFUserName = CFStringCreateWithCString(kCFAllocatorSystemDefault, upwd->pw_name, kCFPlatformInterfaceStringEncoding);
} else {
const char *cuser = getenv("USER");
if (cuser)
__CFUserName = CFStringCreateWithCString(kCFAllocatorSystemDefault, cuser, kCFPlatformInterfaceStringEncoding);
}
}
#endif
static CFURLRef _CFCreateHomeDirectoryURLForUser(CFStringRef uName) {
#if (DEPLOYMENT_TARGET_MACOSX) || defined(__svr4__) || defined(__hpux__) || DEPLOYMENT_TARGET_LINUX || DEPLOYMENT_TARGET_FREEBSD
if (!uName) {
if (geteuid() != __CFEUID || getuid() != __CFUID || !__CFHomeDirectory)
_CFUpdateUserInfo();
if (__CFHomeDirectory) CFRetain(__CFHomeDirectory);
return __CFHomeDirectory;
} else {
struct passwd *upwd = NULL;
char buf[128], *user;
SInt32 len = CFStringGetLength(uName), size = CFStringGetMaximumSizeForEncoding(len, kCFPlatformInterfaceStringEncoding);
CFIndex usedSize;
if (size < 127) {
user = buf;
} else {
user = (char*)CFAllocatorAllocate(kCFAllocatorSystemDefault, size+1, 0);
if (__CFOASafe) __CFSetLastAllocationEventName(user, "CFUtilities (temp)");
}
if (CFStringGetBytes(uName, CFRangeMake(0, len), kCFPlatformInterfaceStringEncoding, 0, true, (uint8_t *)user, size, &usedSize) == len) {
user[usedSize] = '\0';
upwd = getpwnam(user);
}
if (buf != user) {
CFAllocatorDeallocate(kCFAllocatorSystemDefault, user);
}
return _CFCopyHomeDirURLForUser(upwd);
}
#elif DEPLOYMENT_TARGET_WINDOWS
CFStringRef user = !uName ? _CFUserName() : uName;
CFURLRef home = NULL;
if (!uName || CFEqual(user, _CFUserName())) {
const char *cpath = getenv("HOMEPATH");
const char *cdrive = getenv("HOMEDRIVE");
if (cdrive && cpath) {
char fullPath[CFMaxPathSize];
CFStringRef str;
strlcpy(fullPath, cdrive, sizeof(fullPath));
strlcat(fullPath, cpath, sizeof(fullPath));
str = CFStringCreateWithCString(kCFAllocatorSystemDefault, fullPath, kCFPlatformInterfaceStringEncoding);
home = CFURLCreateWithFileSystemPath(kCFAllocatorSystemDefault, str, kCFURLWindowsPathStyle, true);
CFRelease(str);
}
}
if (home == NULL) {
UniChar pathChars[MAX_PATH];
if (S_OK == SHGetFolderPath(NULL, CSIDL_PROFILE, NULL, SHGFP_TYPE_CURRENT, (LPWSTR) pathChars)) {
UniChar* p = pathChars;
CFIndex len = 0;
CFStringRef str;
while (*p++ != 0)
++len;
str = CFStringCreateWithCharacters(kCFAllocatorSystemDefault, pathChars, len);
home = CFURLCreateWithFileSystemPath(kCFAllocatorSystemDefault, str, kCFURLWindowsPathStyle, true);
CFRelease(str);
} else {
// We have to get "some" directory location, so fall-back to the
// processes current directory.
UniChar currDir[MAX_PATH];
DWORD dwChars = GetCurrentDirectory(MAX_PATH + 1, (LPWSTR)currDir);
if (dwChars > 0) {
UniChar* p = currDir;
CFIndex len = 0;
CFStringRef str;
while (*p++ != 0)
++len;
str = CFStringCreateWithCharacters(kCFAllocatorSystemDefault, currDir, len);
home = CFURLCreateWithFileSystemPath(kCFAllocatorSystemDefault, str, kCFURLWindowsPathStyle, true);
}
}
}
// We could do more here (as in KB Article Q101507). If that article is to
// be believed, we should only run into this case on Win95, or through
// user error.
if (home) {
CFStringRef str = CFURLCopyFileSystemPath(home, kCFURLWindowsPathStyle);
if (str && CFStringGetLength(str) == 0) {
CFRelease(home);
home=NULL;
}
if (str) CFRelease(str);
}
return home;
#else
#error Dont know how to compute users home directories on this platform
#endif
}
static CFStringRef _CFUserName(void) {
#if (DEPLOYMENT_TARGET_MACOSX) || defined(__svr4__) || defined(__hpux__) || DEPLOYMENT_TARGET_LINUX || DEPLOYMENT_TARGET_FREEBSD
if (geteuid() != __CFEUID || getuid() != __CFUID)
_CFUpdateUserInfo();
#elif DEPLOYMENT_TARGET_WINDOWS
if (!__CFUserName) {
char username[1040];
DWORD size = 1040;
username[0] = 0;
if (GetUserNameA(username, &size)) {
__CFUserName = CFStringCreateWithCString(kCFAllocatorSystemDefault, username, kCFPlatformInterfaceStringEncoding);
} else {
const char *cname = getenv("USERNAME");
if (cname)
__CFUserName = CFStringCreateWithCString(kCFAllocatorSystemDefault, cname, kCFPlatformInterfaceStringEncoding);
}
}
#else
#error Dont know how to compute user name on this platform
#endif
if (!__CFUserName)
__CFUserName = (CFStringRef)CFRetain(CFSTR(""));
return __CFUserName;
}
__private_extern__ CFStringRef _CFGetUserName(void) {
return CFStringCreateCopy(kCFAllocatorSystemDefault, _CFUserName());
}
#define CFMaxHostNameLength 256
#define CFMaxHostNameSize (CFMaxHostNameLength+1)
__private_extern__ CFStringRef _CFStringCreateHostName(void) {
char myName[CFMaxHostNameSize];
// return @"" instead of nil a la CFUserName() and Ali Ozer
if (0 != gethostname(myName, CFMaxHostNameSize)) myName[0] = '\0';
return CFStringCreateWithCString(kCFAllocatorSystemDefault, myName, kCFPlatformInterfaceStringEncoding);
}
/* These are sanitized versions of the above functions. We might want to eliminate the above ones someday.
These can return NULL.
*/
CF_EXPORT CFStringRef CFGetUserName(void) {
return _CFUserName();
}
CF_EXPORT CFURLRef CFCopyHomeDirectoryURLForUser(CFStringRef uName) {
return _CFCreateHomeDirectoryURLForUser(uName);
}
#undef CFMaxHostNameLength
#undef CFMaxHostNameSize