Imported Upstream version 0.7.20
[platform/upstream/libsolv.git] / src / util.c
index d8ae7ca..72426e0 100644 (file)
 #include <stdlib.h>
 #include <unistd.h>
 #include <string.h>
-#include <sys/time.h>
+#include <fcntl.h>
+#ifdef _WIN32
+  #include <windows.h>
+  #include <io.h>
+#else
+  #include <sys/time.h>
+#endif
 
 #include "util.h"
 
@@ -76,6 +82,30 @@ solv_calloc(size_t num, size_t len)
   return r;
 }
 
+/* this was solv_realloc2(old, len, size), but we now overshoot
+ * for huge len sizes */
+void *
+solv_extend_realloc(void *old, size_t len, size_t size, size_t block)
+{
+  size_t xblock = (block + 1) << 5;
+  len = (len + block) & ~block;
+  if (len >= xblock && xblock)
+    {
+      xblock <<= 1;
+      while (len >= xblock && xblock)
+       xblock <<= 1;
+      if (xblock)
+       {
+         size_t nlen;
+          xblock = (xblock >> 5) - 1;
+         nlen = (len + xblock) & ~xblock;
+         if (nlen > len)
+           len = nlen;
+       }
+    }
+  return solv_realloc2(old, len, size);
+}
+
 void *
 solv_free(void *mem)
 {
@@ -99,6 +129,9 @@ solv_strdup(const char *s)
 unsigned int
 solv_timems(unsigned int subtract)
 {
+#ifdef _WIN32
+  return GetTickCount() - subtract;
+#else
   struct timeval tv;
   unsigned int r;
 
@@ -108,6 +141,17 @@ solv_timems(unsigned int subtract)
   r += ((unsigned int)tv.tv_sec & 0xffff) * 1000;
   r += (unsigned int)tv.tv_usec / 1000;
   return r - subtract;
+#endif
+}
+
+int
+solv_setcloexec(int fd, int state)
+{
+  #ifdef _WIN32
+    return SetHandleInformation((HANDLE) _get_osfhandle(fd), HANDLE_FLAG_INHERIT, state ? 0 : HANDLE_FLAG_INHERIT);
+  #else
+    return fcntl(fd, F_SETFD, state ? FD_CLOEXEC : 0) == 0;
+  #endif
 }
 
 /* bsd's qsort_r has different arguments, so we define our
@@ -115,7 +159,7 @@ solv_timems(unsigned int subtract)
 
    see also: http://sources.redhat.com/ml/libc-alpha/2008-12/msg00003.html
  */
-#if defined(__GLIBC__) && (defined(HAVE_QSORT_R) || defined(HAVE___QSORT_R))
+#if (defined(__GLIBC__) || defined(__NEWLIB__)) && (defined(HAVE_QSORT_R) || defined(HAVE___QSORT_R))
 
 void
 solv_sort(void *base, size_t nmemb, size_t size, int (*compar)(const void *, const void *, void *), void *compard)
@@ -211,7 +255,7 @@ solv_hex2bin(const char **strp, unsigned char *buf, int bufl)
         d = c - ('A' - 10);
       else
        break;
-      c = *++str;
+      c = str[1];
       d <<= 4;
       if (c >= '0' && c <= '9')
         d |= c - '0';
@@ -222,7 +266,7 @@ solv_hex2bin(const char **strp, unsigned char *buf, int bufl)
       else
        break;
       buf[i] = d;
-      ++str;
+      str += 2;
     }
   *strp = str;
   return i;