4 * Implements C run-time functionality as known from UNIX.
6 * Copyright 1996 Marcus Meissner
7 * Copyright 1996 Jukka Iivonen
8 * Copyright 1997 Uwe Bonnes
12 Unresolved issues Uwe Bonnes 970904:
13 - Handling of Binary/Text Files is crude. If in doubt, use fromdos or recode
14 - Arguments in crtdll.spec for functions with double argument
15 - system-call calls another wine process, but without debugging arguments
16 and uses the first wine executable in the path
17 - tested with ftp://ftp.remcomp.com/pub/remcomp/lcc-win32.zip, a C-Compiler
18 for Win32, based on lcc, from Jacob Navia
21 /* FIXME: all the file handling is hopelessly broken -- AJ */
28 #include <sys/times.h>
47 extern int FILE_GetUnixHandle( HFILE32
);
49 static DOS_FULL_NAME CRTDLL_tmpname
;
51 extern INT32
WIN32_wsprintf32W( DWORD
*args
);
53 UINT32 CRTDLL_argc_dll
; /* CRTDLL.23 */
54 LPSTR
*CRTDLL_argv_dll
; /* CRTDLL.24 */
55 LPSTR CRTDLL_acmdln_dll
; /* CRTDLL.38 */
56 UINT32 CRTDLL_basemajor_dll
; /* CRTDLL.42 */
57 UINT32 CRTDLL_baseminor_dll
; /* CRTDLL.43 */
58 UINT32 CRTDLL_baseversion_dll
; /* CRTDLL.44 */
59 UINT32 CRTDLL_commode_dll
; /* CRTDLL.59 */
60 LPSTR CRTDLL_environ_dll
; /* CRTDLL.75 */
61 UINT32 CRTDLL_fmode_dll
; /* CRTDLL.104 */
62 UINT32 CRTDLL_osmajor_dll
; /* CRTDLL.241 */
63 UINT32 CRTDLL_osminor_dll
; /* CRTDLL.242 */
64 UINT32 CRTDLL_osmode_dll
; /* CRTDLL.243 */
65 UINT32 CRTDLL_osver_dll
; /* CRTDLL.244 */
66 UINT32 CRTDLL_osversion_dll
; /* CRTDLL.245 */
67 UINT32 CRTDLL_winmajor_dll
; /* CRTDLL.329 */
68 UINT32 CRTDLL_winminor_dll
; /* CRTDLL.330 */
69 UINT32 CRTDLL_winver_dll
; /* CRTDLL.331 */
71 typedef VOID (*new_handler_type
)(VOID
);
73 static new_handler_type new_handler
;
75 /*********************************************************************
76 * _GetMainArgs (CRTDLL.022)
78 DWORD __cdecl
CRTDLL__GetMainArgs(LPDWORD argc
,LPSTR
**argv
,
79 LPSTR
*environ
,DWORD flag
)
83 int xargc
,i
,afterlastspace
;
86 dprintf_crtdll(stddeb
,"CRTDLL__GetMainArgs(%p,%p,%p,%ld).\n",
87 argc
,argv
,environ
,flag
89 CRTDLL_acmdln_dll
= cmdline
= HEAP_strdupA( GetProcessHeap(), 0,
90 GetCommandLine32A() );
91 dprintf_crtdll(stddeb
,"CRTDLL__GetMainArgs got \"%s\"\n",
94 version
= GetVersion32();
95 CRTDLL_osver_dll
= version
>> 16;
96 CRTDLL_winminor_dll
= version
& 0xFF;
97 CRTDLL_winmajor_dll
= (version
>>8) & 0xFF;
98 CRTDLL_baseversion_dll
= version
>> 16;
99 CRTDLL_winver_dll
= ((version
>> 8) & 0xFF) + ((version
& 0xFF) << 8);
100 CRTDLL_baseminor_dll
= (version
>> 16) & 0xFF;
101 CRTDLL_basemajor_dll
= (version
>> 24) & 0xFF;
102 CRTDLL_osversion_dll
= version
& 0xFFFF;
103 CRTDLL_osminor_dll
= version
& 0xFF;
104 CRTDLL_osmajor_dll
= (version
>>8) & 0xFF;
106 /* missing threading init */
108 i
=0;xargv
=NULL
;xargc
=0;afterlastspace
=0;
110 if (cmdline
[i
]==' ') {
111 xargv
=(char**)HeapReAlloc( GetProcessHeap(), 0, xargv
,
112 sizeof(char*)*(++xargc
));
114 xargv
[xargc
-1] = HEAP_strdupA( GetProcessHeap(), 0,
115 cmdline
+afterlastspace
);
117 while (cmdline
[i
]==' ')
124 xargv
=(char**)HeapReAlloc( GetProcessHeap(), 0, xargv
,
125 sizeof(char*)*(++xargc
));
127 xargv
[xargc
-1] = HEAP_strdupA( GetProcessHeap(), 0,
128 cmdline
+afterlastspace
);
129 CRTDLL_argc_dll
= xargc
;
131 CRTDLL_argv_dll
= xargv
;
134 dprintf_crtdll(stddeb
,"CRTDLL__GetMainArgs found %d arguments\n",
136 CRTDLL_environ_dll
= *environ
= GetEnvironmentStrings32A();
141 typedef void (*_INITTERMFUN
)();
143 /*********************************************************************
144 * _initterm (CRTDLL.135)
146 DWORD __cdecl
CRTDLL__initterm(_INITTERMFUN
*start
,_INITTERMFUN
*end
)
148 _INITTERMFUN
*current
;
150 dprintf_crtdll(stddeb
,"_initterm(%p,%p)\n",start
,end
);
152 while (current
<end
) {
153 if (*current
) (*current
)();
159 /*********************************************************************
160 * _fdopen (CRTDLL.91)
162 DWORD __cdecl
CRTDLL__fdopen(INT32 handle
, LPCSTR mode
)
170 case 1 : file
=stdout
;
172 case 2 : file
=stderr
;
175 file
=fdopen(handle
,mode
);
177 dprintf_crtdll(stddeb
,
178 "CRTDLL_fdopen open handle %d mode %s got file %p\n",
183 /*******************************************************************
184 * _global_unwind2 (CRTDLL.129)
186 void __cdecl
CRTDLL__global_unwind2( CONTEXT
*context
)
188 /* Retrieve the arguments (args[0] is return addr, args[1] is first arg) */
189 DWORD
*args
= (DWORD
*)ESP_reg(context
);
190 RtlUnwind( (PEXCEPTION_FRAME
)args
[1], (LPVOID
)EIP_reg(context
),
194 /*******************************************************************
195 * _local_unwind2 (CRTDLL.173)
197 void __cdecl
CRTDLL__local_unwind2( CONTEXT
*context
)
199 /* Retrieve the arguments (args[0] is return addr, args[1] is first arg) */
200 DWORD
*args
= (DWORD
*)ESP_reg(context
);
201 PEXCEPTION_FRAME endframe
= (PEXCEPTION_FRAME
)args
[1];
203 fprintf(stderr
,"CRTDLL__local_unwind2(%p,%ld)\n",endframe
,nr
);
206 /*********************************************************************
209 DWORD __cdecl
CRTDLL_fopen(LPCSTR path
, LPCSTR mode
)
214 DOS_FULL_NAME full_name
;
216 if (!DOSFS_GetFullName( path
, FALSE
, &full_name
)) {
217 dprintf_crtdll(stddeb
,"CRTDLL_fopen file %s bad name\n",path
);
221 file
=fopen(full_name
.long_name
,mode
);
226 if ((strchr(mode
,'r')&&strchr(mode
,'a'))||
227 (strchr(mode
,'r')&&strchr(mode
,'w'))||
228 (strchr(mode
,'w')&&strchr(mode
,'a')))
231 if (strstr(mode
,"r+")) flagmode
=O_RDWR
;
232 else if (strchr(mode
,'r')) flagmode
= O_RDONLY
;
233 else if (strstr(mode
,"w+")) flagmode
= O_RDWR
| O_TRUNC
| O_CREAT
;
234 else if (strchr(mode
,'w')) flagmode
= O_WRONLY
| O_TRUNC
| O_CREAT
;
235 else if (strstr(mode
,"a+")) flagmode
= O_RDWR
| O_CREAT
| O_APPEND
;
236 else if (strchr(mode
,'w')) flagmode
= O_RDWR
| O_CREAT
| O_APPEND
;
237 else if (strchr(mode
,'b'))
238 dprintf_crtdll(stderr
,
239 "CRTDLL_fopen %s in BINARY mode\n",path
);
241 dos_fildes
=FILE_Open(path
, flagmode
);
242 unix_fildes
=FILE_GetUnixHandle(dos_fildes
);
243 file
= fdopen(unix_fildes
,mode
);
245 dprintf_crtdll(stddeb
,
246 "CRTDLL_fopen file %s mode %s got ufh %d dfh %d file %p\n",
247 path
,mode
,unix_fildes
,dos_fildes
,file
);
251 /*********************************************************************
254 DWORD __cdecl
CRTDLL_fread(LPVOID ptr
, INT32 size
, INT32 nmemb
, LPVOID file
)
261 /* If we would honour CR/LF <-> LF translation, we could do it like this.
262 We should keep track of all files opened, and probably files with \
263 known binary extensions must be unchanged */
264 while ( (i
< (nmemb
*size
)) && (ret
==1)) {
265 ret
=fread(temp
,1,1,file
);
266 dprintf_crtdll(stddeb
,
267 "CRTDLL_fread got %c 0x%02x ret %d\n",
268 (isalpha(*(unsigned char*)temp
))? *(unsigned char*)temp
:
269 ' ',*(unsigned char*)temp
, ret
);
270 if (*(unsigned char*)temp
!= 0xd) { /* skip CR */
275 dprintf_crtdll(stddeb
, "CRTDLL_fread skipping ^M\n");
277 dprintf_crtdll(stddeb
,
278 "CRTDLL_fread 0x%08x items of size %d from file %p to %p%s\n",
279 nmemb
,size
,file
,ptr
,(i
!=nmemb
)?" failed":"");
283 ret
=fread(ptr
,size
,nmemb
,file
);
284 dprintf_crtdll(stddeb
,
285 "CRTDLL_fread 0x%08x items of size %d from file %p to %p%s\n",
286 nmemb
,size
,file
,ptr
,(ret
!=nmemb
)?" failed":"");
291 /*********************************************************************
294 LONG __cdecl
CRTDLL_fseek(LPVOID stream
, LONG offset
, INT32 whence
)
298 ret
=fseek(stream
,offset
,whence
);
299 dprintf_crtdll(stddeb
,
300 "CRTDLL_fseek file %p to 0x%08lx pos %s%s\n",
301 stream
,offset
,(whence
==SEEK_SET
)?"SEEK_SET":
302 (whence
==SEEK_CUR
)?"SEEK_CUR":
303 (whence
==SEEK_END
)?"SEEK_END":"UNKNOWN",
308 /*********************************************************************
311 LONG __cdecl
CRTDLL_ftell(LPVOID stream
)
316 dprintf_crtdll(stddeb
,
317 "CRTDLL_ftell file %p at 0x%08lx\n",
322 /*********************************************************************
323 * fwrite (CRTDLL.386)
325 DWORD __cdecl
CRTDLL_fwrite(LPVOID ptr
, INT32 size
, INT32 nmemb
, LPVOID file
)
329 ret
=fwrite(ptr
,size
,nmemb
,file
);
330 dprintf_crtdll(stddeb
,
331 "CRTDLL_fwrite 0x%08x items of size %d from %p to file %p%s\n",
332 nmemb
,size
,ptr
,file
,(ret
!=nmemb
)?" failed":"");
336 /*********************************************************************
337 * setbuf (CRTDLL.452)
339 INT32 __cdecl
CRTDLL_setbuf(LPVOID file
, LPSTR buf
)
341 dprintf_crtdll(stddeb
,
342 "CRTDLL_setbuf(file %p buf %p)\n",
344 /* this doesn't work:"void value not ignored as it ought to be"
345 return setbuf(file,buf);
351 /*********************************************************************
352 * _open_osfhandle (CRTDLL.240)
354 HFILE32 __cdecl
CRTDLL__open_osfhandle(LONG osfhandle
, INT32 flags
)
359 case STD_INPUT_HANDLE
:
363 case STD_OUTPUT_HANDLE
:
367 case STD_ERROR_HANDLE
:
374 dprintf_crtdll(stddeb
,
375 "CRTDLL_open_osfhandle(handle %08lx,flags %d) return %d\n",
376 osfhandle
,flags
,handle
);
381 /*********************************************************************
384 void __cdecl
CRTDLL_srand(DWORD seed
)
386 /* FIXME: should of course be thread? process? local */
390 /*********************************************************************
391 * fprintf (CRTDLL.373)
393 INT32 __cdecl
CRTDLL_fprintf( FILE *file
, LPSTR format
, ... )
398 va_start( valist
, format
);
399 res
= vfprintf( file
, format
, valist
);
404 /*********************************************************************
405 * vfprintf (CRTDLL.373)
407 INT32 __cdecl
CRTDLL_vfprintf( FILE *file
, LPSTR format
, va_list args
)
409 return vfprintf( file
, format
, args
);
412 /*********************************************************************
415 time_t __cdecl
CRTDLL_time(time_t *timeptr
)
417 time_t curtime
= time(NULL
);
424 /*********************************************************************
427 clock_t __cdecl
CRTDLL_clock(void)
433 res
= alltimes
.tms_utime
+ alltimes
.tms_stime
+
434 alltimes
.tms_cutime
+ alltimes
.tms_cstime
;
435 /* Fixme: We need some symbolic representation
436 for (Hostsystem_)CLOCKS_PER_SEC
437 and (Emulated_system_)CLOCKS_PER_SEC
438 10 holds only for Windows/Linux_i86)
443 /*********************************************************************
444 * _isatty (CRTDLL.137)
446 BOOL32 __cdecl
CRTDLL__isatty(DWORD x
)
448 dprintf_crtdll(stderr
,"CRTDLL__isatty(%ld)\n",x
);
452 /*********************************************************************
453 * _write (CRTDLL.332)
455 INT32 __cdecl
CRTDLL__write(INT32 fd
,LPCVOID buf
,UINT32 count
)
462 len
= (UINT32
)write(fd
,buf
,(LONG
)count
);
464 len
= _lwrite32(fd
,buf
,count
);
465 dprintf_crtdll(stddeb
,"CRTDLL_write %d/%d byte to dfh %d from %p,\n",
471 /*********************************************************************
474 * FIXME: What the heck is the difference between
475 * FIXME _c_exit (CRTDLL.47)
476 * FIXME _cexit (CRTDLL.49)
477 * FIXME _exit (CRTDLL.87)
478 * FIXME exit (CRTDLL.359)
480 * atexit-processing comes to mind -- MW.
483 void __cdecl
CRTDLL__cexit(INT32 ret
)
485 dprintf_crtdll(stddeb
,"CRTDLL__cexit(%d)\n",ret
);
490 /*********************************************************************
493 void __cdecl
CRTDLL_exit(DWORD ret
)
495 dprintf_crtdll(stddeb
,"CRTDLL_exit(%ld)\n",ret
);
500 /*********************************************************************
501 * _abnormal_termination (CRTDLL.36 )
503 INT32 __cdecl
CRTDLL__abnormal_termination(void)
505 dprintf_crtdll(stddeb
,"CRTDLL__abnormal_termination\n");
510 /*********************************************************************
511 * fflush (CRTDLL.365)
513 INT32 __cdecl
CRTDLL_fflush(LPVOID stream
)
517 ret
= fflush(stream
);
518 dprintf_crtdll(stddeb
,"CRTDLL_fflush %p returnd %d %s\n",stream
,ret
,(ret
)?"":" failed");
523 /*********************************************************************
526 LPSTR __cdecl
CRTDLL_gets(LPSTR buf
)
529 /* BAD, for the whole WINE process blocks... just done this way to test
530 * windows95's ftp.exe.
533 dprintf_crtdll(stddeb
,"CRTDLL_gets got %s\n",ret
);
538 /*********************************************************************
541 INT32 __cdecl
CRTDLL_rand()
547 /*********************************************************************
548 * putchar (CRTDLL.442)
550 void __cdecl
CRTDLL_putchar( INT32 x
)
556 /*********************************************************************
559 INT32 __cdecl
CRTDLL_fputc( INT32 c
, FILE *stream
)
561 dprintf_crtdll(stddeb
,
562 "CRTDLL_fputc %c to file %p\n",c
,stream
);
563 return fputc(c
,stream
);
567 /*********************************************************************
570 INT32 __cdecl
CRTDLL_fputs( LPCSTR s
, FILE *stream
)
572 dprintf_crtdll(stddeb
,
573 "CRTDLL_fputs %s to file %p\n",s
,stream
);
574 return fputs(s
,stream
);
578 /*********************************************************************
581 INT32 __cdecl
CRTDLL_puts(LPCSTR s
)
583 dprintf_crtdll(stddeb
,
584 "CRTDLL_fputs %s \n",s
);
589 /*********************************************************************
592 INT32 __cdecl
CRTDLL_putc(INT32 c
, FILE *stream
)
594 dprintf_crtdll(stddeb
,
595 "CRTDLL_putc %c to file %p\n",c
,stream
);
596 return fputc(c
,stream
);
598 /*********************************************************************
601 INT32 __cdecl
CRTDLL_fgetc( FILE *stream
)
603 int ret
= fgetc(stream
);
604 dprintf_crtdll(stddeb
,
605 "CRTDLL_fgetc got %d\n",ret
);
610 /*********************************************************************
613 INT32 __cdecl
CRTDLL_getc( FILE *stream
)
615 int ret
= fgetc(stream
);
616 dprintf_crtdll(stddeb
,
617 "CRTDLL_getc got %d\n",ret
);
621 /*********************************************************************
624 UINT32 __cdecl
CRTDLL__rotl(UINT32 x
,INT32 shift
)
626 unsigned int ret
= (x
>> shift
)|( x
>>((sizeof(x
))-shift
));
628 dprintf_crtdll(stddeb
,
629 "CRTDLL_rotl got 0x%08x rot %d ret 0x%08x\n",
634 /*********************************************************************
635 * _lrotl (CRTDLL.176)
637 DWORD __cdecl
CRTDLL__lrotl(DWORD x
,INT32 shift
)
639 unsigned long ret
= (x
>> shift
)|( x
>>((sizeof(x
))-shift
));
641 dprintf_crtdll(stddeb
,
642 "CRTDLL_lrotl got 0x%08lx rot %d ret 0x%08lx\n",
649 /*********************************************************************
652 CHAR
* __cdecl
CRTDLL_fgets(LPSTR s
,INT32 size
, LPVOID stream
)
657 ret
=fgets(s
, size
,stream
);
658 /*FIXME: Control with CRTDLL_setmode */
659 control_M
= strrchr(s
,'\r');
660 /*delete CR if we read a DOS File */
666 dprintf_crtdll(stddeb
,
667 "CRTDLL_fgets got %s for %d chars from file %p%s\n",
668 s
,size
,stream
,(ret
)?"":" failed");
673 /*********************************************************************
674 * _mbsicmp (CRTDLL.204)
676 int __cdecl
CRTDLL__mbsicmp(unsigned char *x
,unsigned char *y
)
683 /* FIXME: MBCS handling... */
692 /*********************************************************************
693 * _mbsinc (CRTDLL.205)
695 unsigned char * __cdecl
CRTDLL__mbsinc(unsigned char *x
)
702 /*********************************************************************
703 * vsprintf (CRTDLL.500)
705 INT32 __cdecl
CRTDLL_vsprintf( LPSTR buffer
, LPCSTR spec
, va_list args
)
707 return wvsprintf32A( buffer
, spec
, args
);
710 /*********************************************************************
711 * vswprintf (CRTDLL.501)
713 INT32 __cdecl
CRTDLL_vswprintf( LPWSTR buffer
, LPCWSTR spec
, va_list args
)
715 return wvsprintf32W( buffer
, spec
, args
);
718 /*********************************************************************
719 * _mbscpy (CRTDLL.200)
721 unsigned char* __cdecl
CRTDLL__mbscpy(unsigned char *x
,unsigned char *y
)
723 dprintf_crtdll(stddeb
,"CRTDLL_mbscpy %s and %s\n",x
,y
);
728 /*********************************************************************
729 * _mbscat (CRTDLL.197)
731 unsigned char* __cdecl
CRTDLL__mbscat(unsigned char *x
,unsigned char *y
)
737 /*********************************************************************
738 * _strcmpi (CRTDLL.282) (CRTDLL.287)
740 INT32 __cdecl
CRTDLL__strcmpi( LPCSTR s1
, LPCSTR s2
)
742 return lstrcmpi32A( s1
, s2
);
746 /*********************************************************************
747 * _strnicmp (CRTDLL.293)
749 INT32 __cdecl
CRTDLL__strnicmp( LPCSTR s1
, LPCSTR s2
, INT32 n
)
751 return lstrncmpi32A( s1
, s2
, n
);
755 /*********************************************************************
756 * _strlwr (CRTDLL.293)
758 * convert a string in place to lowercase
760 LPSTR
CRTDLL__strlwr(LPSTR x
)
764 dprintf_crtdll(stddeb
,
765 "CRTDLL_strlwr got %s",x
);
767 if ((*y
> 0x40) && (*y
< 0x5b))
771 dprintf_crtdll(stddeb
," returned %s\n",x
);
776 /*********************************************************************
777 * system (CRTDLL.485)
779 INT32
CRTDLL_system(LPSTR x
)
781 #define SYSBUF_LENGTH 1500
782 char buffer
[SYSBUF_LENGTH
];
783 unsigned char *y
= x
;
787 sprintf( buffer
, "%s \"", Options
.argv0
);
788 bp
= buffer
+ strlen(buffer
);
789 i
= strlen(buffer
) + strlen(x
) +2;
791 /* Calculate needed buffer size to prevent overflow. */
796 /* If buffer too short, exit. */
797 if (i
> SYSBUF_LENGTH
) {
798 dprintf_crtdll(stddeb
,"_system buffer to small\n");
807 if (*(y
-1) =='\\') *bp
++ = '\\';
809 /* Remove spaces from end of string. */
810 while (*(y
-1) == ' ') {
815 dprintf_crtdll(stddeb
,
816 "_system got \"%s\", executing \"%s\"\n",x
,buffer
);
818 return system(buffer
);
821 /*********************************************************************
822 * _strupr (CRTDLL.300)
824 LPSTR __cdecl
CRTDLL__strupr(LPSTR x
)
835 /*********************************************************************
836 * _wcsupr (CRTDLL.328)
838 LPWSTR __cdecl
CRTDLL__wcsupr(LPWSTR x
)
849 /*********************************************************************
850 * _wcslwr (CRTDLL.323)
852 LPWSTR __cdecl
CRTDLL__wcslwr(LPWSTR x
)
864 /*********************************************************************
865 * longjmp (CRTDLL.426)
867 VOID __cdecl
CRTDLL_longjmp(jmp_buf env
, int val
)
869 dprintf_crtdll(stdnimp
,"CRTDLL_longjmp semistup, expect crash\n");
870 dprintf_crtdll(stddeb
, "CRTDLL_longjmp semistup, expect crash\n");
871 return longjmp(env
, val
);
874 /*********************************************************************
875 * malloc (CRTDLL.427)
877 VOID
* __cdecl
CRTDLL_malloc(DWORD size
)
879 return HeapAlloc(GetProcessHeap(),0,size
);
882 /*********************************************************************
885 VOID
* __cdecl
CRTDLL_new(DWORD size
)
888 if(!(result
= HeapAlloc(GetProcessHeap(),0,size
)) && new_handler
)
893 /*********************************************************************
894 * set_new_handler(CRTDLL.003)
896 new_handler_type __cdecl
CRTDLL_set_new_handler(new_handler_type func
)
898 new_handler_type old_handler
= new_handler
;
903 /*********************************************************************
904 * calloc (CRTDLL.350)
906 VOID
* __cdecl
CRTDLL_calloc(DWORD size
, DWORD count
)
908 return HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
, size
* count
);
911 /*********************************************************************
912 * realloc (CRTDLL.447)
914 VOID
* __cdecl
CRTDLL_realloc( VOID
*ptr
, DWORD size
)
916 return HeapReAlloc( GetProcessHeap(), 0, ptr
, size
);
919 /*********************************************************************
922 VOID __cdecl
CRTDLL_free(LPVOID ptr
)
924 HeapFree(GetProcessHeap(),0,ptr
);
927 /*********************************************************************
928 * delete (CRTDLL.002)
930 VOID __cdecl
CRTDLL_delete(VOID
* ptr
)
932 HeapFree(GetProcessHeap(),0,ptr
);
935 /*********************************************************************
936 * _strdup (CRTDLL.285)
938 LPSTR __cdecl
CRTDLL__strdup(LPSTR ptr
)
940 return HEAP_strdupA(GetProcessHeap(),0,ptr
);
944 /*********************************************************************
945 * fclose (CRTDLL.362)
947 INT32 __cdecl
CRTDLL_fclose( FILE *stream
)
949 int unix_handle
=fileno(stream
);
950 HFILE32 dos_handle
=1;
953 if (unix_handle
<4) ret
= fclose(stream
);
955 while(FILE_GetUnixHandle(dos_handle
) != unix_handle
) dos_handle
++;
957 ret
= _lclose32( dos_handle
);
959 dprintf_crtdll(stddeb
,"CRTDLL_fclose(%p) ufh %d dfh %d%s\n",
960 stream
,unix_handle
,dos_handle
,(ret
)?" failed":"");
964 /*********************************************************************
965 * _unlink (CRTDLL.315)
967 INT32 __cdecl
CRTDLL__unlink(LPCSTR pathname
)
970 DOS_FULL_NAME full_name
;
972 if (!DOSFS_GetFullName( pathname
, FALSE
, &full_name
)) {
973 dprintf_crtdll(stddeb
,"CRTDLL_unlink file %s bad name\n",pathname
);
977 ret
=unlink(full_name
.long_name
);
978 dprintf_crtdll(stddeb
,"CRTDLL_unlink(%s unix %s)%s\n",
979 pathname
,full_name
.long_name
, (ret
)?" failed":"");
983 /*********************************************************************
984 * rename (CRTDLL.449)
986 INT32 __cdecl
CRTDLL_rename(LPCSTR oldpath
,LPCSTR newpath
)
988 BOOL32 ok
= MoveFileEx32A( oldpath
, newpath
, MOVEFILE_REPLACE_EXISTING
);
993 /*********************************************************************
1012 int __cdecl
CRTDLL__stat(const char * filename
, struct win_stat
* buf
)
1015 DOS_FULL_NAME full_name
;
1018 if (!DOSFS_GetFullName( filename
, TRUE
, &full_name
))
1020 dprintf_crtdll(stddeb
,"CRTDLL__stat filename %s bad name\n",filename
);
1023 ret
=stat(full_name
.long_name
,&mystat
);
1024 dprintf_crtdll(stddeb
,"CRTDLL__stat %s%s\n",
1025 filename
, (ret
)?" failed":"");
1027 /* FIXME: should check what Windows returns */
1029 buf
->win_st_dev
= mystat
.st_dev
;
1030 buf
->win_st_ino
= mystat
.st_ino
;
1031 buf
->win_st_mode
= mystat
.st_mode
;
1032 buf
->win_st_nlink
= mystat
.st_nlink
;
1033 buf
->win_st_uid
= mystat
.st_uid
;
1034 buf
->win_st_gid
= mystat
.st_gid
;
1035 buf
->win_st_rdev
= mystat
.st_rdev
;
1036 buf
->win_st_size
= mystat
.st_size
;
1037 buf
->win_st_atime
= mystat
.st_atime
;
1038 buf
->win_st_mtime
= mystat
.st_mtime
;
1039 buf
->win_st_ctime
= mystat
.st_ctime
;
1043 /*********************************************************************
1044 * _open (CRTDLL.239)
1046 HFILE32 __cdecl
CRTDLL__open(LPCSTR path
,INT32 flags
)
1052 the flags in lcc's header differ from the ones in Linux, e.g.
1053 Linux: define O_APPEND 02000 (= 0x400)
1054 lcc: define _O_APPEND 0x0008
1055 so here a scheme to translate them
1056 Probably lcc is wrong here, but at least a hack to get is going
1058 wineflags
= (flags
& 3);
1059 if (flags
& 0x0008 ) wineflags
|= O_APPEND
;
1060 if (flags
& 0x0100 ) wineflags
|= O_CREAT
;
1061 if (flags
& 0x0200 ) wineflags
|= O_TRUNC
;
1062 if (flags
& 0x0400 ) wineflags
|= O_EXCL
;
1063 if (flags
& 0xf0f4 )
1064 dprintf_crtdll(stddeb
,"CRTDLL_open file unsupported flags 0x%04x\n",flags
);
1067 ret
= FILE_Open(path
,wineflags
);
1068 dprintf_crtdll(stddeb
,"CRTDLL_open file %s mode 0x%04x (lccmode 0x%04x) got dfh %d\n",
1069 path
,wineflags
,flags
,ret
);
1073 /*********************************************************************
1074 * _close (CRTDLL.57)
1076 INT32 __cdecl
CRTDLL__close(HFILE32 fd
)
1078 int ret
=_lclose32(fd
);
1080 dprintf_crtdll(stddeb
,"CRTDLL_close(%d)%s\n",fd
,(ret
)?" failed":"");
1084 /*********************************************************************
1087 INT32 __cdecl
CRTDLL_feof( FILE *stream
)
1092 dprintf_crtdll(stddeb
,"CRTDLL_feof(%p) %s\n",stream
,(ret
)?"true":"false");
1096 /*********************************************************************
1097 * setlocale (CRTDLL.453)
1099 LPSTR __cdecl
CRTDLL_setlocale(INT32 category
,LPCSTR locale
)
1104 case CRTDLL_LC_ALL
: categorystr
="LC_ALL";break;
1105 case CRTDLL_LC_COLLATE
: categorystr
="LC_COLLATE";break;
1106 case CRTDLL_LC_CTYPE
: categorystr
="LC_CTYPE";break;
1107 case CRTDLL_LC_MONETARY
: categorystr
="LC_MONETARY";break;
1108 case CRTDLL_LC_NUMERIC
: categorystr
="LC_NUMERIC";break;
1109 case CRTDLL_LC_TIME
: categorystr
="LC_TIME";break;
1110 default: categorystr
= "UNKNOWN?";break;
1112 fprintf(stderr
,"CRTDLL_setlocale(%s,%s),stub!\n",categorystr
,locale
);
1116 /*********************************************************************
1117 * wcscat (CRTDLL.503)
1119 LPWSTR __cdecl
CRTDLL_wcscat( LPWSTR s1
, LPCWSTR s2
)
1121 return lstrcat32W( s1
, s2
);
1124 /*********************************************************************
1125 * wcschr (CRTDLL.504)
1127 LPWSTR __cdecl
CRTDLL_wcschr(LPCWSTR str
,WCHAR xchar
)
1139 /*********************************************************************
1140 * wcscmp (CRTDLL.505)
1142 INT32 __cdecl
CRTDLL_wcscmp( LPCWSTR s1
, LPCWSTR s2
)
1144 return lstrcmp32W( s1
, s2
);
1147 /*********************************************************************
1148 * wcscpy (CRTDLL.507)
1150 LPWSTR __cdecl
CRTDLL_wcscpy( LPWSTR s1
, LPCWSTR s2
)
1152 return lstrcpy32W( s1
, s2
);
1155 /*********************************************************************
1156 * wcscspn (CRTDLL.508)
1158 INT32 __cdecl
CRTDLL_wcscspn(LPWSTR str
,LPWSTR reject
)
1165 while (*t
) { if (*t
==*s
) break;t
++;}
1169 return s
-str
; /* nr of wchars */
1172 /*********************************************************************
1173 * wcslen (CRTDLL.510)
1175 INT32 __cdecl
CRTDLL_wcslen( LPCWSTR s
)
1177 return lstrlen32W( s
);
1180 /*********************************************************************
1181 * wcsncat (CRTDLL.511)
1183 LPWSTR __cdecl
CRTDLL_wcsncat( LPWSTR s1
, LPCWSTR s2
, INT32 n
)
1185 return lstrcatn32W( s1
, s2
, n
);
1188 /*********************************************************************
1189 * wcsncmp (CRTDLL.512)
1191 INT32 __cdecl
CRTDLL_wcsncmp( LPCWSTR s1
, LPCWSTR s2
, INT32 n
)
1193 return lstrncmp32W( s1
, s2
, n
);
1196 /*********************************************************************
1197 * wcsncpy (CRTDLL.513)
1199 LPWSTR __cdecl
CRTDLL_wcsncpy( LPWSTR s1
, LPCWSTR s2
, INT32 n
)
1201 return lstrcpyn32W( s1
, s2
, n
);
1204 /*********************************************************************
1205 * wcsspn (CRTDLL.516)
1207 INT32 __cdecl
CRTDLL_wcsspn(LPWSTR str
,LPWSTR accept
)
1214 while (*t
) { if (*t
==*s
) break;t
++;}
1218 return s
-str
; /* nr of wchars */
1221 /*********************************************************************
1222 * towupper (CRTDLL.494)
1224 WCHAR __cdecl
CRTDLL_towupper(WCHAR x
)
1226 return (WCHAR
)toupper((CHAR
)x
);
1229 /*********************************************************************
1230 * _wcsicmp (CRTDLL.321)
1232 DWORD __cdecl
CRTDLL__wcsicmp( LPCWSTR s1
, LPCWSTR s2
)
1234 return lstrcmpi32W( s1
, s2
);
1237 /*********************************************************************
1238 * _wcsicoll (CRTDLL.322)
1240 DWORD __cdecl
CRTDLL__wcsicoll(LPCWSTR a1
,LPCWSTR a2
)
1242 /* FIXME: handle collates */
1243 return lstrcmpi32W(a1
,a2
);
1246 /*********************************************************************
1247 * _wcsnicmp (CRTDLL.324)
1249 DWORD __cdecl
CRTDLL__wcsnicmp( LPCWSTR s1
, LPCWSTR s2
, INT32 len
)
1251 return lstrncmpi32W( s1
, s2
, len
);
1254 /*********************************************************************
1255 * wcscoll (CRTDLL.506)
1257 DWORD __cdecl
CRTDLL_wcscoll(LPWSTR a1
,LPWSTR a2
)
1259 /* FIXME: handle collates */
1260 return lstrcmp32W(a1
,a2
);
1263 /*********************************************************************
1264 * _wcsrev (CRTDLL.326)
1266 VOID __cdecl
CRTDLL__wcsrev(LPWSTR s
) {
1280 /*********************************************************************
1281 * wcsstr (CRTDLL.517)
1283 LPWSTR __cdecl
CRTDLL_wcsstr(LPWSTR s
,LPWSTR b
)
1291 while (*y
&& *c
&& *y
==*c
) { c
++;y
++; }
1300 /*********************************************************************
1301 * wcstombs (CRTDLL.521)
1303 INT32 __cdecl
CRTDLL_wcstombs( LPSTR dst
, LPCWSTR src
, INT32 len
)
1305 lstrcpynWtoA( dst
, src
, len
);
1306 return strlen(dst
); /* FIXME: is this right? */
1309 /*********************************************************************
1310 * wcsrchr (CRTDLL.515)
1312 LPWSTR __cdecl
CRTDLL_wcsrchr(LPWSTR str
,WCHAR xchar
)
1316 s
=str
+lstrlen32W(str
);
1325 /*********************************************************************
1326 * _setmode (CRTDLL.265)
1327 * FIXME: At present we ignore the request to translate CR/LF to LF.
1329 * We allways translate when we read with fgets, we never do with fread
1332 INT32 __cdecl
CRTDLL__setmode( INT32 fh
,INT32 mode
)
1335 #define O_TEXT 0x4000
1336 #define O_BINARY 0x8000
1338 dprintf_crtdll(stddeb
,
1339 "CRTDLL._setmode on fhandle %d mode %s, STUB.\n",
1340 fh
,(mode
=O_TEXT
)?"O_TEXT":
1341 (mode
=O_BINARY
)?"O_BINARY":"UNKNOWN");
1345 /*********************************************************************
1346 * atexit (CRTDLL.345)
1348 INT32 __cdecl
CRTDLL_atexit(LPVOID x
)
1351 fprintf(stdnimp
,"CRTDLL.atexit(%p), STUB.\n",x
);
1352 return 0; /* successful */
1355 /*********************************************************************
1356 * mblen (CRTDLL.428)
1357 * FIXME: check multibyte support
1359 WCHAR __cdecl
CRTDLL_mblen(CHAR
*mb
,INT32 size
)
1366 else if ((size
<1)||(!*(mb
+1)))
1371 dprintf_crtdll(stderr
,"CRTDLL_mlen %s for max %d bytes ret %d\n",mb
,size
,ret
);
1376 /*********************************************************************
1377 * mbstowcs (CRTDLL.429)
1378 * FIXME: check multibyte support
1380 INT32 __cdecl
CRTDLL_mbstowcs(LPWSTR wcs
, LPCSTR mbs
, INT32 size
)
1383 /* Slightly modified lstrcpynAtoW functions from memory/strings.c
1384 * We need the number of characters transfered
1385 * FIXME: No multibyte support yet
1392 while ((n
-- > 0) && *src
) {
1393 *p
++ = (WCHAR
)(unsigned char)*src
++;
1398 dprintf_crtdll(stddeb
,"CRTDLL_mbstowcs %s for %d chars put %d wchars\n",
1403 /*********************************************************************
1404 * mbtowc (CRTDLL.430)
1405 * FIXME: check multibyte support
1407 WCHAR __cdecl
CRTDLL_mbtowc(WCHAR
* wc
,CHAR
* mb
,INT32 size
)
1416 if ( (ret
= mblen(mb
,size
)) != -1 )
1418 if (ret
<= sizeof(char))
1419 *wc
= (WCHAR
) ((unsigned char)*mb
);
1426 dprintf_crtdll(stderr
,"CRTDLL_mbtowc %s for %d chars\n",mb
,size
);
1431 /*********************************************************************
1432 * _isctype (CRTDLL.138)
1434 BOOL32 __cdecl
CRTDLL__isctype(CHAR x
,CHAR type
)
1436 if ((type
& CRTDLL_SPACE
) && isspace(x
))
1438 if ((type
& CRTDLL_PUNCT
) && ispunct(x
))
1440 if ((type
& CRTDLL_LOWER
) && islower(x
))
1442 if ((type
& CRTDLL_UPPER
) && isupper(x
))
1444 if ((type
& CRTDLL_ALPHA
) && isalpha(x
))
1446 if ((type
& CRTDLL_DIGIT
) && isdigit(x
))
1448 if ((type
& CRTDLL_CONTROL
) && iscntrl(x
))
1450 /* check CRTDLL_LEADBYTE */
1454 /*********************************************************************
1455 * _chdrive (CRTDLL.52)
1457 BOOL32 __cdecl
CRTDLL__chdrive(INT32 newdrive
)
1459 /* FIXME: generates errnos */
1460 return DRIVE_SetCurrentDrive(newdrive
);
1463 /*********************************************************************
1464 * _chdir (CRTDLL.51)
1466 INT32 __cdecl
CRTDLL__chdir(LPCSTR newdir
)
1468 if (!SetCurrentDirectory32A(newdir
))
1473 /*********************************************************************
1474 * _fullpath (CRTDLL.114)
1476 LPSTR __cdecl
CRTDLL__fullpath(LPSTR buf
, LPCSTR name
, INT32 size
)
1478 DOS_FULL_NAME full_name
;
1483 if(!(buf
= CRTDLL_malloc(size
))) return NULL
;
1485 if (!DOSFS_GetFullName( name
, FALSE
, &full_name
)) return NULL
;
1486 lstrcpyn32A(buf
,full_name
.short_name
,size
);
1487 dprintf_crtdll(stderr
,"CRTDLL_fullpath got %s\n",buf
);
1491 /*********************************************************************
1492 * _splitpath (CRTDLL.279)
1494 VOID __cdecl
CRTDLL__splitpath(LPCSTR path
, LPSTR drive
, LPSTR directory
, LPSTR filename
, LPSTR extension
)
1497 directory includes leading and trailing (forward and backward slashes)
1498 filename without dot and slashes
1499 extension with leading dot
1501 char * drivechar
,*dirchar
,*namechar
;
1503 dprintf_crtdll(stddeb
,"CRTDLL__splitpath got %s\n",path
);
1505 drivechar
= strchr(path
,':');
1506 dirchar
= strrchr(path
,'/');
1507 namechar
= strrchr(path
,'\\');
1508 dirchar
= MAX(dirchar
,namechar
);
1510 namechar
= strrchr(dirchar
,'.');
1512 namechar
= strrchr(path
,'.');
1520 strncat(drive
,path
,drivechar
-path
+1);
1529 strncat(directory
,path
,dirchar
-path
+1);
1538 strncat(filename
,path
,namechar
-path
);
1542 strcat(extension
,namechar
);
1547 dprintf_crtdll(stddeb
,"CRTDLL__splitpath found %s %s %s %s\n",drive
,directory
,filename
,extension
);
1551 /*********************************************************************
1552 * _getcwd (CRTDLL.120)
1554 CHAR
* __cdecl
CRTDLL__getcwd(LPSTR buf
, INT32 size
)
1561 if (size
< 0) /* allocate as big as nescessary */
1562 len
=GetCurrentDirectory32A(1,test
) + 1;
1563 if(!(buf
= CRTDLL_malloc(len
)))
1565 /* set error to OutOfRange */
1570 if(!(len
=GetCurrentDirectory32A(len
,buf
)))
1576 /* set error to ERANGE */
1577 dprintf_crtdll(stddeb
,"CRTDLL_getcwd buffer to small\n");
1584 /*********************************************************************
1585 * _mkdir (CRTDLL.234)
1587 INT32 __cdecl
CRTDLL__mkdir(LPCSTR newdir
)
1589 if (!CreateDirectory32A(newdir
,NULL
))
1594 /*********************************************************************
1595 * _errno (CRTDLL.52)
1596 * Yes, this is a function.
1598 LPINT32 __cdecl
CRTDLL__errno()
1600 static int crtdllerrno
;
1601 extern int LastErrorToErrno(DWORD
);
1603 /* FIXME: we should set the error at the failing function call time */
1604 crtdllerrno
= LastErrorToErrno(GetLastError());
1605 return &crtdllerrno
;
1608 /*********************************************************************
1609 * _tempnam (CRTDLL.305)
1612 LPSTR __cdecl
CRTDLL__tempnam(LPCSTR dir
, LPCSTR prefix
)
1616 DOS_FULL_NAME tempname
;
1618 if ((ret
= tempnam(dir
,prefix
))==NULL
) {
1619 dprintf_crtdll(stddeb
,
1620 "CRTDLL_tempnam Unable to get unique filename\n");
1623 if (!DOSFS_GetFullName(ret
,FALSE
,&tempname
))
1625 dprintf_crtdll(stddeb
,
1626 "CRTDLL_tempnam Wrong path?\n");
1630 if ((ret
= CRTDLL_malloc(strlen(tempname
.short_name
)+1)) == NULL
) {
1631 dprintf_crtdll(stddeb
,
1632 "CRTDLL_tempnam CRTDL_malloc for shortname failed\n");
1635 if ((ret
= strcpy(ret
,tempname
.short_name
)) == NULL
) {
1636 dprintf_crtdll(stddeb
,
1637 "CRTDLL_tempnam Malloc for shortname failed\n");
1641 dprintf_crtdll(stddeb
,"CRTDLL_tempnam dir %s prefix %s got %s\n",
1646 /*********************************************************************
1647 * tmpnam (CRTDLL.490)
1649 * lcclnk from lcc-win32 relies on a terminating dot in the name returned
1652 LPSTR __cdecl
CRTDLL_tmpnam(LPSTR s
)
1656 if ((ret
=tmpnam(s
))== NULL
) {
1657 dprintf_crtdll(stddeb
,
1658 "CRTDLL_tmpnam Unable to get unique filename\n");
1661 if (!DOSFS_GetFullName(ret
,FALSE
,&CRTDLL_tmpname
))
1663 dprintf_crtdll(stddeb
,
1664 "CRTDLL_tmpnam Wrong path?\n");
1667 strcat(CRTDLL_tmpname
.short_name
,".");
1668 dprintf_crtdll(stddeb
,"CRTDLL_tmpnam for buf %p got %s\n",
1669 s
,CRTDLL_tmpname
.short_name
);
1670 dprintf_crtdll(stddeb
,"CRTDLL_tmpnam long got %s\n",
1671 CRTDLL_tmpname
.long_name
);
1673 return strcpy(s
,CRTDLL_tmpname
.short_name
);
1675 return CRTDLL_tmpname
.short_name
;
1679 /*********************************************************************
1680 * _itoa (CRTDLL.165)
1682 LPSTR __cdecl
CRTDLL__itoa(INT32 x
,LPSTR buf
,INT32 buflen
)
1684 wsnprintf32A(buf
,buflen
,"%d",x
);
1688 typedef VOID (*sig_handler_type
)(VOID
);
1690 /*********************************************************************
1691 * signal (CRTDLL.455)
1693 VOID __cdecl
CRTDLL_signal(int sig
, sig_handler_type ptr
)
1695 dprintf_crtdll(stddeb
,"CRTDLL_signal %d %p: STUB!\n",sig
,ptr
);
1698 /*********************************************************************
1699 * _ftol (CRTDLL.113)
1701 LONG __cdecl
CRTDLL__ftol(double fl
) {
1704 /*********************************************************************
1705 * _sleep (CRTDLL.267)
1707 VOID __cdecl
CRTDLL__sleep(unsigned long timeout
)
1709 dprintf_crtdll(stddeb
,"CRTDLL__sleep for %ld milliseconds\n",timeout
);
1710 Sleep((timeout
)?timeout
:1);