summaryrefslogtreecommitdiffstats
path: root/unix/tclUnixCompat.c
diff options
context:
space:
mode:
Diffstat (limited to 'unix/tclUnixCompat.c')
-rw-r--r--unix/tclUnixCompat.c133
1 files changed, 60 insertions, 73 deletions
diff --git a/unix/tclUnixCompat.c b/unix/tclUnixCompat.c
index 06ce031..3d2b7b8 100644
--- a/unix/tclUnixCompat.c
+++ b/unix/tclUnixCompat.c
@@ -36,10 +36,10 @@
* 'length' stay aligned.
*/
-#define PadBuffer(buffer, length, size) \
- if (((length) % (size))) { \
- (buffer) += ((size) - ((length) % (size))); \
- (length) += ((size) - ((length) % (size))); \
+#define PadBuffer(buffer, length, size) \
+ if (((length) % (size))) { \
+ (buffer) += ((size) - ((length) % (size))); \
+ (length) += ((size) - ((length) % (size))); \
}
/*
@@ -49,31 +49,28 @@
#ifdef TCL_THREADS
-#define STANDARD_BUFFER_SIZE 2048
-#define INITIAL_PWGR_BUFFER_SIZE 1024
-
typedef struct ThreadSpecificData {
struct passwd pwd;
#if defined(HAVE_GETPWNAM_R_5) || defined(HAVE_GETPWUID_R_5)
#define NEED_PW_CLEANER 1
char *pbuf;
- size_t pbuflen;
+ int pbuflen;
#else
- char pbuf[STANDARD_BUFFER_SIZE];
+ char pbuf[2048];
#endif
struct group grp;
#if defined(HAVE_GETGRNAM_R_5) || defined(HAVE_GETGRGID_R_5)
#define NEED_GR_CLEANER 1
char *gbuf;
- size_t gbuflen;
+ int gbuflen;
#else
- char gbuf[STANDARD_BUFFER_SIZE];
+ char gbuf[2048];
#endif
#if !defined(HAVE_MTSAFE_GETHOSTBYNAME) || !defined(HAVE_MTSAFE_GETHOSTBYADDR)
struct hostent hent;
- char hbuf[STANDARD_BUFFER_SIZE];
+ char hbuf[2048];
#endif
} ThreadSpecificData;
static Tcl_ThreadDataKey dataKey;
@@ -104,21 +101,19 @@ static Tcl_Mutex compatLock;
#if !defined(HAVE_GETGRNAM_R_5) && !defined(HAVE_GETGRNAM_R_4)
#define NEED_COPYGRP 1
-static int CopyGrp(struct group *tgtPtr, char *buf,
- size_t buflen);
+static int CopyGrp(struct group *tgtPtr, char *buf, int buflen);
#endif
#if !defined(HAVE_GETPWNAM_R_5) && !defined(HAVE_GETPWNAM_R_4)
#define NEED_COPYPWD 1
-static int CopyPwd(struct passwd *tgtPtr, char *buf,
- size_t buflen);
+static int CopyPwd(struct passwd *tgtPtr, char *buf, int buflen);
#endif
-static ssize_t CopyArray(char **src, int elsize, char *buf,
- size_t buflen);
+static int CopyArray(char **src, int elsize, char *buf,
+ int buflen);
static int CopyHostent(struct hostent *tgtPtr, char *buf,
- size_t buflen);
-static ssize_t CopyString(const char *src, char *buf, size_t buflen);
+ int buflen);
+static int CopyString(const char *src, char *buf, int buflen);
#endif
@@ -202,12 +197,9 @@ TclpGetPwNam(
*/
if (tsdPtr->pbuf == NULL) {
- long sizeMax = sysconf(_SC_GETPW_R_SIZE_MAX);
-
- if (sizeMax < 1) {
- tsdPtr->pbuflen = INITIAL_PWGR_BUFFER_SIZE;
- } else {
- tsdPtr->pbuflen = (size_t) sizeMax;
+ tsdPtr->pbuflen = (int) sysconf(_SC_GETPW_R_SIZE_MAX);
+ if (tsdPtr->pbuflen < 1) {
+ tsdPtr->pbuflen = 1024;
}
tsdPtr->pbuf = ckalloc(tsdPtr->pbuflen);
Tcl_CreateThreadExitHandler(FreePwBuf, NULL);
@@ -285,12 +277,9 @@ TclpGetPwUid(
*/
if (tsdPtr->pbuf == NULL) {
- long sizeMax = sysconf(_SC_GETPW_R_SIZE_MAX);
-
- if (sizeMax < 1) {
- tsdPtr->pbuflen = INITIAL_PWGR_BUFFER_SIZE;
- } else {
- tsdPtr->pbuflen = (size_t) sizeMax;
+ tsdPtr->pbuflen = (int) sysconf(_SC_GETPW_R_SIZE_MAX);
+ if (tsdPtr->pbuflen < 1) {
+ tsdPtr->pbuflen = 1024;
}
tsdPtr->pbuf = ckalloc(tsdPtr->pbuflen);
Tcl_CreateThreadExitHandler(FreePwBuf, NULL);
@@ -391,12 +380,9 @@ TclpGetGrNam(
*/
if (tsdPtr->gbuf == NULL) {
- long sizeMax = sysconf(_SC_GETGR_R_SIZE_MAX);
-
- if (sizeMax < 1) {
- tsdPtr->gbuflen = INITIAL_PWGR_BUFFER_SIZE;
- } else {
- tsdPtr->gbuflen = (size_t) sizeMax;
+ tsdPtr->gbuflen = (int) sysconf(_SC_GETGR_R_SIZE_MAX);
+ if (tsdPtr->gbuflen < 1) {
+ tsdPtr->gbuflen = 1024;
}
tsdPtr->gbuf = ckalloc(tsdPtr->gbuflen);
Tcl_CreateThreadExitHandler(FreeGrBuf, NULL);
@@ -474,12 +460,9 @@ TclpGetGrGid(
*/
if (tsdPtr->gbuf == NULL) {
- long sizeMax = sysconf(_SC_GETGR_R_SIZE_MAX);
-
- if (sizeMax < 1) {
- tsdPtr->gbuflen = INITIAL_PWGR_BUFFER_SIZE;
- } else {
- tsdPtr->gbuflen = (size_t) sizeMax;
+ tsdPtr->gbuflen = (int) sysconf(_SC_GETGR_R_SIZE_MAX);
+ if (tsdPtr->gbuflen < 1) {
+ tsdPtr->gbuflen = 1024;
}
tsdPtr->gbuf = ckalloc(tsdPtr->gbuflen);
Tcl_CreateThreadExitHandler(FreeGrBuf, NULL);
@@ -700,10 +683,10 @@ static int
CopyGrp(
struct group *tgtPtr,
char *buf,
- size_t buflen)
+ int buflen)
{
register char *p = buf;
- register ssize_t copied, len = 0;
+ register int copied, len = 0;
/*
* Copy username.
@@ -734,7 +717,7 @@ CopyGrp(
*/
PadBuffer(p, len, sizeof(char *));
- copied = CopyArray((char **)tgtPtr->gr_mem, TCL_STRLEN, p, buflen - len);
+ copied = CopyArray((char **)tgtPtr->gr_mem, -1, p, buflen - len);
if (copied == -1) {
goto range;
}
@@ -773,10 +756,10 @@ static int
CopyHostent(
struct hostent *tgtPtr,
char *buf,
- size_t buflen)
+ int buflen)
{
char *p = buf;
- ssize_t copied, len = 0;
+ int copied, len = 0;
copied = CopyString(tgtPtr->h_name, p, buflen - len);
if (copied == -1) {
@@ -787,7 +770,7 @@ CopyHostent(
p = buf + len;
PadBuffer(p, len, sizeof(char *));
- copied = CopyArray(tgtPtr->h_aliases, TCL_STRLEN, p, buflen - len);
+ copied = CopyArray(tgtPtr->h_aliases, -1, p, buflen - len);
if (copied == -1) {
goto range;
}
@@ -835,10 +818,10 @@ static int
CopyPwd(
struct passwd *tgtPtr,
char *buf,
- size_t buflen)
+ int buflen)
{
char *p = buf;
- ssize_t copied, len = 0;
+ int copied, len = 0;
copied = CopyString(tgtPtr->pw_name, p, buflen - len);
if (copied == -1) {
@@ -894,17 +877,16 @@ CopyPwd(
*/
#ifdef NEED_COPYARRAY
-
-static ssize_t
+static int
CopyArray(
char **src, /* Array of elements to copy. */
- size_t elsize, /* Size of each element, or TCL_STRLEN to
- * indicate that they are C strings of dynamic
+ int elsize, /* Size of each element, or -1 to indicate
+ * that they are C strings of dynamic
* length. */
char *buf, /* Buffer to copy into. */
- size_t buflen) /* Size of buffer. */
+ int buflen) /* Size of buffer. */
{
- size_t i, j, len = 0;
+ int i, j, len = 0;
char *p, **new;
if (src == NULL) {
@@ -925,7 +907,7 @@ CopyArray(
p = buf + len;
for (j = 0; j < i; j++) {
- size_t sz = (elsize==TCL_STRLEN ? (int) strlen(src[j]) + 1 : elsize);
+ int sz = (elsize<0 ? (int) strlen(src[j]) + 1 : elsize);
len += sz;
if (len > buflen) {
@@ -937,7 +919,7 @@ CopyArray(
}
new[j] = NULL;
- return (ssize_t) len;
+ return len;
}
#endif /* NEED_COPYARRAY */
@@ -959,13 +941,13 @@ CopyArray(
*/
#ifdef NEED_COPYSTRING
-static ssize_t
+static int
CopyString(
const char *src, /* String to copy. */
char *buf, /* Buffer to copy into. */
- size_t buflen) /* Size of buffer. */
+ int buflen) /* Size of buffer. */
{
- size_t len = 0;
+ int len = 0;
if (src != NULL) {
len = strlen(src) + 1;
@@ -975,7 +957,7 @@ CopyString(
memcpy(buf, src, len);
}
- return (ssize_t) len;
+ return len;
}
#endif /* NEED_COPYSTRING */
@@ -984,8 +966,7 @@ CopyString(
*
* TclWinCPUID --
*
- * Get CPU ID information on an Intel box under UNIX (either Linux or
- * Cygwin).
+ * Get CPU ID information on an Intel box under UNIX (either Linux or Cygwin)
*
* Results:
* Returns TCL_OK if successful, TCL_ERROR if CPUID is not supported.
@@ -1006,13 +987,19 @@ TclWinCPUID(
/* See: <http://en.wikipedia.org/wiki/CPUID> */
#if defined(HAVE_CPUID)
- __asm__ __volatile__(
- "mov %%ebx, %%edi \n\t" /* save %ebx */
- "cpuid \n\t"
- "mov %%ebx, %%esi \n\t" /* save what cpuid just put in %ebx */
- "mov %%edi, %%ebx \n\t" /* restore the old %ebx */
- : "=a"(regsPtr[0]), "=S"(regsPtr[1]), "=c"(regsPtr[2]), "=d"(regsPtr[3])
- : "a"(index) : "edi");
+#if defined(__x86_64__) || defined(_M_AMD64) || defined (_M_X64)
+ __asm__ __volatile__("movq %%rbx, %%rsi \n\t" /* save %rbx */
+ "cpuid \n\t"
+ "xchgq %%rsi, %%rbx \n\t" /* restore the old %rbx */
+ : "=a"(regsPtr[0]), "=S"(regsPtr[1]), "=c"(regsPtr[2]), "=d"(regsPtr[3])
+ : "a"(index));
+#else
+ __asm__ __volatile__("mov %%ebx, %%esi \n\t" /* save %ebx */
+ "cpuid \n\t"
+ "xchg %%esi, %%ebx \n\t" /* restore the old %ebx */
+ : "=a"(regsPtr[0]), "=S"(regsPtr[1]), "=c"(regsPtr[2]), "=d"(regsPtr[3])
+ : "a"(index));
+#endif
status = TCL_OK;
#endif
return status;