4 * Implements C run-time functionality as known from UNIX.
6 * Copyright 1996,1998 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
20 - needs a proper stdio emulation based on Win32 file handles
21 - should set CRTDLL errno from GetLastError() in every function
24 /* NOTE: This file also implements the wcs* functions. They _ARE_ in
25 * the newer Linux libcs, but use 4 byte wide characters, so are unusable,
26 * since we need 2 byte wide characters. - Marcus Meissner, 981031
34 #include <sys/times.h>
45 #include "debugtools.h"
54 DEFAULT_DEBUG_CHANNEL(crtdll)
56 /* windows.h RAND_MAX is smaller than normal RAND_MAX */
57 #define CRTDLL_RAND_MAX 0x7fff
59 static DOS_FULL_NAME CRTDLL_tmpname;
61 UINT CRTDLL_argc_dll; /* CRTDLL.23 */
62 LPSTR *CRTDLL_argv_dll; /* CRTDLL.24 */
63 LPSTR CRTDLL_acmdln_dll; /* CRTDLL.38 */
64 UINT CRTDLL_basemajor_dll; /* CRTDLL.42 */
65 UINT CRTDLL_baseminor_dll; /* CRTDLL.43 */
66 UINT CRTDLL_baseversion_dll; /* CRTDLL.44 */
67 UINT CRTDLL_commode_dll; /* CRTDLL.59 */
68 LPSTR CRTDLL_environ_dll; /* CRTDLL.75 */
69 UINT CRTDLL_fmode_dll; /* CRTDLL.104 */
70 UINT CRTDLL_osmajor_dll; /* CRTDLL.241 */
71 UINT CRTDLL_osminor_dll; /* CRTDLL.242 */
72 UINT CRTDLL_osmode_dll; /* CRTDLL.243 */
73 UINT CRTDLL_osver_dll; /* CRTDLL.244 */
74 UINT CRTDLL_osversion_dll; /* CRTDLL.245 */
75 UINT CRTDLL_winmajor_dll; /* CRTDLL.329 */
76 UINT CRTDLL_winminor_dll; /* CRTDLL.330 */
77 UINT CRTDLL_winver_dll; /* CRTDLL.331 */
79 /* FIXME: structure layout is obviously not correct */
86 CRTDLL_FILE CRTDLL_iob[3];
88 static CRTDLL_FILE * const CRTDLL_stdin = &CRTDLL_iob[0];
89 static CRTDLL_FILE * const CRTDLL_stdout = &CRTDLL_iob[1];
90 static CRTDLL_FILE * const CRTDLL_stderr = &CRTDLL_iob[2];
92 typedef VOID (*new_handler_type)(VOID);
94 static new_handler_type new_handler;
96 #if defined(__GNUC__) && defined(__i386__)
97 #define USING_REAL_FPU
98 #define DO_FPU(x,y) __asm__ __volatile__( x " %0;fwait" : "=m" (y) : )
99 #define POP_FPU(x) DO_FPU("fstpl",x)
102 CRTDLL_FILE * __cdecl CRTDLL__fdopen(INT handle, LPCSTR mode);
104 /*********************************************************************
105 * CRTDLL_MainInit (CRTDLL.init)
107 BOOL WINAPI CRTDLL_Init(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
109 TRACE("(0x%08x,%ld,%p)\n",hinstDLL,fdwReason,lpvReserved);
111 if (fdwReason == DLL_PROCESS_ATTACH) {
112 CRTDLL__fdopen(0,"r");
113 CRTDLL__fdopen(1,"w");
114 CRTDLL__fdopen(2,"w");
119 /*********************************************************************
120 * _GetMainArgs (CRTDLL.022)
122 DWORD __cdecl CRTDLL__GetMainArgs(LPDWORD argc,LPSTR **argv,
123 LPSTR *environ,DWORD flag)
127 int xargc,i,afterlastspace;
130 TRACE("(%p,%p,%p,%ld).\n",
131 argc,argv,environ,flag
133 CRTDLL_acmdln_dll = cmdline = HEAP_strdupA( GetProcessHeap(), 0,
135 TRACE("got '%s'\n", cmdline);
137 version = GetVersion();
138 CRTDLL_osver_dll = version >> 16;
139 CRTDLL_winminor_dll = version & 0xFF;
140 CRTDLL_winmajor_dll = (version>>8) & 0xFF;
141 CRTDLL_baseversion_dll = version >> 16;
142 CRTDLL_winver_dll = ((version >> 8) & 0xFF) + ((version & 0xFF) << 8);
143 CRTDLL_baseminor_dll = (version >> 16) & 0xFF;
144 CRTDLL_basemajor_dll = (version >> 24) & 0xFF;
145 CRTDLL_osversion_dll = version & 0xFFFF;
146 CRTDLL_osminor_dll = version & 0xFF;
147 CRTDLL_osmajor_dll = (version>>8) & 0xFF;
149 /* missing threading init */
151 i=0;xargv=NULL;xargc=0;afterlastspace=0;
153 if (cmdline[i]==' ') {
154 xargv=(char**)HeapReAlloc( GetProcessHeap(), 0, xargv,
155 sizeof(char*)*(++xargc));
157 xargv[xargc-1] = HEAP_strdupA( GetProcessHeap(), 0,
158 cmdline+afterlastspace);
160 while (cmdline[i]==' ')
167 xargv=(char**)HeapReAlloc( GetProcessHeap(), 0, xargv,
168 sizeof(char*)*(++xargc));
170 xargv[xargc-1] = HEAP_strdupA( GetProcessHeap(), 0,
171 cmdline+afterlastspace);
172 CRTDLL_argc_dll = xargc;
174 CRTDLL_argv_dll = xargv;
177 TRACE("found %d arguments\n",
179 CRTDLL_environ_dll = *environ = GetEnvironmentStringsA();
184 typedef void (*_INITTERMFUN)();
186 /* fixme: move to header */
189 time_t time_create; /* -1 when not avaiable */
190 time_t time_access; /* -1 when not avaiable */
192 unsigned long size; /* fixme: 64 bit ??*/
195 /*********************************************************************
196 * _findfirst (CRTDLL.099)
201 DWORD __cdecl CRTDLL__findfirst(LPCSTR fname, struct find_t * x2)
203 FIXME(":(%s,%p): stub\n",fname,x2);
204 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
208 /*********************************************************************
209 * _findnext (CRTDLL.100)
214 INT __cdecl CRTDLL__findnext(DWORD hand, struct find_t * x2)
216 FIXME(":(%ld,%p): stub\n",hand,x2);
217 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
221 /*********************************************************************
222 * _fstat (CRTDLL.111)
227 int __cdecl CRTDLL__fstat(int file, struct stat* buf)
229 FIXME(":(%d,%p): stub\n",file,buf);
230 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
234 /*********************************************************************
235 * _initterm (CRTDLL.135)
237 DWORD __cdecl CRTDLL__initterm(_INITTERMFUN *start,_INITTERMFUN *end)
239 _INITTERMFUN *current;
241 TRACE("(%p,%p)\n",start,end);
243 while (current<end) {
244 if (*current) (*current)();
250 CRTDLL_FILE * __cdecl CRTDLL__fsopen(LPCSTR x, LPCSTR y, INT z) {
251 FIXME("(%s,%s,%d),stub!\n",x,y,z);
254 /*********************************************************************
255 * _fdopen (CRTDLL.91)
257 CRTDLL_FILE * __cdecl CRTDLL__fdopen(INT handle, LPCSTR mode)
265 if (!file->handle) file->handle = GetStdHandle( STD_INPUT_HANDLE );
268 file = CRTDLL_stdout;
269 if (!file->handle) file->handle = GetStdHandle( STD_OUTPUT_HANDLE );
273 if (!file->handle) file->handle = GetStdHandle( STD_ERROR_HANDLE );
276 file = HeapAlloc( GetProcessHeap(), 0, sizeof(*file) );
277 file->handle = handle;
280 TRACE("open handle %d mode %s got file %p\n",
286 /*******************************************************************
287 * _global_unwind2 (CRTDLL.129)
289 void __cdecl CRTDLL__global_unwind2( PEXCEPTION_FRAME frame )
291 RtlUnwind( frame, 0, NULL, 0 );
294 /*******************************************************************
295 * _local_unwind2 (CRTDLL.173)
297 void __cdecl CRTDLL__local_unwind2( PEXCEPTION_FRAME endframe, DWORD nr )
299 TRACE("(%p,%ld)\n",endframe,nr);
301 /*********************************************************************
307 INT __cdecl CRTDLL__read(INT fd, LPVOID buf, UINT count)
309 FIXME(":(%d,%p,%d): stub\n",fd,buf,count);
310 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
314 /*********************************************************************
317 CRTDLL_FILE * __cdecl CRTDLL_fopen(LPCSTR path, LPCSTR mode)
319 CRTDLL_FILE *file = NULL;
322 DOS_FULL_NAME full_name;
324 if (!DOSFS_GetFullName( path, FALSE, &full_name )) {
325 WARN("file %s bad name\n",path);
329 file=fopen(full_name.long_name ,mode);
331 DWORD access = 0, creation = 0;
333 if ((strchr(mode,'r')&&strchr(mode,'a'))||
334 (strchr(mode,'r')&&strchr(mode,'w'))||
335 (strchr(mode,'w')&&strchr(mode,'a')))
340 access = GENERIC_READ;
341 creation = OPEN_EXISTING;
342 if (mode[1] == '+') access |= GENERIC_WRITE;
344 else if (mode[0] == 'w')
346 access = GENERIC_WRITE;
347 creation = CREATE_ALWAYS;
348 if (mode[1] == '+') access |= GENERIC_READ;
350 else if (mode[0] == 'a')
352 /* FIXME: there is no O_APPEND in CreateFile, should emulate it */
353 access = GENERIC_WRITE;
354 creation = OPEN_ALWAYS;
355 if (mode[1] == '+') access |= GENERIC_READ;
357 /* FIXME: should handle text/binary mode */
359 if ((handle = CreateFileA( path, access, FILE_SHARE_READ | FILE_SHARE_WRITE,
360 NULL, creation, FILE_ATTRIBUTE_NORMAL,
361 -1 )) != INVALID_HANDLE_VALUE)
363 file = HeapAlloc( GetProcessHeap(), 0, sizeof(*file) );
364 file->handle = handle;
366 TRACE("file %s mode %s got handle %d file %p\n",
367 path,mode,handle,file);
371 /*********************************************************************
374 DWORD __cdecl CRTDLL_fread(LPVOID ptr, INT size, INT nmemb, CRTDLL_FILE *file)
380 /* If we would honour CR/LF <-> LF translation, we could do it like this.
381 We should keep track of all files opened, and probably files with \
382 known binary extensions must be unchanged */
383 while ( (i < (nmemb*size)) && (ret==1)) {
384 ret=fread(temp,1,1,file);
385 TRACE("got %c 0x%02x ret %d\n",
386 (isalpha(*(unsigned char*)temp))? *(unsigned char*)temp:
387 ' ',*(unsigned char*)temp, ret);
388 if (*(unsigned char*)temp != 0xd) { /* skip CR */
393 TRACE("skipping ^M\n");
395 TRACE("0x%08x items of size %d from file %p to %p\n",
396 nmemb,size,file,ptr,);
404 TRACE("0x%08x items of size %d from file %p to %p\n",
405 nmemb,size,file,ptr);
406 if (!ReadFile( file->handle, ptr, size * nmemb, &ret, NULL ))
412 /*********************************************************************
413 * freopen (CRTDLL.379)
418 DWORD __cdecl CRTDLL_freopen(LPCSTR path, LPCSTR mode, LPVOID stream)
420 FIXME(":(%s,%s,%p): stub\n", path, mode, stream);
421 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
425 /*********************************************************************
426 * fscanf (CRTDLL.381)
428 INT __cdecl CRTDLL_fscanf( CRTDLL_FILE *stream, LPSTR format, ... )
434 va_start( valist, format );
436 res = vfscanf( xlat_file_ptr(stream), format, valist );
445 /*********************************************************************
448 LONG __cdecl CRTDLL_fseek( CRTDLL_FILE *file, LONG offset, INT whence)
450 TRACE("file %p to 0x%08lx pos %s\n",
451 file,offset,(whence==SEEK_SET)?"SEEK_SET":
452 (whence==SEEK_CUR)?"SEEK_CUR":
453 (whence==SEEK_END)?"SEEK_END":"UNKNOWN");
454 if (SetFilePointer( file->handle, offset, NULL, whence ) != 0xffffffff)
460 /*********************************************************************
461 * fsetpos (CRTDLL.383)
463 INT __cdecl CRTDLL_fsetpos( CRTDLL_FILE *file, INT *pos )
465 TRACE("file %p pos %d\n", file, *pos );
466 return CRTDLL_fseek(file, *pos, SEEK_SET);
469 /*********************************************************************
472 LONG __cdecl CRTDLL_ftell( CRTDLL_FILE *file )
474 return SetFilePointer( file->handle, 0, NULL, SEEK_CUR );
477 /*********************************************************************
478 * fwrite (CRTDLL.386)
480 DWORD __cdecl CRTDLL_fwrite( LPVOID ptr, INT size, INT nmemb, CRTDLL_FILE *file )
484 TRACE("0x%08x items of size %d to file %p(%d) from %p\n",
485 nmemb,size,file,file-(CRTDLL_FILE*)CRTDLL_iob,ptr);
488 if (!WriteFile( file->handle, ptr, size * nmemb, &ret, NULL ))
493 /*********************************************************************
494 * setbuf (CRTDLL.452)
496 INT __cdecl CRTDLL_setbuf(CRTDLL_FILE *file, LPSTR buf)
498 TRACE("(file %p buf %p)\n", file, buf);
499 /* this doesn't work:"void value not ignored as it ought to be"
500 return setbuf(file,buf);
502 /* FIXME: no buffering for now */
506 /*********************************************************************
507 * _open_osfhandle (CRTDLL.240)
509 HFILE __cdecl CRTDLL__open_osfhandle(LONG osfhandle, INT flags)
514 case STD_INPUT_HANDLE :
518 case STD_OUTPUT_HANDLE:
522 case STD_ERROR_HANDLE:
529 TRACE("(handle %08lx,flags %d) return %d\n",
530 osfhandle,flags,handle);
535 /*********************************************************************
538 void __cdecl CRTDLL_srand(DWORD seed)
540 /* FIXME: should of course be thread? process? local */
544 /*********************************************************************
545 * vfprintf (CRTDLL.373)
547 INT __cdecl CRTDLL_vfprintf( CRTDLL_FILE *file, LPSTR format, va_list args )
549 char buffer[2048]; /* FIXME... */
551 vsprintf( buffer, format, args );
552 return CRTDLL_fwrite( buffer, 1, strlen(buffer), file );
555 /*********************************************************************
556 * fprintf (CRTDLL.373)
558 INT __cdecl CRTDLL_fprintf( CRTDLL_FILE *file, LPSTR format, ... )
563 va_start( valist, format );
564 res = CRTDLL_vfprintf( file, format, valist );
569 /*********************************************************************
572 time_t __cdecl CRTDLL_time(time_t *timeptr)
574 time_t curtime = time(NULL);
581 /*********************************************************************
584 clock_t __cdecl CRTDLL_clock(void)
590 res = alltimes.tms_utime + alltimes.tms_stime+
591 alltimes.tms_cutime + alltimes.tms_cstime;
592 /* Fixme: We need some symbolic representation
593 for (Hostsystem_)CLOCKS_PER_SEC
594 and (Emulated_system_)CLOCKS_PER_SEC
595 10 holds only for Windows/Linux_i86)
600 /*********************************************************************
601 * _isatty (CRTDLL.137)
603 BOOL __cdecl CRTDLL__isatty(DWORD x)
609 /*********************************************************************
610 * _write (CRTDLL.332)
612 INT __cdecl CRTDLL__write(INT fd,LPCVOID buf,UINT count)
619 len = (UINT)write(fd,buf,(LONG)count);
621 len = _lwrite(fd,buf,count);
622 TRACE("%d/%d byte to dfh %d from %p,\n",
628 /*********************************************************************
631 * FIXME: What the heck is the difference between
632 * FIXME _c_exit (CRTDLL.47)
633 * FIXME _cexit (CRTDLL.49)
634 * FIXME _exit (CRTDLL.87)
635 * FIXME exit (CRTDLL.359)
637 * atexit-processing comes to mind -- MW.
640 void __cdecl CRTDLL__cexit(INT ret)
647 /*********************************************************************
650 void __cdecl CRTDLL_exit(DWORD ret)
652 TRACE("(%ld)\n",ret);
657 /*********************************************************************
658 * _abnormal_termination (CRTDLL.36)
660 INT __cdecl CRTDLL__abnormal_termination(void)
667 /*********************************************************************
668 * _access (CRTDLL.37)
670 INT __cdecl CRTDLL__access(LPCSTR filename, INT mode)
672 DWORD attr = GetFileAttributesA(filename);
676 if (GetLastError() == ERROR_INVALID_ACCESS)
683 if ((attr & FILE_ATTRIBUTE_READONLY) && (mode & W_OK))
693 /*********************************************************************
694 * fflush (CRTDLL.365)
696 INT __cdecl CRTDLL_fflush( CRTDLL_FILE *file )
698 return FlushFileBuffers( file->handle ) ? 0 : -1;
702 /*********************************************************************
705 INT __cdecl CRTDLL_rand()
707 return (rand() & CRTDLL_RAND_MAX);
711 /*********************************************************************
712 * putchar (CRTDLL.442)
714 void __cdecl CRTDLL_putchar( INT x )
720 /*********************************************************************
723 INT __cdecl CRTDLL_fputc( INT c, CRTDLL_FILE *file )
727 TRACE("%c to file %p\n",c,file);
728 if (!WriteFile( file->handle, &ch, 1, &res, NULL )) return -1;
733 /*********************************************************************
736 INT __cdecl CRTDLL_fputs( LPCSTR s, CRTDLL_FILE *file )
739 TRACE("%s to file %p\n",s,file);
740 if (!WriteFile( file->handle, s, strlen(s), &res, NULL )) return -1;
745 /*********************************************************************
748 INT __cdecl CRTDLL_puts(LPCSTR s)
755 /*********************************************************************
758 INT __cdecl CRTDLL_putc( INT c, CRTDLL_FILE *file )
760 return CRTDLL_fputc( c, file );
763 /*********************************************************************
766 INT __cdecl CRTDLL_fgetc( CRTDLL_FILE *file )
770 if (!ReadFile( file->handle, &ch, 1, &res, NULL )) return -1;
771 if (res != 1) return -1;
776 /*********************************************************************
779 INT __cdecl CRTDLL_getc( CRTDLL_FILE *file )
781 return CRTDLL_fgetc( file );
785 /*********************************************************************
788 CHAR* __cdecl CRTDLL_fgets( LPSTR s, INT size, CRTDLL_FILE *file )
793 /* BAD, for the whole WINE process blocks... just done this way to test
794 * windows95's ftp.exe.
797 for(cc = CRTDLL_fgetc(file); cc != EOF && cc != '\n'; cc = CRTDLL_fgetc(file))
800 if (--size <= 0) break;
806 TRACE("got '%s'\n", buf_start);
811 /*********************************************************************
814 LPSTR __cdecl CRTDLL_gets(LPSTR buf)
817 LPSTR buf_start = buf;
819 /* BAD, for the whole WINE process blocks... just done this way to test
820 * windows95's ftp.exe.
823 for(cc = fgetc(stdin); cc != EOF && cc != '\n'; cc = fgetc(stdin))
824 if(cc != '\r') *buf++ = (char)cc;
828 TRACE("got '%s'\n", buf_start);
833 /*********************************************************************
836 UINT __cdecl CRTDLL__rotl(UINT x,INT shift)
838 unsigned int ret = (x >> shift)|( x >>((sizeof(x))-shift));
840 TRACE("got 0x%08x rot %d ret 0x%08x\n",
845 /*********************************************************************
846 * _lrotl (CRTDLL.176)
848 DWORD __cdecl CRTDLL__lrotl(DWORD x,INT shift)
850 unsigned long ret = (x >> shift)|( x >>((sizeof(x))-shift));
852 TRACE("got 0x%08lx rot %d ret 0x%08lx\n",
859 /*********************************************************************
860 * _mbsicmp (CRTDLL.204)
862 int __cdecl CRTDLL__mbsicmp(unsigned char *x,unsigned char *y)
869 /* FIXME: MBCS handling... */
878 /*********************************************************************
879 * vsprintf (CRTDLL.500)
881 INT __cdecl CRTDLL_vsprintf( LPSTR buffer, LPCSTR spec, va_list args )
883 return wvsprintfA( buffer, spec, args );
886 /*********************************************************************
887 * vswprintf (CRTDLL.501)
889 INT __cdecl CRTDLL_vswprintf( LPWSTR buffer, LPCWSTR spec, va_list args )
891 return wvsprintfW( buffer, spec, args );
894 /*********************************************************************
895 * _strcmpi (CRTDLL.282) (CRTDLL.287)
897 INT __cdecl CRTDLL__strcmpi( LPCSTR s1, LPCSTR s2 )
899 return lstrcmpiA( s1, s2 );
903 /*********************************************************************
904 * _strnicmp (CRTDLL.293)
906 INT __cdecl CRTDLL__strnicmp( LPCSTR s1, LPCSTR s2, INT n )
908 return lstrncmpiA( s1, s2, n );
912 /*********************************************************************
913 * _strlwr (CRTDLL.293)
915 * convert a string in place to lowercase
917 LPSTR __cdecl CRTDLL__strlwr(LPSTR x)
921 TRACE("CRTDLL_strlwr got %s\n", x);
923 if ((*y > 0x40) && (*y< 0x5b))
927 TRACE(" returned %s\n", x);
932 /*********************************************************************
933 * system (CRTDLL.485)
935 INT __cdecl CRTDLL_system(LPSTR x)
937 #define SYSBUF_LENGTH 1500
938 char buffer[SYSBUF_LENGTH];
939 unsigned char *y = x;
943 sprintf( buffer, "%s \"", Options.argv0 );
944 bp = buffer + strlen(buffer);
945 i = strlen(buffer) + strlen(x) +2;
947 /* Calculate needed buffer size to prevent overflow. */
952 /* If buffer too short, exit. */
953 if (i > SYSBUF_LENGTH) {
954 TRACE("_system buffer to small\n");
963 if (*(y-1) =='\\') *bp++ = '\\';
965 /* Remove spaces from end of string. */
966 while (*(y-1) == ' ') {
971 TRACE("_system got '%s', executing '%s'\n",x,buffer);
973 return system(buffer);
976 /*********************************************************************
977 * _strupr (CRTDLL.300)
979 LPSTR __cdecl CRTDLL__strupr(LPSTR x)
990 /*********************************************************************
991 * longjmp (CRTDLL.426)
993 VOID __cdecl CRTDLL_longjmp(jmp_buf env, int val)
995 FIXME("CRTDLL_longjmp semistup, expect crash\n");
999 /*********************************************************************
1000 * malloc (CRTDLL.427)
1002 VOID* __cdecl CRTDLL_malloc(DWORD size)
1004 return HeapAlloc(GetProcessHeap(),0,size);
1007 /*********************************************************************
1010 VOID* __cdecl CRTDLL_new(DWORD size)
1013 if(!(result = HeapAlloc(GetProcessHeap(),0,size)) && new_handler)
1018 /*********************************************************************
1019 * set_new_handler(CRTDLL.003)
1021 new_handler_type __cdecl CRTDLL_set_new_handler(new_handler_type func)
1023 new_handler_type old_handler = new_handler;
1028 /*********************************************************************
1029 * calloc (CRTDLL.350)
1031 VOID* __cdecl CRTDLL_calloc(DWORD size, DWORD count)
1033 return HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, size * count );
1036 /*********************************************************************
1037 * realloc (CRTDLL.447)
1039 VOID* __cdecl CRTDLL_realloc( VOID *ptr, DWORD size )
1041 return HeapReAlloc( GetProcessHeap(), 0, ptr, size );
1044 /*********************************************************************
1047 VOID __cdecl CRTDLL_free(LPVOID ptr)
1049 HeapFree(GetProcessHeap(),0,ptr);
1052 /*********************************************************************
1053 * delete (CRTDLL.002)
1055 VOID __cdecl CRTDLL_delete(VOID* ptr)
1057 HeapFree(GetProcessHeap(),0,ptr);
1060 /*********************************************************************
1061 * _strdup (CRTDLL.285)
1063 LPSTR __cdecl CRTDLL__strdup(LPCSTR ptr)
1065 return HEAP_strdupA(GetProcessHeap(),0,ptr);
1068 /*********************************************************************
1069 * fclose (CRTDLL.362)
1071 INT __cdecl CRTDLL_fclose( CRTDLL_FILE *file )
1073 TRACE("%p\n", file );
1074 if (!CloseHandle( file->handle )) return -1;
1075 HeapFree( GetProcessHeap(), 0, file );
1079 /*********************************************************************
1080 * _unlink (CRTDLL.315)
1082 INT __cdecl CRTDLL__unlink(LPCSTR pathname)
1085 DOS_FULL_NAME full_name;
1087 if (!DOSFS_GetFullName( pathname, FALSE, &full_name )) {
1088 WARN("CRTDLL_unlink file %s bad name\n",pathname);
1092 ret=unlink(full_name.long_name);
1093 TRACE("(%s unix %s)\n",
1094 pathname,full_name.long_name);
1101 /*********************************************************************
1102 * rename (CRTDLL.449)
1104 INT __cdecl CRTDLL_rename(LPCSTR oldpath,LPCSTR newpath)
1106 BOOL ok = MoveFileExA( oldpath, newpath, MOVEFILE_REPLACE_EXISTING );
1111 /*********************************************************************
1112 * _stat (CRTDLL.280)
1130 int __cdecl CRTDLL__stat(const char * filename, struct win_stat * buf)
1133 DOS_FULL_NAME full_name;
1136 if (!DOSFS_GetFullName( filename, TRUE, &full_name ))
1138 WARN("CRTDLL__stat filename %s bad name\n",filename);
1141 ret=stat(full_name.long_name,&mystat);
1142 TRACE("CRTDLL__stat %s\n", filename);
1146 /* FIXME: should check what Windows returns */
1148 buf->win_st_dev = mystat.st_dev;
1149 buf->win_st_ino = mystat.st_ino;
1150 buf->win_st_mode = mystat.st_mode;
1151 buf->win_st_nlink = mystat.st_nlink;
1152 buf->win_st_uid = mystat.st_uid;
1153 buf->win_st_gid = mystat.st_gid;
1154 buf->win_st_rdev = mystat.st_rdev;
1155 buf->win_st_size = mystat.st_size;
1156 buf->win_st_atime = mystat.st_atime;
1157 buf->win_st_mtime = mystat.st_mtime;
1158 buf->win_st_ctime = mystat.st_ctime;
1162 /*********************************************************************
1163 * _open (CRTDLL.239)
1165 HFILE __cdecl CRTDLL__open(LPCSTR path,INT flags)
1167 DWORD access = 0, creation = 0;
1171 the flags in lcc's header differ from the ones in Linux, e.g.
1172 Linux: define O_APPEND 02000 (= 0x400)
1173 lcc: define _O_APPEND 0x0008
1174 so here a scheme to translate them
1175 Probably lcc is wrong here, but at least a hack to get is going
1179 case O_RDONLY: access |= GENERIC_READ; break;
1180 case O_WRONLY: access |= GENERIC_WRITE; break;
1181 case O_RDWR: access |= GENERIC_WRITE | GENERIC_READ; break;
1184 if (flags & 0x0100) /* O_CREAT */
1186 if (flags & 0x0400) /* O_EXCL */
1187 creation = CREATE_NEW;
1188 else if (flags & 0x0200) /* O_TRUNC */
1189 creation = CREATE_ALWAYS;
1191 creation = OPEN_ALWAYS;
1193 else /* no O_CREAT */
1195 if (flags & 0x0200) /* O_TRUNC */
1196 creation = TRUNCATE_EXISTING;
1198 creation = OPEN_EXISTING;
1200 if (flags & 0x0008) /* O_APPEND */
1201 FIXME("O_APPEND not supported\n" );
1203 TRACE("CRTDLL_open file unsupported flags 0x%04x\n",flags);
1206 ret = CreateFileA( path, access, FILE_SHARE_READ | FILE_SHARE_WRITE,
1207 NULL, creation, FILE_ATTRIBUTE_NORMAL, -1 );
1208 TRACE("CRTDLL_open file %s mode 0x%04x got handle %d\n", path,flags,ret);
1212 /*********************************************************************
1213 * _close (CRTDLL.57)
1215 INT __cdecl CRTDLL__close(HFILE fd)
1217 int ret=_lclose(fd);
1226 /*********************************************************************
1229 INT __cdecl CRTDLL_feof( CRTDLL_FILE *file )
1235 /*********************************************************************
1236 * setlocale (CRTDLL.453)
1238 LPSTR __cdecl CRTDLL_setlocale(INT category,LPCSTR locale)
1243 case CRTDLL_LC_ALL: categorystr="LC_ALL";break;
1244 case CRTDLL_LC_COLLATE: categorystr="LC_COLLATE";break;
1245 case CRTDLL_LC_CTYPE: categorystr="LC_CTYPE";break;
1246 case CRTDLL_LC_MONETARY: categorystr="LC_MONETARY";break;
1247 case CRTDLL_LC_NUMERIC: categorystr="LC_NUMERIC";break;
1248 case CRTDLL_LC_TIME: categorystr="LC_TIME";break;
1249 default: categorystr = "UNKNOWN?";break;
1251 FIXME("(%s,%s),stub!\n",categorystr,locale);
1255 /*********************************************************************
1256 * _setmode (CRTDLL.265)
1257 * FIXME: At present we ignore the request to translate CR/LF to LF.
1259 * We allways translate when we read with fgets, we never do with fread
1262 INT __cdecl CRTDLL__setmode( INT fh,INT mode)
1265 #define O_TEXT 0x4000
1266 #define O_BINARY 0x8000
1268 FIXME("on fhandle %d mode %s, STUB.\n",
1269 fh,(mode=O_TEXT)?"O_TEXT":
1270 (mode=O_BINARY)?"O_BINARY":"UNKNOWN");
1274 /*********************************************************************
1275 * _fpreset (CRTDLL.107)
1277 VOID __cdecl CRTDLL__fpreset(void)
1282 /*********************************************************************
1283 * atexit (CRTDLL.345)
1285 INT __cdecl CRTDLL_atexit(LPVOID x)
1287 FIXME("(%p), STUB.\n",x);
1288 return 0; /* successful */
1291 /*********************************************************************
1292 * _isctype (CRTDLL.138)
1294 BOOL __cdecl CRTDLL__isctype(CHAR x,CHAR type)
1296 if ((type & CRTDLL_SPACE) && isspace(x))
1298 if ((type & CRTDLL_PUNCT) && ispunct(x))
1300 if ((type & CRTDLL_LOWER) && islower(x))
1302 if ((type & CRTDLL_UPPER) && isupper(x))
1304 if ((type & CRTDLL_ALPHA) && isalpha(x))
1306 if ((type & CRTDLL_DIGIT) && isdigit(x))
1308 if ((type & CRTDLL_CONTROL) && iscntrl(x))
1310 /* check CRTDLL_LEADBYTE */
1314 /*********************************************************************
1315 * _chdrive (CRTDLL.52)
1317 * newdir [I] drive to change to, A=1
1320 BOOL __cdecl CRTDLL__chdrive(INT newdrive)
1322 /* FIXME: generates errnos */
1323 return DRIVE_SetCurrentDrive(newdrive-1);
1326 /*********************************************************************
1327 * _chdir (CRTDLL.51)
1329 INT __cdecl CRTDLL__chdir(LPCSTR newdir)
1331 if (!SetCurrentDirectoryA(newdir))
1336 /*********************************************************************
1337 * _fullpath (CRTDLL.114)
1339 LPSTR __cdecl CRTDLL__fullpath(LPSTR buf, LPCSTR name, INT size)
1341 DOS_FULL_NAME full_name;
1346 if(!(buf = CRTDLL_malloc(size))) return NULL;
1348 if (!DOSFS_GetFullName( name, FALSE, &full_name )) return NULL;
1349 lstrcpynA(buf,full_name.short_name,size);
1350 TRACE("CRTDLL_fullpath got %s\n",buf);
1354 /*********************************************************************
1355 * _splitpath (CRTDLL.279)
1357 VOID __cdecl CRTDLL__splitpath(LPCSTR path, LPSTR drive, LPSTR directory, LPSTR filename, LPSTR extension )
1360 directory includes leading and trailing (forward and backward slashes)
1361 filename without dot and slashes
1362 extension with leading dot
1364 char * drivechar,*dirchar,*namechar;
1366 TRACE("CRTDLL__splitpath got %s\n",path);
1368 drivechar = strchr(path,':');
1369 dirchar = strrchr(path,'/');
1370 namechar = strrchr(path,'\\');
1371 dirchar = MAX(dirchar,namechar);
1373 namechar = strrchr(dirchar,'.');
1375 namechar = strrchr(path,'.');
1383 strncat(drive,path,drivechar-path+1);
1392 strncat(directory,path,dirchar-path+1);
1401 strncat(filename,path,namechar-path);
1405 strcat(extension,namechar);
1410 TRACE("CRTDLL__splitpath found %s %s %s %s\n",drive,directory,filename,extension);
1415 /*********************************************************************
1416 * _makepath (CRTDLL.182)
1419 VOID __cdecl CRTDLL__makepath(LPSTR path, LPCSTR drive,
1420 LPCSTR directory, LPCSTR filename,
1424 TRACE("CRTDLL__makepath got %s %s %s %s\n", drive, directory,
1425 filename, extension);
1433 sprintf(path, "%c:", drive[0]);
1436 if ( directory[0] ) {
1437 strcat(path, directory);
1438 ch = path[strlen(path)-1];
1439 if (ch != '/' && ch != '\\')
1443 if ( filename[0] ) {
1444 strcat(path, filename);
1446 if ( extension[0] ) {
1447 if ( extension[0] != '.' ) {
1450 strcat(path,extension);
1455 TRACE("CRTDLL__makepath returns %s\n",path);
1458 /*********************************************************************
1459 * _getcwd (CRTDLL.120)
1461 CHAR* __cdecl CRTDLL__getcwd(LPSTR buf, INT size)
1468 if (size < 0) /* allocate as big as nescessary */
1469 len =GetCurrentDirectoryA(1,test) + 1;
1470 if(!(buf = CRTDLL_malloc(len)))
1472 /* set error to OutOfRange */
1477 if(!(len =GetCurrentDirectoryA(len,buf)))
1483 /* set error to ERANGE */
1484 TRACE("CRTDLL_getcwd buffer to small\n");
1491 /*********************************************************************
1492 * _getdcwd (CRTDLL.121)
1494 CHAR* __cdecl CRTDLL__getdcwd(INT drive,LPSTR buf, INT size)
1499 FIXME("(\"%c:\",%s,%d)\n",drive+'A',buf,size);
1502 if (size < 0) /* allocate as big as nescessary */
1503 len =GetCurrentDirectoryA(1,test) + 1;
1504 if(!(buf = CRTDLL_malloc(len)))
1506 /* set error to OutOfRange */
1511 if(!(len =GetCurrentDirectoryA(len,buf)))
1517 /* set error to ERANGE */
1518 TRACE("buffer to small\n");
1525 /*********************************************************************
1526 * _getdrive (CRTDLL.124)
1528 * Return current drive, 1 for A, 2 for B
1530 INT __cdecl CRTDLL__getdrive(VOID)
1532 return DRIVE_GetCurrentDrive() + 1;
1535 /*********************************************************************
1536 * _mkdir (CRTDLL.234)
1538 INT __cdecl CRTDLL__mkdir(LPCSTR newdir)
1540 if (!CreateDirectoryA(newdir,NULL))
1545 /*********************************************************************
1546 * remove (CRTDLL.448)
1548 INT __cdecl CRTDLL_remove(LPCSTR file)
1550 if (!DeleteFileA(file))
1555 /*********************************************************************
1556 * _errno (CRTDLL.52)
1557 * Yes, this is a function.
1559 LPINT __cdecl CRTDLL__errno()
1561 static int crtdllerrno;
1563 /* FIXME: we should set the error at the failing function call time */
1564 crtdllerrno = LastErrorToErrno(GetLastError());
1565 return &crtdllerrno;
1568 /*********************************************************************
1569 * _tempnam (CRTDLL.305)
1572 LPSTR __cdecl CRTDLL__tempnam(LPCSTR dir, LPCSTR prefix)
1576 DOS_FULL_NAME tempname;
1578 if ((ret = tempnam(dir,prefix))==NULL) {
1579 WARN("Unable to get unique filename\n");
1582 if (!DOSFS_GetFullName(ret,FALSE,&tempname))
1584 TRACE("Wrong path?\n");
1588 if ((ret = CRTDLL_malloc(strlen(tempname.short_name)+1)) == NULL) {
1589 WARN("CRTDL_malloc for shortname failed\n");
1592 if ((ret = strcpy(ret,tempname.short_name)) == NULL) {
1593 WARN("Malloc for shortname failed\n");
1597 TRACE("dir %s prefix %s got %s\n",
1602 /*********************************************************************
1603 * tmpnam (CRTDLL.490)
1605 * lcclnk from lcc-win32 relies on a terminating dot in the name returned
1608 LPSTR __cdecl CRTDLL_tmpnam(LPSTR s)
1612 if ((ret =tmpnam(s))== NULL) {
1613 WARN("Unable to get unique filename\n");
1616 if (!DOSFS_GetFullName(ret,FALSE,&CRTDLL_tmpname))
1618 TRACE("Wrong path?\n");
1621 strcat(CRTDLL_tmpname.short_name,".");
1622 TRACE("for buf %p got %s\n",
1623 s,CRTDLL_tmpname.short_name);
1624 TRACE("long got %s\n",
1625 CRTDLL_tmpname.long_name);
1627 return strcpy(s,CRTDLL_tmpname.short_name);
1629 return CRTDLL_tmpname.short_name;
1633 /*********************************************************************
1634 * _itoa (CRTDLL.165)
1636 LPSTR __cdecl CRTDLL__itoa(INT x,LPSTR buf,INT buflen)
1638 wsnprintfA(buf,buflen,"%d",x);
1642 /*********************************************************************
1643 * _ltoa (CRTDLL.180)
1645 LPSTR __cdecl CRTDLL__ltoa(long x,LPSTR buf,INT radix)
1648 case 2: FIXME("binary format not implemented !\n");
1650 case 8: wsnprintfA(buf,0x80,"%o",x);
1652 case 10: wsnprintfA(buf,0x80,"%d",x);
1654 case 16: wsnprintfA(buf,0x80,"%x",x);
1656 default: FIXME("radix %d not implemented !\n", radix);
1661 /*********************************************************************
1662 * _ultoa (CRTDLL.311)
1664 LPSTR __cdecl CRTDLL__ultoa(long x,LPSTR buf,INT radix)
1667 case 2: FIXME("binary format not implemented !\n");
1669 case 8: wsnprintfA(buf,0x80,"%lo",x);
1671 case 10: wsnprintfA(buf,0x80,"%ld",x);
1673 case 16: wsnprintfA(buf,0x80,"%lx",x);
1675 default: FIXME("radix %d not implemented !\n", radix);
1680 typedef VOID (*sig_handler_type)(VOID);
1682 /*********************************************************************
1683 * signal (CRTDLL.455)
1685 VOID __cdecl CRTDLL_signal(int sig, sig_handler_type ptr)
1687 FIXME("(%d %p):stub.\n", sig, ptr);
1690 /*********************************************************************
1691 * _ftol (CRTDLL.113)
1693 #ifdef USING_REAL_FPU
1694 LONG __cdecl CRTDLL__ftol(void) {
1695 /* don't just do DO_FPU("fistp",retval), because the rounding
1696 * mode must also be set to "round towards zero"... */
1702 LONG __cdecl CRTDLL__ftol(double fl) {
1703 FIXME("should be register function\n");
1708 /*********************************************************************
1709 * _CIpow (CRTDLL.14)
1711 #ifdef USING_REAL_FPU
1712 LONG __cdecl CRTDLL__CIpow(void) {
1719 LONG __cdecl CRTDLL__CIpow(double x,double y) {
1720 FIXME("should be register function\n");
1725 /*********************************************************************
1726 * _sleep (CRTDLL.267)
1728 VOID __cdecl CRTDLL__sleep(unsigned long timeout)
1730 TRACE("CRTDLL__sleep for %ld milliseconds\n",timeout);
1731 Sleep((timeout)?timeout:1);
1734 /*********************************************************************
1735 * getenv (CRTDLL.437)
1737 LPSTR __cdecl CRTDLL_getenv(const char *name)
1739 LPSTR environ = GetEnvironmentStringsA();
1740 LPSTR pp,pos = NULL;
1741 unsigned int length;
1743 for (pp = environ; (*pp); pp = pp + strlen(pp) +1)
1745 pos =strchr(pp,'=');
1749 length = strlen(pp);
1750 if (!strncmp(pp,name,length)) break;
1755 TRACE("got %s\n",pp);
1757 FreeEnvironmentStringsA( environ );
1761 /*********************************************************************
1762 * _mbsrchr (CRTDLL.223)
1764 LPSTR __cdecl CRTDLL__mbsrchr(LPSTR s,CHAR x) {
1765 /* FIXME: handle multibyte strings */
1766 return strrchr(s,x);
1769 /*********************************************************************
1770 * _memicmp (CRTDLL.233)(NTDLL.868)
1771 * A stringcompare, without \0 check
1773 * -1:if first string is alphabetically before second string
1774 * 1:if second '' '' '' '' first ''
1775 * 0:if both are equal.
1777 INT __cdecl CRTDLL__memicmp(
1778 LPCSTR s1, /* [in] first string */
1779 LPCSTR s2, /* [in] second string */
1780 DWORD len /* [in] length to compare */
1784 for (i=0;i<len;i++) {
1785 if (tolower(s1[i])<tolower(s2[i]))
1787 if (tolower(s1[i])>tolower(s2[i]))
1792 /*********************************************************************
1793 * __dllonexit (CRTDLL.25)
1795 VOID __cdecl CRTDLL__dllonexit ()
1800 /*********************************************************************
1801 * wcstol (CRTDLL.520)
1802 * Like strtol, but for wide character strings.
1804 INT __cdecl CRTDLL_wcstol(LPWSTR s,LPWSTR *end,INT base) {
1805 LPSTR sA = HEAP_strdupWtoA(GetProcessHeap(),0,s),endA;
1806 INT ret = strtol(sA,&endA,base);
1808 HeapFree(GetProcessHeap(),0,sA);
1809 if (end) *end = s+(endA-sA); /* pointer magic checked. */
1812 /*********************************************************************
1813 * strdate (CRTDLL.283)
1815 LPSTR __cdecl CRTDLL__strdate (LPSTR date)
1816 { FIXME("%p stub\n", date);
1820 /*********************************************************************
1821 * strtime (CRTDLL.299)
1823 LPSTR __cdecl CRTDLL__strtime (LPSTR date)
1824 { FIXME("%p stub\n", date);
1828 /*********************************************************************
1829 * _except_handler2 (CRTDLL.78)
1831 INT __cdecl CRTDLL__except_handler2 (
1832 PEXCEPTION_RECORD rec,
1833 PEXCEPTION_FRAME frame,
1835 PEXCEPTION_FRAME *dispatcher)
1837 FIXME ("exception %lx flags=%lx at %p handler=%p %p %p stub\n",
1838 rec->ExceptionCode, rec->ExceptionFlags, rec->ExceptionAddress,
1839 frame->Handler, context, dispatcher);
1840 return ExceptionContinueSearch;