]> git.sur5r.net Git - openocd/blob - src/helper/replacements.h
- Added support for native MinGW builds (thanks to Spencer Oliver and Michael Fischer...
[openocd] / src / helper / replacements.h
1 /***************************************************************************
2  *   Copyright (C) 2006 by Dominic Rath                                    *
3  *   Dominic.Rath@gmx.de                                                   *
4  *                                                                         *
5  *   This program is free software; you can redistribute it and/or modify  *
6  *   it under the terms of the GNU General Public License as published by  *
7  *   the Free Software Foundation; either version 2 of the License, or     *
8  *   (at your option) any later version.                                   *
9  *                                                                         *
10  *   This program is distributed in the hope that it will be useful,       *
11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
13  *   GNU General Public License for more details.                          *
14  *                                                                         *
15  *   You should have received a copy of the GNU General Public License     *
16  *   along with this program; if not, write to the                         *
17  *   Free Software Foundation, Inc.,                                       *
18  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
19  ***************************************************************************/
20 #ifndef REPLACEMENTS_H
21 #define REPLACEMENTS_H
22
23 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
26
27 /* include necessary headers for socket functionality */
28 #ifdef _WIN32
29 #include <winsock2.h>
30 #else
31 #include <sys/socket.h>
32 #include <sys/poll.h>
33 #include <netinet/in.h>
34 #include <unistd.h>
35 #include <fcntl.h>
36 #endif
37
38 /* gettimeofday() */
39 #ifndef HAVE_GETTIMEOFDAY
40
41 #ifndef _TIMEVAL_DEFINED
42 #define _TIMEVAL_DEFINED
43
44 struct timeval {
45         long tv_sec;
46         long tv_usec;
47 };
48 #endif /* _TIMEVAL_DEFINED */
49
50 struct timezone {
51     int tz_minuteswest;
52         int tz_dsttime;
53 };
54
55 extern int gettimeofday(struct timeval *tv, struct timezone *tz);
56 #endif
57
58 /* GNU extensions to the C library that may be missing on some systems */
59 #ifndef HAVE_STRNDUP
60 extern char* strndup(const char *s, size_t n);
61 #endif /* HAVE_STRNDUP */
62
63 #ifndef HAVE_STRNLEN
64 extern size_t strnlen(const char *s, size_t maxlen);
65 #endif /* HAVE_STRNLEN */
66
67 #ifndef HAVE_USLEEP
68 static __inline unsigned usleep(unsigned int usecs)
69 {
70 #ifdef _WIN32
71         Sleep((usecs/1000));
72         return 0;
73 #else
74 #error no usleep defined for your platform
75 #endif
76 }
77 #endif /* HAVE_USLEEP */
78
79 /* Windows specific */
80 #ifdef _WIN32
81
82 #define WIN32_LEAN_AND_MEAN
83 #include <windows.h>
84 #include <time.h>
85
86 #undef ERROR
87
88 #if IS_MINGW == 1
89 static __inline unsigned char inb(unsigned short int port)
90 {
91         unsigned char _v;
92         __asm__ __volatile__ ("inb %w1,%0":"=a" (_v):"Nd" (port));
93         return _v;
94 }
95
96 static __inline void outb(unsigned char value, unsigned short int port)
97 {
98         __asm__ __volatile__ ("outb %b0,%w1": :"a" (value), "Nd" (port));
99 }
100
101 #endif /* IS_MINGW */
102 #endif  /* _WIN32 */
103
104 /* generic socket functions for Windows and Posix */
105 static __inline int write_socket( int handle, const void *buffer, unsigned int count )
106 {
107 #ifdef _WIN32
108     return send(handle, buffer, count, 0);
109 #else
110     return write(handle, buffer, count);
111 #endif
112 }
113
114 static __inline int read_socket( int handle, void *buffer, unsigned int count )
115 {
116 #ifdef _WIN32
117     return recv(handle, buffer, count, 0);
118 #else
119     return read(handle, buffer, count);
120 #endif
121 }
122
123 static __inline int close_socket(int sock)
124 {
125 #ifdef _WIN32
126     return closesocket(sock);
127 #else
128     return close(sock);
129 #endif
130 }
131
132 static __inline void socket_nonblock(int fd)
133 {
134 #ifdef _WIN32
135         long nonblock = 1;
136         ioctlsocket(fd, FIONBIO, &nonblock );
137 #else
138         int oldopts = fcntl(fd, F_GETFL, 0);
139         fcntl(fd, F_SETFL, oldopts | O_NONBLOCK);
140 #endif
141 }
142
143 #endif /* REPLACEMENTS_H */