- Added support for native MinGW builds (thanks to Spencer Oliver and Michael Fischer...
[fw/openocd] / src / helper / replacements.c
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 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
23
24 #include "replacements.h"
25
26 #include <stdio.h>
27
28 /* replacements for gettimeofday */
29 #ifndef HAVE_GETTIMEOFDAY
30
31 /* Windows */
32 #ifdef _WIN32
33
34 #ifndef __GNUC__
35 #define EPOCHFILETIME (116444736000000000i64)
36 #else
37 #define EPOCHFILETIME (116444736000000000LL)
38 #endif
39
40 int gettimeofday(struct timeval *tv, struct timezone *tz)
41 {
42         FILETIME        ft;
43         LARGE_INTEGER   li;
44         __int64         t;
45         static int      tzflag;
46
47         if (tv)
48         {
49                 GetSystemTimeAsFileTime(&ft);
50                 li.LowPart  = ft.dwLowDateTime;
51                 li.HighPart = ft.dwHighDateTime;
52                 t  = li.QuadPart;                   /* In 100-nanosecond intervals */
53                 t -= EPOCHFILETIME;                 /* Offset to the Epoch time */
54                 t /= 10;                            /* In microseconds */
55                 tv->tv_sec  = (long)(t / 1000000);
56                 tv->tv_usec = (long)(t % 1000000);
57         }
58
59         if (tz)
60         {
61                 if (!tzflag)
62                 {
63                         _tzset();
64                         tzflag++;
65                 }
66                 tz->tz_minuteswest = _timezone / 60;
67                 tz->tz_dsttime = _daylight;
68         }
69
70         return 0;
71 }
72 #endif /* _WIN32 */
73
74 #endif /* HAVE_GETTIMEOFDAY */
75
76 #ifndef HAVE_STRNLEN
77 size_t strnlen(const char *s, size_t maxlen)
78 {
79         const char *end= (const char *)memchr(s, '\0', maxlen);
80         return end ? (size_t) (end - s) : maxlen;
81 }
82 #endif
83
84 #ifndef HAVE_STRNDUP
85 char* strndup(const char *s, size_t n)
86 {
87         size_t len = strnlen (s, n);
88         char *new = (char *) malloc (len + 1);
89
90         if (new == NULL)
91                 return NULL;
92
93         new[len] = '\0';
94         return (char *) memcpy (new, s, len);
95 }
96 #endif