1 /*===========================================================================
2 *
3 *                            PUBLIC DOMAIN NOTICE
4 *               National Center for Biotechnology Information
5 *
6 *  This software/database is a "United States Government Work" under the
7 *  terms of the United States Copyright Act.  It was written as part of
8 *  the author's official duties as a United States Government employee and
9 *  thus cannot be copyrighted.  This software/database is freely available
10 *  to the public for use. The National Library of Medicine and the U.S.
11 *  Government have not placed any restriction on its use or reproduction.
12 *
13 *  Although all reasonable efforts have been taken to ensure the accuracy
14 *  and reliability of the software and data, the NLM and the U.S.
15 *  Government do not and cannot warrant the performance or results that
16 *  may be obtained by using this software or data. The NLM and the U.S.
17 *  Government disclaim all warranties, express or implied, including
18 *  warranties of performance, merchantability or fitness for any particular
19 *  purpose.
20 *
21 *  Please cite the author in any work or product based on this material.
22 *
23 * ===========================================================================
24 *
25 */
26 
27 #ifndef _h_os_native_
28 #define _h_os_native_
29 
30 /* get this guy included so that off_t is 64 bit */
31 #ifndef _STDINT_H
32 #include "stdint.h"
33 #endif
34 
35 #ifndef _h_klib_defs_
36 #include <klib/defs.h>
37 #endif
38 
39 #ifndef _h_klib_text_
40 #include <klib/text.h>
41 #endif
42 
43 /* specify at least NT 4.0 */
44 #ifndef _WIN32_WINNT
45 #define _WIN32_WINNT 0x0400
46 #endif
47 
48 /* we should never include this directly */
49 #define WIN32_LEAN_AND_MEAN
50 #include <windows.h>
51 #include <winsock2.h>
52 #include <ws2tcpip.h>
53 #include <Wincrypt.h>
54 
55 #include <limits.h>
56 
57 #include <ctype.h>
58 #include <direct.h>
59 #include <math.h>
60 
61 #ifdef __cplusplus
62 extern "C" {
63 #endif
64 
65 
66 
67 /* to make code work that depends on POSIX-bits (octal!) under Windows */
68 #define S_IWGRP 0020
69 #define S_IWOTH 0002
70 
71 #define mode_t uint32_t
72 
73 /*--------------------------------------------------------------------------
74  * timeout_t
75  *  a structure for communicating a timeout
76  *  which under Windows is a relative time
77  */
78 struct timeout_t
79 {
80     int64_t ts;
81     uint32_t mS;
82     uint32_t prepared;
83 };
84 
85 #ifndef putenv
86 #define putenv( s ) _putenv ( s )
87 #endif
88 
89 #define mkdir( d, m ) _mkdir( d )
90 #define strcasecmp _stricmp
91 #define strtoll _strtoi64
92 #define strtok_r strtok_s
93 
94 #undef strdup
95 #define strdup( str ) \
96     string_dup_measure ( ( str ), NULL )
97 
98 #undef strndup
99 #define strndup( str, n ) \
100     string_dup ( ( str ), ( n ) )
101 
102 int __cdecl isalnum ( int ch );
103 int __cdecl isalpha ( int ch );
104 int __cdecl iscntrl ( int ch );
105 int __cdecl isdigit ( int ch );
106 int __cdecl isgraph ( int ch );
107 int __cdecl islower ( int ch );
108 int __cdecl isupper ( int ch );
109 int __cdecl isprint ( int ch );
110 int __cdecl ispunct ( int ch );
111 int __cdecl isspace ( int ch );
112 int __cdecl isxdigit ( int ch );
113 int __cdecl tolower ( int ch );
114 int __cdecl toupper ( int ch );
115 
116 #undef isascii
117 int __cdecl isascii ( int ch );
118 
isblank(int x)119 static __inline int isblank(int x)
120 {
121     return (((x) == ' ') || ((x) == '\t'));
122 }
123 
124 #if _MSC_VER < 1900
125 KLIB_EXTERN int CC snprintf ( char * buffer, size_t bufsize, const char * format, ... );
126 #endif
127 /* MSC 1900 (2017) and on, snprintf is defined as an inline */
128 
129 static __inline
memrchr(const void * s,int c,size_t n)130 void *memrchr ( const void *s, int c, size_t n )
131 {
132     size_t i;
133     const char *cp = (const char*)s;
134     for ( i = n; i > 0; )
135     {
136         if ( ( int ) cp [ -- i ] == c )
137             return (void *)(cp + i);
138     }
139     return NULL;
140 }
141 
142 static __inline
strchrnul(const char * s,int c_in)143 char *strchrnul ( const char *s, int c_in )
144 {
145     uint32_t i;
146     for ( i=0; s[i] != 0; ++i )
147     {
148         if ( s[i] == c_in )
149             break;
150     }
151   return ( char * )&s[ i ];
152 }
153 
154 static __inline
strsep(char ** stringp,const char * delim)155 char *strsep ( char **stringp, const char *delim )
156 {
157 	char *s, *tok, c, delim_char;
158 	const char *p_delim;
159 
160 	if ( ( s = *stringp ) == NULL )
161 		return NULL;
162 
163 	for ( tok = s; ; )
164 	{
165 		c = *s++;
166 		p_delim = delim;
167 		do {
168 			if ( ( delim_char = *p_delim++ ) == c )
169 			{
170 				if ( c == 0 )
171 					s = NULL;
172 				else
173 					s[-1] = 0;
174 				*stringp = s;
175 				return ( tok );
176 			}
177 		} while ( delim_char != 0 );
178 	}
179 	return NULL;
180 }
181 
182 #define gmtime_r(t, tm) gmtime_s(tm, t)
183 #define timegm _mkgmtime
184 
185 static __inline
strncasecmp(const char * s1,const char * s2,size_t n)186 int strncasecmp( const char *s1, const char *s2, size_t n )
187 {
188     return _strnicmp( s1, s2, n );
189 }
190 
191 static __inline
strcasestr(const char * s1,const char * s2)192 const char *strcasestr (const char *s1, const char *s2)
193 {
194     unsigned char c2 = tolower((unsigned char) *s2);
195     size_t l1 = strlen(s1), l2 = strlen(s2);
196 
197     if (l2 == 0) {
198         return s1;
199     }
200 
201     while (l1 >= l2) {
202         if (tolower((unsigned char) *s1) == c2
203             &&  (l2 == 1  ||  _strnicmp(s1 + 1, s2 + 1, l2 - 1) == 0)) {
204             return s1;
205         }
206         ++s1;
207         --l1;
208     }
209 
210     return NULL;
211 }
212 
213 static __inline
lround(double x)214 long int lround ( double x )
215 {
216     double val = ( x < 0.0 ) ? ceil ( x - 0.5 ) : floor ( x + 0.5 );
217     if ( val > ( double ) LONG_MAX )
218         return LONG_MAX;
219     if ( val < ( double ) LONG_MIN )
220         return LONG_MIN;
221     return ( long int ) val;
222 }
223 
224 #define isalnum( ch ) isalnum   ( ( unsigned char ) ( ch ) )
225 #define isalpha( ch ) isalpha   ( ( unsigned char ) ( ch ) )
226 #define isascii( ch ) isascii   ( ( unsigned char ) ( ch ) )
227 #define iscntrl( ch ) iscntrl   ( ( unsigned char ) ( ch ) )
228 #define isdigit( ch ) isdigit   ( ( unsigned char ) ( ch ) )
229 #define isgraph( ch ) isgraph   ( ( unsigned char ) ( ch ) )
230 #define islower( ch ) islower   ( ( unsigned char ) ( ch ) )
231 #define isupper( ch ) isupper   ( ( unsigned char ) ( ch ) )
232 #define isprint( ch ) isprint   ( ( unsigned char ) ( ch ) )
233 #define ispunct( ch ) ispunct   ( ( unsigned char ) ( ch ) )
234 #define isspace( ch ) isspace   ( ( unsigned char ) ( ch ) )
235 #define isxdigit( ch ) isxdigit ( ( unsigned char ) ( ch ) )
236 #define tolower( ch ) tolower ( ( unsigned char ) ( ch ) )
237 #define toupper( ch ) toupper ( ( unsigned char ) ( ch ) )
238 
239 #define _Thread_local __declspec( thread )
240 
241 #ifdef __cplusplus
242 }
243 #endif
244 
245 #endif /* _h_os_native_ */
246