os_port.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*
  2. * Copyright (c) 2007, Cameron Rich
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are met:
  8. *
  9. * * Redistributions of source code must retain the above copyright notice,
  10. * this list of conditions and the following disclaimer.
  11. * * Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. * * Neither the name of the axTLS project nor the names of its contributors
  15. * may be used to endorse or promote products derived from this software
  16. * without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  22. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  23. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  24. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  25. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  26. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  27. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  28. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. */
  30. /**
  31. * @file os_port.c
  32. *
  33. * OS specific functions.
  34. */
  35. #include <time.h>
  36. #include <stdlib.h>
  37. #include <errno.h>
  38. #include <stdarg.h>
  39. #include "os_port.h"
  40. #ifdef WIN32
  41. /**
  42. * gettimeofday() not in Win32
  43. */
  44. EXP_FUNC void STDCALL gettimeofday(struct timeval* t, void* timezone)
  45. {
  46. #if defined(_WIN32_WCE)
  47. t->tv_sec = time(NULL);
  48. t->tv_usec = 0; /* 1sec precision only */
  49. #else
  50. struct _timeb timebuffer;
  51. _ftime(&timebuffer);
  52. t->tv_sec = (long)timebuffer.time;
  53. t->tv_usec = 1000 * timebuffer.millitm; /* 1ms precision */
  54. #endif
  55. }
  56. /**
  57. * strcasecmp() not in Win32
  58. */
  59. EXP_FUNC int STDCALL strcasecmp(const char *s1, const char *s2)
  60. {
  61. while (tolower(*s1) == tolower(*s2++))
  62. {
  63. if (*s1++ == '\0')
  64. {
  65. return 0;
  66. }
  67. }
  68. return *(unsigned char *)s1 - *(unsigned char *)(s2 - 1);
  69. }
  70. EXP_FUNC int STDCALL getdomainname(char *buf, int buf_size)
  71. {
  72. HKEY hKey;
  73. unsigned long datatype;
  74. unsigned long bufferlength = buf_size;
  75. if (RegOpenKeyEx(HKEY_LOCAL_MACHINE,
  76. TEXT("SYSTEM\\CurrentControlSet\\Services\\Tcpip\\Parameters"),
  77. 0, KEY_QUERY_VALUE, &hKey) != ERROR_SUCCESS)
  78. return -1;
  79. RegQueryValueEx(hKey, "Domain", NULL, &datatype, buf, &bufferlength);
  80. RegCloseKey(hKey);
  81. return 0;
  82. }
  83. #endif
  84. #undef malloc
  85. #undef realloc
  86. #undef calloc
  87. static const char * out_of_mem_str = "out of memory";
  88. static const char * file_open_str = "Could not open file \"%s\"";
  89. /*
  90. * Some functions that call display some error trace and then call abort().
  91. * This just makes life much easier on embedded systems, since we're
  92. * suffering major trauma...
  93. */
  94. EXP_FUNC void * STDCALL ax_malloc(size_t s)
  95. {
  96. void *x;
  97. if ((x = malloc(s)) == NULL)
  98. exit_now(out_of_mem_str);
  99. return x;
  100. }
  101. EXP_FUNC void * STDCALL ax_realloc(void *y, size_t s)
  102. {
  103. void *x;
  104. if ((x = realloc(y, s)) == NULL)
  105. exit_now(out_of_mem_str);
  106. return x;
  107. }
  108. EXP_FUNC void * STDCALL ax_calloc(size_t n, size_t s)
  109. {
  110. void *x;
  111. if ((x = calloc(n, s)) == NULL)
  112. exit_now(out_of_mem_str);
  113. return x;
  114. }
  115. EXP_FUNC int STDCALL ax_open(const char *pathname, int flags)
  116. {
  117. int x;
  118. if ((x = open(pathname, flags)) < 0)
  119. exit_now(file_open_str, pathname);
  120. return x;
  121. }
  122. /**
  123. * This is a call which will deliberately exit an application, but will
  124. * display some information before dying.
  125. */
  126. void exit_now(const char *format, ...)
  127. {
  128. va_list argp;
  129. va_start(argp, format);
  130. vfprintf(stderr, format, argp);
  131. va_end(argp);
  132. abort();
  133. }