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"
61 DEFAULT_DEBUG_CHANNEL(crtdll);
63 /* windows.h RAND_MAX is smaller than normal RAND_MAX */
64 #define CRTDLL_RAND_MAX 0x7fff
66 static DOS_FULL_NAME CRTDLL_tmpname;
68 UINT CRTDLL_argc_dll; /* CRTDLL.23 */
69 LPSTR *CRTDLL_argv_dll; /* CRTDLL.24 */
70 LPSTR CRTDLL_acmdln_dll; /* CRTDLL.38 */
71 UINT CRTDLL_basemajor_dll; /* CRTDLL.42 */
72 UINT CRTDLL_baseminor_dll; /* CRTDLL.43 */
73 UINT CRTDLL_baseversion_dll; /* CRTDLL.44 */
74 UINT CRTDLL_commode_dll; /* CRTDLL.59 */
75 LPSTR CRTDLL_environ_dll; /* CRTDLL.75 */
76 UINT CRTDLL_fmode_dll; /* CRTDLL.104 */
77 UINT CRTDLL_osmajor_dll; /* CRTDLL.241 */
78 UINT CRTDLL_osminor_dll; /* CRTDLL.242 */
79 UINT CRTDLL_osmode_dll; /* CRTDLL.243 */
80 UINT CRTDLL_osver_dll; /* CRTDLL.244 */
81 UINT CRTDLL_osversion_dll; /* CRTDLL.245 */
82 UINT CRTDLL_winmajor_dll; /* CRTDLL.329 */
83 UINT CRTDLL_winminor_dll; /* CRTDLL.330 */
84 UINT CRTDLL_winver_dll; /* CRTDLL.331 */
86 /* FIXME: structure layout is obviously not correct */
93 CRTDLL_FILE CRTDLL_iob[3];
95 static CRTDLL_FILE * const CRTDLL_stdin = &CRTDLL_iob[0];
96 static CRTDLL_FILE * const CRTDLL_stdout = &CRTDLL_iob[1];
97 static CRTDLL_FILE * const CRTDLL_stderr = &CRTDLL_iob[2];
99 typedef VOID (*new_handler_type)(VOID);
101 static new_handler_type new_handler;
103 CRTDLL_FILE * __cdecl CRTDLL__fdopen(INT handle, LPCSTR mode);
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 * _GetMainArgs (CRTDLL.022)
123 LPSTR * __cdecl CRTDLL__GetMainArgs(LPDWORD argc,LPSTR **argv,
124 LPSTR *environ,DWORD flag)
128 int xargc,end,last_arg,afterlastspace;
131 TRACE("(%p,%p,%p,%ld).\n",
132 argc,argv,environ,flag
135 if (CRTDLL_acmdln_dll != NULL)
136 HeapFree(GetProcessHeap(), 0, CRTDLL_acmdln_dll);
138 CRTDLL_acmdln_dll = cmdline = HEAP_strdupA( GetProcessHeap(), 0,
140 TRACE("got '%s'\n", cmdline);
142 version = GetVersion();
143 CRTDLL_osver_dll = version >> 16;
144 CRTDLL_winminor_dll = version & 0xFF;
145 CRTDLL_winmajor_dll = (version>>8) & 0xFF;
146 CRTDLL_baseversion_dll = version >> 16;
147 CRTDLL_winver_dll = ((version >> 8) & 0xFF) + ((version & 0xFF) << 8);
148 CRTDLL_baseminor_dll = (version >> 16) & 0xFF;
149 CRTDLL_basemajor_dll = (version >> 24) & 0xFF;
150 CRTDLL_osversion_dll = version & 0xFFFF;
151 CRTDLL_osminor_dll = version & 0xFF;
152 CRTDLL_osmajor_dll = (version>>8) & 0xFF;
154 /* missing threading init */
156 end=0;last_arg=0;xargv=NULL;xargc=0;afterlastspace=0;
159 if ((cmdline[end]==' ') || (cmdline[end]=='\0'))
161 if (cmdline[end]=='\0')
165 /* alloc xargc + NULL entry */
166 xargv=(char**)HeapReAlloc( GetProcessHeap(), 0, xargv,
167 sizeof(char*)*(xargc+1));
168 if (strlen(cmdline+afterlastspace))
170 xargv[xargc] = HEAP_strdupA( GetProcessHeap(), 0,
171 cmdline+afterlastspace);
173 if (!last_arg) /* need to seek to the next arg ? */
176 while (cmdline[end]==' ')
183 xargv[xargc] = NULL; /* the last entry is NULL */
190 CRTDLL_argc_dll = xargc;
192 CRTDLL_argv_dll = xargv;
195 TRACE("found %d arguments\n",
197 CRTDLL_environ_dll = *environ = GetEnvironmentStringsA();
202 typedef void (*_INITTERMFUN)();
204 /* fixme: move to header */
207 time_t time_create; /* -1 when not avaiable */
208 time_t time_access; /* -1 when not avaiable */
210 unsigned long size; /* fixme: 64 bit ??*/
213 /*********************************************************************
214 * _findfirst (CRTDLL.099)
219 DWORD __cdecl CRTDLL__findfirst(LPCSTR fname, struct find_t * x2)
221 FIXME(":(%s,%p): stub\n",fname,x2);
222 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
226 /*********************************************************************
227 * _findnext (CRTDLL.100)
232 INT __cdecl CRTDLL__findnext(DWORD hand, struct find_t * x2)
234 FIXME(":(%ld,%p): stub\n",hand,x2);
235 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
239 /*********************************************************************
240 * _fstat (CRTDLL.111)
245 int __cdecl CRTDLL__fstat(int file, struct stat* buf)
247 FIXME(":(%d,%p): stub\n",file,buf);
248 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
252 /*********************************************************************
253 * _initterm (CRTDLL.135)
255 DWORD __cdecl CRTDLL__initterm(_INITTERMFUN *start,_INITTERMFUN *end)
257 _INITTERMFUN *current;
259 TRACE("(%p,%p)\n",start,end);
261 while (current<end) {
262 if (*current) (*current)();
268 /*********************************************************************
269 * _fsopen (CRTDLL.110)
271 CRTDLL_FILE * __cdecl CRTDLL__fsopen(LPCSTR x, LPCSTR y, INT z) {
272 FIXME("(%s,%s,%d),stub!\n",x,y,z);
276 /*********************************************************************
277 * _fdopen (CRTDLL.91)
279 CRTDLL_FILE * __cdecl CRTDLL__fdopen(INT handle, LPCSTR mode)
287 if (!file->handle) file->handle = GetStdHandle( STD_INPUT_HANDLE );
290 file = CRTDLL_stdout;
291 if (!file->handle) file->handle = GetStdHandle( STD_OUTPUT_HANDLE );
295 if (!file->handle) file->handle = GetStdHandle( STD_ERROR_HANDLE );
298 file = HeapAlloc( GetProcessHeap(), 0, sizeof(*file) );
299 file->handle = handle;
302 TRACE("open handle %d mode %s got file %p\n",
308 /*******************************************************************
309 * _global_unwind2 (CRTDLL.129)
311 void __cdecl CRTDLL__global_unwind2( PEXCEPTION_FRAME frame )
313 RtlUnwind( frame, 0, NULL, 0 );
316 /*******************************************************************
317 * _local_unwind2 (CRTDLL.173)
319 void __cdecl CRTDLL__local_unwind2( PEXCEPTION_FRAME endframe, DWORD nr )
321 TRACE("(%p,%ld)\n",endframe,nr);
323 /*******************************************************************
324 * _setjmp (CRTDLL.264)
326 INT __cdecl CRTDLL__setjmp(LPDWORD *jmpbuf)
328 FIXME(":(%p): stub\n",jmpbuf);
332 /*********************************************************************
335 CRTDLL_FILE * __cdecl CRTDLL_fopen(LPCSTR path, LPCSTR mode)
337 CRTDLL_FILE *file = NULL;
340 DOS_FULL_NAME full_name;
342 if (!DOSFS_GetFullName( path, FALSE, &full_name )) {
343 WARN("file %s bad name\n",path);
347 file=fopen(full_name.long_name ,mode);
349 DWORD access = 0, creation = 0;
351 if ((strchr(mode,'r')&&strchr(mode,'a'))||
352 (strchr(mode,'r')&&strchr(mode,'w'))||
353 (strchr(mode,'w')&&strchr(mode,'a')))
358 access = GENERIC_READ;
359 creation = OPEN_EXISTING;
360 if (mode[1] == '+') access |= GENERIC_WRITE;
362 else if (mode[0] == 'w')
364 access = GENERIC_WRITE;
365 creation = CREATE_ALWAYS;
366 if (mode[1] == '+') access |= GENERIC_READ;
368 else if (mode[0] == 'a')
370 /* FIXME: there is no O_APPEND in CreateFile, should emulate it */
371 access = GENERIC_WRITE;
372 creation = OPEN_ALWAYS;
373 if (mode[1] == '+') access |= GENERIC_READ;
375 /* FIXME: should handle text/binary mode */
377 if ((handle = CreateFileA( path, access, FILE_SHARE_READ | FILE_SHARE_WRITE,
378 NULL, creation, FILE_ATTRIBUTE_NORMAL,
379 -1 )) != INVALID_HANDLE_VALUE)
381 file = HeapAlloc( GetProcessHeap(), 0, sizeof(*file) );
382 file->handle = handle;
384 TRACE("file %s mode %s got handle %d file %p\n",
385 path,mode,handle,file);
389 /*********************************************************************
392 DWORD __cdecl CRTDLL_fread(LPVOID ptr, INT size, INT nmemb, CRTDLL_FILE *file)
398 /* If we would honour CR/LF <-> LF translation, we could do it like this.
399 We should keep track of all files opened, and probably files with \
400 known binary extensions must be unchanged */
401 while ( (i < (nmemb*size)) && (ret==1)) {
402 ret=fread(temp,1,1,file);
403 TRACE("got %c 0x%02x ret %d\n",
404 (isalpha(*(unsigned char*)temp))? *(unsigned char*)temp:
405 ' ',*(unsigned char*)temp, ret);
406 if (*(unsigned char*)temp != 0xd) { /* skip CR */
411 TRACE("skipping ^M\n");
413 TRACE("0x%08x items of size %d from file %p to %p\n",
414 nmemb,size,file,ptr,);
422 TRACE("0x%08x items of size %d from file %p to %p\n",
423 nmemb,size,file,ptr);
424 if (!ReadFile( file->handle, ptr, size * nmemb, &ret, NULL ))
430 /*********************************************************************
431 * freopen (CRTDLL.379)
436 DWORD __cdecl CRTDLL_freopen(LPCSTR path, LPCSTR mode, LPVOID stream)
438 FIXME(":(%s,%s,%p): stub\n", path, mode, stream);
439 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
443 /*********************************************************************
444 * fscanf (CRTDLL.381)
446 INT __cdecl CRTDLL_fscanf( CRTDLL_FILE *stream, LPSTR format, ... )
452 va_start( valist, format );
454 res = vfscanf( xlat_file_ptr(stream), format, valist );
463 /*********************************************************************
464 * _lseek (CRTDLL.179)
466 LONG __cdecl CRTDLL__lseek( INT fd, LONG offset, INT whence)
468 TRACE("fd %d to 0x%08lx pos %s\n",
469 fd,offset,(whence==SEEK_SET)?"SEEK_SET":
470 (whence==SEEK_CUR)?"SEEK_CUR":
471 (whence==SEEK_END)?"SEEK_END":"UNKNOWN");
472 return SetFilePointer( fd, offset, NULL, whence );
475 /*********************************************************************
478 LONG __cdecl CRTDLL_fseek( CRTDLL_FILE *file, LONG offset, INT whence)
480 TRACE("file %p to 0x%08lx pos %s\n",
481 file,offset,(whence==SEEK_SET)?"SEEK_SET":
482 (whence==SEEK_CUR)?"SEEK_CUR":
483 (whence==SEEK_END)?"SEEK_END":"UNKNOWN");
484 if (SetFilePointer( file->handle, offset, NULL, whence ) != 0xffffffff)
490 /*********************************************************************
491 * fsetpos (CRTDLL.383)
493 INT __cdecl CRTDLL_fsetpos( CRTDLL_FILE *file, INT *pos )
495 TRACE("file %p pos %d\n", file, *pos );
496 return CRTDLL_fseek(file, *pos, SEEK_SET);
499 /*********************************************************************
502 LONG __cdecl CRTDLL_ftell( CRTDLL_FILE *file )
504 return SetFilePointer( file->handle, 0, NULL, SEEK_CUR );
507 /*********************************************************************
508 * fwrite (CRTDLL.386)
510 DWORD __cdecl CRTDLL_fwrite( LPVOID ptr, INT size, INT nmemb, CRTDLL_FILE *file )
514 TRACE("0x%08x items of size %d to file %p(%d) from %p\n",
515 nmemb,size,file,file-(CRTDLL_FILE*)CRTDLL_iob,ptr);
518 if (!WriteFile( file->handle, ptr, size * nmemb, &ret, NULL ))
523 /*********************************************************************
524 * setbuf (CRTDLL.452)
526 INT __cdecl CRTDLL_setbuf(CRTDLL_FILE *file, LPSTR buf)
528 TRACE("(file %p buf %p)\n", file, buf);
529 /* this doesn't work:"void value not ignored as it ought to be"
530 return setbuf(file,buf);
532 /* FIXME: no buffering for now */
536 /*********************************************************************
537 * _open_osfhandle (CRTDLL.240)
539 HFILE __cdecl CRTDLL__open_osfhandle(LONG osfhandle, INT flags)
544 case STD_INPUT_HANDLE :
548 case STD_OUTPUT_HANDLE:
552 case STD_ERROR_HANDLE:
559 TRACE("(handle %08lx,flags %d) return %d\n",
560 osfhandle,flags,handle);
565 /*********************************************************************
568 void __cdecl CRTDLL_srand(DWORD seed)
570 /* FIXME: should of course be thread? process? local */
574 /*********************************************************************
575 * vfprintf (CRTDLL.373)
577 INT __cdecl CRTDLL_vfprintf( CRTDLL_FILE *file, LPSTR format, va_list args )
579 char buffer[2048]; /* FIXME... */
581 vsprintf( buffer, format, args );
582 return CRTDLL_fwrite( buffer, 1, strlen(buffer), file );
585 /*********************************************************************
586 * fprintf (CRTDLL.373)
588 INT __cdecl CRTDLL_fprintf( CRTDLL_FILE *file, LPSTR format, ... )
593 va_start( valist, format );
594 res = CRTDLL_vfprintf( file, format, valist );
599 /*********************************************************************
602 time_t __cdecl CRTDLL_time(time_t *timeptr)
604 time_t curtime = time(NULL);
611 /*********************************************************************
612 * difftime (CRTDLL.357)
614 double __cdecl CRTDLL_difftime (time_t time1, time_t time2)
618 timediff = (double)(time1 - time2);
622 /*********************************************************************
625 clock_t __cdecl CRTDLL_clock(void)
631 res = alltimes.tms_utime + alltimes.tms_stime+
632 alltimes.tms_cutime + alltimes.tms_cstime;
633 /* Fixme: We need some symbolic representation
634 for (Hostsystem_)CLOCKS_PER_SEC
635 and (Emulated_system_)CLOCKS_PER_SEC
636 10 holds only for Windows/Linux_i86)
641 /*********************************************************************
642 * _isatty (CRTDLL.137)
644 BOOL __cdecl CRTDLL__isatty(DWORD x)
650 /*********************************************************************
654 INT __cdecl CRTDLL__read(INT fd, LPVOID buf, UINT count)
656 TRACE("0x%08x bytes fd %d to %p\n", count,fd,buf);
657 if (!fd) fd = GetStdHandle( STD_INPUT_HANDLE );
658 return _lread( fd, buf, count );
661 /*********************************************************************
662 * _write (CRTDLL.332)
664 INT __cdecl CRTDLL__write(INT fd,LPCVOID buf,UINT count)
671 len = (UINT)write(fd,buf,(LONG)count);
673 len = _lwrite(fd,buf,count);
674 TRACE("%d/%d byte to dfh %d from %p,\n",
680 /*********************************************************************
683 * FIXME: What the heck is the difference between
684 * FIXME _c_exit (CRTDLL.47)
685 * FIXME _cexit (CRTDLL.49)
686 * FIXME _exit (CRTDLL.87)
687 * FIXME exit (CRTDLL.359)
689 * atexit-processing comes to mind -- MW.
692 void __cdecl CRTDLL__cexit(INT ret)
699 /*********************************************************************
702 void __cdecl CRTDLL_exit(DWORD ret)
704 TRACE("(%ld)\n",ret);
709 /*********************************************************************
710 * _abnormal_termination (CRTDLL.36)
712 INT __cdecl CRTDLL__abnormal_termination(void)
719 /*********************************************************************
720 * _access (CRTDLL.37)
722 INT __cdecl CRTDLL__access(LPCSTR filename, INT mode)
724 DWORD attr = GetFileAttributesA(filename);
728 if (GetLastError() == ERROR_INVALID_ACCESS)
735 if ((attr & FILE_ATTRIBUTE_READONLY) && (mode & W_OK))
745 /*********************************************************************
746 * fflush (CRTDLL.365)
748 INT __cdecl CRTDLL_fflush( CRTDLL_FILE *file )
750 return FlushFileBuffers( file->handle ) ? 0 : -1;
754 /*********************************************************************
757 INT __cdecl CRTDLL_rand()
759 return (rand() & CRTDLL_RAND_MAX);
763 /*********************************************************************
764 * putchar (CRTDLL.442)
766 void __cdecl CRTDLL_putchar( INT x )
772 /*********************************************************************
775 INT __cdecl CRTDLL_fputc( INT c, CRTDLL_FILE *file )
779 TRACE("%c to file %p\n",c,file);
780 if (!WriteFile( file->handle, &ch, 1, &res, NULL )) return -1;
785 /*********************************************************************
788 INT __cdecl CRTDLL_fputs( LPCSTR s, CRTDLL_FILE *file )
791 TRACE("%s to file %p\n",s,file);
792 if (!WriteFile( file->handle, s, strlen(s), &res, NULL )) return -1;
797 /*********************************************************************
800 INT __cdecl CRTDLL_puts(LPCSTR s)
807 /*********************************************************************
810 INT __cdecl CRTDLL_putc( INT c, CRTDLL_FILE *file )
812 return CRTDLL_fputc( c, file );
815 /*********************************************************************
818 INT __cdecl CRTDLL_fgetc( CRTDLL_FILE *file )
822 if (!ReadFile( file->handle, &ch, 1, &res, NULL )) return -1;
823 if (res != 1) return -1;
828 /*********************************************************************
831 INT __cdecl CRTDLL_getc( CRTDLL_FILE *file )
833 return CRTDLL_fgetc( file );
837 /*********************************************************************
840 CHAR* __cdecl CRTDLL_fgets( LPSTR s, INT size, CRTDLL_FILE *file )
845 /* BAD, for the whole WINE process blocks... just done this way to test
846 * windows95's ftp.exe.
849 for(cc = CRTDLL_fgetc(file); cc != EOF && cc != '\n'; cc = CRTDLL_fgetc(file))
852 if (--size <= 0) break;
855 if ((cc == EOF) &&(s == buf_start)) /* If nothing read, return 0*/
862 TRACE("got '%s'\n", buf_start);
867 /*********************************************************************
870 LPSTR __cdecl CRTDLL_gets(LPSTR buf)
873 LPSTR buf_start = buf;
875 /* BAD, for the whole WINE process blocks... just done this way to test
876 * windows95's ftp.exe.
879 for(cc = fgetc(stdin); cc != EOF && cc != '\n'; cc = fgetc(stdin))
880 if(cc != '\r') *buf++ = (char)cc;
884 TRACE("got '%s'\n", buf_start);
889 /*********************************************************************
892 UINT __cdecl CRTDLL__rotl(UINT x,INT shift)
894 unsigned int ret = (x >> shift)|( x >>((sizeof(x))-shift));
896 TRACE("got 0x%08x rot %d ret 0x%08x\n",
901 /*********************************************************************
902 * _lrotl (CRTDLL.176)
904 DWORD __cdecl CRTDLL__lrotl(DWORD x,INT shift)
906 unsigned long ret = (x >> shift)|( x >>((sizeof(x))-shift));
908 TRACE("got 0x%08lx rot %d ret 0x%08lx\n",
915 /*********************************************************************
916 * _mbsicmp (CRTDLL.204)
918 int __cdecl CRTDLL__mbsicmp(unsigned char *x,unsigned char *y)
925 /* FIXME: MBCS handling... */
934 /*********************************************************************
935 * vsprintf (CRTDLL.500)
937 INT __cdecl CRTDLL_vsprintf( LPSTR buffer, LPCSTR spec, va_list args )
939 return wvsprintfA( buffer, spec, args );
942 /*********************************************************************
943 * _vsprintf (CRTDLL.@)
945 INT __cdecl CRTDLL__vsnprintf(LPSTR buffer,DWORD size,LPCSTR spec,va_list args )
947 return wvsnprintfA( buffer, size, spec, args );
950 /*********************************************************************
951 * vswprintf (CRTDLL.501)
953 INT __cdecl CRTDLL_vswprintf( LPWSTR buffer, LPCWSTR spec, va_list args )
955 return wvsprintfW( buffer, spec, args );
959 /*********************************************************************
960 * system (CRTDLL.485)
962 INT __cdecl CRTDLL_system(LPSTR x)
964 #define SYSBUF_LENGTH 1500
965 char buffer[SYSBUF_LENGTH];
966 unsigned char *y = x;
970 sprintf( buffer, "%s \"", argv0 );
971 bp = buffer + strlen(buffer);
972 i = strlen(buffer) + strlen(x) +2;
974 /* Calculate needed buffer size to prevent overflow. */
979 /* If buffer too short, exit. */
980 if (i > SYSBUF_LENGTH) {
981 TRACE("_system buffer to small\n");
990 if (*(y-1) =='\\') *bp++ = '\\';
992 /* Remove spaces from end of string. */
993 while (*(y-1) == ' ') {
998 TRACE("_system got '%s', executing '%s'\n",x,buffer);
1000 return system(buffer);
1003 /*********************************************************************
1004 * longjmp (CRTDLL.426)
1006 VOID __cdecl CRTDLL_longjmp(jmp_buf env, int val)
1008 FIXME("CRTDLL_longjmp semistup, expect crash\n");
1012 /*********************************************************************
1013 * malloc (CRTDLL.427)
1015 VOID* __cdecl CRTDLL_malloc(DWORD size)
1017 return HeapAlloc(GetProcessHeap(),0,size);
1020 /*********************************************************************
1023 VOID* __cdecl CRTDLL_new(DWORD size)
1026 if(!(result = HeapAlloc(GetProcessHeap(),0,size)) && new_handler)
1031 /*********************************************************************
1032 * set_new_handler(CRTDLL.003)
1034 new_handler_type __cdecl CRTDLL_set_new_handler(new_handler_type func)
1036 new_handler_type old_handler = new_handler;
1041 /*********************************************************************
1042 * calloc (CRTDLL.350)
1044 VOID* __cdecl CRTDLL_calloc(DWORD size, DWORD count)
1046 return HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, size * count );
1049 /*********************************************************************
1050 * realloc (CRTDLL.447)
1052 VOID* __cdecl CRTDLL_realloc( VOID *ptr, DWORD size )
1054 return HeapReAlloc( GetProcessHeap(), 0, ptr, size );
1057 /*********************************************************************
1060 VOID __cdecl CRTDLL_free(LPVOID ptr)
1062 HeapFree(GetProcessHeap(),0,ptr);
1065 /*********************************************************************
1066 * delete (CRTDLL.002)
1068 VOID __cdecl CRTDLL_delete(VOID* ptr)
1070 HeapFree(GetProcessHeap(),0,ptr);
1073 /*********************************************************************
1074 * _strdup (CRTDLL.285)
1076 LPSTR __cdecl CRTDLL__strdup(LPCSTR ptr)
1078 return HEAP_strdupA(GetProcessHeap(),0,ptr);
1081 /*********************************************************************
1082 * fclose (CRTDLL.362)
1084 INT __cdecl CRTDLL_fclose( CRTDLL_FILE *file )
1086 TRACE("%p\n", file );
1087 if (!CloseHandle( file->handle )) return -1;
1088 HeapFree( GetProcessHeap(), 0, file );
1092 /*********************************************************************
1093 * _unlink (CRTDLL.315)
1095 INT __cdecl CRTDLL__unlink(LPCSTR pathname)
1097 return DeleteFileA( pathname ) ? 0 : -1;
1100 /*********************************************************************
1101 * rename (CRTDLL.449)
1103 INT __cdecl CRTDLL_rename(LPCSTR oldpath,LPCSTR newpath)
1105 BOOL ok = MoveFileExA( oldpath, newpath, MOVEFILE_REPLACE_EXISTING );
1110 /*********************************************************************
1111 * _stat (CRTDLL.280)
1129 int __cdecl CRTDLL__stat(const char * filename, struct win_stat * buf)
1132 DOS_FULL_NAME full_name;
1135 if (!DOSFS_GetFullName( filename, TRUE, &full_name ))
1137 WARN("CRTDLL__stat filename %s bad name\n",filename);
1140 ret=stat(full_name.long_name,&mystat);
1141 TRACE("CRTDLL__stat %s\n", filename);
1145 /* FIXME: should check what Windows returns */
1147 buf->win_st_dev = mystat.st_dev;
1148 buf->win_st_ino = mystat.st_ino;
1149 buf->win_st_mode = mystat.st_mode;
1150 buf->win_st_nlink = mystat.st_nlink;
1151 buf->win_st_uid = mystat.st_uid;
1152 buf->win_st_gid = mystat.st_gid;
1153 buf->win_st_rdev = mystat.st_rdev;
1154 buf->win_st_size = mystat.st_size;
1155 buf->win_st_atime = mystat.st_atime;
1156 buf->win_st_mtime = mystat.st_mtime;
1157 buf->win_st_ctime = mystat.st_ctime;
1161 /*********************************************************************
1162 * _open (CRTDLL.239)
1164 HFILE __cdecl CRTDLL__open(LPCSTR path,INT flags)
1166 DWORD access = 0, creation = 0;
1170 the flags in lcc's header differ from the ones in Linux, e.g.
1171 Linux: define O_APPEND 02000 (= 0x400)
1172 lcc: define _O_APPEND 0x0008
1173 so here a scheme to translate them
1174 Probably lcc is wrong here, but at least a hack to get is going
1178 case O_RDONLY: access |= GENERIC_READ; break;
1179 case O_WRONLY: access |= GENERIC_WRITE; break;
1180 case O_RDWR: access |= GENERIC_WRITE | GENERIC_READ; break;
1183 if (flags & 0x0100) /* O_CREAT */
1185 if (flags & 0x0400) /* O_EXCL */
1186 creation = CREATE_NEW;
1187 else if (flags & 0x0200) /* O_TRUNC */
1188 creation = CREATE_ALWAYS;
1190 creation = OPEN_ALWAYS;
1192 else /* no O_CREAT */
1194 if (flags & 0x0200) /* O_TRUNC */
1195 creation = TRUNCATE_EXISTING;
1197 creation = OPEN_EXISTING;
1199 if (flags & 0x0008) /* O_APPEND */
1200 FIXME("O_APPEND not supported\n" );
1201 if (!(flags & 0x8000 /* O_BINARY */ ) || (flags & 0x4000 /* O_TEXT */))
1202 FIXME(":text mode not supported\n");
1204 TRACE("CRTDLL_open file unsupported flags 0x%04x\n",flags);
1207 ret = CreateFileA( path, access, FILE_SHARE_READ | FILE_SHARE_WRITE,
1208 NULL, creation, FILE_ATTRIBUTE_NORMAL, -1 );
1209 TRACE("CRTDLL_open file %s mode 0x%04x got handle %d\n", path,flags,ret);
1213 /*********************************************************************
1214 * _close (CRTDLL.57)
1216 INT __cdecl CRTDLL__close(HFILE fd)
1218 int ret=_lclose(fd);
1227 /*********************************************************************
1229 * FIXME: Care for large files
1230 * FIXME: Check errors
1232 INT __cdecl CRTDLL_feof( CRTDLL_FILE *file )
1234 DWORD curpos=SetFilePointer( file->handle, 0, NULL, SEEK_CUR );
1235 DWORD endpos=SetFilePointer( file->handle, 0, NULL, FILE_END );
1240 SetFilePointer( file->handle, curpos,0,FILE_BEGIN);
1244 /*********************************************************************
1245 * setlocale (CRTDLL.453)
1247 LPSTR __cdecl CRTDLL_setlocale(INT category,LPCSTR locale)
1252 case CRTDLL_LC_ALL: categorystr="LC_ALL";break;
1253 case CRTDLL_LC_COLLATE: categorystr="LC_COLLATE";break;
1254 case CRTDLL_LC_CTYPE: categorystr="LC_CTYPE";break;
1255 case CRTDLL_LC_MONETARY: categorystr="LC_MONETARY";break;
1256 case CRTDLL_LC_NUMERIC: categorystr="LC_NUMERIC";break;
1257 case CRTDLL_LC_TIME: categorystr="LC_TIME";break;
1258 default: categorystr = "UNKNOWN?";break;
1260 FIXME("(%s,%s),stub!\n",categorystr,locale);
1264 /*********************************************************************
1265 * _setmode (CRTDLL.265)
1266 * FIXME: At present we ignore the request to translate CR/LF to LF.
1268 * We allways translate when we read with fgets, we never do with fread
1271 INT __cdecl CRTDLL__setmode( INT fh,INT mode)
1274 #define O_TEXT 0x4000
1275 #define O_BINARY 0x8000
1277 FIXME("on fhandle %d mode %s, STUB.\n",
1278 fh,(mode=O_TEXT)?"O_TEXT":
1279 (mode=O_BINARY)?"O_BINARY":"UNKNOWN");
1283 /*********************************************************************
1284 * _fpreset (CRTDLL.107)
1286 VOID __cdecl CRTDLL__fpreset(void)
1291 /*********************************************************************
1292 * atexit (CRTDLL.345)
1294 INT __cdecl CRTDLL_atexit(LPVOID x)
1296 FIXME("(%p), STUB.\n",x);
1297 return 0; /* successful */
1300 /*********************************************************************
1301 * _isctype (CRTDLL.138)
1303 BOOL __cdecl CRTDLL__isctype(CHAR x,CHAR type)
1305 if ((type & CRTDLL_SPACE) && isspace(x))
1307 if ((type & CRTDLL_PUNCT) && ispunct(x))
1309 if ((type & CRTDLL_LOWER) && islower(x))
1311 if ((type & CRTDLL_UPPER) && isupper(x))
1313 if ((type & CRTDLL_ALPHA) && isalpha(x))
1315 if ((type & CRTDLL_DIGIT) && isdigit(x))
1317 if ((type & CRTDLL_CONTROL) && iscntrl(x))
1319 /* check CRTDLL_LEADBYTE */
1323 /*********************************************************************
1324 * _chdrive (CRTDLL.52)
1326 * newdir [I] drive to change to, A=1
1329 BOOL __cdecl CRTDLL__chdrive(INT newdrive)
1331 /* FIXME: generates errnos */
1332 return DRIVE_SetCurrentDrive(newdrive-1);
1335 /*********************************************************************
1336 * _chdir (CRTDLL.51)
1338 INT __cdecl CRTDLL__chdir(LPCSTR newdir)
1340 if (!SetCurrentDirectoryA(newdir))
1345 /*********************************************************************
1346 * _fullpath (CRTDLL.114)
1348 LPSTR __cdecl CRTDLL__fullpath(LPSTR buf, LPCSTR name, INT size)
1350 DOS_FULL_NAME full_name;
1355 if(!(buf = CRTDLL_malloc(size))) return NULL;
1357 if (!DOSFS_GetFullName( name, FALSE, &full_name )) return NULL;
1358 lstrcpynA(buf,full_name.short_name,size);
1359 TRACE("CRTDLL_fullpath got %s\n",buf);
1363 /*********************************************************************
1364 * _splitpath (CRTDLL.279)
1366 VOID __cdecl CRTDLL__splitpath(LPCSTR path, LPSTR drive, LPSTR directory, LPSTR filename, LPSTR extension )
1369 directory includes leading and trailing (forward and backward slashes)
1370 filename without dot and slashes
1371 extension with leading dot
1373 char * drivechar,*dirchar,*namechar;
1375 TRACE("CRTDLL__splitpath got %s\n",path);
1377 drivechar = strchr(path,':');
1378 dirchar = strrchr(path,'/');
1379 namechar = strrchr(path,'\\');
1380 dirchar = max(dirchar,namechar);
1382 namechar = strrchr(dirchar,'.');
1384 namechar = strrchr(path,'.');
1392 strncat(drive,path,drivechar-path+1);
1401 strncat(directory,path,dirchar-path+1);
1410 strncat(filename,path,namechar-path);
1414 strcat(extension,namechar);
1419 TRACE("CRTDLL__splitpath found %s %s %s %s\n",drive,directory,filename,extension);
1424 /*********************************************************************
1425 * _makepath (CRTDLL.182)
1428 VOID __cdecl CRTDLL__makepath(LPSTR path, LPCSTR drive,
1429 LPCSTR directory, LPCSTR filename,
1433 TRACE("CRTDLL__makepath got %s %s %s %s\n", drive, directory,
1434 filename, extension);
1442 sprintf(path, "%c:", drive[0]);
1445 if ( directory[0] ) {
1446 strcat(path, directory);
1447 ch = path[strlen(path)-1];
1448 if (ch != '/' && ch != '\\')
1452 if ( filename[0] ) {
1453 strcat(path, filename);
1455 if ( extension[0] ) {
1456 if ( extension[0] != '.' ) {
1459 strcat(path,extension);
1464 TRACE("CRTDLL__makepath returns %s\n",path);
1467 /*********************************************************************
1468 * _getcwd (CRTDLL.120)
1470 CHAR* __cdecl CRTDLL__getcwd(LPSTR buf, INT size)
1477 if (size < 0) /* allocate as big as nescessary */
1478 len =GetCurrentDirectoryA(1,test) + 1;
1479 if(!(buf = CRTDLL_malloc(len)))
1481 /* set error to OutOfRange */
1486 if(!(len =GetCurrentDirectoryA(len,buf)))
1492 /* set error to ERANGE */
1493 TRACE("CRTDLL_getcwd buffer to small\n");
1500 /*********************************************************************
1501 * _getdcwd (CRTDLL.121)
1503 CHAR* __cdecl CRTDLL__getdcwd(INT drive,LPSTR buf, INT size)
1508 FIXME("(\"%c:\",%s,%d)\n",drive+'A',buf,size);
1511 if (size < 0) /* allocate as big as nescessary */
1512 len =GetCurrentDirectoryA(1,test) + 1;
1513 if(!(buf = CRTDLL_malloc(len)))
1515 /* set error to OutOfRange */
1520 if(!(len =GetCurrentDirectoryA(len,buf)))
1526 /* set error to ERANGE */
1527 TRACE("buffer to small\n");
1534 /*********************************************************************
1535 * _getdrive (CRTDLL.124)
1537 * Return current drive, 1 for A, 2 for B
1539 INT __cdecl CRTDLL__getdrive(VOID)
1541 return DRIVE_GetCurrentDrive() + 1;
1544 /*********************************************************************
1545 * _mkdir (CRTDLL.234)
1547 INT __cdecl CRTDLL__mkdir(LPCSTR newdir)
1549 if (!CreateDirectoryA(newdir,NULL))
1554 /*********************************************************************
1555 * remove (CRTDLL.448)
1557 INT __cdecl CRTDLL_remove(LPCSTR file)
1559 if (!DeleteFileA(file))
1564 /*********************************************************************
1565 * _errno (CRTDLL.52)
1566 * Yes, this is a function.
1568 LPINT __cdecl CRTDLL__errno()
1570 static int crtdllerrno;
1572 /* FIXME: we should set the error at the failing function call time */
1573 crtdllerrno = LastErrorToErrno(GetLastError());
1574 return &crtdllerrno;
1577 /*********************************************************************
1578 * _tempnam (CRTDLL.305)
1581 LPSTR __cdecl CRTDLL__tempnam(LPCSTR dir, LPCSTR prefix)
1585 DOS_FULL_NAME tempname;
1587 if ((ret = tempnam(dir,prefix))==NULL) {
1588 WARN("Unable to get unique filename\n");
1591 if (!DOSFS_GetFullName(ret,FALSE,&tempname))
1593 TRACE("Wrong path?\n");
1597 if ((ret = CRTDLL_malloc(strlen(tempname.short_name)+1)) == NULL) {
1598 WARN("CRTDL_malloc for shortname failed\n");
1601 if ((ret = strcpy(ret,tempname.short_name)) == NULL) {
1602 WARN("Malloc for shortname failed\n");
1606 TRACE("dir %s prefix %s got %s\n",
1611 /*********************************************************************
1612 * tmpnam (CRTDLL.490)
1614 * lcclnk from lcc-win32 relies on a terminating dot in the name returned
1617 LPSTR __cdecl CRTDLL_tmpnam(LPSTR s)
1621 if ((ret =tmpnam(s))== NULL) {
1622 WARN("Unable to get unique filename\n");
1625 if (!DOSFS_GetFullName(ret,FALSE,&CRTDLL_tmpname))
1627 TRACE("Wrong path?\n");
1630 strcat(CRTDLL_tmpname.short_name,".");
1631 TRACE("for buf %p got %s\n",
1632 s,CRTDLL_tmpname.short_name);
1633 TRACE("long got %s\n",
1634 CRTDLL_tmpname.long_name);
1636 return strcpy(s,CRTDLL_tmpname.short_name);
1638 return CRTDLL_tmpname.short_name;
1642 /*********************************************************************
1643 * _itoa (CRTDLL.165)
1645 LPSTR __cdecl CRTDLL__itoa(INT x,LPSTR buf,INT buflen)
1647 wsnprintfA(buf,buflen,"%d",x);
1652 typedef VOID (*sig_handler_type)(VOID);
1654 /*********************************************************************
1655 * signal (CRTDLL.455)
1657 void * __cdecl CRTDLL_signal(int sig, sig_handler_type ptr)
1659 FIXME("(%d %p):stub.\n", sig, ptr);
1663 /*********************************************************************
1664 * _sleep (CRTDLL.267)
1666 VOID __cdecl CRTDLL__sleep(unsigned long timeout)
1668 TRACE("CRTDLL__sleep for %ld milliseconds\n",timeout);
1669 Sleep((timeout)?timeout:1);
1672 /*********************************************************************
1673 * getenv (CRTDLL.437)
1675 LPSTR __cdecl CRTDLL_getenv(const char *name)
1677 LPSTR environ = GetEnvironmentStringsA();
1678 LPSTR pp,pos = NULL;
1679 unsigned int length;
1681 for (pp = environ; (*pp); pp = pp + strlen(pp) +1)
1683 pos =strchr(pp,'=');
1687 length = strlen(pp);
1688 if (!strncmp(pp,name,length)) break;
1693 TRACE("got %s\n",pp);
1695 FreeEnvironmentStringsA( environ );
1699 /*********************************************************************
1700 * _mbsrchr (CRTDLL.223)
1702 LPSTR __cdecl CRTDLL__mbsrchr(LPSTR s,CHAR x) {
1703 /* FIXME: handle multibyte strings */
1704 return strrchr(s,x);
1707 /*********************************************************************
1708 * __dllonexit (CRTDLL.25)
1710 VOID __cdecl CRTDLL___dllonexit ()
1715 /*********************************************************************
1716 * _strdate (CRTDLL.283)
1718 LPSTR __cdecl CRTDLL__strdate (LPSTR date)
1719 { FIXME("%p stub\n", date);
1723 /*********************************************************************
1724 * _strtime (CRTDLL.299)
1726 LPSTR __cdecl CRTDLL__strtime (LPSTR date)
1727 { FIXME("%p stub\n", date);
1731 /*********************************************************************
1732 * _except_handler2 (CRTDLL.78)
1734 INT __cdecl CRTDLL__except_handler2 (
1735 PEXCEPTION_RECORD rec,
1736 PEXCEPTION_FRAME frame,
1738 PEXCEPTION_FRAME *dispatcher)
1740 FIXME ("exception %lx flags=%lx at %p handler=%p %p %p stub\n",
1741 rec->ExceptionCode, rec->ExceptionFlags, rec->ExceptionAddress,
1742 frame->Handler, context, dispatcher);
1743 return ExceptionContinueSearch;