summaryrefslogtreecommitdiff
path: root/compatbits.c
blob: a6ce8f593f122321d41d1223b5315eb7bf9a7804 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/*
Copyright 2001-2022 John Wiseman G8BPQ

This file is part of LinBPQ/BPQ32.

LinBPQ/BPQ32 is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

LinBPQ/BPQ32 is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with LinBPQ/BPQ32.  If not, see http://www.gnu.org/licenses
*/	

/*

Stuff to make compiling on WINDOWS and LINUX easier

*/

#ifdef WIN32

#include <windows.h>

typedef unsigned int       uint32_t;

#define pthread_t uint32_t

int pthread_equal(pthread_t T1, pthread_t T2)
{
	return (T1 == T2);
}

#else

#include <stdio.h>
#include <ctype.h>
#include <syslog.h>
#include <stdarg.h>
#include <unistd.h>
#include <stdint.h>

#define BOOL int

#define VOID void
#define UCHAR unsigned char
#define USHORT unsigned short
#define ULONG uint32_t
#define UINT unsigned int
#define SHORT short
#define DWORD int32_t

#define APIENTRY

#define TRUE 1
#define FALSE 0
#define FAR

#define HWND unsigned int
#define HINSTANCE unsigned int

#define strtok_s strtok_r

VOID Debugprintf(const char * format, ...);

int memicmp(unsigned char *a, unsigned char *b, int n)
{
	if (n)
	{
		while (n && (toupper(*a) == toupper(*b)))
			n--, a++, b++;

		if (n)
			return toupper(*a) - toupper(*b);
   }
   return 0;
}
int stricmp(const unsigned char * pStr1, const unsigned char *pStr2)
{
    unsigned char c1, c2;
    int  v;

	if (pStr1 == NULL)
	{
		if (pStr2)
			Debugprintf("stricmp called with NULL 1st param - 2nd %s ", pStr2);
		else
			Debugprintf("stricmp called with two NULL params");

		return 1;
	}


    do {
        c1 = *pStr1++;
        c2 = *pStr2++;
        /* The casts are necessary when pStr1 is shorter & char is signed */
        v = tolower(c1) - tolower(c2);
    } while ((v == 0) && (c1 != '\0') && (c2 != '\0') );

    return v;
}
char * strupr(char* s)
{
  char* p = s;

  if (s == 0)
	  return 0;

  while (*p = toupper( *p )) p++;
  return s;
}

char * strlwr(char* s)
{
  char* p = s;
  while (*p = tolower( *p )) p++;
  return s;
}

int sprintf_s(char * string, int plen, const char * format, ...)
{
	va_list(arglist);
	int Len;

	va_start(arglist, format);
	Len = vsprintf(string, format, arglist);
	va_end(arglist);
	return Len;
}



#include <pthread.h>

pthread_t _beginthread(void(*start_address)(), unsigned stack_size, VOID * arglist)
{
	pthread_t thread;

	// Need to set stack size for Mac

	int s, tnum, opt, num_threads;
	struct thread_info *tinfo;
	pthread_attr_t attr;
	void *res;

	s = pthread_attr_init(&attr);
	if (s != 0)
	{
		perror("pthread_attr_init");
		return 0;
	}
	if (stack_size > 0)
	{
		s = pthread_attr_setstacksize(&attr, stack_size);
		if (s != 0)
		{
			perror("pthread_attr_setstacksize");
			return 0;
		}
	}

	if (pthread_create(&thread, &attr, (void * (*)(void *))start_address, (void*) arglist) != 0)
		perror("New Thread");
	else
		pthread_detach(thread);

	return thread;
}

int Sleep(int ms)
{
	usleep(ms * 1000);
	return 0;
}

VOID OutputDebugString(char * string)
{
	syslog(LOG_DEBUG, "%s", string);
}

void closesocket(int sock)
{
	if (sock)
		close(sock);
}

#endif

#ifdef WIN32

uint32_t GetTickCountINP3()
{
	// Returns system uptime in 10 mS, lower 20 bits only

	return (GetTickCount() / 10) & 0xfffff;
}
#else

uint64_t GetTickCount();

uint32_t GetTickCountINP3()
{
	uint64_t Ticks = GetTickCount();

	// Returns system uptime in 10 mS, lower 20 bits only

	return (Ticks / 10) & 0xfffff;
}

#endif