4 * Implements C run-time functionality as known from UNIX.
6 * Copyright 1996,1998 Marcus Meissner
7 * Copyright 1996 Jukka Iivonen
8 * Copyright 1997,2000 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
20 - needs a proper stdio emulation based on Win32 file handles
21 - should set CRTDLL errno from GetLastError() in every function
23 - probably not thread safe
26 /* NOTE: This file also implements the wcs* functions. They _ARE_ in
27 * the newer Linux libcs, but use 4 byte wide characters, so are unusable,
28 * since we need 2 byte wide characters. - Marcus Meissner, 981031
39 #include <sys/times.h>
52 #include "debugtools.h"
60 DEFAULT_DEBUG_CHANNEL(crtdll
);
62 /* windows.h RAND_MAX is smaller than normal RAND_MAX */
63 #define CRTDLL_RAND_MAX 0x7fff
65 static DOS_FULL_NAME CRTDLL_tmpname
;
67 UINT CRTDLL_argc_dll
; /* CRTDLL.23 */
68 LPSTR
*CRTDLL_argv_dll
; /* CRTDLL.24 */
69 LPSTR CRTDLL_acmdln_dll
; /* CRTDLL.38 */
70 UINT CRTDLL_basemajor_dll
; /* CRTDLL.42 */
71 UINT CRTDLL_baseminor_dll
; /* CRTDLL.43 */
72 UINT CRTDLL_baseversion_dll
; /* CRTDLL.44 */
73 UINT CRTDLL_commode_dll
; /* CRTDLL.59 */
74 LPSTR CRTDLL_environ_dll
; /* CRTDLL.75 */
75 UINT CRTDLL_fmode_dll
; /* CRTDLL.104 */
76 UINT CRTDLL_osmajor_dll
; /* CRTDLL.241 */
77 UINT CRTDLL_osminor_dll
; /* CRTDLL.242 */
78 UINT CRTDLL_osmode_dll
; /* CRTDLL.243 */
79 UINT CRTDLL_osver_dll
; /* CRTDLL.244 */
80 UINT CRTDLL_osversion_dll
; /* CRTDLL.245 */
81 UINT CRTDLL_winmajor_dll
; /* CRTDLL.329 */
82 UINT CRTDLL_winminor_dll
; /* CRTDLL.330 */
83 UINT CRTDLL_winver_dll
; /* CRTDLL.331 */
85 /* FIXME: structure layout is obviously not correct */
92 CRTDLL_FILE CRTDLL_iob
[3];
94 static CRTDLL_FILE
* const CRTDLL_stdin
= &CRTDLL_iob
[0];
95 static CRTDLL_FILE
* const CRTDLL_stdout
= &CRTDLL_iob
[1];
96 static CRTDLL_FILE
* const CRTDLL_stderr
= &CRTDLL_iob
[2];
98 typedef VOID (*new_handler_type
)(VOID
);
100 static new_handler_type new_handler
;
102 CRTDLL_FILE
* __cdecl
CRTDLL__fdopen(INT handle
, LPCSTR mode
);
103 INT __cdecl
CRTDLL_fgetc( CRTDLL_FILE
*file
);
105 /*********************************************************************
106 * CRTDLL_MainInit (CRTDLL.init)
108 BOOL WINAPI
CRTDLL_Init(HINSTANCE hinstDLL
, DWORD fdwReason
, LPVOID lpvReserved
)
110 TRACE("(0x%08x,%ld,%p)\n",hinstDLL
,fdwReason
,lpvReserved
);
112 if (fdwReason
== DLL_PROCESS_ATTACH
) {
113 CRTDLL__fdopen(0,"r");
114 CRTDLL__fdopen(1,"w");
115 CRTDLL__fdopen(2,"w");
120 /*********************************************************************
121 * malloc (CRTDLL.427)
123 VOID
* __cdecl
CRTDLL_malloc(DWORD size
)
125 return HeapAlloc(GetProcessHeap(),0,size
);
128 /*********************************************************************
129 * _strdup (CRTDLL.285)
131 LPSTR __cdecl
CRTDLL__strdup(LPCSTR ptr
)
133 LPSTR ret
= CRTDLL_malloc(strlen(ptr
)+1);
134 if (ret
) strcpy( ret
, ptr
);
138 /*********************************************************************
139 * _GetMainArgs (CRTDLL.022)
141 LPSTR
* __cdecl
CRTDLL__GetMainArgs(LPDWORD argc
,LPSTR
**argv
,
142 LPSTR
*environ
,DWORD flag
)
146 int xargc
,end
,last_arg
,afterlastspace
;
149 TRACE("(%p,%p,%p,%ld).\n",
150 argc
,argv
,environ
,flag
153 if (CRTDLL_acmdln_dll
!= NULL
)
154 HeapFree(GetProcessHeap(), 0, CRTDLL_acmdln_dll
);
156 CRTDLL_acmdln_dll
= cmdline
= CRTDLL__strdup( GetCommandLineA() );
157 TRACE("got '%s'\n", cmdline
);
159 version
= GetVersion();
160 CRTDLL_osver_dll
= version
>> 16;
161 CRTDLL_winminor_dll
= version
& 0xFF;
162 CRTDLL_winmajor_dll
= (version
>>8) & 0xFF;
163 CRTDLL_baseversion_dll
= version
>> 16;
164 CRTDLL_winver_dll
= ((version
>> 8) & 0xFF) + ((version
& 0xFF) << 8);
165 CRTDLL_baseminor_dll
= (version
>> 16) & 0xFF;
166 CRTDLL_basemajor_dll
= (version
>> 24) & 0xFF;
167 CRTDLL_osversion_dll
= version
& 0xFFFF;
168 CRTDLL_osminor_dll
= version
& 0xFF;
169 CRTDLL_osmajor_dll
= (version
>>8) & 0xFF;
171 /* missing threading init */
173 end
=0;last_arg
=0;xargv
=NULL
;xargc
=0;afterlastspace
=0;
176 if ((cmdline
[end
]==' ') || (cmdline
[end
]=='\0'))
178 if (cmdline
[end
]=='\0')
182 /* alloc xargc + NULL entry */
183 xargv
=(char**)HeapReAlloc( GetProcessHeap(), 0, xargv
,
184 sizeof(char*)*(xargc
+1));
185 if (strlen(cmdline
+afterlastspace
))
187 xargv
[xargc
] = CRTDLL__strdup(cmdline
+afterlastspace
);
189 if (!last_arg
) /* need to seek to the next arg ? */
192 while (cmdline
[end
]==' ')
199 xargv
[xargc
] = NULL
; /* the last entry is NULL */
206 CRTDLL_argc_dll
= xargc
;
208 CRTDLL_argv_dll
= xargv
;
211 TRACE("found %d arguments\n",
213 CRTDLL_environ_dll
= *environ
= GetEnvironmentStringsA();
218 typedef void (*_INITTERMFUN
)();
220 /* fixme: move to header */
223 time_t time_create
; /* -1 when not avaiable */
224 time_t time_access
; /* -1 when not avaiable */
226 unsigned long size
; /* fixme: 64 bit ??*/
229 /*********************************************************************
230 * _findfirst (CRTDLL.099)
235 DWORD __cdecl
CRTDLL__findfirst(LPCSTR fname
, struct find_t
* x2
)
237 FIXME(":(%s,%p): stub\n",fname
,x2
);
238 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
242 /*********************************************************************
243 * _findnext (CRTDLL.100)
248 INT __cdecl
CRTDLL__findnext(DWORD hand
, struct find_t
* x2
)
250 FIXME(":(%ld,%p): stub\n",hand
,x2
);
251 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
255 /*********************************************************************
256 * _fstat (CRTDLL.111)
261 int __cdecl
CRTDLL__fstat(int file
, struct stat
* buf
)
263 FIXME(":(%d,%p): stub\n",file
,buf
);
264 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
268 /*********************************************************************
269 * _initterm (CRTDLL.135)
271 DWORD __cdecl
CRTDLL__initterm(_INITTERMFUN
*start
,_INITTERMFUN
*end
)
273 _INITTERMFUN
*current
;
275 TRACE("(%p,%p)\n",start
,end
);
277 while (current
<end
) {
278 if (*current
) (*current
)();
284 /*********************************************************************
285 * _fsopen (CRTDLL.110)
287 CRTDLL_FILE
* __cdecl
CRTDLL__fsopen(LPCSTR x
, LPCSTR y
, INT z
) {
288 FIXME("(%s,%s,%d),stub!\n",x
,y
,z
);
292 /*********************************************************************
293 * _fdopen (CRTDLL.91)
295 CRTDLL_FILE
* __cdecl
CRTDLL__fdopen(INT handle
, LPCSTR mode
)
303 if (!file
->handle
) file
->handle
= GetStdHandle( STD_INPUT_HANDLE
);
306 file
= CRTDLL_stdout
;
307 if (!file
->handle
) file
->handle
= GetStdHandle( STD_OUTPUT_HANDLE
);
311 if (!file
->handle
) file
->handle
= GetStdHandle( STD_ERROR_HANDLE
);
314 file
= HeapAlloc( GetProcessHeap(), 0, sizeof(*file
) );
315 file
->handle
= handle
;
318 TRACE("open handle %d mode %s got file %p\n",
324 /*******************************************************************
325 * _global_unwind2 (CRTDLL.129)
327 void __cdecl
CRTDLL__global_unwind2( PEXCEPTION_FRAME frame
)
329 RtlUnwind( frame
, 0, NULL
, 0 );
332 /*******************************************************************
333 * _local_unwind2 (CRTDLL.173)
335 void __cdecl
CRTDLL__local_unwind2( PEXCEPTION_FRAME endframe
, DWORD nr
)
337 TRACE("(%p,%ld)\n",endframe
,nr
);
339 /*******************************************************************
340 * _setjmp (CRTDLL.264)
342 INT __cdecl
CRTDLL__setjmp(LPDWORD
*jmpbuf
)
344 FIXME(":(%p): stub\n",jmpbuf
);
348 /*********************************************************************
351 CRTDLL_FILE
* __cdecl
CRTDLL_fopen(LPCSTR path
, LPCSTR mode
)
353 CRTDLL_FILE
*file
= NULL
;
356 DOS_FULL_NAME full_name
;
358 if (!DOSFS_GetFullName( path
, FALSE
, &full_name
)) {
359 WARN("file %s bad name\n",path
);
363 file
=fopen(full_name
.long_name
,mode
);
365 DWORD access
= 0, creation
= 0;
367 if ((strchr(mode
,'r')&&strchr(mode
,'a'))||
368 (strchr(mode
,'r')&&strchr(mode
,'w'))||
369 (strchr(mode
,'w')&&strchr(mode
,'a')))
374 access
= GENERIC_READ
;
375 creation
= OPEN_EXISTING
;
376 if (mode
[1] == '+') access
|= GENERIC_WRITE
;
378 else if (mode
[0] == 'w')
380 access
= GENERIC_WRITE
;
381 creation
= CREATE_ALWAYS
;
382 if (mode
[1] == '+') access
|= GENERIC_READ
;
384 else if (mode
[0] == 'a')
386 access
= GENERIC_WRITE
;
387 creation
= OPEN_ALWAYS
;
388 if (mode
[1] == '+') access
|= GENERIC_READ
;
390 /* FIXME: should handle text/binary mode */
392 if ((handle
= CreateFileA( path
, access
, FILE_SHARE_READ
| FILE_SHARE_WRITE
,
393 NULL
, creation
, FILE_ATTRIBUTE_NORMAL
,
394 -1 )) != INVALID_HANDLE_VALUE
)
396 file
= HeapAlloc( GetProcessHeap(), 0, sizeof(*file
) );
397 file
->handle
= handle
;
399 TRACE("file %s mode %s got handle %d file %p\n",
400 path
,mode
,handle
,file
);
403 /* if appending, seek to end of file */
404 SetFilePointer( handle
, 0, NULL
, FILE_END
);
409 /*********************************************************************
412 DWORD __cdecl
CRTDLL_fread(LPVOID ptr
, INT size
, INT nmemb
, CRTDLL_FILE
*file
)
418 /* If we would honour CR/LF <-> LF translation, we could do it like this.
419 We should keep track of all files opened, and probably files with \
420 known binary extensions must be unchanged */
421 while ( (i
< (nmemb
*size
)) && (ret
==1)) {
422 ret
=fread(temp
,1,1,file
);
423 TRACE("got %c 0x%02x ret %d\n",
424 (isalpha(*(unsigned char*)temp
))? *(unsigned char*)temp
:
425 ' ',*(unsigned char*)temp
, ret
);
426 if (*(unsigned char*)temp
!= 0xd) { /* skip CR */
431 TRACE("skipping ^M\n");
433 TRACE("0x%08x items of size %d from file %p to %p\n",
434 nmemb
,size
,file
,ptr
,);
442 TRACE("0x%08x items of size %d from file %p to %p\n",
443 nmemb
,size
,file
,ptr
);
444 if (!ReadFile( file
->handle
, ptr
, size
* nmemb
, &ret
, NULL
))
450 /*********************************************************************
451 * freopen (CRTDLL.379)
456 DWORD __cdecl
CRTDLL_freopen(LPCSTR path
, LPCSTR mode
, LPVOID stream
)
458 FIXME(":(%s,%s,%p): stub\n", path
, mode
, stream
);
459 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
463 /*********************************************************************
464 * fscanf (CRTDLL.381)
466 INT __cdecl
CRTDLL_fscanf( CRTDLL_FILE
*stream
, LPSTR format
, ... )
471 if (!*format
) return 0;
472 WARN("%p (\"%s\"): semi-stub\n", stream
, format
);
473 nch
= CRTDLL_fgetc(stream
);
474 va_start(ap
, format
);
476 if (*format
== ' ') {
477 /* skip whitespace */
478 while ((nch
!=EOF
) && isspace(nch
))
479 nch
= CRTDLL_fgetc(stream
);
481 else if (*format
== '%') {
485 case 'd': { /* read an integer */
486 int*val
= va_arg(ap
, int*);
488 /* skip initial whitespace */
489 while ((nch
!=EOF
) && isspace(nch
))
490 nch
= CRTDLL_fgetc(stream
);
491 /* get sign and first digit */
493 nch
= CRTDLL_fgetc(stream
);
502 nch
= CRTDLL_fgetc(stream
);
503 /* read until no more digits */
504 while ((nch
!=EOF
) && isdigit(nch
)) {
505 cur
= cur
*10 + (nch
- '0');
506 nch
= CRTDLL_fgetc(stream
);
512 case 'f': { /* read a float */
513 float*val
= va_arg(ap
, float*);
515 /* skip initial whitespace */
516 while ((nch
!=EOF
) && isspace(nch
))
517 nch
= CRTDLL_fgetc(stream
);
518 /* get sign and first digit */
520 nch
= CRTDLL_fgetc(stream
);
529 /* read until no more digits */
530 while ((nch
!=EOF
) && isdigit(nch
)) {
531 cur
= cur
*10 + (nch
- '0');
532 nch
= CRTDLL_fgetc(stream
);
535 /* handle decimals */
537 nch
= CRTDLL_fgetc(stream
);
538 while ((nch
!=EOF
) && isdigit(nch
)) {
540 cur
+= dec
* (nch
- '0');
541 nch
= CRTDLL_fgetc(stream
);
548 case 's': { /* read a word */
549 char*str
= va_arg(ap
, char*);
551 /* skip initial whitespace */
552 while ((nch
!=EOF
) && isspace(nch
))
553 nch
= CRTDLL_fgetc(stream
);
554 /* read until whitespace */
555 while ((nch
!=EOF
) && !isspace(nch
)) {
557 nch
= CRTDLL_fgetc(stream
);
561 TRACE("read word: %s\n", str
);
564 default: FIXME("unhandled: %%%c\n", *format
);
570 /* check for character match */
572 nch
= CRTDLL_fgetc(stream
);
579 WARN("need ungetch\n");
581 TRACE("returning %d\n", rd
);
585 /*********************************************************************
586 * _lseek (CRTDLL.179)
588 LONG __cdecl
CRTDLL__lseek( INT fd
, LONG offset
, INT whence
)
590 TRACE("fd %d to 0x%08lx pos %s\n",
591 fd
,offset
,(whence
==SEEK_SET
)?"SEEK_SET":
592 (whence
==SEEK_CUR
)?"SEEK_CUR":
593 (whence
==SEEK_END
)?"SEEK_END":"UNKNOWN");
594 return SetFilePointer( fd
, offset
, NULL
, whence
);
597 /*********************************************************************
600 LONG __cdecl
CRTDLL_fseek( CRTDLL_FILE
*file
, LONG offset
, INT whence
)
602 TRACE("file %p to 0x%08lx pos %s\n",
603 file
,offset
,(whence
==SEEK_SET
)?"SEEK_SET":
604 (whence
==SEEK_CUR
)?"SEEK_CUR":
605 (whence
==SEEK_END
)?"SEEK_END":"UNKNOWN");
606 if (SetFilePointer( file
->handle
, offset
, NULL
, whence
) != 0xffffffff)
612 /*********************************************************************
613 * fsetpos (CRTDLL.383)
615 INT __cdecl
CRTDLL_fsetpos( CRTDLL_FILE
*file
, INT
*pos
)
617 TRACE("file %p pos %d\n", file
, *pos
);
618 return CRTDLL_fseek(file
, *pos
, SEEK_SET
);
621 /*********************************************************************
624 LONG __cdecl
CRTDLL_ftell( CRTDLL_FILE
*file
)
626 return SetFilePointer( file
->handle
, 0, NULL
, SEEK_CUR
);
629 /*********************************************************************
630 * fwrite (CRTDLL.386)
632 DWORD __cdecl
CRTDLL_fwrite( LPVOID ptr
, INT size
, INT nmemb
, CRTDLL_FILE
*file
)
636 TRACE("0x%08x items of size %d to file %p(%d) from %p\n",
637 nmemb
,size
,file
,file
-(CRTDLL_FILE
*)CRTDLL_iob
,ptr
);
640 if (!WriteFile( file
->handle
, ptr
, size
* nmemb
, &ret
, NULL
))
645 /*********************************************************************
646 * setbuf (CRTDLL.452)
648 INT __cdecl
CRTDLL_setbuf(CRTDLL_FILE
*file
, LPSTR buf
)
650 TRACE("(file %p buf %p)\n", file
, buf
);
651 /* this doesn't work:"void value not ignored as it ought to be"
652 return setbuf(file,buf);
654 /* FIXME: no buffering for now */
658 /*********************************************************************
659 * _open_osfhandle (CRTDLL.240)
661 HFILE __cdecl
CRTDLL__open_osfhandle(LONG osfhandle
, INT flags
)
666 case STD_INPUT_HANDLE
:
670 case STD_OUTPUT_HANDLE
:
674 case STD_ERROR_HANDLE
:
681 TRACE("(handle %08lx,flags %d) return %d\n",
682 osfhandle
,flags
,handle
);
687 /*********************************************************************
690 void __cdecl
CRTDLL_srand(DWORD seed
)
692 /* FIXME: should of course be thread? process? local */
696 /*********************************************************************
697 * vfprintf (CRTDLL.373)
699 INT __cdecl
CRTDLL_vfprintf( CRTDLL_FILE
*file
, LPSTR format
, va_list args
)
701 char buffer
[2048]; /* FIXME... */
703 vsprintf( buffer
, format
, args
);
704 return CRTDLL_fwrite( buffer
, 1, strlen(buffer
), file
);
707 /*********************************************************************
708 * fprintf (CRTDLL.373)
710 INT __cdecl
CRTDLL_fprintf( CRTDLL_FILE
*file
, LPSTR format
, ... )
715 va_start( valist
, format
);
716 res
= CRTDLL_vfprintf( file
, format
, valist
);
721 /*********************************************************************
724 time_t __cdecl
CRTDLL_time(time_t *timeptr
)
726 time_t curtime
= time(NULL
);
733 /*********************************************************************
734 * difftime (CRTDLL.357)
736 double __cdecl
CRTDLL_difftime (time_t time1
, time_t time2
)
740 timediff
= (double)(time1
- time2
);
744 /*********************************************************************
747 clock_t __cdecl
CRTDLL_clock(void)
753 res
= alltimes
.tms_utime
+ alltimes
.tms_stime
+
754 alltimes
.tms_cutime
+ alltimes
.tms_cstime
;
755 /* Fixme: We need some symbolic representation
756 for (Hostsystem_)CLOCKS_PER_SEC
757 and (Emulated_system_)CLOCKS_PER_SEC
758 10 holds only for Windows/Linux_i86)
763 /*********************************************************************
764 * _isatty (CRTDLL.137)
766 BOOL __cdecl
CRTDLL__isatty(DWORD x
)
772 /*********************************************************************
776 INT __cdecl
CRTDLL__read(INT fd
, LPVOID buf
, UINT count
)
778 TRACE("0x%08x bytes fd %d to %p\n", count
,fd
,buf
);
779 if (!fd
) fd
= GetStdHandle( STD_INPUT_HANDLE
);
780 return _lread( fd
, buf
, count
);
783 /*********************************************************************
784 * _write (CRTDLL.332)
786 INT __cdecl
CRTDLL__write(INT fd
,LPCVOID buf
,UINT count
)
793 len
= (UINT
)write(fd
,buf
,(LONG
)count
);
795 len
= _lwrite(fd
,buf
,count
);
796 TRACE("%d/%d byte to dfh %d from %p,\n",
802 /*********************************************************************
805 * FIXME: What the heck is the difference between
806 * FIXME _c_exit (CRTDLL.47)
807 * FIXME _cexit (CRTDLL.49)
808 * FIXME _exit (CRTDLL.87)
809 * FIXME exit (CRTDLL.359)
811 * atexit-processing comes to mind -- MW.
814 void __cdecl
CRTDLL__cexit(INT ret
)
821 /*********************************************************************
824 void __cdecl
CRTDLL_exit(DWORD ret
)
826 TRACE("(%ld)\n",ret
);
831 /*********************************************************************
832 * _abnormal_termination (CRTDLL.36)
834 INT __cdecl
CRTDLL__abnormal_termination(void)
841 /*********************************************************************
842 * _access (CRTDLL.37)
844 INT __cdecl
CRTDLL__access(LPCSTR filename
, INT mode
)
846 DWORD attr
= GetFileAttributesA(filename
);
850 if (GetLastError() == ERROR_INVALID_ACCESS
)
857 if ((attr
& FILE_ATTRIBUTE_READONLY
) && (mode
& W_OK
))
867 /*********************************************************************
868 * fflush (CRTDLL.365)
870 INT __cdecl
CRTDLL_fflush( CRTDLL_FILE
*file
)
872 return FlushFileBuffers( file
->handle
) ? 0 : -1;
876 /*********************************************************************
879 INT __cdecl
CRTDLL_rand()
881 return (rand() & CRTDLL_RAND_MAX
);
885 /*********************************************************************
888 INT __cdecl
CRTDLL_fputc( INT c
, CRTDLL_FILE
*file
)
892 TRACE("%c to file %p\n",c
,file
);
893 if (!WriteFile( file
->handle
, &ch
, 1, &res
, NULL
)) return -1;
898 /*********************************************************************
899 * putchar (CRTDLL.442)
901 void __cdecl
CRTDLL_putchar( INT x
)
903 CRTDLL_fputc( x
, CRTDLL_stdout
);
907 /*********************************************************************
910 INT __cdecl
CRTDLL_fputs( LPCSTR s
, CRTDLL_FILE
*file
)
913 TRACE("%s to file %p\n",s
,file
);
914 if (!WriteFile( file
->handle
, s
, strlen(s
), &res
, NULL
)) return -1;
919 /*********************************************************************
922 INT __cdecl
CRTDLL_puts(LPCSTR s
)
925 return CRTDLL_fputs(s
, CRTDLL_stdout
);
929 /*********************************************************************
932 INT __cdecl
CRTDLL_putc( INT c
, CRTDLL_FILE
*file
)
934 return CRTDLL_fputc( c
, file
);
937 /*********************************************************************
940 INT __cdecl
CRTDLL_fgetc( CRTDLL_FILE
*file
)
944 if (!ReadFile( file
->handle
, &ch
, 1, &res
, NULL
)) return -1;
945 if (res
!= 1) return -1;
950 /*********************************************************************
953 INT __cdecl
CRTDLL_getc( CRTDLL_FILE
*file
)
955 return CRTDLL_fgetc( file
);
959 /*********************************************************************
962 CHAR
* __cdecl
CRTDLL_fgets( LPSTR s
, INT size
, CRTDLL_FILE
*file
)
967 /* BAD, for the whole WINE process blocks... just done this way to test
968 * windows95's ftp.exe.
971 for(cc
= CRTDLL_fgetc(file
); cc
!= EOF
&& cc
!= '\n'; cc
= CRTDLL_fgetc(file
))
974 if (--size
<= 0) break;
977 if ((cc
== EOF
) &&(s
== buf_start
)) /* If nothing read, return 0*/
984 TRACE("got '%s'\n", buf_start
);
989 /*********************************************************************
992 LPSTR __cdecl
CRTDLL_gets(LPSTR buf
)
995 LPSTR buf_start
= buf
;
997 /* BAD, for the whole WINE process blocks... just done this way to test
998 * windows95's ftp.exe.
1001 for(cc
= CRTDLL_fgetc(CRTDLL_stdin
); cc
!= EOF
&& cc
!= '\n'; cc
= CRTDLL_fgetc(CRTDLL_stdin
))
1002 if(cc
!= '\r') *buf
++ = (char)cc
;
1006 TRACE("got '%s'\n", buf_start
);
1011 /*********************************************************************
1012 * _rotl (CRTDLL.259)
1014 UINT __cdecl
CRTDLL__rotl(UINT x
,INT shift
)
1016 unsigned int ret
= (x
>> shift
)|( x
>>((sizeof(x
))-shift
));
1018 TRACE("got 0x%08x rot %d ret 0x%08x\n",
1023 /*********************************************************************
1024 * _lrotl (CRTDLL.176)
1026 DWORD __cdecl
CRTDLL__lrotl(DWORD x
,INT shift
)
1028 unsigned long ret
= (x
>> shift
)|( x
>>((sizeof(x
))-shift
));
1030 TRACE("got 0x%08lx rot %d ret 0x%08lx\n",
1037 /*********************************************************************
1038 * _mbsicmp (CRTDLL.204)
1040 int __cdecl
CRTDLL__mbsicmp(unsigned char *x
,unsigned char *y
)
1047 /* FIXME: MBCS handling... */
1056 /*********************************************************************
1057 * vswprintf (CRTDLL.501)
1059 INT __cdecl
CRTDLL_vswprintf( LPWSTR buffer
, LPCWSTR spec
, va_list args
)
1061 return wvsprintfW( buffer
, spec
, args
);
1065 /*********************************************************************
1066 * system (CRTDLL.485)
1068 INT __cdecl
CRTDLL_system(LPSTR x
)
1070 #define SYSBUF_LENGTH 1500
1071 char buffer
[SYSBUF_LENGTH
];
1072 unsigned char *y
= x
;
1076 sprintf( buffer
, "%s \"", argv0
);
1077 bp
= buffer
+ strlen(buffer
);
1078 i
= strlen(buffer
) + strlen(x
) +2;
1080 /* Calculate needed buffer size to prevent overflow. */
1085 /* If buffer too short, exit. */
1086 if (i
> SYSBUF_LENGTH
) {
1087 TRACE("_system buffer to small\n");
1096 if (*(y
-1) =='\\') *bp
++ = '\\';
1098 /* Remove spaces from end of string. */
1099 while (*(y
-1) == ' ') {
1104 TRACE("_system got '%s', executing '%s'\n",x
,buffer
);
1106 return system(buffer
);
1109 /*********************************************************************
1110 * longjmp (CRTDLL.426)
1112 VOID __cdecl
CRTDLL_longjmp(jmp_buf env
, int val
)
1114 FIXME("CRTDLL_longjmp semistup, expect crash\n");
1118 /*********************************************************************
1121 VOID
* __cdecl
CRTDLL_new(DWORD size
)
1124 if(!(result
= HeapAlloc(GetProcessHeap(),0,size
)) && new_handler
)
1129 /*********************************************************************
1130 * set_new_handler(CRTDLL.003)
1132 new_handler_type __cdecl
CRTDLL_set_new_handler(new_handler_type func
)
1134 new_handler_type old_handler
= new_handler
;
1139 /*********************************************************************
1140 * calloc (CRTDLL.350)
1142 VOID
* __cdecl
CRTDLL_calloc(DWORD size
, DWORD count
)
1144 return HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
, size
* count
);
1147 /*********************************************************************
1148 * realloc (CRTDLL.447)
1150 VOID
* __cdecl
CRTDLL_realloc( VOID
*ptr
, DWORD size
)
1152 return HeapReAlloc( GetProcessHeap(), 0, ptr
, size
);
1155 /*********************************************************************
1158 VOID __cdecl
CRTDLL_free(LPVOID ptr
)
1160 HeapFree(GetProcessHeap(),0,ptr
);
1163 /*********************************************************************
1164 * delete (CRTDLL.002)
1166 VOID __cdecl
CRTDLL_delete(VOID
* ptr
)
1168 HeapFree(GetProcessHeap(),0,ptr
);
1171 /*********************************************************************
1172 * fclose (CRTDLL.362)
1174 INT __cdecl
CRTDLL_fclose( CRTDLL_FILE
*file
)
1176 TRACE("%p\n", file
);
1177 if (!CloseHandle( file
->handle
)) return -1;
1178 HeapFree( GetProcessHeap(), 0, file
);
1182 /*********************************************************************
1183 * _unlink (CRTDLL.315)
1185 INT __cdecl
CRTDLL__unlink(LPCSTR pathname
)
1187 return DeleteFileA( pathname
) ? 0 : -1;
1190 /*********************************************************************
1191 * rename (CRTDLL.449)
1193 INT __cdecl
CRTDLL_rename(LPCSTR oldpath
,LPCSTR newpath
)
1195 BOOL ok
= MoveFileExA( oldpath
, newpath
, MOVEFILE_REPLACE_EXISTING
);
1200 /*********************************************************************
1201 * _stat (CRTDLL.280)
1219 int __cdecl
CRTDLL__stat(const char * filename
, struct win_stat
* buf
)
1222 DOS_FULL_NAME full_name
;
1225 if (!DOSFS_GetFullName( filename
, TRUE
, &full_name
))
1227 WARN("CRTDLL__stat filename %s bad name\n",filename
);
1230 ret
=stat(full_name
.long_name
,&mystat
);
1231 TRACE("CRTDLL__stat %s\n", filename
);
1235 /* FIXME: should check what Windows returns */
1237 buf
->win_st_dev
= mystat
.st_dev
;
1238 buf
->win_st_ino
= mystat
.st_ino
;
1239 buf
->win_st_mode
= mystat
.st_mode
;
1240 buf
->win_st_nlink
= mystat
.st_nlink
;
1241 buf
->win_st_uid
= mystat
.st_uid
;
1242 buf
->win_st_gid
= mystat
.st_gid
;
1243 buf
->win_st_rdev
= mystat
.st_rdev
;
1244 buf
->win_st_size
= mystat
.st_size
;
1245 buf
->win_st_atime
= mystat
.st_atime
;
1246 buf
->win_st_mtime
= mystat
.st_mtime
;
1247 buf
->win_st_ctime
= mystat
.st_ctime
;
1251 /*********************************************************************
1252 * _open (CRTDLL.239)
1254 HFILE __cdecl
CRTDLL__open(LPCSTR path
,INT flags
)
1256 DWORD access
= 0, creation
= 0;
1260 the flags in lcc's header differ from the ones in Linux, e.g.
1261 Linux: define O_APPEND 02000 (= 0x400)
1262 lcc: define _O_APPEND 0x0008
1263 so here a scheme to translate them
1264 Probably lcc is wrong here, but at least a hack to get is going
1268 case O_RDONLY
: access
|= GENERIC_READ
; break;
1269 case O_WRONLY
: access
|= GENERIC_WRITE
; break;
1270 case O_RDWR
: access
|= GENERIC_WRITE
| GENERIC_READ
; break;
1273 if (flags
& 0x0100) /* O_CREAT */
1275 if (flags
& 0x0400) /* O_EXCL */
1276 creation
= CREATE_NEW
;
1277 else if (flags
& 0x0200) /* O_TRUNC */
1278 creation
= CREATE_ALWAYS
;
1280 creation
= OPEN_ALWAYS
;
1282 else /* no O_CREAT */
1284 if (flags
& 0x0200) /* O_TRUNC */
1285 creation
= TRUNCATE_EXISTING
;
1287 creation
= OPEN_EXISTING
;
1289 if (flags
& 0x0008) /* O_APPEND */
1290 FIXME("O_APPEND not supported\n" );
1291 if (!(flags
& 0x8000 /* O_BINARY */ ) || (flags
& 0x4000 /* O_TEXT */))
1292 FIXME(":text mode not supported\n");
1294 TRACE("CRTDLL_open file unsupported flags 0x%04x\n",flags
);
1297 ret
= CreateFileA( path
, access
, FILE_SHARE_READ
| FILE_SHARE_WRITE
,
1298 NULL
, creation
, FILE_ATTRIBUTE_NORMAL
, -1 );
1299 TRACE("CRTDLL_open file %s mode 0x%04x got handle %d\n", path
,flags
,ret
);
1303 /*********************************************************************
1304 * _close (CRTDLL.57)
1306 INT __cdecl
CRTDLL__close(HFILE fd
)
1308 int ret
=_lclose(fd
);
1317 /*********************************************************************
1319 * FIXME: Care for large files
1320 * FIXME: Check errors
1322 INT __cdecl
CRTDLL_feof( CRTDLL_FILE
*file
)
1324 DWORD curpos
=SetFilePointer( file
->handle
, 0, NULL
, SEEK_CUR
);
1325 DWORD endpos
=SetFilePointer( file
->handle
, 0, NULL
, FILE_END
);
1330 SetFilePointer( file
->handle
, curpos
,0,FILE_BEGIN
);
1334 /*********************************************************************
1335 * setlocale (CRTDLL.453)
1337 LPSTR __cdecl
CRTDLL_setlocale(INT category
,LPCSTR locale
)
1342 case CRTDLL_LC_ALL
: categorystr
="LC_ALL";break;
1343 case CRTDLL_LC_COLLATE
: categorystr
="LC_COLLATE";break;
1344 case CRTDLL_LC_CTYPE
: categorystr
="LC_CTYPE";break;
1345 case CRTDLL_LC_MONETARY
: categorystr
="LC_MONETARY";break;
1346 case CRTDLL_LC_NUMERIC
: categorystr
="LC_NUMERIC";break;
1347 case CRTDLL_LC_TIME
: categorystr
="LC_TIME";break;
1348 default: categorystr
= "UNKNOWN?";break;
1350 FIXME("(%s,%s),stub!\n",categorystr
,locale
);
1354 /*********************************************************************
1355 * _setmode (CRTDLL.265)
1356 * FIXME: At present we ignore the request to translate CR/LF to LF.
1358 * We allways translate when we read with fgets, we never do with fread
1361 INT __cdecl
CRTDLL__setmode( INT fh
,INT mode
)
1364 #define O_TEXT 0x4000
1365 #define O_BINARY 0x8000
1367 FIXME("on fhandle %d mode %s, STUB.\n",
1368 fh
,(mode
=O_TEXT
)?"O_TEXT":
1369 (mode
=O_BINARY
)?"O_BINARY":"UNKNOWN");
1373 /*********************************************************************
1374 * _fpreset (CRTDLL.107)
1376 VOID __cdecl
CRTDLL__fpreset(void)
1381 /*********************************************************************
1382 * atexit (CRTDLL.345)
1384 INT __cdecl
CRTDLL_atexit(LPVOID x
)
1386 FIXME("(%p), STUB.\n",x
);
1387 return 0; /* successful */
1390 /*********************************************************************
1391 * _isctype (CRTDLL.138)
1393 BOOL __cdecl
CRTDLL__isctype(CHAR x
,CHAR type
)
1395 if ((type
& CRTDLL_SPACE
) && isspace(x
))
1397 if ((type
& CRTDLL_PUNCT
) && ispunct(x
))
1399 if ((type
& CRTDLL_LOWER
) && islower(x
))
1401 if ((type
& CRTDLL_UPPER
) && isupper(x
))
1403 if ((type
& CRTDLL_ALPHA
) && isalpha(x
))
1405 if ((type
& CRTDLL_DIGIT
) && isdigit(x
))
1407 if ((type
& CRTDLL_CONTROL
) && iscntrl(x
))
1409 /* check CRTDLL_LEADBYTE */
1413 /*********************************************************************
1414 * _chdrive (CRTDLL.52)
1416 * newdir [I] drive to change to, A=1
1419 BOOL __cdecl
CRTDLL__chdrive(INT newdrive
)
1421 /* FIXME: generates errnos */
1422 return DRIVE_SetCurrentDrive(newdrive
-1);
1425 /*********************************************************************
1426 * _chdir (CRTDLL.51)
1428 INT __cdecl
CRTDLL__chdir(LPCSTR newdir
)
1430 if (!SetCurrentDirectoryA(newdir
))
1435 /*********************************************************************
1436 * _fullpath (CRTDLL.114)
1438 LPSTR __cdecl
CRTDLL__fullpath(LPSTR buf
, LPCSTR name
, INT size
)
1440 DOS_FULL_NAME full_name
;
1445 if(!(buf
= CRTDLL_malloc(size
))) return NULL
;
1447 if (!DOSFS_GetFullName( name
, FALSE
, &full_name
)) return NULL
;
1448 lstrcpynA(buf
,full_name
.short_name
,size
);
1449 TRACE("CRTDLL_fullpath got %s\n",buf
);
1453 /*********************************************************************
1454 * _splitpath (CRTDLL.279)
1456 VOID __cdecl
CRTDLL__splitpath(LPCSTR path
, LPSTR drive
, LPSTR directory
, LPSTR filename
, LPSTR extension
)
1459 directory includes leading and trailing (forward and backward slashes)
1460 filename without dot and slashes
1461 extension with leading dot
1463 char * drivechar
,*dirchar
,*namechar
;
1465 TRACE("CRTDLL__splitpath got %s\n",path
);
1467 drivechar
= strchr(path
,':');
1468 dirchar
= strrchr(path
,'/');
1469 namechar
= strrchr(path
,'\\');
1470 dirchar
= max(dirchar
,namechar
);
1472 namechar
= strrchr(dirchar
,'.');
1474 namechar
= strrchr(path
,'.');
1482 strncat(drive
,path
,drivechar
-path
+1);
1491 strncat(directory
,path
,dirchar
-path
+1);
1500 strncat(filename
,path
,namechar
-path
);
1504 strcat(extension
,namechar
);
1509 TRACE("CRTDLL__splitpath found %s %s %s %s\n",drive
,directory
,filename
,extension
);
1514 /*********************************************************************
1515 * _makepath (CRTDLL.182)
1518 VOID __cdecl
CRTDLL__makepath(LPSTR path
, LPCSTR drive
,
1519 LPCSTR directory
, LPCSTR filename
,
1523 TRACE("CRTDLL__makepath got %s %s %s %s\n", drive
, directory
,
1524 filename
, extension
);
1532 sprintf(path
, "%c:", drive
[0]);
1535 if ( directory
[0] ) {
1536 strcat(path
, directory
);
1537 ch
= path
[strlen(path
)-1];
1538 if (ch
!= '/' && ch
!= '\\')
1542 if ( filename
[0] ) {
1543 strcat(path
, filename
);
1545 if ( extension
[0] ) {
1546 if ( extension
[0] != '.' ) {
1549 strcat(path
,extension
);
1554 TRACE("CRTDLL__makepath returns %s\n",path
);
1557 /*********************************************************************
1558 * _getcwd (CRTDLL.120)
1560 CHAR
* __cdecl
CRTDLL__getcwd(LPSTR buf
, INT size
)
1567 if (size
< 0) /* allocate as big as nescessary */
1568 len
=GetCurrentDirectoryA(1,test
) + 1;
1569 if(!(buf
= CRTDLL_malloc(len
)))
1571 /* set error to OutOfRange */
1576 if(!(len
=GetCurrentDirectoryA(len
,buf
)))
1582 /* set error to ERANGE */
1583 TRACE("CRTDLL_getcwd buffer to small\n");
1590 /*********************************************************************
1591 * _getdcwd (CRTDLL.121)
1593 CHAR
* __cdecl
CRTDLL__getdcwd(INT drive
,LPSTR buf
, INT size
)
1598 FIXME("(\"%c:\",%s,%d)\n",drive
+'A',buf
,size
);
1601 if (size
< 0) /* allocate as big as nescessary */
1602 len
=GetCurrentDirectoryA(1,test
) + 1;
1603 if(!(buf
= CRTDLL_malloc(len
)))
1605 /* set error to OutOfRange */
1610 if(!(len
=GetCurrentDirectoryA(len
,buf
)))
1616 /* set error to ERANGE */
1617 TRACE("buffer to small\n");
1624 /*********************************************************************
1625 * _getdrive (CRTDLL.124)
1627 * Return current drive, 1 for A, 2 for B
1629 INT __cdecl
CRTDLL__getdrive(VOID
)
1631 return DRIVE_GetCurrentDrive() + 1;
1634 /*********************************************************************
1635 * _mkdir (CRTDLL.234)
1637 INT __cdecl
CRTDLL__mkdir(LPCSTR newdir
)
1639 if (!CreateDirectoryA(newdir
,NULL
))
1644 /*********************************************************************
1645 * remove (CRTDLL.448)
1647 INT __cdecl
CRTDLL_remove(LPCSTR file
)
1649 if (!DeleteFileA(file
))
1654 /*********************************************************************
1655 * _errno (CRTDLL.52)
1656 * Yes, this is a function.
1658 LPINT __cdecl
CRTDLL__errno()
1660 static int crtdllerrno
;
1662 /* FIXME: we should set the error at the failing function call time */
1664 switch(GetLastError())
1666 case ERROR_ACCESS_DENIED
: crtdllerrno
= EPERM
; break;
1667 case ERROR_FILE_NOT_FOUND
: crtdllerrno
= ENOENT
; break;
1668 case ERROR_INVALID_PARAMETER
: crtdllerrno
= EINVAL
; break;
1669 case ERROR_IO_DEVICE
: crtdllerrno
= EIO
; break;
1670 case ERROR_BAD_FORMAT
: crtdllerrno
= ENOEXEC
; break;
1671 case ERROR_INVALID_HANDLE
: crtdllerrno
= EBADF
; break;
1672 case ERROR_OUTOFMEMORY
: crtdllerrno
= ENOMEM
; break;
1673 case ERROR_BUSY
: crtdllerrno
= EBUSY
; break;
1674 case ERROR_FILE_EXISTS
: crtdllerrno
= EEXIST
; break;
1675 case ERROR_BAD_DEVICE
: crtdllerrno
= ENODEV
; break;
1676 case ERROR_TOO_MANY_OPEN_FILES
: crtdllerrno
= EMFILE
; break;
1677 case ERROR_DISK_FULL
: crtdllerrno
= ENOSPC
; break;
1678 case ERROR_SEEK_ON_DEVICE
: crtdllerrno
= ESPIPE
; break;
1679 case ERROR_BROKEN_PIPE
: crtdllerrno
= EPIPE
; break;
1680 case ERROR_POSSIBLE_DEADLOCK
: crtdllerrno
= EDEADLK
; break;
1681 case ERROR_FILENAME_EXCED_RANGE
: crtdllerrno
= ENAMETOOLONG
; break;
1682 case ERROR_DIR_NOT_EMPTY
: crtdllerrno
= ENOTEMPTY
; break;
1684 return &crtdllerrno
;
1687 /*********************************************************************
1688 * _tempnam (CRTDLL.305)
1691 LPSTR __cdecl
CRTDLL__tempnam(LPCSTR dir
, LPCSTR prefix
)
1695 DOS_FULL_NAME tempname
;
1697 if ((ret
= tempnam(dir
,prefix
))==NULL
) {
1698 WARN("Unable to get unique filename\n");
1701 if (!DOSFS_GetFullName(ret
,FALSE
,&tempname
))
1703 TRACE("Wrong path?\n");
1707 if ((ret
= CRTDLL_malloc(strlen(tempname
.short_name
)+1)) == NULL
) {
1708 WARN("CRTDL_malloc for shortname failed\n");
1711 if ((ret
= strcpy(ret
,tempname
.short_name
)) == NULL
) {
1712 WARN("Malloc for shortname failed\n");
1716 TRACE("dir %s prefix %s got %s\n",
1721 /*********************************************************************
1722 * tmpnam (CRTDLL.490)
1724 * lcclnk from lcc-win32 relies on a terminating dot in the name returned
1727 LPSTR __cdecl
CRTDLL_tmpnam(LPSTR s
)
1731 if ((ret
=tmpnam(s
))== NULL
) {
1732 WARN("Unable to get unique filename\n");
1735 if (!DOSFS_GetFullName(ret
,FALSE
,&CRTDLL_tmpname
))
1737 TRACE("Wrong path?\n");
1740 strcat(CRTDLL_tmpname
.short_name
,".");
1741 TRACE("for buf %p got %s\n",
1742 s
,CRTDLL_tmpname
.short_name
);
1743 TRACE("long got %s\n",
1744 CRTDLL_tmpname
.long_name
);
1746 return strcpy(s
,CRTDLL_tmpname
.short_name
);
1748 return CRTDLL_tmpname
.short_name
;
1753 typedef VOID (*sig_handler_type
)(VOID
);
1755 /*********************************************************************
1756 * signal (CRTDLL.455)
1758 void * __cdecl
CRTDLL_signal(int sig
, sig_handler_type ptr
)
1760 FIXME("(%d %p):stub.\n", sig
, ptr
);
1764 /*********************************************************************
1765 * _sleep (CRTDLL.267)
1767 VOID __cdecl
CRTDLL__sleep(unsigned long timeout
)
1769 TRACE("CRTDLL__sleep for %ld milliseconds\n",timeout
);
1770 Sleep((timeout
)?timeout
:1);
1773 /*********************************************************************
1774 * getenv (CRTDLL.437)
1776 LPSTR __cdecl
CRTDLL_getenv(const char *name
)
1778 LPSTR environ
= GetEnvironmentStringsA();
1779 LPSTR pp
,pos
= NULL
;
1780 unsigned int length
;
1782 for (pp
= environ
; (*pp
); pp
= pp
+ strlen(pp
) +1)
1784 pos
=strchr(pp
,'=');
1788 length
= strlen(pp
);
1789 if (!strncmp(pp
,name
,length
)) break;
1794 TRACE("got %s\n",pp
);
1796 FreeEnvironmentStringsA( environ
);
1800 /*********************************************************************
1801 * _mbsrchr (CRTDLL.223)
1803 LPSTR __cdecl
CRTDLL__mbsrchr(LPSTR s
,CHAR x
) {
1804 /* FIXME: handle multibyte strings */
1805 return strrchr(s
,x
);
1808 /*********************************************************************
1809 * __dllonexit (CRTDLL.25)
1811 VOID __cdecl
CRTDLL___dllonexit ()
1816 /*********************************************************************
1817 * _strdate (CRTDLL.283)
1819 LPSTR __cdecl
CRTDLL__strdate (LPSTR date
)
1820 { FIXME("%p stub\n", date
);
1824 /*********************************************************************
1825 * _strtime (CRTDLL.299)
1827 LPSTR __cdecl
CRTDLL__strtime (LPSTR date
)
1828 { FIXME("%p stub\n", date
);
1832 /*********************************************************************
1833 * _except_handler2 (CRTDLL.78)
1835 INT __cdecl
CRTDLL__except_handler2 (
1836 PEXCEPTION_RECORD rec
,
1837 PEXCEPTION_FRAME frame
,
1839 PEXCEPTION_FRAME
*dispatcher
)
1841 FIXME ("exception %lx flags=%lx at %p handler=%p %p %p stub\n",
1842 rec
->ExceptionCode
, rec
->ExceptionFlags
, rec
->ExceptionAddress
,
1843 frame
->Handler
, context
, dispatcher
);
1844 return ExceptionContinueSearch
;