harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ge...@apache.org
Subject svn commit: r350181 [160/198] - in /incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core: ./ depends/ depends/files/ depends/jars/ depends/libs/ depends/libs/linux.IA32/ depends/libs/win.IA32/ depends/oss/ depends/oss/linux.IA32/ depends/oss/win....
Date Thu, 01 Dec 2005 06:04:00 GMT
Added: incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/native-src/linux.IA32/port/hysock.h
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/native-src/linux.IA32/port/hysock.h?rev=350181&view=auto
==============================================================================
--- incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/native-src/linux.IA32/port/hysock.h (added)
+++ incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/native-src/linux.IA32/port/hysock.h Wed Nov 30 21:29:27 2005
@@ -0,0 +1,259 @@
+/* Copyright 1991, 2005 The Apache Software Foundation or its licensors, as applicable
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * 
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#if !defined(hysock_h)
+#define hysock_h
+/******************************************************\
+		Portable socket library implementation.
+\******************************************************/
+#include <errno.h>
+#include <signal.h>
+#include <sys/types.h>
+#include <sys/wait.h>
+#include <unistd.h>
+#include <stdio.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <netinet/tcp.h>
+#include <netdb.h>
+#include <sys/time.h>
+#include <malloc.h>
+#include <sys/socketvar.h>
+#include "hysocket.h"
+#include "hycomp.h"
+#include "hyport.h"
+#if defined(DEBUG)
+#define HYSOCKDEBUG(x, err) printf(x, err)
+#define HYSOCKDEBUGH(x, err) printf(x, err)
+#define HYSOCKDEBUGPRINT(x) printf(x)
+#else
+#define HYSOCKDEBUG(x, err)
+#define HYSOCKDEBUGH(x, err)
+#define HYSOCKDEBUGPRINT(x)
+#endif
+/* the following defines are used to determine how gethostby*_r calls should be handled.*/
+/* HOSTENT_DATA_R: if the HOSTENT_DATA structure is used */
+#define HOSTENT_DATA_R (defined(false))
+/* GLIBC_R: uses the GLIBC versions */
+#define GLIBC_R (defined(LINUX))
+/* ORIGINAL_R: the original gethostby* call is thread safe */
+#define ORIGINAL_R defined(false)
+/* NO_R: gethostby*_r calls do not exist and the normal gethostby* calls are not threadsafe */
+#define NO_R (defined(false))
+/* OTHER_R: everything else */
+#define OTHER_R ((!HOSTENT_DATA_R)&&(!GLIBC_R)&&(!ORIGINAL_R)&&(!NO_R))
+/* os types */
+typedef int OSSOCKET;           /* as returned by socket() */
+typedef struct sockaddr_in OSSOCKADDR;  /* as used by bind() and friends */
+typedef struct hostent OSHOSTENT;
+typedef fd_set OSFDSET;
+typedef struct timeval OSTIMEVAL;
+typedef struct linger OSLINGER;
+typedef struct sockaddr OSADDR;
+typedef struct sockaddr_in6 OSSOCKADDR_IN6;     /* IPv6 */
+typedef struct ipv6_mreq OSIPMREQ6;
+typedef struct addrinfo OSADDRINFO;     /* IPv6 */
+typedef struct sockaddr_storage OSSOCKADDR_STORAGE;     /* IPv6 */
+/*
+ * Socket Types
+ */
+#define OSSOCK_ANY			 0      /* for getaddrinfo hints */
+#define OSSOCK_STREAM     SOCK_STREAM   /* stream socket */
+#define OSSOCK_DGRAM      SOCK_DGRAM    /* datagram socket */
+#define OSSOCK_RAW        SOCK_RAW      /* raw-protocol interface */
+#define OSSOCK_RDM        SOCK_RDM      /* reliably-delivered message */
+#define OSSOCK_SEQPACKET  SOCK_SEQPACKET        /* sequenced packet stream */
+#if defined(HOSTENT_DATA_R)
+typedef struct hostent_data OSHOSTENT_DATA;
+#endif
+typedef struct ip_mreq OSIPMREQ;
+#define OSSOMAXCONN SOMAXCONN
+#define OS_BADSOCKET -1         /* Provide bad socket constant */
+/* defines for socket levels */
+#define OS_SOL_SOCKET SOL_SOCKET
+#define OS_IPPROTO_TCP IPPROTO_TCP
+#define OS_IPPROTO_IP IPPROTO_IP
+#if defined(IPv6_FUNCTION_SUPPORT)
+#define OS_IPPROTO_IPV6 IPPROTO_IPV6
+#endif
+/* defines for socket options */
+#define OS_SO_LINGER SO_LINGER
+#define OS_SO_KEEPALIVE SO_KEEPALIVE
+#define OS_TCP_NODELAY TCP_NODELAY
+#define OS_SO_REUSEADDR SO_REUSEADDR
+#define OS_SO_SNDBUF SO_SNDBUF
+#define OS_SO_RCVBUF SO_RCVBUF
+#define OS_SO_BROADCAST SO_BROADCAST    /* Support datagram broadcasts */
+#define OS_SO_OOBINLINE SO_OOBINLINE
+#define OS_IP_TOS	 IP_TOS
+/* defines for socket options, multicast */
+#define OS_MCAST_TTL IP_MULTICAST_TTL
+#define OS_MCAST_ADD_MEMBERSHIP IP_ADD_MEMBERSHIP
+#define OS_MCAST_DROP_MEMBERSHIP IP_DROP_MEMBERSHIP
+#define OS_MCAST_INTERFACE IP_MULTICAST_IF
+#define OS_MCAST_LOOP IP_MULTICAST_LOOP
+#if defined(IPv6_FUNCTION_SUPPORT)
+#define OS_MCAST_INTERFACE_2 IPV6_MULTICAST_IF
+#define OS_IPV6_ADD_MEMBERSHIP IPV6_ADD_MEMBERSHIP
+#define OS_IPV6_DROP_MEMBERSHIP IPV6_DROP_MEMBERSHIP
+#endif
+/* defines for the unix error constants.  These may be overriden for specific platforms. */
+#define HYPORT_ERROR_SOCKET_UNIX_CONNRESET 		ECONNRESET
+#define HYPORT_ERROR_SOCKET_UNIX_EAGAIN							EAGAIN
+#define HYPORT_ERROR_SOCKET_UNIX_EAFNOSUPPORT			EAFNOSUPPORT
+#define HYPORT_ERROR_SOCKET_UNIX_EBADF								EBADF
+#define HYPORT_ERROR_SOCKET_UNIX_ECONNRESET					ECONNRESET
+#define HYPORT_ERROR_SOCKET_UNIX_EINVAL							EINVAL
+#define HYPORT_ERROR_SOCKET_UNIX_EINTR								EINTR
+#define HYPORT_ERROR_SOCKET_UNIX_EFAULT							EFAULT
+#define HYPORT_ERROR_SOCKET_UNIX_ENOPROTOOPT				ENOPROTOOPT
+#define HYPORT_ERROR_SOCKET_UNIX_ENOTCONN						ENOTCONN
+#define HYPORT_ERROR_SOCKET_UNIX_EPROTONOSUPPORT		EPROTONOSUPPORT
+#define HYPORT_ERROR_SOCKET_UNIX_HOSTNOTFOUND			HOST_NOT_FOUND
+#define HYPORT_ERROR_SOCKET_UNIX_ENOBUFS						ENOBUFS
+#define HYPORT_ERROR_SOCKET_UNIX_NODATA							NO_DATA
+#define HYPORT_ERROR_SOCKET_UNIX_NORECOVERY				NO_RECOVERY
+#define HYPORT_ERROR_SOCKET_UNIX_ENOTSOCK					ENOTSOCK
+#define HYPORT_ERROR_SOCKET_UNIX_TRYAGAIN						TRY_AGAIN
+#define HYPORT_ERROR_SOCKET_UNIX_EOPNOTSUP				EOPNOTSUPP
+#define HYPORT_ERROR_SOCKET_UNIX_ETIMEDOUT				ETIMEDOUT
+#define HYPORT_ERROR_SOCKET_UNIX_CONNREFUSED				ECONNREFUSED
+#define HYPORT_ERROR_SOCKET_UNIX_EINPROGRESS				EINPROGRESS
+#define HYPORT_ERROR_SOCKET_UNIX_ENETUNREACH				ENETUNREACH
+#define HYPORT_ERROR_SOCKET_UNIX_EACCES						EACCES
+/* platform constants */
+#define HYSOCK_MAXCONN OSSOMAXCONN;
+#define HYSOCK_BADSOCKET OS_BADSOCKET   /* Provide bad socket constant */
+/* defines added for IPv6 */
+#define OS_AF_INET4 AF_INET
+#define OS_AF_UNSPEC AF_UNSPEC
+#define OS_PF_UNSPEC PF_UNSPEC
+#define OS_PF_INET4 PF_INET
+#define OS_INET4_ADDRESS_LENGTH INET_ADDRSTRLEN
+#if defined(NI_MAXHOST)
+#define OSNIMAXHOST NI_MAXHOST
+#define OSNIMAXSERV NI_MAXSERV
+#else
+#define OSNIMAXHOST 1025
+#define OSNIMAXSERV 32
+#endif
+
+#if defined(AF_INET6)
+#define OS_AF_INET6 AF_INET6
+#define OS_PF_INET6 PF_INET6
+#define OS_INET6_ADDRESS_LENGTH INET6_ADDRSTRLEN
+#else
+#define OS_AF_INET6 -1
+#define OS_PF_INET6 -1
+#define OS_INET6_ADDRESS_LENGTH 46
+#endif
+/*platform structs */
+typedef struct hysocket_struct
+{
+  OSSOCKET sock;
+  U_16 family;
+} hysocket_struct;
+/* The sockets are now structs  */
+#if defined(NO_LVALUE_CASTING)
+#define SOCKET_CAST(x) ((struct hysocket_struct*)  x)->sock
+#else
+#define SOCKET_CAST(x) ((struct hysocket_struct*)  x)->sock
+#endif
+typedef struct hysockaddr_struct
+{
+#if defined(IPv6_FUNCTION_SUPPORT)
+  OSSOCKADDR_STORAGE addr;
+#else
+  OSSOCKADDR addr;
+#endif
+} hysockaddr_struct;
+typedef struct hyhostent_struct
+{
+  OSHOSTENT *entity;
+} hyhostent_struct;
+typedef struct hyfdset_struct
+{
+  OSFDSET handle;
+} hyfdset_struct;
+typedef struct hytimeval_struct
+{
+  OSTIMEVAL time;
+} hytimeval_struct;
+typedef struct hylinger_struct
+{
+  OSLINGER linger;
+} hylinger_struct;
+typedef struct hyipmreq_struct
+{
+  OSIPMREQ addrpair;
+} hyipmreq_struct;
+#define GET_HOST_BUFFER_SIZE 512
+/* The gethostBuffer is allocated bufferSize + EXTRA_SPACE, while gethostby*_r is only aware of bufferSize
+ * because there seems to be a bug on Linux 386 where gethostbyname_r writes past the end of the
+ * buffer.  This bug has not been observed on other platforms, but EXTRA_SPACE is added anyway as a precaution.*/
+#define EXTRA_SPACE 128
+/*size is 16 because an IP string is "xxx.xxx.xxx.xxx" + 1 null character */
+#define NTOA_SIZE 16
+/** new structure for IPv6 mulitcast requests */
+typedef struct hyipv6_mreq_struct
+{
+#if defined(IPv6_FUNCTION_SUPPORT)
+  OSIPMREQ6 mreq;
+#else
+  int dummy;
+#endif
+} hyipv6_mreq_struct;
+/* structure for returning either and IPV4 or IPV6 ip address */
+typedef struct hyipAddress_struct
+{
+  union
+  {
+#if defined(IPv6_FUNCTION_SUPPORT)
+    U_8 bytes[sizeof (struct in6_addr)];
+#else
+    U_8 bytes[sizeof (struct in_addr)];
+#endif
+struct in_addr inAddr;
+#if defined(IPv6_FUNCTION_SUPPORT)
+    struct in6_addr in6Addr;
+#endif
+} addr;
+  U_32 length;
+  U_32 scope;
+} hyipAddress_struct;
+/* structure for returning network interface information */
+typedef struct hyNetworkInterface_struct
+{
+  char *name;
+  char *displayName;
+  U_32 numberAddresses;
+  U_32 index;
+  struct hyipAddress_struct *addresses;
+} hyNetworkInterface_struct;
+/* array of network interface structures */
+typedef struct hyNetworkInterfaceArray_struct
+{
+  U_32 length;
+  struct hyNetworkInterface_struct *elements;
+} hyNetworkInterfaceArray_struct;
+/** new structure for IPv6 addrinfo will either point to a hostent or 
+	an addr info depending on the IPv6 support for this OS */
+typedef struct hyaddrinfo_struct
+{
+  void *addr_info;
+  int length;
+} hyaddrinfo_struct;
+#endif
+

Added: incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/native-src/linux.IA32/port/hystr.c
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/native-src/linux.IA32/port/hystr.c?rev=350181&view=auto
==============================================================================
--- incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/native-src/linux.IA32/port/hystr.c (added)
+++ incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/native-src/linux.IA32/port/hystr.c Wed Nov 30 21:29:27 2005
@@ -0,0 +1,1124 @@
+/* Copyright 1991, 2005 The Apache Software Foundation or its licensors, as applicable
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * 
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/**
+ * @file
+ * @ingroup Port
+ * @brief String utilities.
+ */
+#include <stdarg.h>
+#include <string.h>
+
+/* for sprintf, which is used for printing floats */
+
+#include <stdio.h>
+
+#include "hycomp.h"
+#include "hyport.h"
+
+#define HYFTYPE_U64 1
+#define HYFTYPE_U32 2
+#define HYFTYPE_DBL 3
+#define HYFTYPE_PTR 4
+#define HYFTYPE_IMMEDIATE 5
+
+#define HYFFLAG_DASH 0x01
+#define HYFFLAG_HASH 0x02
+#define HYFFLAG_ZERO 0x04
+#define HYFFLAG_SPACE 0x08
+#define HYFFLAG_PLUS 0x10
+
+#define HYFSPEC_LL 0x20
+#define HYFSPEC_L 0x40
+
+#define HYF_NO_VALUE ((U_64)-1)
+
+typedef union
+{
+  U_64 u64;
+  double dbl;
+  void *ptr;
+} HyFormatValue;
+
+typedef struct
+{
+  U_8 tag;
+  U_8 index;
+  U_8 widthIndex;
+  U_8 precisionIndex;
+  const char *type;
+} HyFormatSpecifier;
+
+#define HYF_MAX_SPECS 16
+#define HYF_MAX_ARGS (HYF_MAX_SPECS * 3)
+
+typedef struct
+{
+  const char *formatString;
+  HyFormatValue value[HYF_MAX_ARGS];
+  U_8 valueType[HYF_MAX_ARGS];
+  HyFormatSpecifier spec[HYF_MAX_SPECS];
+  U_8 valueCount;
+  U_8 immediateCount;
+  U_8 specCount;
+} HyFormatData;
+
+static const char digits_dec[] = "0123456789";
+static const char digits_hex_lower[] = "0123456789abcdef";
+static const char digits_hex_upper[] = "0123456789ABCDEF";
+
+extern U_32 encodeUTF8Char (UDATA unicode, U_8 * result);
+
+static const char *parseTagChar (const char *format, HyFormatData * result);
+static void readValues (struct HyPortLibrary *portLibrary,
+                        HyFormatData * result, va_list args);
+static int parseFormatString (struct HyPortLibrary *portLibrary,
+                              HyFormatData * result);
+static I_32 writeDoubleToBuffer (char *buf, U_32 bufLen, U_64 width,
+                                 U_64 precision, double value, U_8 type,
+                                 U_8 tag);
+static const char *parseModifier (const char *format, HyFormatData * result);
+static const char *parseType (const char *format, HyFormatData * result);
+static const char *parseWidth (const char *format, HyFormatData * result);
+static U_32 writeFormattedString (struct HyPortLibrary *portLibrary,
+                                  HyFormatData * data, char *result,
+                                  U_32 length);
+static I_32 writeSpec (HyFormatData * data, HyFormatSpecifier * spec,
+                       char *result, U_32 length);
+static const char *parseIndex (const char *format, U_8 * result);
+static I_32 writeStringToBuffer (char *buf, U_32 bufLen, U_64 width,
+                                 U_64 precision, const char *value, U_8 tag);
+static const char *parsePrecision (const char *format, HyFormatData * result);
+static I_32 writeIntToBuffer (char *buf, U_32 bufLen, U_64 width,
+                              U_64 precision, U_64 value, U_8 tag,
+                              int isSigned, const char *digits);
+static I_32 writeUnicodeStringToBuffer (char *buf, U_32 bufLen, U_64 width,
+                                        U_64 precision, const U_16 * value,
+                                        U_8 tag);
+
+/**
+ * Write characaters to a string as specified by format.
+ *
+ * @param[in] portLibrary The port library.
+ * @param[in, out] buf The string buffer to be written.
+ * @param[in] bufLen The size of the string buffer to be written.
+ * @param[in] format The format of the string.
+ * @param[in] ... Arguments for the format string.
+ *
+ * @return The number of characters printed not including the NUL terminator.
+ *
+ * @note When buf is NULL, the size of the buffer required to print to the string, including
+ * the NUL terminator is returned.
+ *
+ */
+U_32 VMCALL
+hystr_printf (struct HyPortLibrary *portLibrary, char *buf, U_32 bufLen,
+              const char *format, ...)
+{
+  U_32 rc;
+  va_list args;
+  va_start (args, format);
+  rc = portLibrary->str_vprintf (portLibrary, buf, bufLen, format, args);
+  va_end (args);
+  return rc;
+}
+
+/**
+ * Write characaters to a string as specified by format.
+ *
+ * @param[in] portLibrary The port library.
+ * @param[in, out] buf The string buffer to be written.
+ * @param[in] bufLen The size of the string buffer to be written.
+ * @param[in] format The format of the string.
+ * @param[in] args Arguments for the format string.
+ *
+ * @return The number of characters printed not including the NUL terminator.
+ *
+ * @note When buf is NULL, the size of the buffer required to print to the string, including
+ * the NUL terminator is returned.
+ *
+ */
+U_32 VMCALL
+hystr_vprintf (struct HyPortLibrary * portLibrary, char *buf, U_32 bufLen,
+               const char *format, va_list args)
+{
+  HyFormatData formatData;
+  memset (&formatData, 0, sizeof (formatData));
+
+  formatData.formatString = format;
+  parseFormatString (portLibrary, &formatData);
+  readValues (portLibrary, &formatData, args);
+  return writeFormattedString (portLibrary, &formatData, buf, bufLen);
+}
+
+static int
+parseFormatString (struct HyPortLibrary *portLibrary, HyFormatData * result)
+{
+  const char *format = result->formatString;
+
+  while (*format)
+    {
+      switch (*format)
+        {
+        case '%':
+          format++;
+          switch (*format)
+            {
+            case '%':
+              /* literal '%' */
+              format++;
+              break;
+            default:
+              format =
+                parseIndex (format, &result->spec[result->specCount].index);
+              format = parseTagChar (format, result);
+              format = parseWidth (format, result);
+              format = parsePrecision (format, result);
+              format = parseModifier (format, result);
+              format = parseType (format, result);
+              if (format == NULL)
+                {
+                  return -1;
+                }
+              result->specCount++;
+            }
+          break;
+        default:
+          format++;
+        }
+    }
+
+  return 0;
+}
+static const char *
+parseTagChar (const char *format, HyFormatData * result)
+{
+
+  switch (*format)
+    {
+    case '0':
+      result->spec[result->specCount].tag |= HYFFLAG_ZERO;
+      format++;
+      break;
+    case ' ':
+      result->spec[result->specCount].tag |= HYFFLAG_SPACE;
+      format++;
+      break;
+    case '-':
+      result->spec[result->specCount].tag |= HYFFLAG_DASH;
+      format++;
+      break;
+    case '+':
+      result->spec[result->specCount].tag |= HYFFLAG_PLUS;
+      format++;
+      break;
+    case '#':
+      result->spec[result->specCount].tag |= HYFFLAG_HASH;
+      format++;
+      break;
+    }
+
+  return format;
+}
+static const char *
+parseWidth (const char *format, HyFormatData * result)
+{
+  U_8 index;
+
+  if (*format == '*')
+    {
+      format =
+        parseIndex (format + 1, &result->spec[result->specCount].widthIndex);
+      index = result->spec[result->specCount].widthIndex;
+      if (index == 0xFF)
+        {
+          index = result->valueCount;
+          result->spec[result->specCount].widthIndex = index;
+        }
+      result->valueCount++;
+      result->valueType[index] = HYFTYPE_U32;
+      result->value[index].u64 = HYF_NO_VALUE;
+
+      return format;
+    }
+  else
+    {
+      U_32 width = 0;
+      int anyDigits = 0;
+      for (;;)
+        {
+          switch (*format)
+            {
+            case '0':
+            case '1':
+            case '2':
+            case '3':
+            case '4':
+            case '5':
+            case '6':
+            case '7':
+            case '8':
+            case '9':
+              anyDigits = 1;
+              width = width * 10 + (*format - '0');
+              format += 1;
+              break;
+            default:
+              index = HYF_MAX_ARGS - ++result->immediateCount;
+              result->spec[result->specCount].widthIndex = index;
+              result->valueType[index] = HYFTYPE_IMMEDIATE;
+              if (anyDigits)
+                {
+                  result->value[index].u64 = width;
+                }
+              else
+                {
+                  result->value[index].u64 = HYF_NO_VALUE;
+                }
+              return format;
+            }
+        }
+    }
+}
+static const char *
+parsePrecision (const char *format, HyFormatData * result)
+{
+  U_8 index;
+
+  if (*format == '.')
+    {
+      format += 1;
+    }
+  else
+    {
+      index = HYF_MAX_ARGS - ++result->immediateCount;
+      result->spec[result->specCount].precisionIndex = index;
+      result->valueType[index] = HYFTYPE_IMMEDIATE;
+      result->value[index].u64 = HYF_NO_VALUE;
+
+      return format;
+    }
+
+  if (*format == '*')
+    {
+      format =
+        parseIndex (format + 1,
+                    &result->spec[result->specCount].precisionIndex);
+      index = result->spec[result->specCount].precisionIndex;
+      if (index == 0xFF)
+        {
+          index = result->valueCount;
+          result->spec[result->specCount].precisionIndex = index;
+        }
+      result->valueCount++;
+      result->valueType[index] = HYFTYPE_U32;
+      result->value[index].u64 = HYF_NO_VALUE;
+
+      return format;
+    }
+  else
+    {
+      U_32 precision = 0;
+      int anyDigits = 0;
+      for (;;)
+        {
+          switch (*format)
+            {
+            case '0':
+            case '1':
+            case '2':
+            case '3':
+            case '4':
+            case '5':
+            case '6':
+            case '7':
+            case '8':
+            case '9':
+              anyDigits = 1;
+              precision = precision * 10 + (*format - '0');
+              format += 1;
+              break;
+            default:
+              index = HYF_MAX_ARGS - ++result->immediateCount;
+              result->spec[result->specCount].precisionIndex = index;
+              result->valueType[index] = HYFTYPE_IMMEDIATE;
+              if (anyDigits)
+                {
+                  result->value[index].u64 = precision;
+                }
+              else
+                {
+                  result->value[index].u64 = HYF_NO_VALUE;
+                }
+              return format;
+            }
+        }
+    }
+}
+static const char *
+parseModifier (const char *format, HyFormatData * result)
+{
+
+  switch (*format)
+    {
+    case 'z':
+      format++;
+
+      break;
+    case 'l':
+      format++;
+      if (*format == 'l')
+        {
+          format++;
+          result->spec[result->specCount].tag |= HYFSPEC_LL;
+        }
+      else
+        {
+          result->spec[result->specCount].tag |= HYFSPEC_L;
+        }
+      break;
+    }
+
+  return format;
+}
+static const char *
+parseType (const char *format, HyFormatData * result)
+{
+  const char *type = format++;
+  U_8 tag = result->spec[result->specCount].tag;
+  U_8 index = result->spec[result->specCount].index;
+
+  if (index == 0xFF)
+    {
+      index = result->valueCount;
+      result->spec[result->specCount].index = index;
+    }
+  result->valueCount++;
+
+  result->spec[result->specCount].type = type;
+
+  switch (*type)
+    {
+      /* integers */
+    case 'c':
+      result->valueType[index] = HYFTYPE_U32;
+      break;
+    case 'i':
+    case 'd':
+    case 'u':
+    case 'x':
+    case 'X':
+      result->valueType[index] = tag & HYFSPEC_LL ? HYFTYPE_U64 : HYFTYPE_U32;
+      break;
+
+      /* pointers */
+    case 'p':
+    case 's':
+      result->valueType[index] = HYFTYPE_PTR;
+      break;
+
+      /* floats */
+    case 'f':
+    case 'e':
+    case 'E':
+    case 'F':
+    case 'g':
+    case 'G':
+      result->valueType[index] = HYFTYPE_DBL;
+      break;
+
+    default:
+      return NULL;
+    }
+
+  return format;
+}
+static void
+readValues (struct HyPortLibrary *portLibrary, HyFormatData * result,
+            va_list args)
+{
+  U_8 index;
+
+  for (index = 0; index < result->valueCount; index++)
+    {
+      switch (result->valueType[index])
+        {
+        case HYFTYPE_U64:
+          result->value[index].u64 = va_arg (args, U_64);
+          break;
+        case HYFTYPE_U32:
+          result->value[index].u64 = va_arg (args, U_32);
+          break;
+        case HYFTYPE_DBL:
+          result->value[index].dbl = va_arg (args, double);
+          break;
+        case HYFTYPE_PTR:
+          result->value[index].ptr = va_arg (args, void *);
+          break;
+        case HYFTYPE_IMMEDIATE:
+          /* shouldn't be encountered -- these should all be at the end of the value array */
+          break;
+        }
+    }
+}
+
+static I_32
+writeSpec (HyFormatData * data, HyFormatSpecifier * spec, char *result,
+           U_32 length)
+{
+  HyFormatValue *value = &data->value[spec->index];
+  U_64 width = data->value[spec->widthIndex].u64;
+  U_64 precision = data->value[spec->precisionIndex].u64;
+  I_32 index = 0;
+
+  switch (*spec->type)
+    {
+    case 'i':
+    case 'd':
+      index =
+        writeIntToBuffer (result, length, width, precision, value->u64,
+                          spec->tag, 1, digits_dec);
+      break;
+    case 'u':
+      index =
+        writeIntToBuffer (result, length, width, precision, value->u64,
+                          spec->tag, 0, digits_dec);
+      break;
+    case 'x':
+      index =
+        writeIntToBuffer (result, length, width, precision, value->u64,
+                          spec->tag, 0, digits_hex_lower);
+      break;
+    case 'X':
+      index =
+        writeIntToBuffer (result, length, width, precision, value->u64,
+                          spec->tag, 0, digits_hex_upper);
+      break;
+
+    case 'p':
+      index =
+        writeIntToBuffer (result, length, sizeof (UDATA) * 2,
+                          sizeof (UDATA) * 2, (UDATA) value->ptr, 0, 0,
+                          digits_hex_upper);
+      break;
+
+    case 'c':
+      if (spec->tag & HYFSPEC_L)
+        {
+          char asUTF8[4];
+          U_32 numberWritten =
+            encodeUTF8Char ((UDATA) value->u64, (U_8 *) asUTF8);
+
+          /* what if width/precision is less than size of asUTF8? [truncate?] */
+          asUTF8[numberWritten] = '\0';
+          index =
+            writeStringToBuffer (result, length, width, precision, asUTF8,
+                                 spec->tag);
+        }
+      else
+        {
+          index =
+            writeStringToBuffer (result, length, width, precision, " ",
+                                 spec->tag);
+          if (index <= length)
+            {
+              if (result)
+                {
+                  result[index - 1] = (char) value->u64;
+                }
+            }
+        }
+      break;
+
+    case 's':
+      if (value->ptr)
+        {
+          if (spec->tag & HYFSPEC_L)
+            {
+              index =
+                writeUnicodeStringToBuffer (result, length, width, precision,
+                                            (const U_16 *) value->ptr,
+                                            spec->tag);
+            }
+          else
+            {
+              index =
+                writeStringToBuffer (result, length, width, precision,
+                                     (const char *) value->ptr, spec->tag);
+            }
+        }
+      else
+        {
+          index =
+            writeStringToBuffer (result, length, width, precision, "<NULL>",
+                                 spec->tag);
+        }
+      break;
+
+      /* floats */
+    case 'f':
+    case 'e':
+    case 'E':
+    case 'F':
+    case 'g':
+    case 'G':
+      index =
+        writeDoubleToBuffer (result, length, width, precision, value->dbl,
+                             *spec->type, spec->tag);
+      break;
+    }
+
+  return index;
+}
+
+static I_32
+writeIntToBuffer (char *buf, U_32 bufLen, U_64 width, U_64 precision,
+                  U_64 value, U_8 tag, int isSigned, const char *digits)
+{
+  I_32 index = 0;
+  I_32 length = 0;
+  I_32 rightSpace = 0;
+  U_64 temp;
+  int base = strlen (digits);
+  I_32 actualPrecision = 0;
+  char signChar = 0;
+
+  if (isSigned)
+    {
+      I_64 signedValue;
+      if (tag & HYFSPEC_LL)
+        {
+          signedValue = (I_64) value;
+        }
+      else
+        {
+          signedValue = (I_32) value;
+        }
+
+      if (signedValue < 0)
+        {
+          signChar = '-';
+          value = (U_64) (signedValue * -1);
+        }
+      else if (signedValue >= 0 && (tag & HYFFLAG_PLUS))
+        {
+          signChar = '+';
+        }
+    }
+
+  /* find the end of the number */
+  temp = value;
+  do
+    {
+      length++;
+      temp /= base;
+    }
+  while (temp);
+
+  if (precision != HYF_NO_VALUE)
+    {
+      actualPrecision = (I_32) precision;
+      if (actualPrecision > length)
+        {
+          length = actualPrecision;
+        }
+    }
+
+  /* Account for "-" Must be after setting actualPrecision, before calculation of rightSpace */
+  if (signChar)
+    {
+      length++;
+    }
+
+  if (width != HYF_NO_VALUE && (I_32) width > length)
+    {
+      if (tag & HYFFLAG_DASH)
+        {
+          rightSpace = (I_32) width - length;
+        }
+      length = (I_32) width;
+    }
+
+  if (tag & HYFFLAG_ZERO)
+    {
+      actualPrecision = length - (signChar ? 1 : 0);
+    }
+
+  /* now write the number out backwards */
+  for (; rightSpace > 0; rightSpace--)
+    {
+      length -= 1;
+      if (bufLen > length)
+        {
+          if (buf)
+            {
+              buf[length] = ' ';
+            }
+          index += 1;
+        }
+    }
+
+  /* write out the digits */
+  temp = value;
+  do
+    {
+      length -= 1;
+      actualPrecision -= 1;
+      if (bufLen > length)
+        {
+          if (buf)
+            {
+              buf[length] = digits[(int) (temp % base)];
+            }
+          index += 1;
+        }
+      temp /= base;
+    }
+  while (temp);
+
+  /* zero extend to the left according the the requested precision */
+  while (length > 0)
+    {
+      length -= 1;
+      actualPrecision -= 1;
+      if (bufLen > length)
+        {
+          if (buf)
+            {
+              if (actualPrecision >= 0)
+                {
+                  buf[length] = '0';
+                }
+              else
+                {
+                  if (signChar)
+                    {
+                      buf[length] = signChar;
+                      /* only print the sign char once */
+                      signChar = 0;
+                    }
+                  else
+                    {
+                      buf[length] = ' ';
+                    }
+                }
+            }
+          index += 1;
+        }
+    }
+
+  return index;
+}
+
+static I_32
+writeStringToBuffer (char *buf, U_32 bufLen, U_64 width, U_64 precision,
+                     const char *value, U_8 tag)
+{
+  size_t leftPadding = 0;
+  size_t rightPadding = 0;
+
+  if (precision == HYF_NO_VALUE)
+    {
+      precision = strlen (value);
+    }
+  else
+    {
+      I_32 n;
+      /* detect if the string is shorter than precision */
+      for (n = 0; n < precision; n++)
+        {
+          if (value[n] == 0)
+            {
+              precision = n;
+              break;
+            }
+        }
+    }
+
+  if (width == HYF_NO_VALUE)
+    {
+      width = precision;
+    }
+
+  if (width > precision)
+    {
+      if (tag & HYFFLAG_DASH)
+        {
+          rightPadding = (size_t) (width - precision);
+        }
+      else
+        {
+          leftPadding = (size_t) (width - precision);
+        }
+    }
+
+  if (leftPadding > bufLen)
+    {
+      leftPadding = bufLen;
+    }
+  if (buf)
+    {
+      memset (buf, ' ', leftPadding);
+      buf += leftPadding;
+    }
+  bufLen -= leftPadding;
+
+  if (precision > bufLen)
+    {
+      precision = bufLen;
+    }
+  if (buf)
+    {
+      memcpy (buf, value, (size_t) precision);
+      buf += (size_t) precision;
+    }
+  bufLen -= (size_t) precision;
+
+  if (rightPadding > bufLen)
+    {
+      rightPadding = bufLen;
+    }
+  if (buf)
+    {
+      memset (buf, ' ', rightPadding);
+      buf += rightPadding;
+    }
+
+  return (I_32) (leftPadding + (size_t) precision + rightPadding);
+}
+
+/*
+ * To determine size of buffer required for format string pass in a NULL buffer and the maximum
+ * size willing to create.  For example for no restrictions result=NULL, length=(U_32)(-1), to restrict
+ * the buffer to 2k, result=NULL, length=2048
+ *
+ * Value returned does not include space required for the null terminator
+ */
+static U_32
+writeFormattedString (struct HyPortLibrary *portLibrary, HyFormatData * data,
+                      char *result, U_32 length)
+{
+  const char *format = data->formatString;
+  U_8 specIndex = 0;
+  U_32 index = 0;
+
+  if (NULL == result)
+    {
+      length = (U_32) - 1;
+    }
+  else if (0 == length)
+    {
+      /* empty buffer */
+      return 0;
+    }
+
+  while (*format && index < length - 1)
+    {
+      switch (*format)
+        {
+        case '%':
+          format++;
+          switch (*format)
+            {
+            case '%':
+              /* literal '%' */
+              if (result)
+                {
+                  result[index] = '%';
+                }
+              index++;
+              format++;
+              break;
+            default:
+              if (result)
+                {
+                  index +=
+                    writeSpec (data, &data->spec[specIndex], result + index,
+                               length - index);
+                }
+              else
+                {
+                  index +=
+                    writeSpec (data, &data->spec[specIndex], result, length);
+                }
+
+              format = data->spec[specIndex].type + 1;
+              specIndex += 1;
+              break;
+            }
+          break;
+        default:
+          if (result)
+            {
+              result[index] = *format;
+            }
+          *format++;
+          index++;
+        }
+    }
+
+  /* writeSpec can return value > 1, so index does not increment sequentially */
+  if (index > length - 1)
+    {
+      index = length - 1;
+    }
+
+  if (result)
+    {
+      result[index] = 0;
+    }
+
+  if (NULL == result)
+    {
+      return index + 1;         /* For the NUL terminator */
+    }
+
+  return index;
+}
+
+static I_32
+writeDoubleToBuffer (char *buf, U_32 bufLen, U_64 width, U_64 precision,
+                     double value, U_8 type, U_8 tag)
+{
+  char format[sizeof ("%+4294967295.4294967295f")];
+  char *formatCursor = format;
+  char *lastFormat = format + sizeof (format);
+  char tempBuf[510];            /* 509 is maximum size of a converted double */
+
+  *formatCursor++ = '%';
+
+  if (tag & HYFFLAG_DASH)
+    {
+      *formatCursor++ = '-';
+    }
+  else if (tag & HYFFLAG_PLUS)
+    {
+      *formatCursor++ = '+';
+    }
+  else if (tag & HYFFLAG_SPACE)
+    {
+      *formatCursor++ = ' ';
+    }
+  else if (tag & HYFFLAG_ZERO)
+    {
+      *formatCursor++ = '0';
+    }
+  else if (tag & HYFFLAG_HASH)
+    {
+      *formatCursor++ = '#';
+    }
+
+  if (width != HYF_NO_VALUE)
+    {
+      formatCursor +=
+        writeIntToBuffer (formatCursor, lastFormat - formatCursor,
+                          HYF_NO_VALUE, HYF_NO_VALUE, width, 'u', 0,
+                          digits_dec);
+    }
+  if (precision != HYF_NO_VALUE)
+    {
+      *formatCursor++ = '.';
+      formatCursor +=
+        writeIntToBuffer (formatCursor, lastFormat - formatCursor,
+                          HYF_NO_VALUE, HYF_NO_VALUE, precision, 'u', 0,
+                          digits_dec);
+    }
+
+  *formatCursor++ = type;
+  *formatCursor++ = '\0';
+
+  sprintf (tempBuf, format, value);
+
+  if (buf)
+    {
+      strncpy (buf, tempBuf, bufLen);
+      buf[bufLen - 1] = '\0';
+      return strlen (buf);
+    }
+
+  return strlen (tempBuf);
+}
+
+/**
+ * PortLibrary shutdown.
+ *
+ * This function is called during shutdown of the portLibrary.  Any resources that were created by @ref hystr_startup
+ * should be destroyed here.
+ *
+ * @param[in] portLibrary The port library.
+ *
+ * @note Most implementations will be empty.
+ */
+void VMCALL
+hystr_shutdown (struct HyPortLibrary *portLibrary)
+{
+}
+
+/**
+ * PortLibrary startup.
+ *
+ * This function is called during startup of the portLibrary.  Any resources that are required for
+ * the string operations may be created here.  All resources created here should be destroyed
+ * in @ref hystr_shutdown.
+ *
+ * @param[in] portLibrary The port library.
+ *
+ * @return 0 on success, negative error code on failure.  Error code values returned are
+ * \arg HYPORT_ERROR_STARTUP_STR
+ *
+ * @note Most implementations will simply return success.
+ */
+I_32 VMCALL
+hystr_startup (struct HyPortLibrary *portLibrary)
+{
+  return 0;
+}
+
+static I_32
+writeUnicodeStringToBuffer (char *buf, U_32 bufLen, U_64 width,
+                            U_64 precision, const U_16 * value, U_8 tag)
+{
+  U_32 numberOfUnicodeChar = 0;
+  U_32 numberOfUTF8Char = 0;
+  U_32 encodingLength;
+  size_t leftPadding = 0;
+  size_t rightPadding = 0;
+  const U_16 *currentU16;
+
+  if (precision == HYF_NO_VALUE)
+    {
+      currentU16 = value;
+      precision = 0;
+      while (*currentU16++ != 0)
+        {
+          precision++;
+        }
+    }
+  else
+    {
+      I_32 n;
+      /* detect if the string is shorter than precision */
+      for (n = 0; n < precision; n++)
+        {
+          if (value[n] == 0)
+            {
+              precision = n;
+              break;
+            }
+        }
+    }
+
+  currentU16 = value;
+  while (numberOfUnicodeChar < precision)
+    {
+
+      encodingLength = encodeUTF8Char ((UDATA) * currentU16++, NULL);
+      if (numberOfUTF8Char + encodingLength > bufLen)
+        {
+          break;
+        }
+
+      /* If the character fits then increment counts */
+      numberOfUnicodeChar++;
+      numberOfUTF8Char += encodingLength;
+    }
+
+  if (width == HYF_NO_VALUE)
+    {
+      width = numberOfUTF8Char;
+    }
+
+  if (width > numberOfUTF8Char)
+    {
+      if (tag & HYFFLAG_DASH)
+        {
+          rightPadding = (size_t) (width - numberOfUTF8Char);
+        }
+      else
+        {
+          leftPadding = (size_t) (width - numberOfUTF8Char);
+        }
+    }
+
+  if (leftPadding > bufLen)
+    {
+      leftPadding = bufLen;
+    }
+  if (buf)
+    {
+      memset (buf, ' ', leftPadding);
+      buf += leftPadding;
+    }
+  bufLen -= leftPadding;
+
+  /* The space required for the UTF8 chars is guaranteed to be there */
+  if (buf)
+    {
+      currentU16 = value;
+      while (numberOfUnicodeChar-- > 0)
+        {
+          buf += encodeUTF8Char ((UDATA) * currentU16++, buf);
+        }
+    }
+  bufLen -= numberOfUTF8Char;
+
+  if (rightPadding > bufLen)
+    {
+      rightPadding = bufLen;
+    }
+  if (buf)
+    {
+      memset (buf, ' ', rightPadding);
+      buf += rightPadding;
+    }
+
+  return (I_32) (leftPadding + numberOfUTF8Char + rightPadding);
+}
+static const char *
+parseIndex (const char *format, U_8 * result)
+{
+  const char *start = format;
+  U_8 index = 0;
+
+  for (;;)
+    {
+      switch (*format)
+        {
+        case '0':
+        case '1':
+        case '2':
+        case '3':
+        case '4':
+        case '5':
+        case '6':
+        case '7':
+        case '8':
+        case '9':
+          index = index * 10 + (*format - '0');
+          format += 1;
+          break;
+        case '$':
+          if (index > 0)
+            {
+              *result = index - 1;
+              return format + 1;
+            }
+          /* fall through */
+        default:
+          *result = 0xFF;
+          return start;
+        }
+    }
+}

Added: incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/native-src/linux.IA32/port/hystrftime.c
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/native-src/linux.IA32/port/hystrftime.c?rev=350181&view=auto
==============================================================================
--- incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/native-src/linux.IA32/port/hystrftime.c (added)
+++ incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/native-src/linux.IA32/port/hystrftime.c Wed Nov 30 21:29:27 2005
@@ -0,0 +1,364 @@
+/* Copyright 1991, 2005 The Apache Software Foundation or its licensors, as applicable
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * 
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/**
+ * @file
+ * @ingroup Port
+ * @brief Time formatting utilities.
+ */
+
+#include <string.h>
+#include "hycomp.h"
+#include "hyport.h"
+
+typedef struct HyTimeInfo
+{
+  U_32 second;
+  U_32 minute;
+  U_32 hour;
+  U_32 day;
+  U_32 month;
+  U_32 year;
+} HyTimeInfo;
+
+static void gettimestruct (I_64 millis, struct HyTimeInfo *tm);
+
+/**
+ * Returns the current time in as a formatted string.  Formatted according to the 'format' parameter.
+ *
+ * @param[in] portLibrary  The port library.
+ * @param[in,out] buf A pointer to a character buffer where the resulting time string will be stored.
+ * @param[in] bufLen The length of the 'buf' character buffer.
+ * @param[in] format The format string, ordinary characters placed in the format string are copied.
+ * to buf without conversion.  Conversion specifiers are introduced by a '%' character, and are replaced in buf as follows:.
+ * <ul>
+ * <li>%b The abbreviated month name in english
+ * <li>%d The  day  of the month as a decimal number (range 0 to 31).
+ * <li>%H The hour as a decimal number using a 24-hour  clock (range 00 to 23).
+ * <li>%m The month as a decimal number (range 01 to 12).
+ * <li>%M The minute as a decimal number.
+ * <li>%S The second as a decimal number.
+ * <li>%Y The year as a decimal number including the century.
+ * <li>%% A literal '%' character.
+ * <li>all other '%' specifiers will be ignored
+ * </ul>
+ *
+ * @return The number of characters placed in the array buf, not including NULL terminator.
+ * If the value equals 'bufLen', it means that the array was too small.
+ */
+U_32 VMCALL
+hystrftime (struct HyPortLibrary *portLibrary, char *buf, U_32 bufLen,
+            const char *format)
+{
+  I_64 millis;
+  U_32 index = 0;
+  HyTimeInfo tm;
+  static const char abbMonthName[12][4] =
+    { "JAN", "FEB", "MAR", "APR", "MAY", "JUN", "JUL", "AUG", "SEP", "OCT",
+    "NOV", "DEC"
+  };
+
+  if (buf == NULL || bufLen < 1)
+    {
+      return 0;
+    }
+
+  millis = portLibrary->time_current_time_millis (portLibrary);
+
+  gettimestruct (millis, &tm);
+
+  /* format the date/time information according to the format string */
+  while (*format && index < bufLen - 1)
+    {
+      switch (*format)
+        {
+        case '%':
+          format++;
+          switch (*format)
+            {
+            case '\0':
+              /* end of the format string */
+              break;
+            case '%':
+              /* literal '%' */
+              buf[index] = '%';
+              index++;
+              format++;
+              break;
+            case 'Y':
+              if (index + 4 >= bufLen - 1)
+                {
+                  return bufLen;
+                }
+              index +=
+                portLibrary->str_printf (portLibrary, buf + index,
+                                         bufLen - index, "%04u", tm.year);
+              format++;
+              break;
+            case 'm':
+              if (index + 2 >= bufLen - 1)
+                {
+                  return bufLen;
+                }
+              index +=
+                portLibrary->str_printf (portLibrary, buf + index,
+                                         bufLen - index, "%02u", tm.month);
+              format++;
+              break;
+            case 'b':
+              if (index + strlen (abbMonthName[tm.month - 1]) >= bufLen - 1)
+                {
+                  return bufLen;
+                }
+              index +=
+                portLibrary->str_printf (portLibrary, buf + index,
+                                         bufLen - index, "%s",
+                                         abbMonthName[tm.month - 1]);
+              format++;
+              break;
+            case 'd':
+              if (index + 2 >= bufLen - 1)
+                {
+                  return bufLen;
+                }
+              index +=
+                portLibrary->str_printf (portLibrary, buf + index,
+                                         bufLen - index, "%02u", tm.day);
+              format++;
+              break;
+            case 'H':
+              if (index + 2 >= bufLen - 1)
+                {
+                  return bufLen;
+                }
+              index +=
+                portLibrary->str_printf (portLibrary, buf + index,
+                                         bufLen - index, "%02u", tm.hour);
+              format++;
+              break;
+            case 'M':
+              if (index + 2 >= bufLen - 1)
+                {
+                  return bufLen;
+                }
+              index +=
+                portLibrary->str_printf (portLibrary, buf + index,
+                                         bufLen - index, "%02u", tm.minute);
+              format++;
+              break;
+            case 'S':
+              if (index + 2 >= bufLen - 1)
+                {
+                  return bufLen;
+                }
+              index +=
+                portLibrary->str_printf (portLibrary, buf + index,
+                                         bufLen - index, "%02u", tm.second);
+              format++;
+              break;
+            default:
+              /* ignore unsupported format specifiers */
+              format++;
+              break;
+            }
+          break;
+        default:
+          buf[index] = *format++;
+          index++;
+          break;
+        }
+    }
+
+  buf[index] = 0;
+
+  return index;
+}
+
+static void
+gettimestruct (I_64 millis, struct HyTimeInfo *tm)
+{
+#define HYSFT_NUM_MONTHS         (12)
+#define HYSFT_NUM_SECS_IN_MINUTE (60)
+#define HYSFT_NUM_SECS_IN_HOUR   (60*HYSFT_NUM_SECS_IN_MINUTE)
+#define HYSFT_NUM_SECS_IN_DAY    (24*(I_32)HYSFT_NUM_SECS_IN_HOUR)
+#define HYSFT_NUM_SECS_IN_YEAR   (365*HYSFT_NUM_SECS_IN_DAY)
+#define HYSFT_NUM_SECS_IN_LEAP_YEAR (366*HYSFT_NUM_SECS_IN_DAY)
+#define HYSFT_NUM_SECS_IN_JAN (31*HYSFT_NUM_SECS_IN_DAY)
+#define HYSFT_NUM_SECS_IN_FEB (28*HYSFT_NUM_SECS_IN_DAY)
+#define HYSFT_NUM_SECS_IN_MAR (31*HYSFT_NUM_SECS_IN_DAY)
+#define HYSFT_NUM_SECS_IN_APR (30*HYSFT_NUM_SECS_IN_DAY)
+#define HYSFT_NUM_SECS_IN_MAY (31*HYSFT_NUM_SECS_IN_DAY)
+#define HYSFT_NUM_SECS_IN_JUN (30*HYSFT_NUM_SECS_IN_DAY)
+#define HYSFT_NUM_SECS_IN_JUL (31*HYSFT_NUM_SECS_IN_DAY)
+#define HYSFT_NUM_SECS_IN_AUG (31*HYSFT_NUM_SECS_IN_DAY)
+#define HYSFT_NUM_SECS_IN_SEP (30*HYSFT_NUM_SECS_IN_DAY)
+#define HYSFT_NUM_SECS_IN_OCT (31*HYSFT_NUM_SECS_IN_DAY)
+#define HYSFT_NUM_SECS_IN_NOV (30*HYSFT_NUM_SECS_IN_DAY)
+#define HYSFT_NUM_SECS_IN_DEC (31*HYSFT_NUM_SECS_IN_DAY)
+#define HYSFT_NUM_SECS_IN_LEAP_FEB (29*HYSFT_NUM_SECS_IN_DAY)
+
+  I_64 timeLeft;
+  I_32 i;
+  I_32 *secondsInMonth;
+  I_32 normalSecondsInMonth[12] = {
+    HYSFT_NUM_SECS_IN_JAN,
+    HYSFT_NUM_SECS_IN_FEB,
+    HYSFT_NUM_SECS_IN_MAR,
+    HYSFT_NUM_SECS_IN_APR,
+    HYSFT_NUM_SECS_IN_MAY,
+    HYSFT_NUM_SECS_IN_JUN,
+    HYSFT_NUM_SECS_IN_JUL,
+    HYSFT_NUM_SECS_IN_AUG,
+    HYSFT_NUM_SECS_IN_SEP,
+    HYSFT_NUM_SECS_IN_OCT,
+    HYSFT_NUM_SECS_IN_NOV,
+    HYSFT_NUM_SECS_IN_DEC
+  };
+  I_32 leapYearSecondsInMonth[12] = {
+    HYSFT_NUM_SECS_IN_JAN,
+    HYSFT_NUM_SECS_IN_LEAP_FEB,
+    HYSFT_NUM_SECS_IN_MAR,
+    HYSFT_NUM_SECS_IN_APR,
+    HYSFT_NUM_SECS_IN_MAY,
+    HYSFT_NUM_SECS_IN_JUN,
+    HYSFT_NUM_SECS_IN_JUL,
+    HYSFT_NUM_SECS_IN_AUG,
+    HYSFT_NUM_SECS_IN_SEP,
+    HYSFT_NUM_SECS_IN_OCT,
+    HYSFT_NUM_SECS_IN_NOV,
+    HYSFT_NUM_SECS_IN_DEC
+  };
+  BOOLEAN leapYear = FALSE;
+
+  if (!tm)
+    return;
+
+  memset (tm, 0, sizeof (struct HyTimeInfo));
+
+  tm->year = 1970;
+
+  /* -- obtain the current time in seconds */
+  timeLeft = millis / 1000;
+  /* -- determine the year */
+  while (timeLeft)
+    {
+      I_64 numSecondsInAYear = HYSFT_NUM_SECS_IN_YEAR;
+      leapYear = FALSE;
+      if (tm->year % 4 == 0)
+        {
+          /* potential leap year */
+          if ((tm->year % 100 != 0) || (tm->year % 400 == 0))
+            {
+              /* we have leap year! */
+              leapYear = TRUE;
+              numSecondsInAYear = HYSFT_NUM_SECS_IN_LEAP_YEAR;
+            }
+        }
+      if (timeLeft < numSecondsInAYear)
+        {
+          /* under a year's time left */
+          break;
+        }
+      /* increment the year and take the appropriate number 
+         of seconds off the timeLeft */
+      tm->year++;
+      timeLeft -= numSecondsInAYear;
+    }
+  /* -- determine the month */
+  if (leapYear)
+    {
+      secondsInMonth = leapYearSecondsInMonth;
+    }
+  else
+    {
+      secondsInMonth = normalSecondsInMonth;
+    }
+  for (i = 0; i < HYSFT_NUM_MONTHS; i++)
+    {
+      if (timeLeft >= secondsInMonth[i])
+        {
+          timeLeft -= secondsInMonth[i];
+        }
+      else
+        {
+          break;
+        }
+    }
+  tm->month = i + 1;
+  /* -- determine the day of the month */
+  tm->day = 1;
+  while (timeLeft)
+    {
+      if (timeLeft >= HYSFT_NUM_SECS_IN_DAY)
+        {
+          timeLeft -= HYSFT_NUM_SECS_IN_DAY;
+        }
+      else
+        {
+          break;
+        }
+      tm->day++;
+    }
+  /* -- determine the hour of the day */
+  tm->hour = 0;
+  while (timeLeft)
+    {
+      if (timeLeft >= HYSFT_NUM_SECS_IN_HOUR)
+        {
+          timeLeft -= HYSFT_NUM_SECS_IN_HOUR;
+        }
+      else
+        {
+          break;
+        }
+      tm->hour++;
+    }
+  /* -- determine the minute of the hour */
+  tm->minute = 0;
+  while (timeLeft)
+    {
+      if (timeLeft >= HYSFT_NUM_SECS_IN_MINUTE)
+        {
+          timeLeft -= HYSFT_NUM_SECS_IN_MINUTE;
+        }
+      else
+        {
+          break;
+        }
+      tm->minute++;
+    }
+  /* -- and the rest is seconds */
+  tm->second = (U_32) timeLeft;
+
+#undef HYSFT_NUM_MONTHS
+#undef HYSFT_NUM_SECS_IN_MINUTE
+#undef HYSFT_NUM_SECS_IN_HOUR
+#undef HYSFT_NUM_SECS_IN_DAY
+#undef HYSFT_NUM_SECS_IN_YEAR
+#undef HYSFT_NUM_SECS_IN_LEAP_YEAR
+#undef HYSFT_NUM_SECS_IN_JAN
+#undef HYSFT_NUM_SECS_IN_FEB
+#undef HYSFT_NUM_SECS_IN_MAR
+#undef HYSFT_NUM_SECS_IN_APR
+#undef HYSFT_NUM_SECS_IN_MAY
+#undef HYSFT_NUM_SECS_IN_JUN
+#undef HYSFT_NUM_SECS_IN_JUL
+#undef HYSFT_NUM_SECS_IN_AUG
+#undef HYSFT_NUM_SECS_IN_SEP
+#undef HYSFT_NUM_SECS_IN_OCT
+#undef HYSFT_NUM_SECS_IN_NOV
+#undef HYSFT_NUM_SECS_IN_DEC
+#undef HYSFT_NUM_SECS_IN_LEAP_FEB
+}

Added: incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/native-src/linux.IA32/port/hystsl.c
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/native-src/linux.IA32/port/hystsl.c?rev=350181&view=auto
==============================================================================
--- incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/native-src/linux.IA32/port/hystsl.c (added)
+++ incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/native-src/linux.IA32/port/hystsl.c Wed Nov 30 21:29:27 2005
@@ -0,0 +1,238 @@
+/* Copyright 1991, 2005 The Apache Software Foundation or its licensors, as applicable
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * 
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/**
+ * @file
+ * @ingroup Port
+ * @brief Static shared library
+ */
+#include <string.h>
+#include <stdlib.h>
+#include "hyport.h"
+
+#if defined(HYVM_STATIC_LINKAGE)
+#include "hystatic.h"
+extern HyPrimitiveTable HyLinkedNatives;
+void *
+upLookupName (char *name, HyPrimitiveTableSlot * table)
+{
+  void *result;
+  while (!((table->name == (char *) 0) && (table->funcOrTable == (void *) 0)))
+    {
+      if (table->name == (char *) 0)
+        {
+          result =
+            upLookupName (name,
+                          (HyPrimitiveTableSlot *) (table->funcOrTable));
+          if (result != (void *) 0 /*HyDefaultFailPrimitive */ )
+            return result;
+        }
+      else
+        {
+          if (strcmp (name, table->name) == 0)
+            return table->funcOrTable;
+        }
+      table++;
+    }
+  return (void *) 0 /*HyDefaultFailPrimitive */ ;
+}
+
+/**
+ * Close a static library.
+ *
+ * @param[in] portLibrary The port library.
+ * @param[in] descriptor Static library handle to close.
+ *
+ * @return 0 on success, any other value on failure.
+ */
+UDATA VMCALL
+hysl_up_close_shared_library (struct HyPortLibrary *portLibrary,
+                              UDATA descriptor)
+{
+  return 0;
+}
+
+/** 
+ * Opens a static library .
+ *
+ * @param[in] portLibrary The port library.
+ * @param[in] name path Null-terminated string containing the static library.
+ * @param[out] descriptor Pointer to memory which is filled in with static-library handle on success.
+ * @param[in] decorate Boolean value indicates whether name should be decorated if it contains path information and cannot be found.
+ *
+ * @return 0 on success, any other value on failure.
+ *
+ * @note contents of descriptor are undefined on failure.
+ */
+UDATA VMCALL
+hysl_up_open_shared_library (struct HyPortLibrary * portLibrary, char *name,
+                             UDATA * descriptor, BOOLEAN decorate)
+{
+  void *result;
+  if (result = upLookupName (name, &HyLinkedNatives[0]))
+    {
+      *descriptor = (UDATA) result;
+      return 0;
+    }
+  else
+    {
+      return portLibrary->error_set_last_error_with_message (portLibrary,
+                                                             HYPORT_SL_NOT_FOUND,
+                                                             "Not statically linked");
+    }
+}
+
+/**
+ * Search for a function named 'name' taking argCount in the static library 'descriptor'.
+ *
+ * @param[in] portLibrary The port library.
+ * @param[in] descriptor Static library to search.
+ * @param[in] name Function to look up.
+ * @param[out] func Pointer to the function.
+ * @param[in] argSignature Argument signature.
+ *
+ * @return 0 on success, any other value on failure.
+ *
+ * @note contents of func are undefined on failure.
+ */
+UDATA VMCALL
+hysl_up_lookup_name (struct HyPortLibrary * portLibrary, UDATA descriptor,
+                     char *name, UDATA * func, const char *argSignature)
+{
+  void *result;
+  if (result = upLookupName (name, (HyPrimitiveTableSlot *) descriptor))
+    {
+      *func = (UDATA) result;
+      return 0;
+    }
+  else
+    {
+      return 1;
+    }
+}
+typedef struct
+{
+  UDATA descriptor;
+  UDATA tag;
+} hysplit_sl_handle;
+#define HYFLAG_SL_LINKED 0
+#define HYFLAG_SL_LOADED 1
+/* support BOTH types of libraries */
+/** 
+ * Opens a static/shared library .
+ *
+ * @param[in] portLibrary The port library.
+ * @param[in] name path Null-terminated string containing the library.
+ * @param[out] descriptor Pointer to memory which is filled in with library handle on success.
+ * @param[in] decorate Boolean value indicates whether name should be decorated if it contains path information and cannot be found.
+ *
+ * @return 0 on success, any other value on failure.
+ *
+ * @note contents of descriptor are undefined on failure.
+ */
+UDATA VMCALL
+hysl_split_open_shared_library (struct HyPortLibrary *portLibrary, char *name,
+                                UDATA * descriptor, BOOLEAN decorate)
+{
+  UDATA result;
+  UDATA tag;
+  tag = HYFLAG_SL_LINKED;
+  result =
+    hysl_up_open_shared_library (portLibrary, name, descriptor, decorate);
+  if (result == HYPORT_SL_NOT_FOUND)
+    {
+      result =
+        hysl_open_shared_library (portLibrary, name, descriptor, decorate);
+      tag = HYFLAG_SL_LOADED;
+    }
+  if (result == 0)
+    {
+      hysplit_sl_handle *handle;
+      handle =
+        portLibrary->mem_allocate_memory (portLibrary,
+                                          sizeof (hysplit_sl_handle));
+      if (handle == NULL)
+        {
+          if (tag == HYFLAG_SL_LOADED)
+            {
+              hysl_close_shared_library (portLibrary, *descriptor);
+            }
+          return portLibrary->error_set_last_error_with_message (portLibrary,
+                                                                 HYPORT_SL_INVALID,
+                                                                 "Out of memory");
+        }
+      handle->descriptor = *descriptor;
+      handle->tag = tag;
+      *descriptor = (UDATA) handle;
+    }
+  return result;
+}
+
+/**
+ * Close a static or shared library.
+ *
+ * @param[in] portLibrary The port library.
+ * @param[in] descriptor Library handle to close.
+ *
+ * @return 0 on success, any other value on failure.
+ */
+UDATA VMCALL
+hysl_split_close_shared_library (struct HyPortLibrary * portLibrary,
+                                 UDATA descriptor)
+{
+  hysplit_sl_handle *handle = (hysplit_sl_handle *) descriptor;
+  UDATA result;
+  if (handle->tag == HYFLAG_SL_LOADED)
+    {
+      result = hysl_close_shared_library (portLibrary, handle->descriptor);
+    }
+  else
+    {
+      result = hysl_up_close_shared_library (portLibrary, handle->descriptor);
+    }
+  portLibrary->mem_free_memory (portLibrary, handle);
+  return result;
+}
+
+/**
+ * Search for a function named 'name' taking argCount in the static/shared library 'descriptor'.
+ *
+ * @param[in] portLibrary The port library.
+ * @param[in] descriptor Library to search.
+ * @param[in] name Function to look up.
+ * @param[out] func Pointer to the function.
+ * @param[in] argSignature Argument signature.
+ *
+ * @return 0 on success, any other value on failure.
+ *
+ * @note contents of func are undefined on failure.
+ */
+UDATA VMCALL
+hysl_split_lookup_name (struct HyPortLibrary * portLibrary, UDATA descriptor,
+                        char *name, UDATA * func, const char *argSignature)
+{
+  hysplit_sl_handle *handle = (hysplit_sl_handle *) descriptor;
+  if (handle->tag == HYFLAG_SL_LOADED)
+    {
+      return hysl_lookup_name (portLibrary, handle->descriptor, name, func,
+                               argSignature);
+    }
+  else
+    {
+      return hysl_up_lookup_name (portLibrary, handle->descriptor, name, func,
+                                  argSignature);
+    }
+}
+#endif

Added: incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/native-src/linux.IA32/port/hysysinfo.c
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/native-src/linux.IA32/port/hysysinfo.c?rev=350181&view=auto
==============================================================================
--- incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/native-src/linux.IA32/port/hysysinfo.c (added)
+++ incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/native-src/linux.IA32/port/hysysinfo.c Wed Nov 30 21:29:27 2005
@@ -0,0 +1,848 @@
+/* Copyright 1991, 2005 The Apache Software Foundation or its licensors, as applicable
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * 
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#define CDEV_CURRENT_FUNCTION _comment_
+/**
+ * @file
+ * @ingroup Port
+ * @brief System information
+ */
+
+#undef CDEV_CURRENT_FUNCTION
+
+#include <stdlib.h>
+#include <sys/utsname.h>
+
+#include <sys/stat.h>
+#include <limits.h>
+#include <dirent.h>
+#include <errno.h>
+#include <string.h>
+#include <pwd.h>
+#include <sys/types.h>
+
+#if defined(LINUX)
+#include <sys/sysinfo.h>
+#endif
+
+#include <unistd.h>
+
+#include "portpriv.h"
+#include "hyportpg.h"
+
+#define CDEV_CURRENT_FUNCTION _prototypes_private
+static BOOLEAN isSymbolicLink (struct HyPortLibrary *portLibrary,
+                               char *filename);
+static IDATA cwdname (struct HyPortLibrary *portLibrary, char **result);
+static IDATA readSymbolicLink (struct HyPortLibrary *portLibrary,
+                               char *linkFilename, char **result);
+static IDATA searchSystemPath (struct HyPortLibrary *portLibrary,
+                               char *filename, char **result);
+
+#undef CDEV_CURRENT_FUNCTION
+
+#define CDEV_CURRENT_FUNCTION _prototypes_public
+IDATA VMCALL hysysinfo_get_executable_name (struct HyPortLibrary *portLibrary,
+                                            char *argv0, char **result);
+const char *VMCALL hysysinfo_get_OS_type (struct HyPortLibrary *portLibrary);
+U_64 VMCALL hysysinfo_get_physical_memory (struct HyPortLibrary *portLibrary);
+UDATA VMCALL hysysinfo_DLPAR_max_CPUs (struct HyPortLibrary *portLibrary);
+UDATA VMCALL hysysinfo_get_number_CPUs (struct HyPortLibrary *portLibrary);
+UDATA VMCALL hysysinfo_get_processing_capacity (struct HyPortLibrary
+                                                *portLibrary);
+const char *VMCALL hysysinfo_get_CPU_architecture (struct HyPortLibrary
+                                                   *portLibrary);
+const char *VMCALL hysysinfo_get_OS_version (struct HyPortLibrary
+                                             *portLibrary);
+I_32 VMCALL hysysinfo_startup (struct HyPortLibrary *portLibrary);
+UDATA VMCALL hysysinfo_DLPAR_enabled (struct HyPortLibrary *portLibrary);
+void VMCALL hysysinfo_shutdown (struct HyPortLibrary *portLibrary);
+UDATA VMCALL hysysinfo_get_pid (struct HyPortLibrary *portLibrary);
+U_16 VMCALL hysysinfo_get_classpathSeparator (struct HyPortLibrary
+                                              *portLibrary);
+IDATA VMCALL hysysinfo_get_username (struct HyPortLibrary *portLibrary,
+                                     char *buffer, UDATA length);
+UDATA VMCALL hysysinfo_weak_memory_consistency (struct HyPortLibrary
+                                                *portLibrary);
+IDATA VMCALL hysysinfo_get_env (struct HyPortLibrary *portLibrary,
+                                char *envVar, char *infoString,
+                                UDATA bufSize);
+
+#undef CDEV_CURRENT_FUNCTION
+
+#define CDEV_CURRENT_FUNCTION hysysinfo_get_CPU_architecture
+/**
+ * Determine the CPU architecture.
+ *
+ * @param[in] portLibrary The port library.
+ *
+ * @return A null-terminated string describing the CPU architecture of the hardware, NULL on error.
+ * 
+ * @note portLibrary is responsible for allocation/deallocation of returned buffer.
+ * @note See http://www.tolstoy.com/samizdat/sysprops.html for good values to return.
+ */
+const char *VMCALL
+hysysinfo_get_CPU_architecture (struct HyPortLibrary *portLibrary)
+{
+#if defined(LINUXPPC)
+#if defined(PPC64)
+  return HYPORT_ARCH_PPC64;
+#else
+  return HYPORT_ARCH_PPC;
+#endif
+#elif defined(HYX86)
+  return HYPORT_ARCH_X86;
+#elif defined(ARMGNU)
+  return HYPORT_ARCH_ARM;
+#else
+  return "unknown";
+#endif
+
+}
+
+#undef CDEV_CURRENT_FUNCTION
+
+#define CDEV_CURRENT_FUNCTION hysysinfo_get_env
+/**
+ * Query the operating system for environment variables.
+ *
+ * Obtain the value of the environment variable specified by envVar from the operating system 
+ * and write out to supplied buffer.
+ *
+ * @param[in] portLibrary The port library.
+ * @param[in] envVar Environment variable to query.
+ * @param[out] infoString Buffer for value string describing envVar.
+ * @param[in] bufSize Size of buffer to hold returned string.
+ *
+ * @return 0 on success, number of bytes required to hold the 
+ *	information if the output buffer was too small, -1 on failure.
+ *
+ * @note infoString is undefined on error or when supplied buffer was too small.
+ */
+IDATA VMCALL
+hysysinfo_get_env (struct HyPortLibrary * portLibrary, char *envVar,
+                   char *infoString, UDATA bufSize)
+{
+  char *value = (char *) getenv (envVar);
+  UDATA len;
+
+  if (NULL == value)
+    {
+      return -1;
+    }
+  else if ((len = strlen (value)) >= bufSize)
+    {
+      return len + 1;
+    }
+  else
+    {
+      strcpy (infoString, value);
+      return 0;
+    }
+}
+
+#undef CDEV_CURRENT_FUNCTION
+
+#define CDEV_CURRENT_FUNCTION hysysinfo_get_OS_type
+/**
+ * Determine the OS type.
+ * 
+ * @param[in] portLibrary The port library.
+ *
+ * @return OS type string (NULL terminated) on success, NULL on error.
+ *
+ * @note portLibrary is responsible for allocation/deallocation of returned buffer.
+ */
+const char *VMCALL
+hysysinfo_get_OS_type (struct HyPortLibrary *portLibrary)
+{
+
+  if (NULL == PPG_si_osType)
+    {
+      int rc;
+      int len;
+      char *buffer;
+      struct utsname sysinfo;
+
+      rc = uname (&sysinfo);
+
+      if (rc >= 0)
+        {
+          len = strlen (sysinfo.sysname) + 1;
+          buffer = portLibrary->mem_allocate_memory (portLibrary, len);
+          if (NULL == buffer)
+            {
+              return NULL;
+            }
+          /* copy and null terminte (just in case) */
+          strncpy (buffer, sysinfo.sysname, len - 1);
+          buffer[len - 1] = '\0';
+          PPG_si_osType = buffer;
+        }
+    }
+  return PPG_si_osType;
+
+}
+
+#undef CDEV_CURRENT_FUNCTION
+
+#define CDEV_CURRENT_FUNCTION hysysinfo_get_OS_version
+/**
+ * Determine version information from the operating system.
+ *
+ * @param[in] portLibrary The port library.
+ *
+ * @return OS version string (NULL terminated) on success, NULL on error.
+ *
+ * @note portLibrary is responsible for allocation/deallocation of returned buffer.
+ */
+const char *VMCALL
+hysysinfo_get_OS_version (struct HyPortLibrary *portLibrary)
+{
+
+  if (NULL == PPG_si_osVersion)
+    {
+      int rc;
+      struct utsname sysinfo;
+
+      rc = uname (&sysinfo);
+
+      if (rc >= 0)
+        {
+          int len;
+          char *buffer;
+
+          len = strlen (sysinfo.release) + 1;
+          buffer = portLibrary->mem_allocate_memory (portLibrary, len);
+          if (NULL == buffer)
+            {
+              return NULL;
+            }
+          strncpy (buffer, sysinfo.release, len);
+          buffer[len - 1] = '\0';
+
+          PPG_si_osVersion = buffer;
+        }
+    }
+  return PPG_si_osVersion;
+
+}
+
+#undef CDEV_CURRENT_FUNCTION
+
+#define CDEV_CURRENT_FUNCTION hysysinfo_get_pid
+/**
+ * Determine the process ID of the calling process.
+ *
+ * @param[in] portLibrary The port library.
+ *
+ * @return the PID.
+ */
+UDATA VMCALL
+hysysinfo_get_pid (struct HyPortLibrary * portLibrary)
+{
+
+  return getpid ();
+
+}
+
+#undef CDEV_CURRENT_FUNCTION
+
+#define CDEV_CURRENT_FUNCTION hysysinfo_get_classpathSeparator
+/**
+ * Determine the character used to separate entries on the classpath.
+ *
+ * @param[in] portLibrary The port library.
+ *
+ * @return the classpathSeparator character.
+ */
+U_16 VMCALL
+hysysinfo_get_classpathSeparator (struct HyPortLibrary * portLibrary)
+{
+  return ':';
+}
+
+#undef CDEV_CURRENT_FUNCTION
+
+#define CDEV_CURRENT_FUNCTION hysysinfo_get_executable_name
+/**
+ * Determines an absolute pathname for the executable.
+ * 
+ * @param[in] portLibrary The port library.
+ * @param[in] argv0 argv[0] value
+ * @param[out] result Null terminated pathname string
+ * 
+ * @return 0 on success, -1 on error (or information is not available).
+ *
+ * @note Caller is responsible for de-allocating memory for result buffer with @ref hymem_free_memory.
+ */
+IDATA VMCALL
+hysysinfo_get_executable_name (struct HyPortLibrary * portLibrary,
+                               char *argv0, char **result)
+{
+#if defined(LINUX)
+  return readSymbolicLink (portLibrary, "/proc/self/exe", result);
+#else
+  IDATA retval = -1;
+  IDATA length;
+  char *p;
+  char *currentName = NULL;
+  char *currentPath = NULL;
+  char *originalWorkingDirectory = NULL;
+
+  if (!argv0)
+    {
+      return -1;
+    }
+  currentPath =
+    (portLibrary->mem_allocate_memory) (portLibrary, strlen (argv0) + 1);
+  if (currentPath)
+    {
+      strcpy (currentPath, argv0);
+    }
+  if (!currentPath)
+    {
+      retval = -1;
+      goto cleanup;
+    }
+  retval = cwdname (portLibrary, &originalWorkingDirectory);
+  if (retval)
+    {
+      retval = -1;
+      goto cleanup;
+    }
+gotPathName:
+  /* split path into directory part and filename part. */
+  p = strrchr (currentPath, '/');
+  if (p)
+    {
+      *p++ = '\0';
+      currentName =
+        (portLibrary->mem_allocate_memory) (portLibrary, strlen (p) + 1);
+      if (!currentName)
+        {
+          retval = -1;
+          goto cleanup;
+        }
+      strcpy (currentName, p);
+    }
+  else
+    {
+      currentName = currentPath;
+      currentPath = NULL;
+      retval = searchSystemPath (portLibrary, currentName, &currentPath);
+      if (retval)
+        {
+          retval = -1;
+          goto cleanup;
+        }
+    }
+  /* go there */
+  if (currentPath)
+    {
+      if (currentPath[0])
+        {
+          if (0 != chdir (currentPath))
+            {
+              retval = -1;
+              goto cleanup;
+            }
+        }
+      (portLibrary->mem_free_memory) (portLibrary, currentPath);
+      currentPath = NULL;
+    }
+  if (isSymbolicLink (portLibrary, currentName))
+    {
+      /* try to follow the link. */
+      retval = readSymbolicLink (portLibrary, currentName, &currentPath);
+#if defined(DEBUG)
+      (portLibrary->tty_printf) (portLibrary, "read cp=%s\n", currentPath);
+#endif
+      if (retval)
+        {
+          retval = -1;
+          goto cleanup;
+        }
+      (portLibrary->mem_free_memory) (portLibrary, currentName);
+      currentName = NULL;
+      goto gotPathName;
+    }
+  retval = cwdname (portLibrary, &currentPath);
+  if (retval)
+    {
+      retval = -1;
+      goto cleanup;
+    }
+  /* Put name and path back together */
+  *result =
+    (portLibrary->mem_allocate_memory) (portLibrary,
+                                        strlen (currentPath) +
+                                        strlen (currentName) + 2);
+  if (!*result)
+    {
+      retval = -1;
+      goto cleanup;
+    }
+  strcpy (*result, currentPath);
+  if (currentPath[0] && (currentPath[strlen (currentPath) - 1] != '/'))
+    {
+      strcat (*result, "/");
+    }
+  strcat (*result, currentName);
+  /* Finished. */
+  retval = 0;
+cleanup:
+  if (originalWorkingDirectory)
+    {
+      chdir (originalWorkingDirectory);
+      (portLibrary->mem_free_memory) (portLibrary, originalWorkingDirectory);
+      originalWorkingDirectory = NULL;
+    }
+  if (currentPath)
+    {
+      (portLibrary->mem_free_memory) (portLibrary, currentPath);
+      currentPath = NULL;
+    }
+  if (currentName)
+    {
+      (portLibrary->mem_free_memory) (portLibrary, currentName);
+      currentName = NULL;
+    }
+  return retval;
+#endif
+
+}
+
+#undef CDEV_CURRENT_FUNCTION
+
+#define CDEV_CURRENT_FUNCTION cwdname
+/**
+ * @internal  Returns the current working directory.  
+ *
+ * @return 0 on success, -1 on failure.
+ *
+ * @note The buffer to hold this string (including its terminating NUL) is allocated with 
+ * portLibrary->mem_allocate_memory.  The caller should free this memory with 
+ * portLibrary->mem_free_memory when it is no longer needed. 
+ */
+static IDATA
+cwdname (struct HyPortLibrary *portLibrary, char **result)
+{
+  char *cwd;
+  int allocSize = 256;
+
+doAlloc:
+  cwd = (portLibrary->mem_allocate_memory) (portLibrary, allocSize);
+  if (!cwd)
+    {
+      return -1;
+    }
+  if (!getcwd (cwd, allocSize - 1))
+    {
+      (portLibrary->mem_free_memory) (portLibrary, cwd);
+      if (errno == ERANGE)
+        {
+          allocSize += 256;
+          goto doAlloc;
+        }
+      return -1;
+    }
+  *result = cwd;
+  return 0;
+}
+
+#undef CDEV_CURRENT_FUNCTION
+
+#define CDEV_CURRENT_FUNCTION isSymbolicLink
+/**
+ * @internal  Examines the named file to determine if it is a symbolic link.  On platforms which don't have
+ * symbolic links (or where we can't tell) or if an unexpected error occurs, just answer FALSE.
+ */
+static BOOLEAN
+isSymbolicLink (struct HyPortLibrary *portLibrary, char *filename)
+{
+  struct stat statbuf;
+#if defined(DEBUG)
+  portLibrary->tty_printf (portLibrary, "isSymbolicLink(\"%s\")\n", filename);
+#endif
+
+  if (!lstat (filename, &statbuf))
+    {
+      if (S_ISLNK (statbuf.st_mode))
+        {
+#if defined(DEBUG)
+          portLibrary->tty_printf (portLibrary, "TRUE\n");
+#endif
+
+          return TRUE;
+        }
+    }
+#if defined(DEBUG)
+  portLibrary->tty_printf (portLibrary, "FALSE\n");
+#endif
+
+  return FALSE;
+}
+
+#undef CDEV_CURRENT_FUNCTION
+
+#define CDEV_CURRENT_FUNCTION readSymbolicLink
+/**
+ * @internal  Attempts to read the contents of a symbolic link.  (The contents are the relative pathname of
+ * the thing linked to).  A buffer large enough to hold the result (and the terminating NUL) is
+ * allocated with portLibrary->mem_allocate_memory.  The caller should free this buffer with
+ * portLibrary->mem_free_memory when it is no longer needed.
+ * On success, returns 0.  On error, returns -1.
+ */
+static IDATA
+readSymbolicLink (struct HyPortLibrary *portLibrary, char *linkFilename,
+                  char **result)
+{
+  /* TODO: remove this ifdef and find out what other builds break (if any) */
+#if defined(LINUX)
+  char fixedBuffer[PATH_MAX + 1];
+  int size = readlink (linkFilename, fixedBuffer, sizeof (fixedBuffer) - 1);
+#if defined(DEBUG)
+  portLibrary->tty_printf (portLibrary, "readSymbolicLink: \"%s\"\n%i\n",
+                           linkFilename, size);
+#endif
+  if (size <= 0)
+    {
+      return -1;
+    }
+  fixedBuffer[size++] = '\0';
+  *result = (portLibrary->mem_allocate_memory) (portLibrary, size);
+  if (!*result)
+    {
+      return -1;
+    }
+  strcpy (*result, fixedBuffer);
+  return 0;
+#else
+  return -1;
+#endif
+
+}
+
+#undef CDEV_CURRENT_FUNCTION
+
+#define CDEV_CURRENT_FUNCTION searchSystemPath
+/**
+ * @internal  Searches through the system PATH for the named file.  If found, it returns the path entry
+ * which matched the file.  A buffer large enough to hold the proper path entry (without a
+ * trailing slash, but with the terminating NUL) is allocated with portLibrary->mem_allocate_memory.
+ * The caller should free this buffer with portLibrary->mem_free_memory when it is no longer
+ * needed.  On success, returns 0.  On error (including if the file is not found), -1 is returned.
+ */
+static IDATA
+searchSystemPath (struct HyPortLibrary *portLibrary, char *filename,
+                  char **result)
+{
+  char *pathCurrent;
+  char *pathNext;
+  int length;
+  DIR *sdir = NULL;
+  struct dirent *dirEntry;
+  /* This should be sufficient for a single entry in the PATH var, though the var itself */
+  /* could be considerably longer.. */
+  char temp[PATH_MAX + 1];
+
+  /* We use getenv() instead of the portLibrary function because getenv() doesn't require */
+  /* us to allocate any memory, or guess how much to allocate. */
+  if (!(pathNext = getenv ("PATH")))
+    {
+      return -1;
+    }
+
+  while (pathNext)
+    {
+      pathCurrent = pathNext;
+      pathNext = strchr (pathCurrent, ':');
+      if (pathNext)
+        {
+          length = (pathNext - pathCurrent);
+          pathNext += 1;
+        }
+      else
+        {
+          length = strlen (pathCurrent);
+        }
+      if (length > PATH_MAX)
+        {
+          length = PATH_MAX;
+        }
+      memcpy (temp, pathCurrent, length);
+      temp[length] = '\0';
+#if defined(DEBUG)
+      (portLibrary->tty_printf) (portLibrary, "Searching path: \"%s\"\n",
+                                 temp);
+#endif
+
+      if (!length)
+        {                       /* empty path entry */
+          continue;
+        }
+      if (sdir = opendir (temp))
+        {
+          while (dirEntry = readdir (sdir))
+            {
+#if defined(DEBUG)
+              (portLibrary->tty_printf) (portLibrary, "dirent: \"%s\"\n",
+                                         dirEntry->d_name);
+#endif
+
+              if (!strcmp (dirEntry->d_name, filename))
+                {
+                  closedir (sdir);
+                  /* found! */
+                  *result =
+                    (portLibrary->mem_allocate_memory) (portLibrary,
+                                                        strlen (temp) + 1);
+                  if (!result)
+                    {
+                      return -1;
+                    }
+                  strcpy (*result, temp);
+                  return 0;
+                }
+            }
+          closedir (sdir);
+        }
+    }
+  /* not found */
+  return -1;
+}
+
+#undef CDEV_CURRENT_FUNCTION
+
+#define CDEV_CURRENT_FUNCTION hysysinfo_get_number_CPUs
+/**
+ * Determine the number of CPUs on this platform.
+ *
+ * @param[in] portLibrary The port library.
+ *
+ * @return The number of supported CPUs.
+ */
+UDATA VMCALL
+hysysinfo_get_number_CPUs (struct HyPortLibrary * portLibrary)
+{
+#if defined(LINUX)
+  /* returns number of online(_SC_NPROCESSORS_ONLN) processors, number configured(_SC_NPROCESSORS_CONF) may  be more than online */
+  return sysconf (_SC_NPROCESSORS_ONLN);
+#else
+  return 0;
+#endif
+
+}
+
+#undef CDEV_CURRENT_FUNCTION
+
+#define CDEV_CURRENT_FUNCTION hysysinfo_get_physical_memory
+/**
+ * Determine the size of the total physical memory in the system, in bytes.
+ * 
+ * @param[in] portLibrary The port library.
+ *
+ * @return 0 if the information was unavailable, otherwise total physical memory in bytes.
+ */
+U_64 VMCALL
+hysysinfo_get_physical_memory (struct HyPortLibrary * portLibrary)
+{
+
+  IDATA pagesize, num_pages;
+
+  pagesize = sysconf (_SC_PAGESIZE);
+  num_pages = sysconf (_SC_PHYS_PAGES);
+
+  if (pagesize == -1 || num_pages == -1)
+    {
+      return 0;
+    }
+  else
+    {
+      return (U_64) pagesize *num_pages;
+    }
+
+}
+
+#undef CDEV_CURRENT_FUNCTION
+
+#define CDEV_CURRENT_FUNCTION hysysinfo_DLPAR_enabled
+/**
+ * Determine if DLPAR (i.e. the ability to change number of CPUs and amount of memory dynamically)
+ * is enabled on this platform.
+ *
+ * @param[in] portLibrary The port library.
+ *
+ * @return 1 if DLPAR is supported, otherwise 0.
+ */
+UDATA VMCALL
+hysysinfo_DLPAR_enabled (struct HyPortLibrary * portLibrary)
+{
+
+  return FALSE;
+}
+
+#undef CDEV_CURRENT_FUNCTION
+
+#define CDEV_CURRENT_FUNCTION hysysinfo_weak_memory_consistency
+/**
+ * Determine if the platform has weak memory consistency behaviour.
+ * 
+ * @param[in] portLibrary The port library.
+ *
+ * @return 1 if weak memory consistency, 0 otherwise.
+ */
+UDATA VMCALL
+hysysinfo_weak_memory_consistency (struct HyPortLibrary * portLibrary)
+{
+  return FALSE;
+}
+
+#undef CDEV_CURRENT_FUNCTION
+
+#define CDEV_CURRENT_FUNCTION hysysinfo_shutdown
+/**
+ * PortLibrary shutdown.
+ *
+ * This function is called during shutdown of the portLibrary.  Any resources that were created by @ref hysysinfo_startup
+ * should be destroyed here.
+ *
+ * @param[in] portLibrary The port library.
+ *
+ * @note Most implementations will be empty.
+ */
+void VMCALL
+hysysinfo_shutdown (struct HyPortLibrary *portLibrary)
+{
+  if (PPG_si_osVersion)
+    {
+      portLibrary->mem_free_memory (portLibrary, PPG_si_osVersion);
+      PPG_si_osVersion = NULL;
+    }
+
+  if (PPG_si_osType)
+    {
+      portLibrary->mem_free_memory (portLibrary, PPG_si_osType);
+      PPG_si_osType = NULL;
+    }
+}
+
+#undef CDEV_CURRENT_FUNCTION
+
+#define CDEV_CURRENT_FUNCTION hysysinfo_startup
+/**
+ * PortLibrary startup.
+ *
+ * This function is called during startup of the portLibrary.  Any resources that are required for
+ * the system information operations may be created here.  All resources created here should be destroyed
+ * in @ref hysysinfo_shutdown.
+ *
+ * @param[in] portLibrary The port library.
+ *
+ * @return 0 on success, negative error code on failure.  Error code values returned are
+ * \arg HYPORT_ERROR_STARTUP_SYSINFO
+ *
+ * @note Most implementations will simply return success.
+ */
+I_32 VMCALL
+hysysinfo_startup (struct HyPortLibrary *portLibrary)
+{
+  return 0;
+}
+
+#undef CDEV_CURRENT_FUNCTION
+
+#define CDEV_CURRENT_FUNCTION hysysinfo_DLPAR_max_CPUs
+/**
+ * Determine the maximum number of CPUs on this platform
+ *
+ * @param[in] portLibrary The port library.
+ *
+ * @return The maximum number of supported CPUs..
+ */
+UDATA VMCALL
+hysysinfo_DLPAR_max_CPUs (struct HyPortLibrary * portLibrary)
+{
+
+  return portLibrary->sysinfo_get_number_CPUs (portLibrary);
+
+}
+
+#undef CDEV_CURRENT_FUNCTION
+
+#define CDEV_CURRENT_FUNCTION hysysinfo_get_username
+/**
+ * Query the operating system for the name of the user associate with the current thread
+ * 
+ * Obtain the value of the name of the user associated with the current thread, and then write it out into the buffer
+* supplied by the user
+*
+* @param[in] portLibrary The port Library
+* @param[out] buffer Buffer for the name of the user
+* @param[in,out] length The length of the buffer
+*
+* @return 0 on success, number of bytes required to hold the 
+* information if the output buffer was too small, -1 on failure.
+*
+* @note buffer is undefined on error or when supplied buffer was too small.
+*/
+IDATA VMCALL
+hysysinfo_get_username (struct HyPortLibrary * portLibrary, char *buffer,
+                        UDATA length)
+{
+  uid_t uid = getuid ();
+  int nameLen;
+  struct passwd *pwent = getpwuid (uid);
+
+  if (pwent == NULL)
+    {
+      return -1;
+    }
+
+  nameLen = strlen (pwent->pw_name);
+
+  if ((nameLen + 1) > length)
+    {
+      return nameLen + 1;
+    }
+
+  portLibrary->str_printf (portLibrary, buffer, length, "%s", pwent->pw_name);
+
+  return 0;
+}
+
+#undef CDEV_CURRENT_FUNCTION
+
+#define CDEV_CURRENT_FUNCTION hysysinfo_get_processing_capacity
+/**
+ * Determine the collective processing capacity available to the VM
+ * in units of 1% of a physical processor. In environments without
+ * some kind of virtual partitioning, this will simply be the number
+ * of CPUs * 100.
+ *
+ * @param[in] portLibrary The port library.
+ *
+ * @return The processing capacity available to the VM.
+ */
+UDATA VMCALL
+hysysinfo_get_processing_capacity (struct HyPortLibrary * portLibrary)
+{
+
+  return portLibrary->sysinfo_get_number_CPUs (portLibrary) * 100;
+
+}
+
+#undef CDEV_CURRENT_FUNCTION



Mime
View raw message