]>
Commit | Line | Data |
---|---|---|
c7de829c WD |
1 | /**************************************************************************** |
2 | * | |
3 | * SciTech MGL Graphics Library | |
4 | * | |
5 | * ======================================================================== | |
6 | * | |
7 | * The contents of this file are subject to the SciTech MGL Public | |
8 | * License Version 1.0 (the "License"); you may not use this file | |
9 | * except in compliance with the License. You may obtain a copy of | |
10 | * the License at http://www.scitechsoft.com/mgl-license.txt | |
11 | * | |
12 | * Software distributed under the License is distributed on an | |
13 | * "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or | |
14 | * implied. See the License for the specific language governing | |
15 | * rights and limitations under the License. | |
16 | * | |
17 | * The Original Code is Copyright (C) 1991-1998 SciTech Software, Inc. | |
18 | * | |
19 | * The Initial Developer of the Original Code is SciTech Software, Inc. | |
20 | * All Rights Reserved. | |
21 | * | |
22 | * ======================================================================== | |
23 | * | |
24 | * Language: ANSI C | |
25 | * Environment: Any | |
26 | * | |
27 | * Description: Module to implement a the OS specific side of the Binary | |
28 | * Portable DLL C runtime library. The functions in here | |
29 | * are imported into the Binary Portable DLL's to implement | |
30 | * OS specific services. | |
31 | * | |
32 | ****************************************************************************/ | |
33 | ||
34 | #include "pmapi.h" | |
35 | #if defined(__WIN32_VXD__) || defined(__NT_DRIVER__) | |
36 | #include "drvlib/peloader.h" | |
37 | #include "drvlib/attrib.h" | |
38 | #include "drvlib/libc/init.h" | |
39 | #define __BUILDING_PE_LOADER__ | |
40 | #include "drvlib/libc/file.h" | |
41 | #if defined(__WIN32_VXD__) | |
42 | #include "vxdfile.h" | |
43 | #endif | |
44 | #else | |
45 | #include <stdio.h> | |
46 | #include <stdlib.h> | |
47 | #include <string.h> | |
48 | #include <malloc.h> | |
49 | #include <time.h> | |
50 | #include <signal.h> | |
51 | #include <fcntl.h> | |
52 | #if defined(__GNUC__) || defined(__UNIX__) | |
53 | #include <unistd.h> | |
54 | #include <sys/types.h> | |
55 | #include <sys/stat.h> | |
56 | #else | |
57 | #include <io.h> | |
58 | #endif | |
59 | #include "drvlib/attrib.h" | |
60 | #include "drvlib/libc/init.h" | |
61 | #define __BUILDING_PE_LOADER__ | |
62 | #include "drvlib/libc/file.h" | |
63 | #if defined(__WINDOWS__) || defined(TNT) || defined(__RTTARGET__) | |
64 | #define WIN32_LEAN_AND_MEAN | |
65 | #define STRICT | |
66 | #include <windows.h> | |
67 | #endif | |
68 | #ifdef __MSDOS__ | |
69 | #include <dos.h> | |
70 | #endif | |
71 | #ifdef __OS2__ | |
72 | #define INCL_DOS | |
73 | #define INCL_DOSERRORS | |
74 | #define INCL_SUB | |
75 | #include <os2.h> | |
76 | #endif | |
77 | #endif | |
78 | ||
79 | /* No text or binary modes for Unix */ | |
80 | ||
81 | #ifndef O_BINARY | |
82 | #define O_BINARY 0 | |
83 | #define O_TEXT 0 | |
84 | #endif | |
85 | ||
86 | /*--------------------------- Global variables ----------------------------*/ | |
87 | ||
88 | #if defined(__WIN32_VXD__) || defined(__NT_DRIVER__) | |
89 | #define MAX_FILES 16 | |
90 | static FILE *openHandles[MAX_FILES] = {NULL}; | |
91 | #endif | |
92 | ||
93 | /* <stdlib.h> stub functions */ | |
94 | void _CDECL stub_abort(void); | |
95 | int _CDECL stub_atexit(void (*)(void)); | |
96 | void * _CDECL stub_calloc(size_t _nelem, size_t _size); | |
97 | void _CDECL stub_exit(int _status); | |
98 | void _CDECL stub_free(void *_ptr); | |
99 | char * _CDECL stub_getenv(const char *_name); | |
100 | void * _CDECL stub_malloc(size_t _size); | |
101 | void * _CDECL stub_realloc(void *_ptr, size_t _size); | |
102 | int _CDECL stub_system(const char *_s); | |
103 | int _CDECL stub_putenv(const char *_val); | |
104 | ||
105 | /* <libc/file.h> stub functions */ | |
106 | int _CDECL stub_open(const char *_path, int _oflag, unsigned _mode); | |
107 | int _CDECL stub_access(const char *_path, int _amode); | |
108 | int _CDECL stub_close(int _fildes); | |
109 | off_t _CDECL stub_lseek(int _fildes, off_t _offset, int _whence); | |
110 | size_t _CDECL stub_read(int _fildes, void *_buf, size_t _nbyte); | |
111 | int _CDECL stub_unlink(const char *_path); | |
112 | size_t _CDECL stub_write(int _fildes, const void *_buf, size_t _nbyte); | |
113 | int _CDECL stub_isatty(int _fildes); | |
114 | ||
115 | /* <stdio.h> stub functions */ | |
116 | int _CDECL stub_remove(const char *_filename); | |
117 | int _CDECL stub_rename(const char *_old, const char *_new); | |
118 | ||
119 | /* <time.h> stub functions */ | |
120 | time_t _CDECL stub_time(time_t *_tod); | |
121 | ||
122 | /* <signal.h> stub functions */ | |
123 | int _CDECL stub_raise(int); | |
124 | void * _CDECL stub_signal(int, void *); | |
125 | ||
126 | /* <drvlib/attrib.h> functions */ | |
127 | #define stub_OS_setfileattr _OS_setfileattr | |
128 | #define stub_OS_getcurrentdate _OS_getcurrentdate | |
129 | ||
130 | LIBC_imports _VARAPI ___imports = { | |
131 | sizeof(LIBC_imports), | |
132 | ||
133 | /* <stdlib.h> exports */ | |
134 | stub_abort, | |
135 | stub_atexit, | |
136 | stub_calloc, | |
137 | stub_exit, | |
138 | stub_free, | |
139 | stub_getenv, | |
140 | stub_malloc, | |
141 | stub_realloc, | |
142 | stub_system, | |
143 | stub_putenv, | |
144 | ||
145 | /* <libc/file.h> exports */ | |
146 | stub_open, | |
147 | stub_access, | |
148 | stub_close, | |
149 | stub_lseek, | |
150 | stub_read, | |
151 | stub_unlink, | |
152 | stub_write, | |
153 | stub_isatty, | |
154 | ||
155 | /* <stdio.h> exports */ | |
156 | stub_remove, | |
157 | stub_rename, | |
158 | ||
159 | /* <signal.h> functions */ | |
160 | stub_raise, | |
161 | stub_signal, | |
162 | ||
163 | /* <time.h> exports */ | |
164 | stub_time, | |
165 | ||
166 | /* <drvlib/attrib.h> exports */ | |
167 | stub_OS_setfileattr, | |
168 | stub_OS_getcurrentdate, | |
169 | }; | |
170 | ||
171 | /*---------------------- Stub function implementation ---------------------*/ | |
172 | ||
173 | /* <stdlib.h> stub functions */ | |
174 | void _CDECL stub_abort(void) | |
175 | { | |
176 | #if !defined( __WIN32_VXD__) && !defined(__NT_DRIVER__) | |
177 | abort(); | |
178 | #endif | |
179 | } | |
180 | ||
181 | int _CDECL stub_atexit(void (*func)(void)) | |
182 | { | |
183 | #if !defined( __WIN32_VXD__) && !defined(__NT_DRIVER__) | |
184 | return atexit((void(*)(void))func); | |
185 | #else | |
186 | return -1; | |
187 | #endif | |
188 | } | |
189 | ||
190 | void * _CDECL stub_calloc(size_t _nelem, size_t _size) | |
191 | { return __PM_calloc(_nelem,_size); } | |
192 | ||
193 | void _CDECL stub_exit(int _status) | |
194 | { | |
195 | #if !defined( __WIN32_VXD__) && !defined(__NT_DRIVER__) | |
196 | exit(_status); | |
197 | #endif | |
198 | } | |
199 | ||
200 | void _CDECL stub_free(void *_ptr) | |
201 | { __PM_free(_ptr); } | |
202 | ||
203 | char * _CDECL stub_getenv(const char *_name) | |
204 | { | |
205 | #if defined( __WIN32_VXD__) || defined(__NT_DRIVER__) | |
206 | return NULL; | |
207 | #else | |
208 | return getenv(_name); | |
209 | #endif | |
210 | } | |
211 | ||
212 | void * _CDECL stub_malloc(size_t _size) | |
213 | { return __PM_malloc(_size); } | |
214 | ||
215 | void * _CDECL stub_realloc(void *_ptr, size_t _size) | |
216 | { return __PM_realloc(_ptr,_size); } | |
217 | ||
218 | int _CDECL stub_system(const char *_s) | |
219 | { | |
220 | #if defined(__WINDOWS__) || defined(__WIN32_VXD__) || defined(__NT_DRIVER__) || defined(__SMX32__) || defined(__RTTARGET__) | |
221 | (void)_s; | |
222 | return -1; | |
223 | #else | |
224 | return system(_s); | |
225 | #endif | |
226 | } | |
227 | ||
228 | int _CDECL stub_putenv(const char *_val) | |
229 | { | |
230 | #if defined( __WIN32_VXD__) || defined(__NT_DRIVER__) | |
231 | return -1; | |
232 | #else | |
233 | return putenv((char*)_val); | |
234 | #endif | |
235 | } | |
236 | ||
237 | time_t _CDECL stub_time(time_t *_tod) | |
238 | { | |
239 | #if defined( __WIN32_VXD__) || defined(__NT_DRIVER__) | |
240 | return 0; | |
241 | #else | |
242 | return time(_tod); | |
243 | #endif | |
244 | } | |
245 | ||
246 | #if defined(__MSDOS__) | |
247 | ||
248 | #if defined(TNT) && defined(_MSC_VER) | |
249 | ||
250 | void _CDECL _OS_setfileattr(const char *filename,unsigned attrib) | |
251 | { SetFileAttributes((LPSTR)filename, (DWORD)attrib); } | |
252 | ||
253 | #else | |
254 | ||
255 | void _CDECL _OS_setfileattr(const char *filename,unsigned attrib) | |
256 | { _dos_setfileattr(filename,attrib); } | |
257 | ||
258 | #endif | |
259 | ||
260 | #elif defined(__WIN32_VXD__) | |
261 | ||
262 | #define USE_LOCAL_FILEIO | |
263 | #define USE_LOCAL_GETDATE | |
264 | ||
265 | /* <libc/file.h> stub functions */ | |
266 | int _CDECL stub_open(const char *_path, int _oflag, unsigned _mode) | |
267 | { | |
268 | char mode[10]; | |
269 | int i; | |
270 | ||
271 | /* Find an empty file handle to use */ | |
272 | for (i = 3; i < MAX_FILES; i++) { | |
8bde7f77 WD |
273 | if (!openHandles[i]) |
274 | break; | |
275 | } | |
c7de829c | 276 | if (openHandles[i]) |
8bde7f77 | 277 | return -1; |
c7de829c WD |
278 | |
279 | /* Find the open flags to use */ | |
280 | if (_oflag & ___O_TRUNC) | |
8bde7f77 | 281 | strcpy(mode,"w"); |
c7de829c | 282 | else if (_oflag & ___O_CREAT) |
8bde7f77 | 283 | strcpy(mode,"a"); |
c7de829c | 284 | else |
8bde7f77 | 285 | strcpy(mode,"r"); |
c7de829c | 286 | if (_oflag & ___O_BINARY) |
8bde7f77 | 287 | strcat(mode,"b"); |
c7de829c | 288 | if (_oflag & ___O_TEXT) |
8bde7f77 | 289 | strcat(mode,"t"); |
c7de829c WD |
290 | |
291 | /* Open the file and store the file handle */ | |
292 | if ((openHandles[i] = fopen(_path,mode)) == NULL) | |
8bde7f77 | 293 | return -1; |
c7de829c WD |
294 | return i; |
295 | } | |
296 | ||
297 | int _CDECL stub_access(const char *_path, int _amode) | |
298 | { return -1; } | |
299 | ||
300 | int _CDECL stub_close(int _fildes) | |
301 | { | |
302 | if (_fildes >= 3 && openHandles[_fildes]) { | |
8bde7f77 WD |
303 | fclose(openHandles[_fildes]); |
304 | openHandles[_fildes] = NULL; | |
305 | } | |
c7de829c WD |
306 | return 0; |
307 | } | |
308 | ||
309 | off_t _CDECL stub_lseek(int _fildes, off_t _offset, int _whence) | |
310 | { | |
311 | if (_fildes >= 3) { | |
8bde7f77 WD |
312 | fseek(openHandles[_fildes],_offset,_whence); |
313 | return ftell(openHandles[_fildes]); | |
314 | } | |
c7de829c WD |
315 | return 0; |
316 | } | |
317 | ||
318 | size_t _CDECL stub_read(int _fildes, void *_buf, size_t _nbyte) | |
319 | { | |
320 | if (_fildes >= 3) | |
8bde7f77 | 321 | return fread(_buf,1,_nbyte,openHandles[_fildes]); |
c7de829c WD |
322 | return 0; |
323 | } | |
324 | ||
325 | int _CDECL stub_unlink(const char *_path) | |
326 | { | |
327 | WORD error; | |
328 | ||
329 | if (initComplete) { | |
8bde7f77 WD |
330 | if (R0_DeleteFile((char*)_path,0,&error)) |
331 | return 0; | |
332 | return -1; | |
333 | } | |
c7de829c | 334 | else |
8bde7f77 | 335 | return i_remove(_path); |
c7de829c WD |
336 | } |
337 | ||
338 | size_t _CDECL stub_write(int _fildes, const void *_buf, size_t _nbyte) | |
339 | { | |
340 | if (_fildes >= 3) | |
8bde7f77 | 341 | return fwrite(_buf,1,_nbyte,openHandles[_fildes]); |
c7de829c WD |
342 | return _nbyte; |
343 | } | |
344 | ||
345 | int _CDECL stub_isatty(int _fildes) | |
346 | { return 0; } | |
347 | ||
348 | /* <stdio.h> stub functions */ | |
349 | int _CDECL stub_remove(const char *_filename) | |
350 | { return stub_unlink(_filename); } | |
351 | ||
352 | int _CDECL stub_rename(const char *_old, const char *_new) | |
353 | { return -1; } | |
354 | ||
355 | void _CDECL _OS_setfileattr(const char *filename,unsigned attrib) | |
356 | { | |
357 | WORD error; | |
358 | if (initComplete) | |
8bde7f77 | 359 | R0_SetFileAttributes((char*)filename,attrib,&error); |
c7de829c WD |
360 | } |
361 | ||
362 | /* Return the current date in days since 1/1/1980 */ | |
363 | ulong _CDECL _OS_getcurrentdate(void) | |
364 | { | |
365 | DWORD date; | |
366 | VTD_Get_Date_And_Time(&date); | |
367 | return date; | |
368 | } | |
369 | ||
370 | #elif defined(__NT_DRIVER__) | |
371 | ||
372 | #define USE_LOCAL_FILEIO | |
373 | #define USE_LOCAL_GETDATE | |
374 | ||
375 | /* <libc/file.h> stub functions */ | |
376 | int _CDECL stub_open(const char *_path, int _oflag, unsigned _mode) | |
377 | { | |
378 | char mode[10]; | |
379 | int i; | |
380 | ||
381 | /* Find an empty file handle to use */ | |
382 | for (i = 3; i < MAX_FILES; i++) { | |
8bde7f77 WD |
383 | if (!openHandles[i]) |
384 | break; | |
385 | } | |
c7de829c | 386 | if (openHandles[i]) |
8bde7f77 | 387 | return -1; |
c7de829c WD |
388 | |
389 | /* Find the open flags to use */ | |
390 | if (_oflag & ___O_TRUNC) | |
8bde7f77 | 391 | strcpy(mode,"w"); |
c7de829c | 392 | else if (_oflag & ___O_CREAT) |
8bde7f77 | 393 | strcpy(mode,"a"); |
c7de829c | 394 | else |
8bde7f77 | 395 | strcpy(mode,"r"); |
c7de829c | 396 | if (_oflag & ___O_BINARY) |
8bde7f77 | 397 | strcat(mode,"b"); |
c7de829c | 398 | if (_oflag & ___O_TEXT) |
8bde7f77 | 399 | strcat(mode,"t"); |
c7de829c WD |
400 | |
401 | /* Open the file and store the file handle */ | |
402 | if ((openHandles[i] = fopen(_path,mode)) == NULL) | |
8bde7f77 | 403 | return -1; |
c7de829c WD |
404 | return i; |
405 | } | |
406 | ||
407 | int _CDECL stub_close(int _fildes) | |
408 | { | |
409 | if (_fildes >= 3 && openHandles[_fildes]) { | |
8bde7f77 WD |
410 | fclose(openHandles[_fildes]); |
411 | openHandles[_fildes] = NULL; | |
412 | } | |
c7de829c WD |
413 | return 0; |
414 | } | |
415 | ||
416 | off_t _CDECL stub_lseek(int _fildes, off_t _offset, int _whence) | |
417 | { | |
418 | if (_fildes >= 3) { | |
8bde7f77 WD |
419 | fseek(openHandles[_fildes],_offset,_whence); |
420 | return ftell(openHandles[_fildes]); | |
421 | } | |
c7de829c WD |
422 | return 0; |
423 | } | |
424 | ||
425 | size_t _CDECL stub_read(int _fildes, void *_buf, size_t _nbyte) | |
426 | { | |
427 | if (_fildes >= 3) | |
8bde7f77 | 428 | return fread(_buf,1,_nbyte,openHandles[_fildes]); |
c7de829c WD |
429 | return 0; |
430 | } | |
431 | ||
432 | size_t _CDECL stub_write(int _fildes, const void *_buf, size_t _nbyte) | |
433 | { | |
434 | if (_fildes >= 3) | |
8bde7f77 | 435 | return fwrite(_buf,1,_nbyte,openHandles[_fildes]); |
c7de829c WD |
436 | return _nbyte; |
437 | } | |
438 | ||
439 | int _CDECL stub_access(const char *_path, int _amode) | |
440 | { return -1; } | |
441 | ||
442 | int _CDECL stub_isatty(int _fildes) | |
443 | { return 0; } | |
444 | ||
445 | int _CDECL stub_unlink(const char *_path) | |
446 | { | |
8bde7f77 | 447 | /* TODO: Implement this! */ |
c7de829c WD |
448 | return -1; |
449 | } | |
450 | ||
451 | /* <stdio.h> stub functions */ | |
452 | int _CDECL stub_remove(const char *_filename) | |
453 | { return stub_unlink(_filename); } | |
454 | ||
455 | int _CDECL stub_rename(const char *_old, const char *_new) | |
456 | { | |
8bde7f77 | 457 | /* TODO: Implement this! */ |
c7de829c WD |
458 | return -1; |
459 | } | |
460 | ||
461 | void _CDECL _OS_setfileattr(const char *filename,unsigned attrib) | |
462 | { | |
463 | uint _attr = 0; | |
464 | if (attrib & __A_RDONLY) | |
8bde7f77 | 465 | _attr |= FILE_ATTRIBUTE_READONLY; |
c7de829c | 466 | if (attrib & __A_HIDDEN) |
8bde7f77 | 467 | _attr |= FILE_ATTRIBUTE_HIDDEN; |
c7de829c | 468 | if (attrib & __A_SYSTEM) |
8bde7f77 | 469 | _attr |= FILE_ATTRIBUTE_SYSTEM; |
c7de829c WD |
470 | PM_setFileAttr(filename,_attr); |
471 | } | |
472 | ||
473 | /* Return the current date in days since 1/1/1980 */ | |
474 | ulong _CDECL _OS_getcurrentdate(void) | |
475 | { | |
476 | TIME_FIELDS tm; | |
477 | _int64 count,count_1_1_1980; | |
478 | ||
479 | tm.Year = 1980; | |
480 | tm.Month = 1; | |
481 | tm.Day = 1; | |
482 | tm.Hour = 0; | |
483 | tm.Minute = 0; | |
484 | tm.Second = 0; | |
485 | tm.Milliseconds = 0; | |
486 | tm.Weekday = 0; | |
487 | RtlTimeFieldsToTime(&tm,(PLARGE_INTEGER)&count_1_1_1980); | |
488 | KeQuerySystemTime((PLARGE_INTEGER)&count); | |
489 | return (ulong)( (count - count_1_1_1980) / ((_int64)24 * (_int64)3600 * (_int64)10000000) ); | |
490 | } | |
491 | ||
492 | #elif defined(__WINDOWS32__) || defined(__RTTARGET__) | |
493 | ||
494 | void _CDECL _OS_setfileattr(const char *filename,unsigned attrib) | |
495 | { SetFileAttributes((LPSTR)filename, (DWORD)attrib); } | |
496 | ||
497 | #elif defined(__OS2__) | |
498 | ||
499 | #define USE_LOCAL_FILEIO | |
500 | ||
501 | #ifndef W_OK | |
502 | #define W_OK 0x02 | |
503 | #endif | |
504 | ||
505 | void _CDECL _OS_setfileattr(const char *filename,unsigned attrib) | |
506 | { | |
507 | FILESTATUS3 s; | |
508 | if (DosQueryPathInfo((PSZ)filename,FIL_STANDARD,(PVOID)&s,sizeof(s))) | |
8bde7f77 | 509 | return; |
c7de829c WD |
510 | s.attrFile = attrib; |
511 | DosSetPathInfo((PSZ)filename,FIL_STANDARD,(PVOID)&s,sizeof(s),0L); | |
512 | } | |
513 | ||
514 | /* <libc/file.h> stub functions */ | |
515 | ||
516 | #define BUF_SIZE 4096 | |
517 | ||
518 | /* Note: the implementation of the standard Unix-ish handle-based I/O isn't | |
519 | * complete - but that wasn't the intent either. Note also that we | |
520 | * don't presently support text file I/O, so all text files end | |
521 | * up in Unix format (and are not translated!). | |
522 | */ | |
523 | int _CDECL stub_open(const char *_path, int _oflag, unsigned _mode) | |
524 | { | |
525 | HFILE handle; | |
526 | ULONG error, actiontaken, openflag, openmode; | |
527 | char path[PM_MAX_PATH]; | |
528 | ||
529 | /* Determine open flags */ | |
530 | if (_oflag & ___O_CREAT) { | |
8bde7f77 WD |
531 | if (_oflag & ___O_EXCL) |
532 | openflag = OPEN_ACTION_FAIL_IF_EXISTS | OPEN_ACTION_CREATE_IF_NEW; | |
533 | else if (_oflag & ___O_TRUNC) | |
534 | openflag = OPEN_ACTION_REPLACE_IF_EXISTS | OPEN_ACTION_CREATE_IF_NEW; | |
535 | else | |
536 | openflag = OPEN_ACTION_OPEN_IF_EXISTS | OPEN_ACTION_CREATE_IF_NEW; | |
537 | } | |
c7de829c | 538 | else if (_oflag & ___O_TRUNC) |
8bde7f77 | 539 | openflag = OPEN_ACTION_REPLACE_IF_EXISTS; |
c7de829c | 540 | else |
8bde7f77 | 541 | openflag = OPEN_ACTION_OPEN_IF_EXISTS; |
c7de829c WD |
542 | |
543 | /* Determine open mode flags */ | |
544 | if (_oflag & ___O_RDONLY) | |
8bde7f77 | 545 | openmode = OPEN_ACCESS_READONLY | OPEN_SHARE_DENYNONE; |
c7de829c | 546 | else if (_oflag & ___O_WRONLY) |
8bde7f77 | 547 | openmode = OPEN_ACCESS_WRITEONLY | OPEN_SHARE_DENYWRITE; |
c7de829c | 548 | else |
8bde7f77 | 549 | openmode = OPEN_ACCESS_READWRITE | OPEN_SHARE_DENYWRITE; |
c7de829c WD |
550 | |
551 | /* Copy the path to a variable on the stack. We need to do this | |
552 | * for OS/2 as when the drivers are loaded into shared kernel | |
553 | * memory, we can't pass an address from that memory range to | |
554 | * this function. | |
555 | */ | |
556 | strcpy(path,_path); | |
557 | if (DosOpen(path, &handle, &actiontaken, 0, FILE_NORMAL, | |
8bde7f77 WD |
558 | openflag, openmode, NULL) != NO_ERROR) |
559 | return -1; | |
c7de829c WD |
560 | |
561 | /* Handle append mode of operation */ | |
562 | if (_oflag & ___O_APPEND) { | |
8bde7f77 WD |
563 | if (DosSetFilePtr(handle, 0, FILE_END, &error) != NO_ERROR) |
564 | return -1; | |
565 | } | |
c7de829c WD |
566 | return handle; |
567 | } | |
568 | ||
569 | int _CDECL stub_access(const char *_path, int _amode) | |
570 | { | |
571 | char path[PM_MAX_PATH]; | |
572 | FILESTATUS fs; | |
573 | ||
574 | /* Copy the path to a variable on the stack. We need to do this | |
575 | * for OS/2 as when the drivers are loaded into shared kernel | |
576 | * memory, we can't pass an address from that memory range to | |
577 | * this function. | |
578 | */ | |
579 | strcpy(path,_path); | |
580 | if (DosQueryPathInfo(path, FIL_STANDARD, &fs, sizeof(fs)) != NO_ERROR) | |
8bde7f77 | 581 | return -1; |
c7de829c | 582 | if ((_amode & W_OK) && (fs.attrFile & FILE_READONLY)) |
8bde7f77 | 583 | return -1; |
c7de829c WD |
584 | return 0; |
585 | } | |
586 | ||
587 | int _CDECL stub_close(int _fildes) | |
588 | { | |
589 | if (DosClose(_fildes) != NO_ERROR) | |
8bde7f77 | 590 | return -1; |
c7de829c WD |
591 | return 0; |
592 | } | |
593 | ||
594 | off_t _CDECL stub_lseek(int _fildes, off_t _offset, int _whence) | |
595 | { | |
596 | ULONG cbActual, origin; | |
597 | ||
598 | switch (_whence) { | |
8bde7f77 WD |
599 | case SEEK_CUR: |
600 | origin = FILE_CURRENT; | |
601 | break; | |
602 | case SEEK_END: | |
603 | origin = FILE_END; | |
604 | break; | |
605 | default: | |
606 | origin = FILE_BEGIN; | |
607 | } | |
c7de829c | 608 | if (DosSetFilePtr(_fildes, _offset, origin, &cbActual) != NO_ERROR) |
8bde7f77 | 609 | return -1; |
c7de829c WD |
610 | return cbActual; |
611 | } | |
612 | ||
613 | size_t _CDECL stub_read(int _fildes, void *_buf, size_t _nbyte) | |
614 | { | |
615 | ULONG cbActual = 0,cbRead; | |
616 | uchar *p = _buf; | |
617 | uchar file_io_buf[BUF_SIZE]; | |
618 | ||
619 | /* We need to perform the physical read in chunks into a | |
620 | * a temporary static buffer, since the buffer passed in may be | |
621 | * in kernel space and will cause DosRead to bail internally. | |
622 | */ | |
623 | while (_nbyte > BUF_SIZE) { | |
8bde7f77 WD |
624 | if (DosRead(_fildes, file_io_buf, BUF_SIZE, &cbRead) != NO_ERROR) |
625 | return -1; | |
626 | cbActual += cbRead; | |
627 | memcpy(p,file_io_buf,BUF_SIZE); | |
628 | p += BUF_SIZE; | |
629 | _nbyte -= BUF_SIZE; | |
630 | } | |
c7de829c | 631 | if (_nbyte) { |
8bde7f77 WD |
632 | if (DosRead(_fildes, file_io_buf, _nbyte, &cbRead) != NO_ERROR) |
633 | return -1; | |
634 | cbActual += cbRead; | |
635 | memcpy(p,file_io_buf,_nbyte); | |
636 | } | |
c7de829c WD |
637 | return cbActual; |
638 | } | |
639 | ||
640 | size_t _CDECL stub_write(int _fildes, const void *_buf, size_t _nbyte) | |
641 | { | |
642 | ULONG cbActual = 0,cbWrite; | |
643 | uchar *p = (PVOID)_buf; | |
644 | uchar file_io_buf[BUF_SIZE]; | |
645 | ||
646 | /* We need to perform the physical write in chunks from a | |
647 | * a temporary static buffer, since the buffer passed in may be | |
648 | * in kernel space and will cause DosWrite to bail internally. | |
649 | */ | |
650 | while (_nbyte > BUF_SIZE) { | |
8bde7f77 WD |
651 | memcpy(file_io_buf,p,BUF_SIZE); |
652 | if (DosWrite(_fildes, file_io_buf, BUF_SIZE, &cbWrite) != NO_ERROR) | |
653 | return -1; | |
654 | cbActual += cbWrite; | |
655 | p += BUF_SIZE; | |
656 | _nbyte -= BUF_SIZE; | |
657 | } | |
c7de829c | 658 | if (_nbyte) { |
8bde7f77 WD |
659 | memcpy(file_io_buf,p,_nbyte); |
660 | if (DosWrite(_fildes, file_io_buf, _nbyte, &cbWrite) != NO_ERROR) | |
661 | return -1; | |
662 | cbActual += cbWrite; | |
663 | } | |
c7de829c WD |
664 | return cbActual; |
665 | } | |
666 | ||
667 | int _CDECL stub_unlink(const char *_path) | |
668 | { | |
669 | char path[PM_MAX_PATH]; | |
670 | ||
671 | /* Copy the path to a variable on the stack. We need to do this | |
672 | * for OS/2 as when the drivers are loaded into shared kernel | |
673 | * memory, we can't pass an address from that memory range to | |
674 | * this function. | |
675 | */ | |
676 | strcpy(path,_path); | |
677 | if (DosDelete(path) != NO_ERROR) | |
8bde7f77 | 678 | return -1; |
c7de829c WD |
679 | return 0; |
680 | } | |
681 | ||
682 | int _CDECL stub_isatty(int _fildes) | |
683 | { | |
684 | ULONG htype, flags; | |
685 | ||
686 | if (DosQueryHType(_fildes, &htype, &flags) != NO_ERROR) | |
8bde7f77 | 687 | return 0; |
c7de829c WD |
688 | return ((htype & 0xFF) == HANDTYPE_DEVICE); |
689 | } | |
690 | ||
691 | /* <stdio.h> stub functions */ | |
692 | int _CDECL stub_remove(const char *_path) | |
693 | { | |
694 | char path[PM_MAX_PATH]; | |
695 | ||
696 | /* Copy the path to a variable on the stack. We need to do this | |
697 | * for OS/2 as when the drivers are loaded into shared kernel | |
698 | * memory, we can't pass an address from that memory range to | |
699 | * this function. | |
700 | */ | |
701 | strcpy(path,_path); | |
702 | if (DosDelete(path) != NO_ERROR) | |
8bde7f77 | 703 | return -1; |
c7de829c WD |
704 | return 0; |
705 | } | |
706 | ||
707 | int _CDECL stub_rename(const char *_old, const char *_new) | |
708 | { | |
709 | char old[PM_MAX_PATH]; | |
710 | char new[PM_MAX_PATH]; | |
711 | ||
712 | /* Copy the path to a variable on the stack. We need to do this | |
713 | * for OS/2 as when the drivers are loaded into shared kernel | |
714 | * memory, we can't pass an address from that memory range to | |
715 | * this function. | |
716 | */ | |
717 | strcpy(old,_old); | |
718 | strcpy(new,_new); | |
719 | if (DosMove(old, new) != NO_ERROR) | |
8bde7f77 | 720 | return -1; |
c7de829c WD |
721 | return 0; |
722 | } | |
723 | ||
724 | #else | |
725 | ||
726 | void _CDECL _OS_setfileattr(const char *filename,unsigned attrib) | |
727 | { /* Unable to set hidden, system attributes on Unix. */ } | |
728 | ||
729 | #endif | |
730 | ||
731 | #ifndef USE_LOCAL_FILEIO | |
732 | ||
733 | /* <libc/file.h> stub functions */ | |
734 | int _CDECL stub_open(const char *_path, int _oflag, unsigned _mode) | |
735 | { | |
736 | int oflag_tab[] = { | |
8bde7f77 WD |
737 | ___O_RDONLY, O_RDONLY, |
738 | ___O_WRONLY, O_WRONLY, | |
739 | ___O_RDWR, O_RDWR, | |
740 | ___O_BINARY, O_BINARY, | |
741 | ___O_TEXT, O_TEXT, | |
742 | ___O_CREAT, O_CREAT, | |
743 | ___O_EXCL, O_EXCL, | |
744 | ___O_TRUNC, O_TRUNC, | |
745 | ___O_APPEND, O_APPEND, | |
746 | }; | |
c7de829c WD |
747 | int i,oflag = 0; |
748 | ||
749 | /* Translate the oflag's to the OS dependent versions */ | |
750 | for (i = 0; i < sizeof(oflag_tab) / sizeof(int); i += 2) { | |
8bde7f77 WD |
751 | if (_oflag & oflag_tab[i]) |
752 | oflag |= oflag_tab[i+1]; | |
753 | } | |
c7de829c WD |
754 | return open(_path,oflag,_mode); |
755 | } | |
756 | ||
757 | int _CDECL stub_access(const char *_path, int _amode) | |
758 | { return access(_path,_amode); } | |
759 | ||
760 | int _CDECL stub_close(int _fildes) | |
761 | { return close(_fildes); } | |
762 | ||
763 | off_t _CDECL stub_lseek(int _fildes, off_t _offset, int _whence) | |
764 | { return lseek(_fildes,_offset,_whence); } | |
765 | ||
766 | size_t _CDECL stub_read(int _fildes, void *_buf, size_t _nbyte) | |
767 | { return read(_fildes,_buf,_nbyte); } | |
768 | ||
769 | int _CDECL stub_unlink(const char *_path) | |
770 | { return unlink(_path); } | |
771 | ||
772 | size_t _CDECL stub_write(int _fildes, const void *_buf, size_t _nbyte) | |
773 | { return write(_fildes,_buf,_nbyte); } | |
774 | ||
775 | int _CDECL stub_isatty(int _fildes) | |
776 | { return isatty(_fildes); } | |
777 | ||
778 | /* <stdio.h> stub functions */ | |
779 | int _CDECL stub_remove(const char *_filename) | |
780 | { return remove(_filename); } | |
781 | ||
782 | int _CDECL stub_rename(const char *_old, const char *_new) | |
783 | { return rename(_old,_new); } | |
784 | ||
785 | #endif | |
786 | ||
787 | #ifndef USE_LOCAL_GETDATE | |
788 | ||
789 | /* Return the current date in days since 1/1/1980 */ | |
790 | ulong _CDECL _OS_getcurrentdate(void) | |
791 | { | |
792 | struct tm refTime; | |
793 | refTime.tm_year = 80; | |
794 | refTime.tm_mon = 0; | |
795 | refTime.tm_mday = 1; | |
796 | refTime.tm_hour = 0; | |
797 | refTime.tm_min = 0; | |
798 | refTime.tm_sec = 0; | |
799 | refTime.tm_isdst = -1; | |
800 | return (time(NULL) - mktime(&refTime)) / (24 * 3600L); | |
801 | } | |
802 | ||
803 | #endif | |
804 | ||
805 | int _CDECL stub_raise(int sig) | |
806 | { | |
807 | #if defined(__WIN32_VXD__) || defined(__NT_DRIVER__) || defined(__SMX32__) | |
808 | return -1; | |
809 | #else | |
810 | return raise(sig); | |
811 | #endif | |
812 | } | |
813 | ||
814 | #ifdef __WINDOWS32__ | |
815 | typedef void (*__code_ptr)(int); | |
816 | #else | |
817 | typedef void (*__code_ptr)(); | |
818 | #endif | |
819 | ||
820 | void * _CDECL stub_signal(int sig, void *handler) | |
821 | { | |
822 | #if defined(__WIN32_VXD__) || defined(__NT_DRIVER__) || defined(__SMX32__) | |
823 | return NULL; | |
824 | #else | |
825 | return (void*)signal(sig,(__code_ptr)handler); | |
826 | #endif | |
827 | } |