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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
|
/* MISSING.C
Implementation for standard and semi-standard C library calls missing in WinCE
environment.
by Vasyl Tsvirkunov
*/
#include <windows.h>
#include <tchar.h>
#include <string.h>
#include "sys/stat.h"
#include "sys/time.h"
#include "time.h"
#include "dirent.h"
/* forward declaration */
#if _WIN32_WCE < 300
#include "portdefs.h"
#else
char *strdup(const char *strSource);
#endif
/* Limited dirent implementation. Used by UI.C and DEVICES.C */
static WIN32_FIND_DATA wfd;
DIR* opendir(const char* fname)
{
DIR* pdir;
char fnameMask[MAX_PATH+1];
TCHAR fnameUnc[MAX_PATH+1];
char nameFound[MAX_PATH+1];
if(fname == NULL)
return NULL;
strcpy(fnameMask, fname);
if(!strlen(fnameMask) || fnameMask[strlen(fnameMask)-1] != '\\')
strncat(fnameMask, "\\", MAX_PATH-strlen(fnameMask)-1);
strncat(fnameMask, "*.*", MAX_PATH-strlen(fnameMask)-4);
pdir = (DIR*)malloc(sizeof(DIR)+strlen(fname));
pdir->dd_dir.d_ino = 0;
pdir->dd_dir.d_reclen = 0;
pdir->dd_dir.d_name = 0;
pdir->dd_dir.d_namlen = 0;
pdir->dd_handle = 0;
pdir->dd_stat = 0;
strcpy(pdir->dd_name, fname); /* it has exactly enough space for fname and nul char */
MultiByteToWideChar(CP_ACP, 0, fnameMask, -1, fnameUnc, MAX_PATH);
if((pdir->dd_handle = (long)FindFirstFile(fnameUnc, &wfd)) == (long)INVALID_HANDLE_VALUE)
{
free(pdir);
return NULL;
}
else
{
WideCharToMultiByte(CP_ACP, 0, wfd.cFileName, -1, nameFound, MAX_PATH, NULL, NULL);
pdir->dd_dir.d_name = strdup(nameFound);
pdir->dd_dir.d_namlen = strlen(nameFound);
}
return pdir;
}
struct dirent* readdir(DIR* dir)
{
char nameFound[MAX_PATH+1];
static struct dirent dummy;
if(dir->dd_stat == 0)
{
dummy.d_name = ".";
dummy.d_namlen = 1;
dir->dd_stat ++;
return &dummy;
}
else if(dir->dd_stat == 1)
{
dummy.d_name = "..";
dummy.d_namlen = 2;
dir->dd_stat ++;
return &dummy;
}
else if(dir->dd_stat == 2)
{
dir->dd_stat++;
return &dir->dd_dir;
}
else
{
if(FindNextFile((HANDLE)dir->dd_handle, &wfd) == 0)
{
dir->dd_stat = -1;
return NULL;
}
WideCharToMultiByte(CP_ACP, 0, wfd.cFileName, -1, nameFound, MAX_PATH, NULL, NULL);
if(dir->dd_dir.d_name)
free(dir->dd_dir.d_name);
dir->dd_dir.d_name = strdup(nameFound);
dir->dd_dir.d_namlen = strlen(nameFound);
dir->dd_stat ++;
return &dir->dd_dir;
}
}
int closedir(DIR* dir)
{
if(dir == NULL)
return 0;
if(dir->dd_handle)
FindClose((HANDLE)dir->dd_handle);
if(dir->dd_dir.d_name)
free(dir->dd_dir.d_name);
free(dir);
return 1;
}
/* Very limited implementation of stat. Used by UI.C, MEMORY-P.C (latter is not critical) */
int stat(const char *fname, struct stat *ss)
{
TCHAR fnameUnc[MAX_PATH+1];
HANDLE handle;
int len;
if(fname == NULL || ss == NULL)
return -1;
/* Special case (dummy on WinCE) */
len = strlen(fname);
if(len >= 2 && fname[len-1] == '.' && fname[len-2] == '.' &&
(len == 2 || fname[len-3] == '\\'))
{
/* That's everything implemented so far */
memset(ss, 0, sizeof(struct stat));
ss->st_size = 1024;
ss->st_mode |= S_IFDIR;
return 0;
}
MultiByteToWideChar(CP_ACP, 0, fname, -1, fnameUnc, MAX_PATH);
handle = FindFirstFile(fnameUnc, &wfd);
if(handle == INVALID_HANDLE_VALUE)
return -1;
else
{
/* That's everything implemented so far */
memset(ss, 0, sizeof(struct stat));
ss->st_size = wfd.nFileSizeLow;
if(wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
ss->st_mode |= S_IFDIR;
FindClose(handle);
}
return 0;
}
/* Remove file by name */
int remove(const char* path)
{
TCHAR pathUnc[MAX_PATH+1];
MultiByteToWideChar(CP_ACP, 0, path, -1, pathUnc, MAX_PATH);
return !DeleteFile(pathUnc);
}
/* in our case unlink is the same as remove */
int unlink(const char* path)
{
return remove(path);
}
/* Make directory, Unix style */
void mkdir(char* dirname, int mode)
{
char path[MAX_PATH+1];
TCHAR pathUnc[MAX_PATH+1];
char* ptr;
strncpy(path, dirname, MAX_PATH);
if(*path == '/')
*path = '\\';
/* Run through the string and attempt creating all subdirs on the path */
for(ptr = path+1; *ptr; ptr ++)
{
if(*ptr == '\\' || *ptr == '/')
{
*ptr = 0;
MultiByteToWideChar(CP_ACP, 0, path, -1, pathUnc, MAX_PATH);
CreateDirectory(pathUnc, 0);
*ptr = '\\';
}
}
MultiByteToWideChar(CP_ACP, 0, path, -1, pathUnc, MAX_PATH);
CreateDirectory(pathUnc, 0);
}
/* Used in DEVICES.C and UI.C for some purpose. Not critical in this port */
int system(const char* path) { return 0; }
#if 0
char *tmpnam(char *string)
{
TCHAR pTemp[MAX_PATH+1];
static char buffer[MAX_PATH+1];
GetTempFileName(TEXT("."), TEXT("A8_"), 0, pTemp);
WideCharToMultiByte(CP_ACP, 0, pTemp, -1, buffer, MAX_PATH, NULL, NULL);
if(string)
{
strcpy(string, buffer);
return string;
}
else
return buffer;
}
FILE *tmpfile()
{
TCHAR pTemp[MAX_PATH+1];
if(!GetTempFileName(TEXT("."), TEXT("A8_"), 0, pTemp))
return _wfopen(pTemp, TEXT("w+b"));
else
return 0;
}
#endif
void rewind(FILE *stream)
{
fseek(stream, 0, SEEK_SET);
}
char *strdup(const char *strSource)
{
char* buffer;
buffer = (char*)malloc(strlen(strSource)+1);
if(buffer)
strcpy(buffer, strSource);
return buffer;
}
/* Used in UI.C */
char cwd[MAX_PATH+1] = "";
char *getcwd(char *buffer, int maxlen)
{
TCHAR fileUnc[MAX_PATH+1];
char* plast;
if(cwd[0] == 0)
{
GetModuleFileName(NULL, fileUnc, MAX_PATH);
WideCharToMultiByte(CP_ACP, 0, fileUnc, -1, cwd, MAX_PATH, NULL, NULL);
plast = strrchr(cwd, '\\');
if(plast)
*plast = 0;
/* Special trick to keep start menu clean... */
if(_stricmp(cwd, "\\windows\\start menu") == 0)
strcpy(cwd, "\\Apps");
}
if(buffer)
strncpy(buffer, cwd, maxlen);
return cwd;
}
/* Limited implementation of time.h. time_t formula is possibly incorrect. */
time_t time(time_t* res)
{
time_t t;
SYSTEMTIME st;
GetLocalTime(&st);
t = (time_t)(((((((st.wYear-1970)*12+st.wMonth)*31+st.wDay)*7+st.wDayOfWeek)*24+st.wHour)*60+st.wMinute)*60+st.wSecond);
if(res)
*res = t;
return t;
}
struct tm* localtime(time_t* timer)
{
static struct tm tmLocalTime;
unsigned long rem = *timer;
tmLocalTime.tm_sec = (short)(rem % 60);
rem /= 60;
tmLocalTime.tm_min = (short)(rem % 60);
rem /= 60;
tmLocalTime.tm_hour = (short)(rem % 24);
rem /= 24;
tmLocalTime.tm_mday = (short)(rem % 7);
rem /= 7;
tmLocalTime.tm_mday = (short)(rem % 31);
rem /= 31;
tmLocalTime.tm_mon = (short)(rem % 12);
rem /= 12;
tmLocalTime.tm_year = (short)(rem+1970);
return &tmLocalTime;
}
/* Very limited implementation of sys/time.h */
void gettimeofday(struct timeval* tp, void* dummy)
{
DWORD dt = GetTickCount();
tp->tv_sec = dt/1000;
tp->tv_usec = dt*1000;
}
void usleep(long usec)
{
long msec = usec/1000;
if(msec <= 0)
Sleep(0);
else
Sleep(msec);
}
/*
Windows CE fopen has non-standard behavior -- not
fully qualified paths refer to root folder rather
than current folder (concept not implemented in CE).
*/
#undef fopen
FILE* wce_fopen(const char* fname, const char* fmode)
{
char fullname[MAX_PATH+1];
if(!fname || fname[0] == '\0')
return NULL;
if(fname[0] != '\\' && fname[0] != '/')
{
getcwd(fullname, MAX_PATH);
strncat(fullname, "\\", MAX_PATH-strlen(fullname)-1);
strncat(fullname, fname, MAX_PATH-strlen(fullname)-strlen(fname));
return fopen(fullname, fmode);
}
else
return fopen(fname, fmode);
}
/* This may provide for better sync mechanism */
unsigned int clock()
{
return GetTickCount();
}
/* And why do people use this? */
void abort()
{
exit(1);
}
/*
IMHO, no project should use this one, it is not portable at all. This implementation
at least allows some projects to work.
*/
char* getenv(char* name)
{
static char buffer[MAX_PATH+1];
if(strcmp(name, "HOME") == 0 || strcmp(name, "HOMEDIR") == 0)
{
getcwd(buffer, MAX_PATH);
return buffer;
}
else
return "";
}
void *bsearch(const void *key, const void *base, size_t nmemb,
size_t size, int (*compar)(const void *, const void *)) {
size_t i;
for (i=0; i<nmemb; i++)
if (compar(key, (void*)((size_t)base + size * i)) == 0)
return (void*)((size_t)base + size * i);
return NULL;
}
#if _WIN32_WCE < 300
void *calloc(size_t n, size_t s) {
void *result = malloc(n * s);
if (result)
memset(result, 0, n * s);
return result;
}
int isdigit(int c) {
return (c >='0' && c <= '9');
}
void assert (int expression) {
if (!expression)
abort();
}
void assert (void *expression) {
if (!expression)
abort();
}
int stricmp( const char *string1, const char *string2 ) {
char src[4096];
char dest[4096];
int i;
for (i=0; i<strlen(string1); i++)
if (string1[i] >= 'A' && string1[i] <= 'Z')
src[i] = string1[i] + 32;
else
src[i] = string1[i];
src[i] = 0;
for (i=0; i<strlen(string2); i++)
if (string2[i] >= 'A' && string2[i] <= 'Z')
dest[i] = string2[i] + 32;
else
dest[i] = string2[i];
dest[i] = 0;
return strcmp(src, dest);
}
int _stricmp( const char *string1, const char *string2 ) {
return stricmp(string1, string2);
}
char *strrchr(const char *s, int c) {
int i;
for (i = strlen(s) - 1; i > 0; i--)
if (s[i] == c)
return (char*)(s + i);
return NULL;
}
long int strtol(const char *nptr, char **endptr, int base) {
// not correct but that's all we are using
long int result;
sscanf(nptr, "%ld", &result);
return result;
}
#endif
|