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 */
46 extern int FILE_GetUnixHandle( HFILE32
);
48 static DOS_FULL_NAME CRTDLL_tmpname
;
50 extern INT32
WIN32_wsprintf32W( DWORD
*args
);
52 UINT32 CRTDLL_argc_dll
; /* CRTDLL.23 */
53 LPSTR
*CRTDLL_argv_dll
; /* CRTDLL.24 */
54 LPSTR CRTDLL_acmdln_dll
; /* CRTDLL.38 */
55 UINT32 CRTDLL_basemajor_dll
; /* CRTDLL.42 */
56 UINT32 CRTDLL_baseminor_dll
; /* CRTDLL.43 */
57 UINT32 CRTDLL_baseversion_dll
; /* CRTDLL.44 */
58 UINT32 CRTDLL_commode_dll
; /* CRTDLL.59 */
59 LPSTR CRTDLL_environ_dll
; /* CRTDLL.75 */
60 UINT32 CRTDLL_fmode_dll
; /* CRTDLL.104 */
61 UINT32 CRTDLL_osmajor_dll
; /* CRTDLL.241 */
62 UINT32 CRTDLL_osminor_dll
; /* CRTDLL.242 */
63 UINT32 CRTDLL_osmode_dll
; /* CRTDLL.243 */
64 UINT32 CRTDLL_osver_dll
; /* CRTDLL.244 */
65 UINT32 CRTDLL_osversion_dll
; /* CRTDLL.245 */
66 UINT32 CRTDLL_winmajor_dll
; /* CRTDLL.329 */
67 UINT32 CRTDLL_winminor_dll
; /* CRTDLL.330 */
68 UINT32 CRTDLL_winver_dll
; /* CRTDLL.331 */
70 typedef VOID (*new_handler_type
)(VOID
);
72 static new_handler_type new_handler
;
74 /*********************************************************************
75 * _GetMainArgs (CRTDLL.022)
77 DWORD __cdecl
CRTDLL__GetMainArgs(LPDWORD argc
,LPSTR
**argv
,
78 LPSTR
*environ
,DWORD flag
)
82 int xargc
,i
,afterlastspace
;
85 dprintf_crtdll(stddeb
,"CRTDLL__GetMainArgs(%p,%p,%p,%ld).\n",
86 argc
,argv
,environ
,flag
88 CRTDLL_acmdln_dll
= cmdline
= HEAP_strdupA( GetProcessHeap(), 0,
89 GetCommandLine32A() );
90 dprintf_crtdll(stddeb
,"CRTDLL__GetMainArgs got \"%s\"\n",
93 version
= GetVersion32();
94 CRTDLL_osver_dll
= version
>> 16;
95 CRTDLL_winminor_dll
= version
& 0xFF;
96 CRTDLL_winmajor_dll
= (version
>>8) & 0xFF;
97 CRTDLL_baseversion_dll
= version
>> 16;
98 CRTDLL_winver_dll
= ((version
>> 8) & 0xFF) + ((version
& 0xFF) << 8);
99 CRTDLL_baseminor_dll
= (version
>> 16) & 0xFF;
100 CRTDLL_basemajor_dll
= (version
>> 24) & 0xFF;
101 CRTDLL_osversion_dll
= version
& 0xFFFF;
102 CRTDLL_osminor_dll
= version
& 0xFF;
103 CRTDLL_osmajor_dll
= (version
>>8) & 0xFF;
105 /* missing threading init */
107 i
=0;xargv
=NULL
;xargc
=0;afterlastspace
=0;
109 if (cmdline
[i
]==' ') {
110 xargv
=(char**)HeapReAlloc( GetProcessHeap(), 0, xargv
,
111 sizeof(char*)*(++xargc
));
113 xargv
[xargc
-1] = HEAP_strdupA( GetProcessHeap(), 0,
114 cmdline
+afterlastspace
);
116 while (cmdline
[i
]==' ')
123 xargv
=(char**)HeapReAlloc( GetProcessHeap(), 0, xargv
,
124 sizeof(char*)*(++xargc
));
126 xargv
[xargc
-1] = HEAP_strdupA( GetProcessHeap(), 0,
127 cmdline
+afterlastspace
);
128 CRTDLL_argc_dll
= xargc
;
130 CRTDLL_argv_dll
= xargv
;
133 dprintf_crtdll(stddeb
,"CRTDLL__GetMainArgs found %d arguments\n",
135 CRTDLL_environ_dll
= *environ
= GetEnvironmentStrings32A();
140 typedef void (*_INITTERMFUN
)();
142 /*********************************************************************
143 * _initterm (CRTDLL.135)
145 DWORD __cdecl
CRTDLL__initterm(_INITTERMFUN
*start
,_INITTERMFUN
*end
)
147 _INITTERMFUN
*current
;
149 dprintf_crtdll(stddeb
,"_initterm(%p,%p)\n",start
,end
);
151 while (current
<end
) {
152 if (*current
) (*current
)();
158 /*********************************************************************
159 * _fdopen (CRTDLL.91)
161 DWORD __cdecl
CRTDLL__fdopen(INT32 handle
, LPCSTR mode
)
169 case 1 : file
=stdout
;
171 case 2 : file
=stderr
;
174 file
=fdopen(handle
,mode
);
176 dprintf_crtdll(stddeb
,
177 "CRTDLL_fdopen open handle %d mode %s got file %p\n",
182 /*******************************************************************
183 * _global_unwind2 (CRTDLL.129)
185 void __cdecl
CRTDLL__global_unwind2( CONTEXT
*context
)
187 /* Retrieve the arguments (args[0] is return addr, args[1] is first arg) */
188 DWORD
*args
= (DWORD
*)ESP_reg(context
);
189 RtlUnwind( (PEXCEPTION_FRAME
)args
[1], (LPVOID
)EIP_reg(context
),
193 /*******************************************************************
194 * _local_unwind2 (CRTDLL.173)
196 void __cdecl
CRTDLL__local_unwind2( CONTEXT
*context
)
198 /* Retrieve the arguments (args[0] is return addr, args[1] is first arg) */
199 DWORD
*args
= (DWORD
*)ESP_reg(context
);
200 PEXCEPTION_FRAME endframe
= (PEXCEPTION_FRAME
)args
[1];
202 fprintf(stderr
,"CRTDLL__local_unwind2(%p,%ld)\n",endframe
,nr
);
205 /*********************************************************************
208 DWORD __cdecl
CRTDLL_fopen(LPCSTR path
, LPCSTR mode
)
213 DOS_FULL_NAME full_name
;
215 if (!DOSFS_GetFullName( path
, FALSE
, &full_name
)) {
216 dprintf_crtdll(stddeb
,"CRTDLL_fopen file %s bad name\n",path
);
220 file
=fopen(full_name
.long_name
,mode
);
225 if ((strchr(mode
,'r')&&strchr(mode
,'a'))||
226 (strchr(mode
,'r')&&strchr(mode
,'w'))||
227 (strchr(mode
,'w')&&strchr(mode
,'a')))
230 if (strstr(mode
,"r+")) flagmode
=O_RDWR
;
231 else if (strchr(mode
,'r')) flagmode
= O_RDONLY
;
232 else if (strstr(mode
,"w+")) flagmode
= O_RDWR
| O_TRUNC
| O_CREAT
;
233 else if (strchr(mode
,'w')) flagmode
= O_WRONLY
| O_TRUNC
| O_CREAT
;
234 else if (strstr(mode
,"a+")) flagmode
= O_RDWR
| O_CREAT
| O_APPEND
;
235 else if (strchr(mode
,'w')) flagmode
= O_RDWR
| O_CREAT
| O_APPEND
;
236 else if (strchr(mode
,'b'))
237 dprintf_crtdll(stderr
,
238 "CRTDLL_fopen %s in BINARY mode\n",path
);
240 dos_fildes
=FILE_Open(path
, flagmode
);
241 unix_fildes
=FILE_GetUnixHandle(dos_fildes
);
242 file
= fdopen(unix_fildes
,mode
);
244 dprintf_crtdll(stddeb
,
245 "CRTDLL_fopen file %s mode %s got ufh %d dfh %d file %p\n",
246 path
,mode
,unix_fildes
,dos_fildes
,file
);
250 /*********************************************************************
253 DWORD __cdecl
CRTDLL_fread(LPVOID ptr
, INT32 size
, INT32 nmemb
, LPVOID file
)
260 /* If we would honour CR/LF <-> LF translation, we could do it like this.
261 We should keep track of all files opened, and probably files with \
262 known binary extensions must be unchanged */
263 while ( (i
< (nmemb
*size
)) && (ret
==1)) {
264 ret
=fread(temp
,1,1,file
);
265 dprintf_crtdll(stddeb
,
266 "CRTDLL_fread got %c 0x%02x ret %d\n",
267 (isalpha(*(unsigned char*)temp
))? *(unsigned char*)temp
:
268 ' ',*(unsigned char*)temp
, ret
);
269 if (*(unsigned char*)temp
!= 0xd) { /* skip CR */
274 dprintf_crtdll(stddeb
, "CRTDLL_fread skipping ^M\n");
276 dprintf_crtdll(stddeb
,
277 "CRTDLL_fread 0x%08x items of size %d from file %p to %p%s\n",
278 nmemb
,size
,file
,ptr
,(i
!=nmemb
)?" failed":"");
282 ret
=fread(ptr
,size
,nmemb
,file
);
283 dprintf_crtdll(stddeb
,
284 "CRTDLL_fread 0x%08x items of size %d from file %p to %p%s\n",
285 nmemb
,size
,file
,ptr
,(ret
!=nmemb
)?" failed":"");
290 /*********************************************************************
293 LONG __cdecl
CRTDLL_fseek(LPVOID stream
, LONG offset
, INT32 whence
)
297 ret
=fseek(stream
,offset
,whence
);
298 dprintf_crtdll(stddeb
,
299 "CRTDLL_fseek file %p to 0x%08lx pos %s%s\n",
300 stream
,offset
,(whence
==SEEK_SET
)?"SEEK_SET":
301 (whence
==SEEK_CUR
)?"SEEK_CUR":
302 (whence
==SEEK_END
)?"SEEK_END":"UNKNOWN",
307 /*********************************************************************
310 LONG __cdecl
CRTDLL_ftell(LPVOID stream
)
315 dprintf_crtdll(stddeb
,
316 "CRTDLL_ftell file %p at 0x%08lx\n",
321 /*********************************************************************
322 * fwrite (CRTDLL.386)
324 DWORD __cdecl
CRTDLL_fwrite(LPVOID ptr
, INT32 size
, INT32 nmemb
, LPVOID file
)
328 ret
=fwrite(ptr
,size
,nmemb
,file
);
329 dprintf_crtdll(stddeb
,
330 "CRTDLL_fwrite 0x%08x items of size %d from %p to file %p%s\n",
331 nmemb
,size
,ptr
,file
,(ret
!=nmemb
)?" failed":"");
335 /*********************************************************************
336 * setbuf (CRTDLL.452)
338 INT32 __cdecl
CRTDLL_setbuf(LPVOID file
, LPSTR buf
)
340 dprintf_crtdll(stddeb
,
341 "CRTDLL_setbuf(file %p buf %p)\n",
343 /* this doesn't work:"void value not ignored as it ought to be"
344 return setbuf(file,buf);
350 /*********************************************************************
351 * _open_osfhandle (CRTDLL.240)
353 HFILE32 __cdecl
CRTDLL__open_osfhandle(LONG osfhandle
, INT32 flags
)
358 case STD_INPUT_HANDLE
:
362 case STD_OUTPUT_HANDLE
:
366 case STD_ERROR_HANDLE
:
373 dprintf_crtdll(stddeb
,
374 "CRTDLL_open_osfhandle(handle %08lx,flags %d) return %d\n",
375 osfhandle
,flags
,handle
);
380 /*********************************************************************
383 void __cdecl
CRTDLL_srand(DWORD seed
)
385 /* FIXME: should of course be thread? process? local */
389 /*********************************************************************
390 * fprintf (CRTDLL.373)
392 INT32 __cdecl
CRTDLL_fprintf( FILE *file
, LPSTR format
, ... )
397 va_start( valist
, format
);
398 res
= vfprintf( file
, format
, valist
);
403 /*********************************************************************
404 * vfprintf (CRTDLL.373)
406 INT32 __cdecl
CRTDLL_vfprintf( FILE *file
, LPSTR format
, va_list args
)
408 return vfprintf( file
, format
, args
);
411 /*********************************************************************
414 time_t __cdecl
CRTDLL_time(time_t *timeptr
)
416 time_t curtime
= time(NULL
);
423 /*********************************************************************
424 * _isatty (CRTDLL.137)
426 BOOL32 __cdecl
CRTDLL__isatty(DWORD x
)
428 dprintf_crtdll(stderr
,"CRTDLL__isatty(%ld)\n",x
);
432 /*********************************************************************
433 * _write (CRTDLL.332)
435 INT32 __cdecl
CRTDLL__write(INT32 fd
,LPCVOID buf
,UINT32 count
)
442 len
= (UINT32
)write(fd
,buf
,(LONG
)count
);
444 len
= _lwrite32(fd
,buf
,count
);
445 dprintf_crtdll(stddeb
,"CRTDLL_write %d/%d byte to dfh %d from %p,\n",
451 /*********************************************************************
454 * FIXME: What the heck is the difference between
455 * FIXME _c_exit (CRTDLL.47)
456 * FIXME _cexit (CRTDLL.49)
457 * FIXME _exit (CRTDLL.87)
458 * FIXME exit (CRTDLL.359)
460 * atexit-processing comes to mind -- MW.
463 void __cdecl
CRTDLL__cexit(INT32 ret
)
465 dprintf_crtdll(stddeb
,"CRTDLL__cexit(%d)\n",ret
);
470 /*********************************************************************
473 void __cdecl
CRTDLL_exit(DWORD ret
)
475 dprintf_crtdll(stddeb
,"CRTDLL_exit(%ld)\n",ret
);
480 /*********************************************************************
481 * _abnormal_termination (CRTDLL.36 )
483 INT32 __cdecl
CRTDLL__abnormal_termination(void)
485 dprintf_crtdll(stddeb
,"CRTDLL__abnormal_termination\n");
490 /*********************************************************************
491 * fflush (CRTDLL.365)
493 INT32 __cdecl
CRTDLL_fflush(LPVOID stream
)
497 ret
= fflush(stream
);
498 dprintf_crtdll(stddeb
,"CRTDLL_fflush %p returnd %d %s\n",stream
,ret
,(ret
)?"":" failed");
503 /*********************************************************************
506 LPSTR __cdecl
CRTDLL_gets(LPSTR buf
)
509 /* BAD, for the whole WINE process blocks... just done this way to test
510 * windows95's ftp.exe.
513 dprintf_crtdll(stddeb
,"CRTDLL_gets got %s\n",ret
);
518 /*********************************************************************
521 INT32 __cdecl
CRTDLL_rand()
527 /*********************************************************************
528 * putchar (CRTDLL.442)
530 void __cdecl
CRTDLL_putchar( INT32 x
)
536 /*********************************************************************
539 INT32 __cdecl
CRTDLL_fputc( INT32 c
, FILE *stream
)
541 dprintf_crtdll(stddeb
,
542 "CRTDLL_fputc %c to file %p\n",c
,stream
);
543 return fputc(c
,stream
);
547 /*********************************************************************
550 INT32 __cdecl
CRTDLL_fputs( LPCSTR s
, FILE *stream
)
552 dprintf_crtdll(stddeb
,
553 "CRTDLL_fputs %s to file %p\n",s
,stream
);
554 return fputs(s
,stream
);
558 /*********************************************************************
561 INT32 __cdecl
CRTDLL_puts(LPCSTR s
)
563 dprintf_crtdll(stddeb
,
564 "CRTDLL_fputs %s \n",s
);
569 /*********************************************************************
572 INT32 __cdecl
CRTDLL_putc(INT32 c
, FILE *stream
)
574 dprintf_crtdll(stddeb
,
575 "CRTDLL_putc %c to file %p\n",c
,stream
);
576 return fputc(c
,stream
);
578 /*********************************************************************
581 INT32 __cdecl
CRTDLL_fgetc( FILE *stream
)
583 int ret
= fgetc(stream
);
584 dprintf_crtdll(stddeb
,
585 "CRTDLL_fgetc got %d\n",ret
);
590 /*********************************************************************
593 INT32 __cdecl
CRTDLL_getc( FILE *stream
)
595 int ret
= fgetc(stream
);
596 dprintf_crtdll(stddeb
,
597 "CRTDLL_getc got %d\n",ret
);
601 /*********************************************************************
604 UINT32 __cdecl
CRTDLL__rotl(UINT32 x
,INT32 shift
)
606 unsigned int ret
= (x
>> shift
)|( x
>>((sizeof(x
))-shift
));
608 dprintf_crtdll(stddeb
,
609 "CRTDLL_rotl got 0x%08x rot %d ret 0x%08x\n",
614 /*********************************************************************
615 * _lrotl (CRTDLL.176)
617 DWORD __cdecl
CRTDLL__lrotl(DWORD x
,INT32 shift
)
619 unsigned long ret
= (x
>> shift
)|( x
>>((sizeof(x
))-shift
));
621 dprintf_crtdll(stddeb
,
622 "CRTDLL_lrotl got 0x%08lx rot %d ret 0x%08lx\n",
629 /*********************************************************************
632 CHAR
* __cdecl
CRTDLL_fgets(LPSTR s
,INT32 size
, LPVOID stream
)
637 ret
=fgets(s
, size
,stream
);
638 /*FIXME: Control with CRTDLL_setmode */
639 control_M
= strrchr(s
,'\r');
640 /*delete CR if we read a DOS File */
646 dprintf_crtdll(stddeb
,
647 "CRTDLL_fgets got %s for %d chars from file %p%s\n",
648 s
,size
,stream
,(ret
)?"":" failed");
653 /*********************************************************************
654 * _mbsicmp (CRTDLL.204)
656 int __cdecl
CRTDLL__mbsicmp(unsigned char *x
,unsigned char *y
)
663 /* FIXME: MBCS handling... */
672 /*********************************************************************
673 * _mbsinc (CRTDLL.205)
675 unsigned char * __cdecl
CRTDLL__mbsinc(unsigned char *x
)
682 /*********************************************************************
683 * vsprintf (CRTDLL.500)
685 INT32 __cdecl
CRTDLL_vsprintf( LPSTR buffer
, LPCSTR spec
, va_list args
)
687 return wvsprintf32A( buffer
, spec
, args
);
690 /*********************************************************************
691 * vswprintf (CRTDLL.501)
693 INT32 __cdecl
CRTDLL_vswprintf( LPWSTR buffer
, LPCWSTR spec
, va_list args
)
695 return wvsprintf32W( buffer
, spec
, args
);
698 /*********************************************************************
699 * _mbscpy (CRTDLL.200)
701 unsigned char* __cdecl
CRTDLL__mbscpy(unsigned char *x
,unsigned char *y
)
703 dprintf_crtdll(stddeb
,"CRTDLL_mbscpy %s and %s\n",x
,y
);
708 /*********************************************************************
709 * _mbscat (CRTDLL.197)
711 unsigned char* __cdecl
CRTDLL__mbscat(unsigned char *x
,unsigned char *y
)
717 /*********************************************************************
718 * _strcmpi (CRTDLL.282) (CRTDLL.287)
720 INT32 __cdecl
CRTDLL__strcmpi( LPCSTR s1
, LPCSTR s2
)
722 return lstrcmpi32A( s1
, s2
);
726 /*********************************************************************
727 * _strnicmp (CRTDLL.293)
729 INT32 __cdecl
CRTDLL__strnicmp( LPCSTR s1
, LPCSTR s2
, INT32 n
)
731 return lstrncmpi32A( s1
, s2
, n
);
735 /*********************************************************************
736 * _strlwr (CRTDLL.293)
738 * convert a string in place to lowercase
740 LPSTR
CRTDLL__strlwr(LPSTR x
)
744 dprintf_crtdll(stddeb
,
745 "CRTDLL_strlwr got %s",x
);
747 if ((*y
> 0x40) && (*y
< 0x5b))
751 dprintf_crtdll(stddeb
," returned %s\n",x
);
756 /*********************************************************************
757 * system (CRTDLL.485)
759 INT32
CRTDLL_system(LPSTR x
)
761 #define SYSBUF_LENGTH 1500
762 char buffer
[SYSBUF_LENGTH
];
763 unsigned char *y
= x
;
767 sprintf( buffer
, "%s \"", Options
.argv0
);
768 bp
= buffer
+ strlen(buffer
);
769 i
= strlen(buffer
) + strlen(x
) +2;
771 /* Calculate needed buffer size to prevent overflow. */
776 /* If buffer too short, exit. */
777 if (i
> SYSBUF_LENGTH
) {
778 dprintf_crtdll(stddeb
,"_system buffer to small\n");
787 if (*(y
-1) =='\\') *bp
++ = '\\';
789 /* Remove spaces from end of string. */
790 while (*(y
-1) == ' ') {
795 dprintf_crtdll(stddeb
,
796 "_system got \"%s\", executing \"%s\"\n",x
,buffer
);
798 return system(buffer
);
801 /*********************************************************************
802 * _strupr (CRTDLL.300)
804 LPSTR __cdecl
CRTDLL__strupr(LPSTR x
)
815 /*********************************************************************
816 * _wcsupr (CRTDLL.328)
818 LPWSTR __cdecl
CRTDLL__wcsupr(LPWSTR x
)
829 /*********************************************************************
830 * _wcslwr (CRTDLL.323)
832 LPWSTR __cdecl
CRTDLL__wcslwr(LPWSTR x
)
844 /*********************************************************************
845 * longjmp (CRTDLL.426)
847 VOID __cdecl
CRTDLL_longjmp(jmp_buf env
, int val
)
849 dprintf_crtdll(stdnimp
,"CRTDLL_longjmp semistup, expect crash\n");
850 dprintf_crtdll(stddeb
, "CRTDLL_longjmp semistup, expect crash\n");
851 return longjmp(env
, val
);
854 /*********************************************************************
855 * malloc (CRTDLL.427)
857 VOID
* __cdecl
CRTDLL_malloc(DWORD size
)
859 return HeapAlloc(GetProcessHeap(),0,size
);
862 /*********************************************************************
865 VOID
* __cdecl
CRTDLL_new(DWORD size
)
868 if(!(result
= HeapAlloc(GetProcessHeap(),0,size
)) && new_handler
)
873 /*********************************************************************
874 * set_new_handler(CRTDLL.003)
876 new_handler_type __cdecl
CRTDLL_set_new_handler(new_handler_type func
)
878 new_handler_type old_handler
= new_handler
;
883 /*********************************************************************
884 * calloc (CRTDLL.350)
886 VOID
* __cdecl
CRTDLL_calloc(DWORD size
, DWORD count
)
888 return HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
, size
* count
);
891 /*********************************************************************
892 * realloc (CRTDLL.447)
894 VOID
* __cdecl
CRTDLL_realloc( VOID
*ptr
, DWORD size
)
896 return HeapReAlloc( GetProcessHeap(), 0, ptr
, size
);
899 /*********************************************************************
902 VOID __cdecl
CRTDLL_free(LPVOID ptr
)
904 HeapFree(GetProcessHeap(),0,ptr
);
907 /*********************************************************************
908 * delete (CRTDLL.002)
910 VOID __cdecl
CRTDLL_delete(VOID
* ptr
)
912 HeapFree(GetProcessHeap(),0,ptr
);
915 /*********************************************************************
916 * _strdup (CRTDLL.285)
918 LPSTR __cdecl
CRTDLL__strdup(LPSTR ptr
)
920 return HEAP_strdupA(GetProcessHeap(),0,ptr
);
924 /*********************************************************************
925 * fclose (CRTDLL.362)
927 INT32 __cdecl
CRTDLL_fclose( FILE *stream
)
929 int unix_handle
=fileno(stream
);
930 HFILE32 dos_handle
=1;
933 if (unix_handle
<4) ret
= fclose(stream
);
935 while(FILE_GetUnixHandle(dos_handle
) != unix_handle
) dos_handle
++;
937 ret
= _lclose32( dos_handle
);
939 dprintf_crtdll(stddeb
,"CRTDLL_fclose(%p) ufh %d dfh %d%s\n",
940 stream
,unix_handle
,dos_handle
,(ret
)?" failed":"");
944 /*********************************************************************
945 * _unlink (CRTDLL.315)
947 INT32 __cdecl
CRTDLL__unlink(LPCSTR pathname
)
950 DOS_FULL_NAME full_name
;
952 if (!DOSFS_GetFullName( pathname
, FALSE
, &full_name
)) {
953 dprintf_crtdll(stddeb
,"CRTDLL_unlink file %s bad name\n",pathname
);
957 ret
=unlink(full_name
.long_name
);
958 dprintf_crtdll(stddeb
,"CRTDLL_unlink(%s unix %s)%s\n",
959 pathname
,full_name
.long_name
, (ret
)?" failed":"");
963 /*********************************************************************
964 * rename (CRTDLL.449)
966 INT32 __cdecl
CRTDLL_rename(LPCSTR oldpath
,LPCSTR newpath
)
968 BOOL32 ok
= MoveFileEx32A( oldpath
, newpath
, MOVEFILE_REPLACE_EXISTING
);
973 /*********************************************************************
992 int __cdecl
CRTDLL__stat(const char * filename
, struct win_stat
* buf
)
995 DOS_FULL_NAME full_name
;
998 if (!DOSFS_GetFullName( filename
, TRUE
, &full_name
))
1000 dprintf_crtdll(stddeb
,"CRTDLL__stat filename %s bad name\n",filename
);
1003 ret
=stat(full_name
.long_name
,&mystat
);
1004 dprintf_crtdll(stddeb
,"CRTDLL__stat %s%s\n",
1005 filename
, (ret
)?" failed":"");
1007 /* FIXME: should check what Windows returns */
1009 buf
->win_st_dev
= mystat
.st_dev
;
1010 buf
->win_st_ino
= mystat
.st_ino
;
1011 buf
->win_st_mode
= mystat
.st_mode
;
1012 buf
->win_st_nlink
= mystat
.st_nlink
;
1013 buf
->win_st_uid
= mystat
.st_uid
;
1014 buf
->win_st_gid
= mystat
.st_gid
;
1015 buf
->win_st_rdev
= mystat
.st_rdev
;
1016 buf
->win_st_size
= mystat
.st_size
;
1017 buf
->win_st_atime
= mystat
.st_atime
;
1018 buf
->win_st_mtime
= mystat
.st_mtime
;
1019 buf
->win_st_ctime
= mystat
.st_ctime
;
1023 /*********************************************************************
1024 * _open (CRTDLL.239)
1026 HFILE32 __cdecl
CRTDLL__open(LPCSTR path
,INT32 flags
)
1032 the flags in lcc's header differ from the ones in Linux, e.g.
1033 Linux: define O_APPEND 02000 (= 0x400)
1034 lcc: define _O_APPEND 0x0008
1035 so here a scheme to translate them
1036 Probably lcc is wrong here, but at least a hack to get is going
1038 wineflags
= (flags
& 3);
1039 if (flags
& 0x0008 ) wineflags
|= O_APPEND
;
1040 if (flags
& 0x0100 ) wineflags
|= O_CREAT
;
1041 if (flags
& 0x0200 ) wineflags
|= O_TRUNC
;
1042 if (flags
& 0x0400 ) wineflags
|= O_EXCL
;
1043 if (flags
& 0xf0f4 )
1044 dprintf_crtdll(stddeb
,"CRTDLL_open file unsupported flags 0x%04x\n",flags
);
1047 ret
= FILE_Open(path
,wineflags
);
1048 dprintf_crtdll(stddeb
,"CRTDLL_open file %s mode 0x%04x (lccmode 0x%04x) got dfh %d\n",
1049 path
,wineflags
,flags
,ret
);
1053 /*********************************************************************
1054 * _close (CRTDLL.57)
1056 INT32 __cdecl
CRTDLL__close(HFILE32 fd
)
1058 int ret
=_lclose32(fd
);
1060 dprintf_crtdll(stddeb
,"CRTDLL_close(%d)%s\n",fd
,(ret
)?" failed":"");
1064 /*********************************************************************
1067 INT32 __cdecl
CRTDLL_feof( FILE *stream
)
1072 dprintf_crtdll(stddeb
,"CRTDLL_feof(%p) %s\n",stream
,(ret
)?"true":"false");
1076 /*********************************************************************
1077 * setlocale (CRTDLL.453)
1079 LPSTR __cdecl
CRTDLL_setlocale(INT32 category
,LPCSTR locale
)
1084 case CRTDLL_LC_ALL
: categorystr
="LC_ALL";break;
1085 case CRTDLL_LC_COLLATE
: categorystr
="LC_COLLATE";break;
1086 case CRTDLL_LC_CTYPE
: categorystr
="LC_CTYPE";break;
1087 case CRTDLL_LC_MONETARY
: categorystr
="LC_MONETARY";break;
1088 case CRTDLL_LC_NUMERIC
: categorystr
="LC_NUMERIC";break;
1089 case CRTDLL_LC_TIME
: categorystr
="LC_TIME";break;
1090 default: categorystr
= "UNKNOWN?";break;
1092 fprintf(stderr
,"CRTDLL_setlocale(%s,%s),stub!\n",categorystr
,locale
);
1096 /*********************************************************************
1097 * wcscat (CRTDLL.503)
1099 LPWSTR __cdecl
CRTDLL_wcscat( LPWSTR s1
, LPCWSTR s2
)
1101 return lstrcat32W( s1
, s2
);
1104 /*********************************************************************
1105 * wcschr (CRTDLL.504)
1107 LPWSTR __cdecl
CRTDLL_wcschr(LPCWSTR str
,WCHAR xchar
)
1119 /*********************************************************************
1120 * wcscmp (CRTDLL.505)
1122 INT32 __cdecl
CRTDLL_wcscmp( LPCWSTR s1
, LPCWSTR s2
)
1124 return lstrcmp32W( s1
, s2
);
1127 /*********************************************************************
1128 * wcscpy (CRTDLL.507)
1130 LPWSTR __cdecl
CRTDLL_wcscpy( LPWSTR s1
, LPCWSTR s2
)
1132 return lstrcpy32W( s1
, s2
);
1135 /*********************************************************************
1136 * wcscspn (CRTDLL.508)
1138 INT32 __cdecl
CRTDLL_wcscspn(LPWSTR str
,LPWSTR reject
)
1145 while (*t
) { if (*t
==*s
) break;t
++;}
1149 return s
-str
; /* nr of wchars */
1152 /*********************************************************************
1153 * wcslen (CRTDLL.510)
1155 INT32 __cdecl
CRTDLL_wcslen( LPCWSTR s
)
1157 return lstrlen32W( s
);
1160 /*********************************************************************
1161 * wcsncat (CRTDLL.511)
1163 LPWSTR __cdecl
CRTDLL_wcsncat( LPWSTR s1
, LPCWSTR s2
, INT32 n
)
1165 return lstrcatn32W( s1
, s2
, n
);
1168 /*********************************************************************
1169 * wcsncmp (CRTDLL.512)
1171 INT32 __cdecl
CRTDLL_wcsncmp( LPCWSTR s1
, LPCWSTR s2
, INT32 n
)
1173 return lstrncmp32W( s1
, s2
, n
);
1176 /*********************************************************************
1177 * wcsncpy (CRTDLL.513)
1179 LPWSTR __cdecl
CRTDLL_wcsncpy( LPWSTR s1
, LPCWSTR s2
, INT32 n
)
1181 return lstrcpyn32W( s1
, s2
, n
);
1184 /*********************************************************************
1185 * wcsspn (CRTDLL.516)
1187 INT32 __cdecl
CRTDLL_wcsspn(LPWSTR str
,LPWSTR accept
)
1194 while (*t
) { if (*t
==*s
) break;t
++;}
1198 return s
-str
; /* nr of wchars */
1201 /*********************************************************************
1202 * towupper (CRTDLL.494)
1204 WCHAR __cdecl
CRTDLL_towupper(WCHAR x
)
1206 return (WCHAR
)toupper((CHAR
)x
);
1209 /*********************************************************************
1210 * _wcsicmp (CRTDLL.321)
1212 DWORD __cdecl
CRTDLL__wcsicmp( LPCWSTR s1
, LPCWSTR s2
)
1214 return lstrcmpi32W( s1
, s2
);
1217 /*********************************************************************
1218 * _wcsicoll (CRTDLL.322)
1220 DWORD __cdecl
CRTDLL__wcsicoll(LPCWSTR a1
,LPCWSTR a2
)
1222 /* FIXME: handle collates */
1223 return lstrcmpi32W(a1
,a2
);
1226 /*********************************************************************
1227 * _wcsnicmp (CRTDLL.324)
1229 DWORD __cdecl
CRTDLL__wcsnicmp( LPCWSTR s1
, LPCWSTR s2
, INT32 len
)
1231 return lstrncmpi32W( s1
, s2
, len
);
1234 /*********************************************************************
1235 * wcscoll (CRTDLL.506)
1237 DWORD __cdecl
CRTDLL_wcscoll(LPWSTR a1
,LPWSTR a2
)
1239 /* FIXME: handle collates */
1240 return lstrcmp32W(a1
,a2
);
1243 /*********************************************************************
1244 * _wcsrev (CRTDLL.326)
1246 VOID __cdecl
CRTDLL__wcsrev(LPWSTR s
) {
1260 /*********************************************************************
1261 * wcsstr (CRTDLL.517)
1263 LPWSTR __cdecl
CRTDLL_wcsstr(LPWSTR s
,LPWSTR b
)
1271 while (*y
&& *c
&& *y
==*c
) { c
++;y
++; }
1280 /*********************************************************************
1281 * wcstombs (CRTDLL.521)
1283 INT32 __cdecl
CRTDLL_wcstombs( LPSTR dst
, LPCWSTR src
, INT32 len
)
1285 lstrcpynWtoA( dst
, src
, len
);
1286 return strlen(dst
); /* FIXME: is this right? */
1289 /*********************************************************************
1290 * wcsrchr (CRTDLL.515)
1292 LPWSTR __cdecl
CRTDLL_wcsrchr(LPWSTR str
,WCHAR xchar
)
1296 s
=str
+lstrlen32W(str
);
1305 /*********************************************************************
1306 * _setmode (CRTDLL.265)
1307 * FIXME: At present we ignore the request to translate CR/LF to LF.
1309 * We allways translate when we read with fgets, we never do with fread
1312 INT32 __cdecl
CRTDLL__setmode( INT32 fh
,INT32 mode
)
1315 #define O_TEXT 0x4000
1316 #define O_BINARY 0x8000
1318 dprintf_crtdll(stddeb
,
1319 "CRTDLL._setmode on fhandle %d mode %s, STUB.\n",
1320 fh
,(mode
=O_TEXT
)?"O_TEXT":
1321 (mode
=O_BINARY
)?"O_BINARY":"UNKNOWN");
1325 /*********************************************************************
1326 * atexit (CRTDLL.345)
1328 INT32 __cdecl
CRTDLL_atexit(LPVOID x
)
1331 fprintf(stdnimp
,"CRTDLL.atexit(%p), STUB.\n",x
);
1332 return 0; /* successful */
1335 /*********************************************************************
1336 * mblen (CRTDLL.428)
1337 * FIXME: check multibyte support
1339 WCHAR __cdecl
CRTDLL_mblen(CHAR
*mb
,INT32 size
)
1346 else if ((size
<1)||(!*(mb
+1)))
1351 dprintf_crtdll(stderr
,"CRTDLL_mlen %s for max %d bytes ret %d\n",mb
,size
,ret
);
1356 /*********************************************************************
1357 * mbstowcs (CRTDLL.429)
1358 * FIXME: check multibyte support
1360 INT32 __cdecl
CRTDLL_mbstowcs(LPWSTR wcs
, LPCSTR mbs
, INT32 size
)
1363 /* Slightly modified lstrcpynAtoW functions from memory/strings.c
1364 * We need the number of characters transfered
1365 * FIXME: No multibyte support yet
1372 while ((n
-- > 0) && *src
) {
1373 *p
++ = (WCHAR
)(unsigned char)*src
++;
1378 dprintf_crtdll(stddeb
,"CRTDLL_mbstowcs %s for %d chars put %d wchars\n",
1383 /*********************************************************************
1384 * mbtowc (CRTDLL.430)
1385 * FIXME: check multibyte support
1387 WCHAR __cdecl
CRTDLL_mbtowc(WCHAR
* wc
,CHAR
* mb
,INT32 size
)
1396 if ( (ret
= mblen(mb
,size
)) != -1 )
1398 if (ret
<= sizeof(char))
1399 *wc
= (WCHAR
) ((unsigned char)*mb
);
1406 dprintf_crtdll(stderr
,"CRTDLL_mbtowc %s for %d chars\n",mb
,size
);
1411 /*********************************************************************
1412 * _isctype (CRTDLL.138)
1414 BOOL32 __cdecl
CRTDLL__isctype(CHAR x
,CHAR type
)
1416 if ((type
& CRTDLL_SPACE
) && isspace(x
))
1418 if ((type
& CRTDLL_PUNCT
) && ispunct(x
))
1420 if ((type
& CRTDLL_LOWER
) && islower(x
))
1422 if ((type
& CRTDLL_UPPER
) && isupper(x
))
1424 if ((type
& CRTDLL_ALPHA
) && isalpha(x
))
1426 if ((type
& CRTDLL_DIGIT
) && isdigit(x
))
1428 if ((type
& CRTDLL_CONTROL
) && iscntrl(x
))
1430 /* check CRTDLL_LEADBYTE */
1434 /*********************************************************************
1435 * _chdrive (CRTDLL.52)
1437 BOOL32 __cdecl
CRTDLL__chdrive(INT32 newdrive
)
1439 /* FIXME: generates errnos */
1440 return DRIVE_SetCurrentDrive(newdrive
);
1443 /*********************************************************************
1444 * _chdir (CRTDLL.51)
1446 INT32 __cdecl
CRTDLL__chdir(LPCSTR newdir
)
1448 if (!SetCurrentDirectory32A(newdir
))
1453 /*********************************************************************
1454 * _fullpath (CRTDLL.114)
1456 LPSTR __cdecl
CRTDLL__fullpath(LPSTR buf
, LPCSTR name
, INT32 size
)
1458 DOS_FULL_NAME full_name
;
1463 if(!(buf
= CRTDLL_malloc(size
))) return NULL
;
1465 if (!DOSFS_GetFullName( name
, FALSE
, &full_name
)) return NULL
;
1466 lstrcpyn32A(buf
,full_name
.short_name
,size
);
1467 dprintf_crtdll(stderr
,"CRTDLL_fullpath got %s\n",buf
);
1471 /*********************************************************************
1472 * _splitpath (CRTDLL.279)
1474 VOID __cdecl
CRTDLL__splitpath(LPCSTR path
, LPSTR drive
, LPSTR directory
, LPSTR filename
, LPSTR extension
)
1477 directory includes leading and trailing (forward and backward slashes)
1478 filename without dot and slashes
1479 extension with leading dot
1481 char * drivechar
,*dirchar
,*namechar
;
1483 dprintf_crtdll(stddeb
,"CRTDLL__splitpath got %s\n",path
);
1485 drivechar
= strchr(path
,':');
1486 dirchar
= strrchr(path
,'/');
1487 namechar
= strrchr(path
,'\\');
1488 dirchar
= MAX(dirchar
,namechar
);
1490 namechar
= strrchr(dirchar
,'.');
1492 namechar
= strrchr(path
,'.');
1500 strncat(drive
,path
,drivechar
-path
+1);
1509 strncat(directory
,path
,dirchar
-path
+1);
1518 strncat(filename
,path
,namechar
-path
);
1522 strcat(extension
,namechar
);
1527 dprintf_crtdll(stddeb
,"CRTDLL__splitpath found %s %s %s %s\n",drive
,directory
,filename
,extension
);
1531 /*********************************************************************
1532 * _getcwd (CRTDLL.120)
1534 CHAR
* __cdecl
CRTDLL__getcwd(LPSTR buf
, INT32 size
)
1541 if (size
< 0) /* allocate as big as nescessary */
1542 len
=GetCurrentDirectory32A(1,test
) + 1;
1543 if(!(buf
= CRTDLL_malloc(len
)))
1545 /* set error to OutOfRange */
1550 if(!(len
=GetCurrentDirectory32A(len
,buf
)))
1556 /* set error to ERANGE */
1557 dprintf_crtdll(stddeb
,"CRTDLL_getcwd buffer to small\n");
1564 /*********************************************************************
1565 * _mkdir (CRTDLL.234)
1567 INT32 __cdecl
CRTDLL__mkdir(LPCSTR newdir
)
1569 if (!CreateDirectory32A(newdir
,NULL
))
1574 /*********************************************************************
1575 * _errno (CRTDLL.52)
1576 * Yes, this is a function.
1578 LPINT32 __cdecl
CRTDLL__errno()
1580 static int crtdllerrno
;
1581 extern int LastErrorToErrno(DWORD
);
1583 /* FIXME: we should set the error at the failing function call time */
1584 crtdllerrno
= LastErrorToErrno(GetLastError());
1585 return &crtdllerrno
;
1588 /*********************************************************************
1589 * _tempnam (CRTDLL.305)
1592 LPSTR __cdecl
CRTDLL__tempnam(LPCSTR dir
, LPCSTR prefix
)
1596 DOS_FULL_NAME tempname
;
1598 if ((ret
= tempnam(dir
,prefix
))==NULL
) {
1599 dprintf_crtdll(stddeb
,
1600 "CRTDLL_tempnam Unable to get unique filename\n");
1603 if (!DOSFS_GetFullName(ret
,FALSE
,&tempname
))
1605 dprintf_crtdll(stddeb
,
1606 "CRTDLL_tempnam Wrong path?\n");
1610 if ((ret
= CRTDLL_malloc(strlen(tempname
.short_name
)+1)) == NULL
) {
1611 dprintf_crtdll(stddeb
,
1612 "CRTDLL_tempnam CRTDL_malloc for shortname failed\n");
1615 if ((ret
= strcpy(ret
,tempname
.short_name
)) == NULL
) {
1616 dprintf_crtdll(stddeb
,
1617 "CRTDLL_tempnam Malloc for shortname failed\n");
1621 dprintf_crtdll(stddeb
,"CRTDLL_tempnam dir %s prefix %s got %s\n",
1626 /*********************************************************************
1627 * tmpnam (CRTDLL.490)
1629 * lcclnk from lcc-win32 relies on a terminating dot in the name returned
1632 LPSTR __cdecl
CRTDLL_tmpnam(LPSTR s
)
1636 if ((ret
=tmpnam(s
))== NULL
) {
1637 dprintf_crtdll(stddeb
,
1638 "CRTDLL_tmpnam Unable to get unique filename\n");
1641 if (!DOSFS_GetFullName(ret
,FALSE
,&CRTDLL_tmpname
))
1643 dprintf_crtdll(stddeb
,
1644 "CRTDLL_tmpnam Wrong path?\n");
1647 strcat(CRTDLL_tmpname
.short_name
,".");
1648 dprintf_crtdll(stddeb
,"CRTDLL_tmpnam for buf %p got %s\n",
1649 s
,CRTDLL_tmpname
.short_name
);
1650 dprintf_crtdll(stddeb
,"CRTDLL_tmpnam long got %s\n",
1651 CRTDLL_tmpname
.long_name
);
1653 return strcpy(s
,CRTDLL_tmpname
.short_name
);
1655 return CRTDLL_tmpname
.short_name
;
1659 /*********************************************************************
1660 * _itoa (CRTDLL.165)
1662 LPSTR __cdecl
CRTDLL__itoa(INT32 x
,LPSTR buf
,INT32 buflen
)
1664 wsnprintf32A(buf
,buflen
,"%d",x
);
1668 typedef VOID (*sig_handler_type
)(VOID
);
1670 /*********************************************************************
1671 * signal (CRTDLL.455)
1673 VOID __cdecl
CRTDLL_signal(int sig
, sig_handler_type ptr
)
1675 dprintf_crtdll(stddeb
,"CRTDLL_signal %d %p: STUB!\n",sig
,ptr
);
1678 /*********************************************************************
1679 * _ftol (CRTDLL.113)
1681 LONG __cdecl
CRTDLL__ftol(double fl
) {