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
37 #include <sys/times.h>
50 #include "debugtools.h"
59 DEFAULT_DEBUG_CHANNEL(crtdll);
61 /* windows.h RAND_MAX is smaller than normal RAND_MAX */
62 #define CRTDLL_RAND_MAX 0x7fff
64 static DOS_FULL_NAME CRTDLL_tmpname;
66 UINT CRTDLL_argc_dll; /* CRTDLL.23 */
67 LPSTR *CRTDLL_argv_dll; /* CRTDLL.24 */
68 LPSTR CRTDLL_acmdln_dll; /* CRTDLL.38 */
69 UINT CRTDLL_basemajor_dll; /* CRTDLL.42 */
70 UINT CRTDLL_baseminor_dll; /* CRTDLL.43 */
71 UINT CRTDLL_baseversion_dll; /* CRTDLL.44 */
72 UINT CRTDLL_commode_dll; /* CRTDLL.59 */
73 LPSTR CRTDLL_environ_dll; /* CRTDLL.75 */
74 UINT CRTDLL_fmode_dll; /* CRTDLL.104 */
75 UINT CRTDLL_osmajor_dll; /* CRTDLL.241 */
76 UINT CRTDLL_osminor_dll; /* CRTDLL.242 */
77 UINT CRTDLL_osmode_dll; /* CRTDLL.243 */
78 UINT CRTDLL_osver_dll; /* CRTDLL.244 */
79 UINT CRTDLL_osversion_dll; /* CRTDLL.245 */
80 UINT CRTDLL_winmajor_dll; /* CRTDLL.329 */
81 UINT CRTDLL_winminor_dll; /* CRTDLL.330 */
82 UINT CRTDLL_winver_dll; /* CRTDLL.331 */
84 /* FIXME: structure layout is obviously not correct */
91 CRTDLL_FILE CRTDLL_iob[3];
93 static CRTDLL_FILE * const CRTDLL_stdin = &CRTDLL_iob[0];
94 static CRTDLL_FILE * const CRTDLL_stdout = &CRTDLL_iob[1];
95 static CRTDLL_FILE * const CRTDLL_stderr = &CRTDLL_iob[2];
97 typedef VOID (*new_handler_type)(VOID);
99 static new_handler_type new_handler;
101 #if defined(__GNUC__) && defined(__i386__)
102 #define USING_REAL_FPU
103 #define DO_FPU(x,y) __asm__ __volatile__( x " %0;fwait" : "=m" (y) : )
104 #define POP_FPU(x) DO_FPU("fstpl",x)
107 CRTDLL_FILE * __cdecl CRTDLL__fdopen(INT handle, LPCSTR mode);
109 /*********************************************************************
110 * CRTDLL_MainInit (CRTDLL.init)
112 BOOL WINAPI CRTDLL_Init(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
114 TRACE("(0x%08x,%ld,%p)\n",hinstDLL,fdwReason,lpvReserved);
116 if (fdwReason == DLL_PROCESS_ATTACH) {
117 CRTDLL__fdopen(0,"r");
118 CRTDLL__fdopen(1,"w");
119 CRTDLL__fdopen(2,"w");
124 /*********************************************************************
125 * _GetMainArgs (CRTDLL.022)
127 LPSTR * __cdecl CRTDLL__GetMainArgs(LPDWORD argc,LPSTR **argv,
128 LPSTR *environ,DWORD flag)
132 int xargc,end,last_arg,afterlastspace;
135 TRACE("(%p,%p,%p,%ld).\n",
136 argc,argv,environ,flag
139 if (CRTDLL_acmdln_dll != NULL)
140 HeapFree(GetProcessHeap(), 0, CRTDLL_acmdln_dll);
142 CRTDLL_acmdln_dll = cmdline = HEAP_strdupA( GetProcessHeap(), 0,
144 TRACE("got '%s'\n", cmdline);
146 version = GetVersion();
147 CRTDLL_osver_dll = version >> 16;
148 CRTDLL_winminor_dll = version & 0xFF;
149 CRTDLL_winmajor_dll = (version>>8) & 0xFF;
150 CRTDLL_baseversion_dll = version >> 16;
151 CRTDLL_winver_dll = ((version >> 8) & 0xFF) + ((version & 0xFF) << 8);
152 CRTDLL_baseminor_dll = (version >> 16) & 0xFF;
153 CRTDLL_basemajor_dll = (version >> 24) & 0xFF;
154 CRTDLL_osversion_dll = version & 0xFFFF;
155 CRTDLL_osminor_dll = version & 0xFF;
156 CRTDLL_osmajor_dll = (version>>8) & 0xFF;
158 /* missing threading init */
160 end=0;last_arg=0;xargv=NULL;xargc=0;afterlastspace=0;
163 if ((cmdline[end]==' ') || (cmdline[end]=='\0'))
165 if (cmdline[end]=='\0')
169 /* alloc xargc + NULL entry */
170 xargv=(char**)HeapReAlloc( GetProcessHeap(), 0, xargv,
171 sizeof(char*)*(xargc+1));
172 if (strlen(cmdline+afterlastspace))
174 xargv[xargc] = HEAP_strdupA( GetProcessHeap(), 0,
175 cmdline+afterlastspace);
177 if (!last_arg) /* need to seek to the next arg ? */
180 while (cmdline[end]==' ')
187 xargv[xargc] = NULL; /* the last entry is NULL */
194 CRTDLL_argc_dll = xargc;
196 CRTDLL_argv_dll = xargv;
199 TRACE("found %d arguments\n",
201 CRTDLL_environ_dll = *environ = GetEnvironmentStringsA();
206 typedef void (*_INITTERMFUN)();
208 /* fixme: move to header */
211 time_t time_create; /* -1 when not avaiable */
212 time_t time_access; /* -1 when not avaiable */
214 unsigned long size; /* fixme: 64 bit ??*/
217 /*********************************************************************
218 * _findfirst (CRTDLL.099)
223 DWORD __cdecl CRTDLL__findfirst(LPCSTR fname, struct find_t * x2)
225 FIXME(":(%s,%p): stub\n",fname,x2);
226 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
230 /*********************************************************************
231 * _findnext (CRTDLL.100)
236 INT __cdecl CRTDLL__findnext(DWORD hand, struct find_t * x2)
238 FIXME(":(%ld,%p): stub\n",hand,x2);
239 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
243 /*********************************************************************
244 * _fstat (CRTDLL.111)
249 int __cdecl CRTDLL__fstat(int file, struct stat* buf)
251 FIXME(":(%d,%p): stub\n",file,buf);
252 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
256 /*********************************************************************
257 * _initterm (CRTDLL.135)
259 DWORD __cdecl CRTDLL__initterm(_INITTERMFUN *start,_INITTERMFUN *end)
261 _INITTERMFUN *current;
263 TRACE("(%p,%p)\n",start,end);
265 while (current<end) {
266 if (*current) (*current)();
272 /*********************************************************************
273 * _fsopen (CRTDLL.110)
275 CRTDLL_FILE * __cdecl CRTDLL__fsopen(LPCSTR x, LPCSTR y, INT z) {
276 FIXME("(%s,%s,%d),stub!\n",x,y,z);
280 /*********************************************************************
281 * _fdopen (CRTDLL.91)
283 CRTDLL_FILE * __cdecl CRTDLL__fdopen(INT handle, LPCSTR mode)
291 if (!file->handle) file->handle = GetStdHandle( STD_INPUT_HANDLE );
294 file = CRTDLL_stdout;
295 if (!file->handle) file->handle = GetStdHandle( STD_OUTPUT_HANDLE );
299 if (!file->handle) file->handle = GetStdHandle( STD_ERROR_HANDLE );
302 file = HeapAlloc( GetProcessHeap(), 0, sizeof(*file) );
303 file->handle = handle;
306 TRACE("open handle %d mode %s got file %p\n",
312 /*******************************************************************
313 * _global_unwind2 (CRTDLL.129)
315 void __cdecl CRTDLL__global_unwind2( PEXCEPTION_FRAME frame )
317 RtlUnwind( frame, 0, NULL, 0 );
320 /*******************************************************************
321 * _local_unwind2 (CRTDLL.173)
323 void __cdecl CRTDLL__local_unwind2( PEXCEPTION_FRAME endframe, DWORD nr )
325 TRACE("(%p,%ld)\n",endframe,nr);
327 /*******************************************************************
328 * _setjmp (CRTDLL.264)
330 INT __cdecl CRTDLL__setjmp(LPDWORD *jmpbuf)
332 FIXME(":(%p): stub\n",jmpbuf);
335 /*********************************************************************
341 INT __cdecl CRTDLL__read(INT fd, LPVOID buf, UINT count)
343 FIXME(":(%d,%p,%d): stub\n",fd,buf,count);
344 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
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 /* FIXME: there is no O_APPEND in CreateFile, should emulate it */
387 access = GENERIC_WRITE;
388 creation = OPEN_ALWAYS;
389 if (mode[1] == '+') access |= GENERIC_READ;
391 /* FIXME: should handle text/binary mode */
393 if ((handle = CreateFileA( path, access, FILE_SHARE_READ | FILE_SHARE_WRITE,
394 NULL, creation, FILE_ATTRIBUTE_NORMAL,
395 -1 )) != INVALID_HANDLE_VALUE)
397 file = HeapAlloc( GetProcessHeap(), 0, sizeof(*file) );
398 file->handle = handle;
400 TRACE("file %s mode %s got handle %d file %p\n",
401 path,mode,handle,file);
405 /*********************************************************************
408 DWORD __cdecl CRTDLL_fread(LPVOID ptr, INT size, INT nmemb, CRTDLL_FILE *file)
414 /* If we would honour CR/LF <-> LF translation, we could do it like this.
415 We should keep track of all files opened, and probably files with \
416 known binary extensions must be unchanged */
417 while ( (i < (nmemb*size)) && (ret==1)) {
418 ret=fread(temp,1,1,file);
419 TRACE("got %c 0x%02x ret %d\n",
420 (isalpha(*(unsigned char*)temp))? *(unsigned char*)temp:
421 ' ',*(unsigned char*)temp, ret);
422 if (*(unsigned char*)temp != 0xd) { /* skip CR */
427 TRACE("skipping ^M\n");
429 TRACE("0x%08x items of size %d from file %p to %p\n",
430 nmemb,size,file,ptr,);
438 TRACE("0x%08x items of size %d from file %p to %p\n",
439 nmemb,size,file,ptr);
440 if (!ReadFile( file->handle, ptr, size * nmemb, &ret, NULL ))
446 /*********************************************************************
447 * freopen (CRTDLL.379)
452 DWORD __cdecl CRTDLL_freopen(LPCSTR path, LPCSTR mode, LPVOID stream)
454 FIXME(":(%s,%s,%p): stub\n", path, mode, stream);
455 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
459 /*********************************************************************
460 * fscanf (CRTDLL.381)
462 INT __cdecl CRTDLL_fscanf( CRTDLL_FILE *stream, LPSTR format, ... )
468 va_start( valist, format );
470 res = vfscanf( xlat_file_ptr(stream), format, valist );
479 /*********************************************************************
482 LONG __cdecl CRTDLL_fseek( CRTDLL_FILE *file, LONG offset, INT whence)
484 TRACE("file %p to 0x%08lx pos %s\n",
485 file,offset,(whence==SEEK_SET)?"SEEK_SET":
486 (whence==SEEK_CUR)?"SEEK_CUR":
487 (whence==SEEK_END)?"SEEK_END":"UNKNOWN");
488 if (SetFilePointer( file->handle, offset, NULL, whence ) != 0xffffffff)
494 /*********************************************************************
495 * fsetpos (CRTDLL.383)
497 INT __cdecl CRTDLL_fsetpos( CRTDLL_FILE *file, INT *pos )
499 TRACE("file %p pos %d\n", file, *pos );
500 return CRTDLL_fseek(file, *pos, SEEK_SET);
503 /*********************************************************************
506 LONG __cdecl CRTDLL_ftell( CRTDLL_FILE *file )
508 return SetFilePointer( file->handle, 0, NULL, SEEK_CUR );
511 /*********************************************************************
512 * fwrite (CRTDLL.386)
514 DWORD __cdecl CRTDLL_fwrite( LPVOID ptr, INT size, INT nmemb, CRTDLL_FILE *file )
518 TRACE("0x%08x items of size %d to file %p(%d) from %p\n",
519 nmemb,size,file,file-(CRTDLL_FILE*)CRTDLL_iob,ptr);
522 if (!WriteFile( file->handle, ptr, size * nmemb, &ret, NULL ))
527 /*********************************************************************
528 * setbuf (CRTDLL.452)
530 INT __cdecl CRTDLL_setbuf(CRTDLL_FILE *file, LPSTR buf)
532 TRACE("(file %p buf %p)\n", file, buf);
533 /* this doesn't work:"void value not ignored as it ought to be"
534 return setbuf(file,buf);
536 /* FIXME: no buffering for now */
540 /*********************************************************************
541 * _open_osfhandle (CRTDLL.240)
543 HFILE __cdecl CRTDLL__open_osfhandle(LONG osfhandle, INT flags)
548 case STD_INPUT_HANDLE :
552 case STD_OUTPUT_HANDLE:
556 case STD_ERROR_HANDLE:
563 TRACE("(handle %08lx,flags %d) return %d\n",
564 osfhandle,flags,handle);
569 /*********************************************************************
572 void __cdecl CRTDLL_srand(DWORD seed)
574 /* FIXME: should of course be thread? process? local */
578 /*********************************************************************
579 * vfprintf (CRTDLL.373)
581 INT __cdecl CRTDLL_vfprintf( CRTDLL_FILE *file, LPSTR format, va_list args )
583 char buffer[2048]; /* FIXME... */
585 vsprintf( buffer, format, args );
586 return CRTDLL_fwrite( buffer, 1, strlen(buffer), file );
589 /*********************************************************************
590 * fprintf (CRTDLL.373)
592 INT __cdecl CRTDLL_fprintf( CRTDLL_FILE *file, LPSTR format, ... )
597 va_start( valist, format );
598 res = CRTDLL_vfprintf( file, format, valist );
603 /*********************************************************************
606 time_t __cdecl CRTDLL_time(time_t *timeptr)
608 time_t curtime = time(NULL);
615 /*********************************************************************
618 clock_t __cdecl CRTDLL_clock(void)
624 res = alltimes.tms_utime + alltimes.tms_stime+
625 alltimes.tms_cutime + alltimes.tms_cstime;
626 /* Fixme: We need some symbolic representation
627 for (Hostsystem_)CLOCKS_PER_SEC
628 and (Emulated_system_)CLOCKS_PER_SEC
629 10 holds only for Windows/Linux_i86)
634 /*********************************************************************
635 * _isatty (CRTDLL.137)
637 BOOL __cdecl CRTDLL__isatty(DWORD x)
643 /*********************************************************************
644 * _write (CRTDLL.332)
646 INT __cdecl CRTDLL__write(INT fd,LPCVOID buf,UINT count)
653 len = (UINT)write(fd,buf,(LONG)count);
655 len = _lwrite(fd,buf,count);
656 TRACE("%d/%d byte to dfh %d from %p,\n",
662 /*********************************************************************
665 * FIXME: What the heck is the difference between
666 * FIXME _c_exit (CRTDLL.47)
667 * FIXME _cexit (CRTDLL.49)
668 * FIXME _exit (CRTDLL.87)
669 * FIXME exit (CRTDLL.359)
671 * atexit-processing comes to mind -- MW.
674 void __cdecl CRTDLL__cexit(INT ret)
681 /*********************************************************************
684 void __cdecl CRTDLL_exit(DWORD ret)
686 TRACE("(%ld)\n",ret);
691 /*********************************************************************
692 * _abnormal_termination (CRTDLL.36)
694 INT __cdecl CRTDLL__abnormal_termination(void)
701 /*********************************************************************
702 * _access (CRTDLL.37)
704 INT __cdecl CRTDLL__access(LPCSTR filename, INT mode)
706 DWORD attr = GetFileAttributesA(filename);
710 if (GetLastError() == ERROR_INVALID_ACCESS)
717 if ((attr & FILE_ATTRIBUTE_READONLY) && (mode & W_OK))
727 /*********************************************************************
728 * fflush (CRTDLL.365)
730 INT __cdecl CRTDLL_fflush( CRTDLL_FILE *file )
732 return FlushFileBuffers( file->handle ) ? 0 : -1;
736 /*********************************************************************
739 INT __cdecl CRTDLL_rand()
741 return (rand() & CRTDLL_RAND_MAX);
745 /*********************************************************************
746 * putchar (CRTDLL.442)
748 void __cdecl CRTDLL_putchar( INT x )
754 /*********************************************************************
757 INT __cdecl CRTDLL_fputc( INT c, CRTDLL_FILE *file )
761 TRACE("%c to file %p\n",c,file);
762 if (!WriteFile( file->handle, &ch, 1, &res, NULL )) return -1;
767 /*********************************************************************
770 INT __cdecl CRTDLL_fputs( LPCSTR s, CRTDLL_FILE *file )
773 TRACE("%s to file %p\n",s,file);
774 if (!WriteFile( file->handle, s, strlen(s), &res, NULL )) return -1;
779 /*********************************************************************
782 INT __cdecl CRTDLL_puts(LPCSTR s)
789 /*********************************************************************
792 INT __cdecl CRTDLL_putc( INT c, CRTDLL_FILE *file )
794 return CRTDLL_fputc( c, file );
797 /*********************************************************************
800 INT __cdecl CRTDLL_fgetc( CRTDLL_FILE *file )
804 if (!ReadFile( file->handle, &ch, 1, &res, NULL )) return -1;
805 if (res != 1) return -1;
810 /*********************************************************************
813 INT __cdecl CRTDLL_getc( CRTDLL_FILE *file )
815 return CRTDLL_fgetc( file );
819 /*********************************************************************
822 CHAR* __cdecl CRTDLL_fgets( LPSTR s, INT size, CRTDLL_FILE *file )
827 /* BAD, for the whole WINE process blocks... just done this way to test
828 * windows95's ftp.exe.
831 for(cc = CRTDLL_fgetc(file); cc != EOF && cc != '\n'; cc = CRTDLL_fgetc(file))
834 if (--size <= 0) break;
840 TRACE("got '%s'\n", buf_start);
845 /*********************************************************************
848 LPSTR __cdecl CRTDLL_gets(LPSTR buf)
851 LPSTR buf_start = buf;
853 /* BAD, for the whole WINE process blocks... just done this way to test
854 * windows95's ftp.exe.
857 for(cc = fgetc(stdin); cc != EOF && cc != '\n'; cc = fgetc(stdin))
858 if(cc != '\r') *buf++ = (char)cc;
862 TRACE("got '%s'\n", buf_start);
867 /*********************************************************************
870 UINT __cdecl CRTDLL__rotl(UINT x,INT shift)
872 unsigned int ret = (x >> shift)|( x >>((sizeof(x))-shift));
874 TRACE("got 0x%08x rot %d ret 0x%08x\n",
879 /*********************************************************************
880 * _lrotl (CRTDLL.176)
882 DWORD __cdecl CRTDLL__lrotl(DWORD x,INT shift)
884 unsigned long ret = (x >> shift)|( x >>((sizeof(x))-shift));
886 TRACE("got 0x%08lx rot %d ret 0x%08lx\n",
893 /*********************************************************************
894 * _mbsicmp (CRTDLL.204)
896 int __cdecl CRTDLL__mbsicmp(unsigned char *x,unsigned char *y)
903 /* FIXME: MBCS handling... */
912 /*********************************************************************
913 * vsprintf (CRTDLL.500)
915 INT __cdecl CRTDLL_vsprintf( LPSTR buffer, LPCSTR spec, va_list args )
917 return wvsprintfA( buffer, spec, args );
920 /*********************************************************************
921 * vswprintf (CRTDLL.501)
923 INT __cdecl CRTDLL_vswprintf( LPWSTR buffer, LPCWSTR spec, va_list args )
925 return wvsprintfW( buffer, spec, args );
928 /*********************************************************************
929 * _strcmpi (CRTDLL.282) (CRTDLL.287)
931 INT __cdecl CRTDLL__strcmpi( LPCSTR s1, LPCSTR s2 )
933 return lstrcmpiA( s1, s2 );
937 /*********************************************************************
938 * _strnicmp (CRTDLL.293)
940 INT __cdecl CRTDLL__strnicmp( LPCSTR s1, LPCSTR s2, INT n )
942 return lstrncmpiA( s1, s2, n );
946 /*********************************************************************
947 * _strlwr (CRTDLL.293)
949 * convert a string in place to lowercase
951 LPSTR __cdecl CRTDLL__strlwr(LPSTR x)
955 TRACE("CRTDLL_strlwr got %s\n", x);
957 if ((*y > 0x40) && (*y< 0x5b))
961 TRACE(" returned %s\n", x);
966 /*********************************************************************
967 * system (CRTDLL.485)
969 INT __cdecl CRTDLL_system(LPSTR x)
971 #define SYSBUF_LENGTH 1500
972 char buffer[SYSBUF_LENGTH];
973 unsigned char *y = x;
977 sprintf( buffer, "%s \"", argv0 );
978 bp = buffer + strlen(buffer);
979 i = strlen(buffer) + strlen(x) +2;
981 /* Calculate needed buffer size to prevent overflow. */
986 /* If buffer too short, exit. */
987 if (i > SYSBUF_LENGTH) {
988 TRACE("_system buffer to small\n");
997 if (*(y-1) =='\\') *bp++ = '\\';
999 /* Remove spaces from end of string. */
1000 while (*(y-1) == ' ') {
1005 TRACE("_system got '%s', executing '%s'\n",x,buffer);
1007 return system(buffer);
1010 /*********************************************************************
1011 * _strupr (CRTDLL.300)
1013 LPSTR __cdecl CRTDLL__strupr(LPSTR x)
1024 /*********************************************************************
1025 * longjmp (CRTDLL.426)
1027 VOID __cdecl CRTDLL_longjmp(jmp_buf env, int val)
1029 FIXME("CRTDLL_longjmp semistup, expect crash\n");
1033 /*********************************************************************
1034 * malloc (CRTDLL.427)
1036 VOID* __cdecl CRTDLL_malloc(DWORD size)
1038 return HeapAlloc(GetProcessHeap(),0,size);
1041 /*********************************************************************
1044 VOID* __cdecl CRTDLL_new(DWORD size)
1047 if(!(result = HeapAlloc(GetProcessHeap(),0,size)) && new_handler)
1052 /*********************************************************************
1053 * set_new_handler(CRTDLL.003)
1055 new_handler_type __cdecl CRTDLL_set_new_handler(new_handler_type func)
1057 new_handler_type old_handler = new_handler;
1062 /*********************************************************************
1063 * calloc (CRTDLL.350)
1065 VOID* __cdecl CRTDLL_calloc(DWORD size, DWORD count)
1067 return HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, size * count );
1070 /*********************************************************************
1071 * realloc (CRTDLL.447)
1073 VOID* __cdecl CRTDLL_realloc( VOID *ptr, DWORD size )
1075 return HeapReAlloc( GetProcessHeap(), 0, ptr, size );
1078 /*********************************************************************
1081 VOID __cdecl CRTDLL_free(LPVOID ptr)
1083 HeapFree(GetProcessHeap(),0,ptr);
1086 /*********************************************************************
1087 * delete (CRTDLL.002)
1089 VOID __cdecl CRTDLL_delete(VOID* ptr)
1091 HeapFree(GetProcessHeap(),0,ptr);
1094 /*********************************************************************
1095 * _strdup (CRTDLL.285)
1097 LPSTR __cdecl CRTDLL__strdup(LPCSTR ptr)
1099 return HEAP_strdupA(GetProcessHeap(),0,ptr);
1102 /*********************************************************************
1103 * fclose (CRTDLL.362)
1105 INT __cdecl CRTDLL_fclose( CRTDLL_FILE *file )
1107 TRACE("%p\n", file );
1108 if (!CloseHandle( file->handle )) return -1;
1109 HeapFree( GetProcessHeap(), 0, file );
1113 /*********************************************************************
1114 * _unlink (CRTDLL.315)
1116 INT __cdecl CRTDLL__unlink(LPCSTR pathname)
1118 return DeleteFileA( pathname ) ? 0 : -1;
1121 /*********************************************************************
1122 * rename (CRTDLL.449)
1124 INT __cdecl CRTDLL_rename(LPCSTR oldpath,LPCSTR newpath)
1126 BOOL ok = MoveFileExA( oldpath, newpath, MOVEFILE_REPLACE_EXISTING );
1131 /*********************************************************************
1132 * _stat (CRTDLL.280)
1150 int __cdecl CRTDLL__stat(const char * filename, struct win_stat * buf)
1153 DOS_FULL_NAME full_name;
1156 if (!DOSFS_GetFullName( filename, TRUE, &full_name ))
1158 WARN("CRTDLL__stat filename %s bad name\n",filename);
1161 ret=stat(full_name.long_name,&mystat);
1162 TRACE("CRTDLL__stat %s\n", filename);
1166 /* FIXME: should check what Windows returns */
1168 buf->win_st_dev = mystat.st_dev;
1169 buf->win_st_ino = mystat.st_ino;
1170 buf->win_st_mode = mystat.st_mode;
1171 buf->win_st_nlink = mystat.st_nlink;
1172 buf->win_st_uid = mystat.st_uid;
1173 buf->win_st_gid = mystat.st_gid;
1174 buf->win_st_rdev = mystat.st_rdev;
1175 buf->win_st_size = mystat.st_size;
1176 buf->win_st_atime = mystat.st_atime;
1177 buf->win_st_mtime = mystat.st_mtime;
1178 buf->win_st_ctime = mystat.st_ctime;
1182 /*********************************************************************
1183 * _open (CRTDLL.239)
1185 HFILE __cdecl CRTDLL__open(LPCSTR path,INT flags)
1187 DWORD access = 0, creation = 0;
1191 the flags in lcc's header differ from the ones in Linux, e.g.
1192 Linux: define O_APPEND 02000 (= 0x400)
1193 lcc: define _O_APPEND 0x0008
1194 so here a scheme to translate them
1195 Probably lcc is wrong here, but at least a hack to get is going
1199 case O_RDONLY: access |= GENERIC_READ; break;
1200 case O_WRONLY: access |= GENERIC_WRITE; break;
1201 case O_RDWR: access |= GENERIC_WRITE | GENERIC_READ; break;
1204 if (flags & 0x0100) /* O_CREAT */
1206 if (flags & 0x0400) /* O_EXCL */
1207 creation = CREATE_NEW;
1208 else if (flags & 0x0200) /* O_TRUNC */
1209 creation = CREATE_ALWAYS;
1211 creation = OPEN_ALWAYS;
1213 else /* no O_CREAT */
1215 if (flags & 0x0200) /* O_TRUNC */
1216 creation = TRUNCATE_EXISTING;
1218 creation = OPEN_EXISTING;
1220 if (flags & 0x0008) /* O_APPEND */
1221 FIXME("O_APPEND not supported\n" );
1223 TRACE("CRTDLL_open file unsupported flags 0x%04x\n",flags);
1226 ret = CreateFileA( path, access, FILE_SHARE_READ | FILE_SHARE_WRITE,
1227 NULL, creation, FILE_ATTRIBUTE_NORMAL, -1 );
1228 TRACE("CRTDLL_open file %s mode 0x%04x got handle %d\n", path,flags,ret);
1232 /*********************************************************************
1233 * _close (CRTDLL.57)
1235 INT __cdecl CRTDLL__close(HFILE fd)
1237 int ret=_lclose(fd);
1246 /*********************************************************************
1249 INT __cdecl CRTDLL_feof( CRTDLL_FILE *file )
1255 /*********************************************************************
1256 * setlocale (CRTDLL.453)
1258 LPSTR __cdecl CRTDLL_setlocale(INT category,LPCSTR locale)
1263 case CRTDLL_LC_ALL: categorystr="LC_ALL";break;
1264 case CRTDLL_LC_COLLATE: categorystr="LC_COLLATE";break;
1265 case CRTDLL_LC_CTYPE: categorystr="LC_CTYPE";break;
1266 case CRTDLL_LC_MONETARY: categorystr="LC_MONETARY";break;
1267 case CRTDLL_LC_NUMERIC: categorystr="LC_NUMERIC";break;
1268 case CRTDLL_LC_TIME: categorystr="LC_TIME";break;
1269 default: categorystr = "UNKNOWN?";break;
1271 FIXME("(%s,%s),stub!\n",categorystr,locale);
1275 /*********************************************************************
1276 * _setmode (CRTDLL.265)
1277 * FIXME: At present we ignore the request to translate CR/LF to LF.
1279 * We allways translate when we read with fgets, we never do with fread
1282 INT __cdecl CRTDLL__setmode( INT fh,INT mode)
1285 #define O_TEXT 0x4000
1286 #define O_BINARY 0x8000
1288 FIXME("on fhandle %d mode %s, STUB.\n",
1289 fh,(mode=O_TEXT)?"O_TEXT":
1290 (mode=O_BINARY)?"O_BINARY":"UNKNOWN");
1294 /*********************************************************************
1295 * _fpreset (CRTDLL.107)
1297 VOID __cdecl CRTDLL__fpreset(void)
1302 /*********************************************************************
1303 * atexit (CRTDLL.345)
1305 INT __cdecl CRTDLL_atexit(LPVOID x)
1307 FIXME("(%p), STUB.\n",x);
1308 return 0; /* successful */
1311 /*********************************************************************
1312 * _isctype (CRTDLL.138)
1314 BOOL __cdecl CRTDLL__isctype(CHAR x,CHAR type)
1316 if ((type & CRTDLL_SPACE) && isspace(x))
1318 if ((type & CRTDLL_PUNCT) && ispunct(x))
1320 if ((type & CRTDLL_LOWER) && islower(x))
1322 if ((type & CRTDLL_UPPER) && isupper(x))
1324 if ((type & CRTDLL_ALPHA) && isalpha(x))
1326 if ((type & CRTDLL_DIGIT) && isdigit(x))
1328 if ((type & CRTDLL_CONTROL) && iscntrl(x))
1330 /* check CRTDLL_LEADBYTE */
1334 /*********************************************************************
1335 * _chdrive (CRTDLL.52)
1337 * newdir [I] drive to change to, A=1
1340 BOOL __cdecl CRTDLL__chdrive(INT newdrive)
1342 /* FIXME: generates errnos */
1343 return DRIVE_SetCurrentDrive(newdrive-1);
1346 /*********************************************************************
1347 * _chdir (CRTDLL.51)
1349 INT __cdecl CRTDLL__chdir(LPCSTR newdir)
1351 if (!SetCurrentDirectoryA(newdir))
1356 /*********************************************************************
1357 * _fullpath (CRTDLL.114)
1359 LPSTR __cdecl CRTDLL__fullpath(LPSTR buf, LPCSTR name, INT size)
1361 DOS_FULL_NAME full_name;
1366 if(!(buf = CRTDLL_malloc(size))) return NULL;
1368 if (!DOSFS_GetFullName( name, FALSE, &full_name )) return NULL;
1369 lstrcpynA(buf,full_name.short_name,size);
1370 TRACE("CRTDLL_fullpath got %s\n",buf);
1374 /*********************************************************************
1375 * _splitpath (CRTDLL.279)
1377 VOID __cdecl CRTDLL__splitpath(LPCSTR path, LPSTR drive, LPSTR directory, LPSTR filename, LPSTR extension )
1380 directory includes leading and trailing (forward and backward slashes)
1381 filename without dot and slashes
1382 extension with leading dot
1384 char * drivechar,*dirchar,*namechar;
1386 TRACE("CRTDLL__splitpath got %s\n",path);
1388 drivechar = strchr(path,':');
1389 dirchar = strrchr(path,'/');
1390 namechar = strrchr(path,'\\');
1391 dirchar = MAX(dirchar,namechar);
1393 namechar = strrchr(dirchar,'.');
1395 namechar = strrchr(path,'.');
1403 strncat(drive,path,drivechar-path+1);
1412 strncat(directory,path,dirchar-path+1);
1421 strncat(filename,path,namechar-path);
1425 strcat(extension,namechar);
1430 TRACE("CRTDLL__splitpath found %s %s %s %s\n",drive,directory,filename,extension);
1435 /*********************************************************************
1436 * _makepath (CRTDLL.182)
1439 VOID __cdecl CRTDLL__makepath(LPSTR path, LPCSTR drive,
1440 LPCSTR directory, LPCSTR filename,
1444 TRACE("CRTDLL__makepath got %s %s %s %s\n", drive, directory,
1445 filename, extension);
1453 sprintf(path, "%c:", drive[0]);
1456 if ( directory[0] ) {
1457 strcat(path, directory);
1458 ch = path[strlen(path)-1];
1459 if (ch != '/' && ch != '\\')
1463 if ( filename[0] ) {
1464 strcat(path, filename);
1466 if ( extension[0] ) {
1467 if ( extension[0] != '.' ) {
1470 strcat(path,extension);
1475 TRACE("CRTDLL__makepath returns %s\n",path);
1478 /*********************************************************************
1479 * _getcwd (CRTDLL.120)
1481 CHAR* __cdecl CRTDLL__getcwd(LPSTR buf, INT size)
1488 if (size < 0) /* allocate as big as nescessary */
1489 len =GetCurrentDirectoryA(1,test) + 1;
1490 if(!(buf = CRTDLL_malloc(len)))
1492 /* set error to OutOfRange */
1497 if(!(len =GetCurrentDirectoryA(len,buf)))
1503 /* set error to ERANGE */
1504 TRACE("CRTDLL_getcwd buffer to small\n");
1511 /*********************************************************************
1512 * _getdcwd (CRTDLL.121)
1514 CHAR* __cdecl CRTDLL__getdcwd(INT drive,LPSTR buf, INT size)
1519 FIXME("(\"%c:\",%s,%d)\n",drive+'A',buf,size);
1522 if (size < 0) /* allocate as big as nescessary */
1523 len =GetCurrentDirectoryA(1,test) + 1;
1524 if(!(buf = CRTDLL_malloc(len)))
1526 /* set error to OutOfRange */
1531 if(!(len =GetCurrentDirectoryA(len,buf)))
1537 /* set error to ERANGE */
1538 TRACE("buffer to small\n");
1545 /*********************************************************************
1546 * _getdrive (CRTDLL.124)
1548 * Return current drive, 1 for A, 2 for B
1550 INT __cdecl CRTDLL__getdrive(VOID)
1552 return DRIVE_GetCurrentDrive() + 1;
1555 /*********************************************************************
1556 * _mkdir (CRTDLL.234)
1558 INT __cdecl CRTDLL__mkdir(LPCSTR newdir)
1560 if (!CreateDirectoryA(newdir,NULL))
1565 /*********************************************************************
1566 * remove (CRTDLL.448)
1568 INT __cdecl CRTDLL_remove(LPCSTR file)
1570 if (!DeleteFileA(file))
1575 /*********************************************************************
1576 * _errno (CRTDLL.52)
1577 * Yes, this is a function.
1579 LPINT __cdecl CRTDLL__errno()
1581 static int crtdllerrno;
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 WARN("Unable to get unique filename\n");
1602 if (!DOSFS_GetFullName(ret,FALSE,&tempname))
1604 TRACE("Wrong path?\n");
1608 if ((ret = CRTDLL_malloc(strlen(tempname.short_name)+1)) == NULL) {
1609 WARN("CRTDL_malloc for shortname failed\n");
1612 if ((ret = strcpy(ret,tempname.short_name)) == NULL) {
1613 WARN("Malloc for shortname failed\n");
1617 TRACE("dir %s prefix %s got %s\n",
1622 /*********************************************************************
1623 * tmpnam (CRTDLL.490)
1625 * lcclnk from lcc-win32 relies on a terminating dot in the name returned
1628 LPSTR __cdecl CRTDLL_tmpnam(LPSTR s)
1632 if ((ret =tmpnam(s))== NULL) {
1633 WARN("Unable to get unique filename\n");
1636 if (!DOSFS_GetFullName(ret,FALSE,&CRTDLL_tmpname))
1638 TRACE("Wrong path?\n");
1641 strcat(CRTDLL_tmpname.short_name,".");
1642 TRACE("for buf %p got %s\n",
1643 s,CRTDLL_tmpname.short_name);
1644 TRACE("long got %s\n",
1645 CRTDLL_tmpname.long_name);
1647 return strcpy(s,CRTDLL_tmpname.short_name);
1649 return CRTDLL_tmpname.short_name;
1653 /*********************************************************************
1654 * _itoa (CRTDLL.165)
1656 LPSTR __cdecl CRTDLL__itoa(INT x,LPSTR buf,INT buflen)
1658 wsnprintfA(buf,buflen,"%d",x);
1662 /*********************************************************************
1663 * _ltoa (CRTDLL.180)
1665 LPSTR __cdecl CRTDLL__ltoa(long x,LPSTR buf,INT radix)
1668 case 2: FIXME("binary format not implemented !\n");
1670 case 8: wsnprintfA(buf,0x80,"%o",x);
1672 case 10: wsnprintfA(buf,0x80,"%d",x);
1674 case 16: wsnprintfA(buf,0x80,"%x",x);
1676 default: FIXME("radix %d not implemented !\n", radix);
1681 /*********************************************************************
1682 * _ultoa (CRTDLL.311)
1684 LPSTR __cdecl CRTDLL__ultoa(long x,LPSTR buf,INT radix)
1687 case 2: FIXME("binary format not implemented !\n");
1689 case 8: wsnprintfA(buf,0x80,"%lo",x);
1691 case 10: wsnprintfA(buf,0x80,"%ld",x);
1693 case 16: wsnprintfA(buf,0x80,"%lx",x);
1695 default: FIXME("radix %d not implemented !\n", radix);
1700 typedef VOID (*sig_handler_type)(VOID);
1702 /*********************************************************************
1703 * signal (CRTDLL.455)
1705 VOID __cdecl CRTDLL_signal(int sig, sig_handler_type ptr)
1707 FIXME("(%d %p):stub.\n", sig, ptr);
1710 /*********************************************************************
1711 * _ftol (CRTDLL.113)
1713 #ifdef USING_REAL_FPU
1714 LONG __cdecl CRTDLL__ftol(void) {
1715 /* don't just do DO_FPU("fistp",retval), because the rounding
1716 * mode must also be set to "round towards zero"... */
1722 LONG __cdecl CRTDLL__ftol(double fl) {
1723 FIXME("should be register function\n");
1728 /*********************************************************************
1729 * _CIpow (CRTDLL.14)
1731 #ifdef USING_REAL_FPU
1732 LONG __cdecl CRTDLL__CIpow(void) {
1739 LONG __cdecl CRTDLL__CIpow(double x,double y) {
1740 FIXME("should be register function\n");
1745 /*********************************************************************
1746 * _sleep (CRTDLL.267)
1748 VOID __cdecl CRTDLL__sleep(unsigned long timeout)
1750 TRACE("CRTDLL__sleep for %ld milliseconds\n",timeout);
1751 Sleep((timeout)?timeout:1);
1754 /*********************************************************************
1755 * getenv (CRTDLL.437)
1757 LPSTR __cdecl CRTDLL_getenv(const char *name)
1759 LPSTR environ = GetEnvironmentStringsA();
1760 LPSTR pp,pos = NULL;
1761 unsigned int length;
1763 for (pp = environ; (*pp); pp = pp + strlen(pp) +1)
1765 pos =strchr(pp,'=');
1769 length = strlen(pp);
1770 if (!strncmp(pp,name,length)) break;
1775 TRACE("got %s\n",pp);
1777 FreeEnvironmentStringsA( environ );
1781 /*********************************************************************
1782 * _mbsrchr (CRTDLL.223)
1784 LPSTR __cdecl CRTDLL__mbsrchr(LPSTR s,CHAR x) {
1785 /* FIXME: handle multibyte strings */
1786 return strrchr(s,x);
1789 /*********************************************************************
1790 * _memicmp (CRTDLL.233)(NTDLL.868)
1791 * A stringcompare, without \0 check
1793 * -1:if first string is alphabetically before second string
1794 * 1:if second '' '' '' '' first ''
1795 * 0:if both are equal.
1797 INT __cdecl CRTDLL__memicmp(
1798 LPCSTR s1, /* [in] first string */
1799 LPCSTR s2, /* [in] second string */
1800 DWORD len /* [in] length to compare */
1804 for (i=0;i<len;i++) {
1805 if (tolower(s1[i])<tolower(s2[i]))
1807 if (tolower(s1[i])>tolower(s2[i]))
1812 /*********************************************************************
1813 * __dllonexit (CRTDLL.25)
1815 VOID __cdecl CRTDLL__dllonexit ()
1820 /*********************************************************************
1821 * wcstol (CRTDLL.520)
1822 * Like strtol, but for wide character strings.
1824 INT __cdecl CRTDLL_wcstol(LPWSTR s,LPWSTR *end,INT base) {
1825 LPSTR sA = HEAP_strdupWtoA(GetProcessHeap(),0,s),endA;
1826 INT ret = strtol(sA,&endA,base);
1828 HeapFree(GetProcessHeap(),0,sA);
1829 if (end) *end = s+(endA-sA); /* pointer magic checked. */
1832 /*********************************************************************
1833 * _strdate (CRTDLL.283)
1835 LPSTR __cdecl CRTDLL__strdate (LPSTR date)
1836 { FIXME("%p stub\n", date);
1840 /*********************************************************************
1841 * _strtime (CRTDLL.299)
1843 LPSTR __cdecl CRTDLL__strtime (LPSTR date)
1844 { FIXME("%p stub\n", date);
1848 /*********************************************************************
1849 * _except_handler2 (CRTDLL.78)
1851 INT __cdecl CRTDLL__except_handler2 (
1852 PEXCEPTION_RECORD rec,
1853 PEXCEPTION_FRAME frame,
1855 PEXCEPTION_FRAME *dispatcher)
1857 FIXME ("exception %lx flags=%lx at %p handler=%p %p %p stub\n",
1858 rec->ExceptionCode, rec->ExceptionFlags, rec->ExceptionAddress,
1859 frame->Handler, context, dispatcher);
1860 return ExceptionContinueSearch;