Ashley Mills / axTLS
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers os_port.c Source File

os_port.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2007, Cameron Rich
00003  * 
00004  * All rights reserved.
00005  * 
00006  * Redistribution and use in source and binary forms, with or without 
00007  * modification, are permitted provided that the following conditions are met:
00008  *
00009  * * Redistributions of source code must retain the above copyright notice, 
00010  *   this list of conditions and the following disclaimer.
00011  * * Redistributions in binary form must reproduce the above copyright notice, 
00012  *   this list of conditions and the following disclaimer in the documentation 
00013  *   and/or other materials provided with the distribution.
00014  * * Neither the name of the axTLS project nor the names of its contributors 
00015  *   may be used to endorse or promote products derived from this software 
00016  *   without specific prior written permission.
00017  *
00018  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00019  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00020  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
00021  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
00022  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00023  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00024  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00025  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00026  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00027  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00028  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00029  */
00030 
00031 /**
00032  * @file os_port.c
00033  *
00034  * OS specific functions.
00035  */
00036 #include <time.h>
00037 #include <stdlib.h>
00038 #include <errno.h>
00039 #include <stdarg.h>
00040 #include "os_port.h"
00041 #include <stdio.h>
00042 #include "sockets.h"
00043 
00044 #ifdef MBED
00045 /**
00046  * gettimeofday() not in mbed 
00047  */
00048 EXP_FUNC void STDCALL gettimeofday(struct timeval* t, void* timezone)
00049 {       
00050     t->tv_sec = time(NULL);
00051     t->tv_usec = 0;                         /* 1sec precision only */ 
00052 
00053 }
00054 
00055 #endif
00056 
00057 #ifdef WIN32
00058 /**
00059  * gettimeofday() not in Win32 
00060  */
00061 EXP_FUNC void STDCALL gettimeofday(struct timeval* t, void* timezone)
00062 {       
00063 #if defined(_WIN32_WCE)
00064     t->tv_sec = time(NULL);
00065     t->tv_usec = 0;                         /* 1sec precision only */ 
00066 #else
00067     struct _timeb timebuffer;
00068     _ftime(&timebuffer);
00069     t->tv_sec = (long)timebuffer.time;
00070     t->tv_usec = 1000 * timebuffer.millitm; /* 1ms precision */
00071 #endif
00072 }
00073 
00074 
00075 /**
00076  * strcasecmp() not in Win32
00077  */
00078 EXP_FUNC int STDCALL strcasecmp(const char *s1, const char *s2)
00079 {
00080     while (tolower(*s1) == tolower(*s2++))
00081     {
00082         if (*s1++ == '\0')
00083         {
00084             return 0;
00085         }
00086     }
00087 
00088     return *(unsigned char *)s1 - *(unsigned char *)(s2 - 1);
00089 }
00090 
00091 
00092 EXP_FUNC int STDCALL getdomainname(char *buf, int buf_size)
00093 {
00094     HKEY hKey;
00095     unsigned long datatype;
00096     unsigned long bufferlength = buf_size;
00097 
00098     if (RegOpenKeyEx(HKEY_LOCAL_MACHINE,
00099             TEXT("SYSTEM\\CurrentControlSet\\Services\\Tcpip\\Parameters"),
00100                         0, KEY_QUERY_VALUE, &hKey) != ERROR_SUCCESS)
00101         return -1;
00102 
00103     RegQueryValueEx(hKey, "Domain", NULL, &datatype, buf, &bufferlength);
00104     RegCloseKey(hKey);
00105     return 0; 
00106 }
00107 #endif
00108 
00109 #undef malloc
00110 #undef realloc
00111 #undef calloc
00112 
00113 static const char * out_of_mem_str = "out of memory";
00114 static const char * file_open_str = "Could not open file \"%s\"";
00115 
00116 /* 
00117  * Some functions that call display some error trace and then call abort().
00118  * This just makes life much easier on embedded systems, since we're 
00119  * suffering major trauma...
00120  */
00121 EXP_FUNC void * STDCALL ax_malloc(size_t s)
00122 {
00123     void *x;
00124 
00125     if ((x = malloc(s)) == NULL)
00126         exit_now(out_of_mem_str);
00127 
00128     return x;
00129 }
00130 
00131 EXP_FUNC void * STDCALL ax_realloc(void *y, size_t s)
00132 {
00133     void *x;
00134 
00135     if ((x = realloc(y, s)) == NULL)
00136         exit_now(out_of_mem_str);
00137 
00138     return x;
00139 }
00140 
00141 EXP_FUNC void * STDCALL ax_calloc(size_t n, size_t s)
00142 {
00143     void *x;
00144   
00145     if ((x = calloc(n, s)) == NULL) {
00146         exit_now(out_of_mem_str);
00147     }
00148 
00149     return x;
00150 }
00151 /*
00152 EXP_FUNC int STDCALL ax_open(const char *pathname, int flags)
00153 {
00154     int x;
00155 
00156     if ((x = open(pathname, flags)) < 0)
00157         exit_now(file_open_str, pathname);
00158 
00159     return x;
00160 }
00161 */
00162 
00163 /**
00164  * This is a call which will deliberately exit an application, but will
00165  * display some information before dying.
00166  */
00167 void exit_now(const char *format, ...)
00168 {
00169     va_list argp;
00170 
00171     va_start(argp, format);
00172     vfprintf(stderr, format, argp);
00173     va_end(argp);
00174     abort();
00175 }
00176