| 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 | /** |
| 32 | * @file os_port.h |
| 33 | * |
| 34 | * Some stuff to minimise the differences between windows and linux/unix |
| 35 | */ |
| 36 | |
| 37 | #ifndef HEADER_OS_PORT_H |
| 38 | #define HEADER_OS_PORT_H |
| 39 | |
| 40 | #ifdef __cplusplus |
| 41 | extern "C" { |
| 42 | #endif |
| 43 | |
| 44 | #include <stdio.h> |
| 45 | |
| 46 | #if defined(WIN32) |
| 47 | #define STDCALL /* Would be __stdcall but we don't want it */ |
| 48 | #define EXP_FUNC __declspec(dllexport) |
| 49 | #else |
| 50 | #define STDCALL |
| 51 | #define EXP_FUNC |
| 52 | #endif |
| 53 | |
| 54 | #if defined(_WIN32_WCE) |
| 55 | #undef WIN32 |
| 56 | #define WIN32 |
| 57 | #endif |
| 58 | |
| 59 | #ifdef WIN32 |
| 60 | |
| 61 | /* Windows CE stuff */ |
| 62 | #if defined(_WIN32_WCE) |
| 63 | #include <basetsd.h> |
| 64 | #define abort() exit(1) |
| 65 | #else |
| 66 | #include <io.h> |
| 67 | #include <process.h> |
| 68 | #include <sys/timeb.h> |
| 69 | #include <fcntl.h> |
| 70 | #endif /* _WIN32_WCE */ |
| 71 | |
| 72 | #include <winsock.h> |
| 73 | #include <direct.h> |
| 74 | #undef getpid |
| 75 | #undef open |
| 76 | #undef close |
| 77 | #undef sleep |
| 78 | #undef gettimeofday |
| 79 | #undef dup2 |
| 80 | #undef unlink |
| 81 | |
| 82 | #define SOCKET_READ(A,B,C) recv(A,B,C,0) |
| 83 | #define SOCKET_WRITE(A,B,C) send(A,B,C,0) |
| 84 | #define SOCKET_CLOSE(A) closesocket(A) |
| 85 | #define srandom(A) srand(A) |
| 86 | #define random() rand() |
| 87 | #define getpid() _getpid() |
| 88 | #define snprintf _snprintf |
| 89 | #define open(A,B) _open(A,B) |
| 90 | #define dup2(A,B) _dup2(A,B) |
| 91 | #define unlink(A) _unlink(A) |
| 92 | #define close(A) _close(A) |
| 93 | #define read(A,B,C) _read(A,B,C) |
| 94 | #define write(A,B,C) _write(A,B,C) |
| 95 | #define sleep(A) Sleep(A*1000) |
| 96 | #define usleep(A) Sleep(A/1000) |
| 97 | #define strdup(A) _strdup(A) |
| 98 | #define chroot(A) _chdir(A) |
| 99 | #define chdir(A) _chdir(A) |
| 100 | #ifndef lseek |
| 101 | #define lseek(A,B,C) _lseek(A,B,C) |
| 102 | #endif |
| 103 | |
| 104 | /* This fix gets around a problem where a win32 application on a cygwin xterm |
| 105 | doesn't display regular output (until a certain buffer limit) - but it works |
| 106 | fine under a normal DOS window. This is a hack to get around the issue - |
| 107 | see http://www.khngai.com/emacs/tty.php */ |
| 108 | #define TTY_FLUSH() if (!_isatty(_fileno(stdout))) fflush(stdout); |
| 109 | |
| 110 | |
| 111 | typedef UINT8 uint8_t; |
| 112 | typedef INT8 int8_t; |
| 113 | typedef UINT16 uint16_t; |
| 114 | typedef INT16 int16_t; |
| 115 | typedef UINT32 uint32_t; |
| 116 | typedef INT32 int32_t; |
| 117 | typedef UINT64 uint64_t; |
| 118 | typedef INT64 int64_t; |
| 119 | typedef int socklen_t; |
| 120 | |
| 121 | #else /* Not Win32 */ |
| 122 | |
| 123 | #ifdef __sun__ |
| 124 | #include <inttypes.h> |
| 125 | #else |
| 126 | #include <stdint.h> |
| 127 | #endif /* Not Solaris */ |
| 128 | |
| 129 | #include <unistd.h> |
| 130 | #include <pwd.h> |
| 131 | #include <netdb.h> |
| 132 | #include <dirent.h> |
| 133 | #include <fcntl.h> |
| 134 | #include <errno.h> |
| 135 | #include <sys/stat.h> |
| 136 | #include <sys/time.h> |
| 137 | #include <sys/socket.h> |
| 138 | #include <sys/wait.h> |
| 139 | #include <netinet/in.h> |
| 140 | #include <arpa/inet.h> |
| 141 | |
| 142 | #define SOCKET_READ(A,B,C) read(A,B,C) |
| 143 | #define SOCKET_WRITE(A,B,C) write(A,B,C) |
| 144 | #define SOCKET_CLOSE(A) if (A >= 0) close(A) |
| 145 | #define TTY_FLUSH() |
| 146 | |
| 147 | #endif /* Not Win32 */ |
| 148 | |
| 149 | /* some functions to mutate the way these work */ |
| 150 | #define ax_malloc(A) malloc(A) |
| 151 | #define ax_realloc(A) realloc(A) |
| 152 | #define ax_calloc(A) calloc(A) |
| 153 | |
| 154 | #ifdef __cplusplus |
| 155 | } |
| 156 | #endif |
| 157 | |
| 158 | #endif |