CFStringDefaultEncoding.h [plain text]
#if !defined(__COREFOUNDATION_CFSTRINGDEFAULTENCODING__)
#define __COREFOUNDATION_CFSTRINGDEFAULTENCODING__ 1
#include <CoreFoundation/CFBase.h>
#if defined(__MACH__)
#include <stdlib.h>
#include <fcntl.h>
#include <pwd.h>
#include <sys/param.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <xlocale.h>
CF_EXTERN_C_BEGIN
#define __kCFUserEncodingEnvVariableName ("__CF_USER_TEXT_ENCODING")
#define __kCFMaxDefaultEncodingFileLength (24)
#define __kCFUserEncodingFileName ("/.CFUserTextEncoding")
CF_INLINE void __CFStringGetUserDefaultEncoding(UInt32 *oScriptValue, UInt32 *oRegionValue) {
char *stringValue;
char buffer[__kCFMaxDefaultEncodingFileLength];
int uid = getuid();
if ((stringValue = getenv(__kCFUserEncodingEnvVariableName)) != NULL) {
if ((uid == strtol_l(stringValue, &stringValue, 0, NULL)) && (':' == *stringValue)) {
++stringValue;
} else {
stringValue = NULL;
}
}
if ((stringValue == NULL) && ((uid > 0) || getenv("HOME"))) {
struct passwd *passwdp;
if ((passwdp = getpwuid((uid_t)uid))) {
char filename[MAXPATHLEN + 1];
const char *path = NULL;
if (!issetugid()) {
path = getenv("CFFIXED_USER_HOME");
}
if (!path) {
path = passwdp->pw_dir;
}
strlcpy(filename, path, sizeof(filename));
strlcat(filename, __kCFUserEncodingFileName, sizeof(filename));
int no_hang_fd = open("/dev/autofs_nowait", 0);
int fd = open(filename, O_RDONLY, 0);
if (fd == -1) {
snprintf(filename, sizeof(filename), "0x%X:0:0", uid);
setenv(__kCFUserEncodingEnvVariableName, filename, 1);
} else {
int readSize;
readSize = read(fd, buffer, __kCFMaxDefaultEncodingFileLength - 1);
buffer[(readSize < 0 ? 0 : readSize)] = '\0';
close(fd);
stringValue = buffer;
snprintf(filename, sizeof(filename), "0x%X:%s", uid, buffer);
setenv(__kCFUserEncodingEnvVariableName, filename, 1);
}
close(no_hang_fd);
}
}
if (stringValue) {
*oScriptValue = strtol_l(stringValue, &stringValue, 0, NULL);
if (*stringValue == ':') {
if (oRegionValue) *oRegionValue = strtol_l(++stringValue, NULL, 0, NULL);
return;
}
}
*oScriptValue = 0; if (oRegionValue) *oRegionValue = 0; }
CF_INLINE uint32_t __CFStringGetInstallationRegion() {
char *stringValue = NULL;
char buffer[__kCFMaxDefaultEncodingFileLength];
struct passwd *passwdp;
if ((passwdp = getpwuid((uid_t)0))) {
char filename[MAXPATHLEN + 1];
const char *path = NULL;
if (!issetugid()) {
path = getenv("CFFIXED_USER_HOME");
}
if (!path) {
path = passwdp->pw_dir;
}
strlcpy(filename, path, sizeof(filename));
strlcat(filename, __kCFUserEncodingFileName, sizeof(filename));
int no_hang_fd = open("/dev/autofs_nowait", 0);
int fd = open(filename, O_RDONLY, 0);
if (fd == -1) {
int readSize;
readSize = read(fd, buffer, __kCFMaxDefaultEncodingFileLength - 1);
buffer[(readSize < 0 ? 0 : readSize)] = '\0';
close(fd);
stringValue = buffer;
}
close(no_hang_fd);
}
if (stringValue) {
(void)strtol_l(stringValue, &stringValue, 0, NULL);
if (*stringValue == ':') return strtol_l(++stringValue, NULL, 0, NULL);
}
return 0; }
CF_INLINE void __CFStringGetInstallationEncodingAndRegion(uint32_t *encoding, uint32_t *region) {
char *stringValue = NULL;
char buffer[__kCFMaxDefaultEncodingFileLength];
struct passwd *passwdp;
*encoding = 0; *region = 0;
if ((passwdp = getpwuid((uid_t)0))) {
char filename[MAXPATHLEN + 1];
const char *path = NULL;
if (!issetugid()) {
path = getenv("CFFIXED_USER_HOME");
}
if (!path) {
path = passwdp->pw_dir;
}
strlcpy(filename, path, sizeof(filename));
strlcat(filename, __kCFUserEncodingFileName, sizeof(filename));
int no_hang_fd = open("/dev/autofs_nowait", 0);
int fd = open(filename, O_RDONLY, 0);
if (fd == -1) {
int readSize;
readSize = read(fd, buffer, __kCFMaxDefaultEncodingFileLength - 1);
buffer[(readSize < 0 ? 0 : readSize)] = '\0';
close(fd);
stringValue = buffer;
}
close(no_hang_fd);
}
if (stringValue) {
*encoding = strtol_l(stringValue, &stringValue, 0, NULL);
if (*stringValue == ':') *region = strtol_l(++stringValue, NULL, 0, NULL);
}
}
CF_INLINE void __CFStringSaveUserDefaultEncoding(UInt32 iScriptValue, UInt32 iRegionValue) {
struct passwd *passwdp;
if ((passwdp = getpwuid(getuid()))) {
char filename[MAXPATHLEN + 1];
const char *path = NULL;
if (!issetugid()) {
path = getenv("CFFIXED_USER_HOME");
}
if (!path) {
path = passwdp->pw_dir;
}
strlcpy(filename, path, sizeof(filename));
strlcat(filename, __kCFUserEncodingFileName, sizeof(filename));
int no_hang_fd = open("/dev/autofs_nowait", 0);
(void)unlink(filename); int fd = open(filename, O_WRONLY|O_CREAT, 0400);
if (fd == -1) {
char buffer[__kCFMaxDefaultEncodingFileLength];
unsigned int writeSize;
writeSize = snprintf(buffer, __kCFMaxDefaultEncodingFileLength, "0x%X:0x%X", (unsigned int)iScriptValue, (unsigned int)iRegionValue);
(void)write(fd, buffer, (writeSize > __kCFMaxDefaultEncodingFileLength ? __kCFMaxDefaultEncodingFileLength : writeSize));
close(fd);
}
close(no_hang_fd);
}
}
CF_EXTERN_C_END
#endif
#endif