- add support for ELF images (thanks to Vincent Palatin for this patch)
[fw/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 #ifdef HAVE_SYS_PARAM_H
39 #include <sys/param.h> /* for MIN/MAX macros */
40 #endif
41
42 /* MIN,MAX macros */
43 #ifndef MIN
44 #define MIN(a,b) (((a)<(b))?(a):(b))
45 #endif
46 #ifndef MAX
47 #define MAX(a,b) (((a)>(b))?(a):(b))
48 #endif
49                                                                                                                                  
50 /* gettimeofday() */
51 #ifndef HAVE_GETTIMEOFDAY
52
53 #ifndef _TIMEVAL_DEFINED
54 #define _TIMEVAL_DEFINED
55
56 struct timeval {
57         long tv_sec;
58         long tv_usec;
59 };
60 #endif /* _TIMEVAL_DEFINED */
61
62 struct timezone {
63     int tz_minuteswest;
64         int tz_dsttime;
65 };
66
67 extern int gettimeofday(struct timeval *tv, struct timezone *tz);
68 #endif
69
70 /* GNU extensions to the C library that may be missing on some systems */
71 #ifndef HAVE_STRNDUP
72 extern char* strndup(const char *s, size_t n);
73 #endif /* HAVE_STRNDUP */
74
75 #ifndef HAVE_STRNLEN
76 extern size_t strnlen(const char *s, size_t maxlen);
77 #endif /* HAVE_STRNLEN */
78
79 #ifndef HAVE_USLEEP
80 static __inline unsigned usleep(unsigned int usecs)
81 {
82 #ifdef _WIN32
83         Sleep((usecs/1000));
84         return 0;
85 #else
86 #error no usleep defined for your platform
87 #endif
88 }
89 #endif /* HAVE_USLEEP */
90
91 /* Windows specific */
92 #ifdef _WIN32
93
94 #define WIN32_LEAN_AND_MEAN
95 #include <windows.h>
96 #include <time.h>
97
98 #undef ERROR
99
100 #if IS_MINGW == 1
101 static __inline unsigned char inb(unsigned short int port)
102 {
103         unsigned char _v;
104         __asm__ __volatile__ ("inb %w1,%0":"=a" (_v):"Nd" (port));
105         return _v;
106 }
107
108 static __inline void outb(unsigned char value, unsigned short int port)
109 {
110         __asm__ __volatile__ ("outb %b0,%w1": :"a" (value), "Nd" (port));
111 }
112
113 #endif /* IS_MINGW */
114 #endif  /* _WIN32 */
115
116 /* generic socket functions for Windows and Posix */
117 static __inline int write_socket( int handle, const void *buffer, unsigned int count )
118 {
119 #ifdef _WIN32
120     return send(handle, buffer, count, 0);
121 #else
122     return write(handle, buffer, count);
123 #endif
124 }
125
126 static __inline int read_socket( int handle, void *buffer, unsigned int count )
127 {
128 #ifdef _WIN32
129     return recv(handle, buffer, count, 0);
130 #else
131     return read(handle, buffer, count);
132 #endif
133 }
134
135 static __inline int close_socket(int sock)
136 {
137 #ifdef _WIN32
138     return closesocket(sock);
139 #else
140     return close(sock);
141 #endif
142 }
143
144 static __inline void socket_nonblock(int fd)
145 {
146 #ifdef _WIN32
147         long nonblock = 1;
148         ioctlsocket(fd, FIONBIO, &nonblock );
149 #else
150         int oldopts = fcntl(fd, F_GETFL, 0);
151         fcntl(fd, F_SETFL, oldopts | O_NONBLOCK);
152 #endif
153 }
154
155 #endif /* REPLACEMENTS_H */