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 * vswprintf (CRTDLL.501)
945 INT __cdecl
CRTDLL_vswprintf( LPWSTR buffer
, LPCWSTR spec
, va_list args
)
947 return wvsprintfW( buffer
, spec
, args
);
951 /*********************************************************************
952 * system (CRTDLL.485)
954 INT __cdecl
CRTDLL_system(LPSTR x
)
956 #define SYSBUF_LENGTH 1500
957 char buffer
[SYSBUF_LENGTH
];
958 unsigned char *y
= x
;
962 sprintf( buffer
, "%s \"", argv0
);
963 bp
= buffer
+ strlen(buffer
);
964 i
= strlen(buffer
) + strlen(x
) +2;
966 /* Calculate needed buffer size to prevent overflow. */
971 /* If buffer too short, exit. */
972 if (i
> SYSBUF_LENGTH
) {
973 TRACE("_system buffer to small\n");
982 if (*(y
-1) =='\\') *bp
++ = '\\';
984 /* Remove spaces from end of string. */
985 while (*(y
-1) == ' ') {
990 TRACE("_system got '%s', executing '%s'\n",x
,buffer
);
992 return system(buffer
);
995 /*********************************************************************
996 * longjmp (CRTDLL.426)
998 VOID __cdecl
CRTDLL_longjmp(jmp_buf env
, int val
)
1000 FIXME("CRTDLL_longjmp semistup, expect crash\n");
1004 /*********************************************************************
1005 * malloc (CRTDLL.427)
1007 VOID
* __cdecl
CRTDLL_malloc(DWORD size
)
1009 return HeapAlloc(GetProcessHeap(),0,size
);
1012 /*********************************************************************
1015 VOID
* __cdecl
CRTDLL_new(DWORD size
)
1018 if(!(result
= HeapAlloc(GetProcessHeap(),0,size
)) && new_handler
)
1023 /*********************************************************************
1024 * set_new_handler(CRTDLL.003)
1026 new_handler_type __cdecl
CRTDLL_set_new_handler(new_handler_type func
)
1028 new_handler_type old_handler
= new_handler
;
1033 /*********************************************************************
1034 * calloc (CRTDLL.350)
1036 VOID
* __cdecl
CRTDLL_calloc(DWORD size
, DWORD count
)
1038 return HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
, size
* count
);
1041 /*********************************************************************
1042 * realloc (CRTDLL.447)
1044 VOID
* __cdecl
CRTDLL_realloc( VOID
*ptr
, DWORD size
)
1046 return HeapReAlloc( GetProcessHeap(), 0, ptr
, size
);
1049 /*********************************************************************
1052 VOID __cdecl
CRTDLL_free(LPVOID ptr
)
1054 HeapFree(GetProcessHeap(),0,ptr
);
1057 /*********************************************************************
1058 * delete (CRTDLL.002)
1060 VOID __cdecl
CRTDLL_delete(VOID
* ptr
)
1062 HeapFree(GetProcessHeap(),0,ptr
);
1065 /*********************************************************************
1066 * _strdup (CRTDLL.285)
1068 LPSTR __cdecl
CRTDLL__strdup(LPCSTR ptr
)
1070 return HEAP_strdupA(GetProcessHeap(),0,ptr
);
1073 /*********************************************************************
1074 * fclose (CRTDLL.362)
1076 INT __cdecl
CRTDLL_fclose( CRTDLL_FILE
*file
)
1078 TRACE("%p\n", file
);
1079 if (!CloseHandle( file
->handle
)) return -1;
1080 HeapFree( GetProcessHeap(), 0, file
);
1084 /*********************************************************************
1085 * _unlink (CRTDLL.315)
1087 INT __cdecl
CRTDLL__unlink(LPCSTR pathname
)
1089 return DeleteFileA( pathname
) ? 0 : -1;
1092 /*********************************************************************
1093 * rename (CRTDLL.449)
1095 INT __cdecl
CRTDLL_rename(LPCSTR oldpath
,LPCSTR newpath
)
1097 BOOL ok
= MoveFileExA( oldpath
, newpath
, MOVEFILE_REPLACE_EXISTING
);
1102 /*********************************************************************
1103 * _stat (CRTDLL.280)
1121 int __cdecl
CRTDLL__stat(const char * filename
, struct win_stat
* buf
)
1124 DOS_FULL_NAME full_name
;
1127 if (!DOSFS_GetFullName( filename
, TRUE
, &full_name
))
1129 WARN("CRTDLL__stat filename %s bad name\n",filename
);
1132 ret
=stat(full_name
.long_name
,&mystat
);
1133 TRACE("CRTDLL__stat %s\n", filename
);
1137 /* FIXME: should check what Windows returns */
1139 buf
->win_st_dev
= mystat
.st_dev
;
1140 buf
->win_st_ino
= mystat
.st_ino
;
1141 buf
->win_st_mode
= mystat
.st_mode
;
1142 buf
->win_st_nlink
= mystat
.st_nlink
;
1143 buf
->win_st_uid
= mystat
.st_uid
;
1144 buf
->win_st_gid
= mystat
.st_gid
;
1145 buf
->win_st_rdev
= mystat
.st_rdev
;
1146 buf
->win_st_size
= mystat
.st_size
;
1147 buf
->win_st_atime
= mystat
.st_atime
;
1148 buf
->win_st_mtime
= mystat
.st_mtime
;
1149 buf
->win_st_ctime
= mystat
.st_ctime
;
1153 /*********************************************************************
1154 * _open (CRTDLL.239)
1156 HFILE __cdecl
CRTDLL__open(LPCSTR path
,INT flags
)
1158 DWORD access
= 0, creation
= 0;
1162 the flags in lcc's header differ from the ones in Linux, e.g.
1163 Linux: define O_APPEND 02000 (= 0x400)
1164 lcc: define _O_APPEND 0x0008
1165 so here a scheme to translate them
1166 Probably lcc is wrong here, but at least a hack to get is going
1170 case O_RDONLY
: access
|= GENERIC_READ
; break;
1171 case O_WRONLY
: access
|= GENERIC_WRITE
; break;
1172 case O_RDWR
: access
|= GENERIC_WRITE
| GENERIC_READ
; break;
1175 if (flags
& 0x0100) /* O_CREAT */
1177 if (flags
& 0x0400) /* O_EXCL */
1178 creation
= CREATE_NEW
;
1179 else if (flags
& 0x0200) /* O_TRUNC */
1180 creation
= CREATE_ALWAYS
;
1182 creation
= OPEN_ALWAYS
;
1184 else /* no O_CREAT */
1186 if (flags
& 0x0200) /* O_TRUNC */
1187 creation
= TRUNCATE_EXISTING
;
1189 creation
= OPEN_EXISTING
;
1191 if (flags
& 0x0008) /* O_APPEND */
1192 FIXME("O_APPEND not supported\n" );
1193 if (!(flags
& 0x8000 /* O_BINARY */ ) || (flags
& 0x4000 /* O_TEXT */))
1194 FIXME(":text mode not supported\n");
1196 TRACE("CRTDLL_open file unsupported flags 0x%04x\n",flags
);
1199 ret
= CreateFileA( path
, access
, FILE_SHARE_READ
| FILE_SHARE_WRITE
,
1200 NULL
, creation
, FILE_ATTRIBUTE_NORMAL
, -1 );
1201 TRACE("CRTDLL_open file %s mode 0x%04x got handle %d\n", path
,flags
,ret
);
1205 /*********************************************************************
1206 * _close (CRTDLL.57)
1208 INT __cdecl
CRTDLL__close(HFILE fd
)
1210 int ret
=_lclose(fd
);
1219 /*********************************************************************
1221 * FIXME: Care for large files
1222 * FIXME: Check errors
1224 INT __cdecl
CRTDLL_feof( CRTDLL_FILE
*file
)
1226 DWORD curpos
=SetFilePointer( file
->handle
, 0, NULL
, SEEK_CUR
);
1227 DWORD endpos
=SetFilePointer( file
->handle
, 0, NULL
, FILE_END
);
1232 SetFilePointer( file
->handle
, curpos
,0,FILE_BEGIN
);
1236 /*********************************************************************
1237 * setlocale (CRTDLL.453)
1239 LPSTR __cdecl
CRTDLL_setlocale(INT category
,LPCSTR locale
)
1244 case CRTDLL_LC_ALL
: categorystr
="LC_ALL";break;
1245 case CRTDLL_LC_COLLATE
: categorystr
="LC_COLLATE";break;
1246 case CRTDLL_LC_CTYPE
: categorystr
="LC_CTYPE";break;
1247 case CRTDLL_LC_MONETARY
: categorystr
="LC_MONETARY";break;
1248 case CRTDLL_LC_NUMERIC
: categorystr
="LC_NUMERIC";break;
1249 case CRTDLL_LC_TIME
: categorystr
="LC_TIME";break;
1250 default: categorystr
= "UNKNOWN?";break;
1252 FIXME("(%s,%s),stub!\n",categorystr
,locale
);
1256 /*********************************************************************
1257 * _setmode (CRTDLL.265)
1258 * FIXME: At present we ignore the request to translate CR/LF to LF.
1260 * We allways translate when we read with fgets, we never do with fread
1263 INT __cdecl
CRTDLL__setmode( INT fh
,INT mode
)
1266 #define O_TEXT 0x4000
1267 #define O_BINARY 0x8000
1269 FIXME("on fhandle %d mode %s, STUB.\n",
1270 fh
,(mode
=O_TEXT
)?"O_TEXT":
1271 (mode
=O_BINARY
)?"O_BINARY":"UNKNOWN");
1275 /*********************************************************************
1276 * _fpreset (CRTDLL.107)
1278 VOID __cdecl
CRTDLL__fpreset(void)
1283 /*********************************************************************
1284 * atexit (CRTDLL.345)
1286 INT __cdecl
CRTDLL_atexit(LPVOID x
)
1288 FIXME("(%p), STUB.\n",x
);
1289 return 0; /* successful */
1292 /*********************************************************************
1293 * _isctype (CRTDLL.138)
1295 BOOL __cdecl
CRTDLL__isctype(CHAR x
,CHAR type
)
1297 if ((type
& CRTDLL_SPACE
) && isspace(x
))
1299 if ((type
& CRTDLL_PUNCT
) && ispunct(x
))
1301 if ((type
& CRTDLL_LOWER
) && islower(x
))
1303 if ((type
& CRTDLL_UPPER
) && isupper(x
))
1305 if ((type
& CRTDLL_ALPHA
) && isalpha(x
))
1307 if ((type
& CRTDLL_DIGIT
) && isdigit(x
))
1309 if ((type
& CRTDLL_CONTROL
) && iscntrl(x
))
1311 /* check CRTDLL_LEADBYTE */
1315 /*********************************************************************
1316 * _chdrive (CRTDLL.52)
1318 * newdir [I] drive to change to, A=1
1321 BOOL __cdecl
CRTDLL__chdrive(INT newdrive
)
1323 /* FIXME: generates errnos */
1324 return DRIVE_SetCurrentDrive(newdrive
-1);
1327 /*********************************************************************
1328 * _chdir (CRTDLL.51)
1330 INT __cdecl
CRTDLL__chdir(LPCSTR newdir
)
1332 if (!SetCurrentDirectoryA(newdir
))
1337 /*********************************************************************
1338 * _fullpath (CRTDLL.114)
1340 LPSTR __cdecl
CRTDLL__fullpath(LPSTR buf
, LPCSTR name
, INT size
)
1342 DOS_FULL_NAME full_name
;
1347 if(!(buf
= CRTDLL_malloc(size
))) return NULL
;
1349 if (!DOSFS_GetFullName( name
, FALSE
, &full_name
)) return NULL
;
1350 lstrcpynA(buf
,full_name
.short_name
,size
);
1351 TRACE("CRTDLL_fullpath got %s\n",buf
);
1355 /*********************************************************************
1356 * _splitpath (CRTDLL.279)
1358 VOID __cdecl
CRTDLL__splitpath(LPCSTR path
, LPSTR drive
, LPSTR directory
, LPSTR filename
, LPSTR extension
)
1361 directory includes leading and trailing (forward and backward slashes)
1362 filename without dot and slashes
1363 extension with leading dot
1365 char * drivechar
,*dirchar
,*namechar
;
1367 TRACE("CRTDLL__splitpath got %s\n",path
);
1369 drivechar
= strchr(path
,':');
1370 dirchar
= strrchr(path
,'/');
1371 namechar
= strrchr(path
,'\\');
1372 dirchar
= max(dirchar
,namechar
);
1374 namechar
= strrchr(dirchar
,'.');
1376 namechar
= strrchr(path
,'.');
1384 strncat(drive
,path
,drivechar
-path
+1);
1393 strncat(directory
,path
,dirchar
-path
+1);
1402 strncat(filename
,path
,namechar
-path
);
1406 strcat(extension
,namechar
);
1411 TRACE("CRTDLL__splitpath found %s %s %s %s\n",drive
,directory
,filename
,extension
);
1416 /*********************************************************************
1417 * _makepath (CRTDLL.182)
1420 VOID __cdecl
CRTDLL__makepath(LPSTR path
, LPCSTR drive
,
1421 LPCSTR directory
, LPCSTR filename
,
1425 TRACE("CRTDLL__makepath got %s %s %s %s\n", drive
, directory
,
1426 filename
, extension
);
1434 sprintf(path
, "%c:", drive
[0]);
1437 if ( directory
[0] ) {
1438 strcat(path
, directory
);
1439 ch
= path
[strlen(path
)-1];
1440 if (ch
!= '/' && ch
!= '\\')
1444 if ( filename
[0] ) {
1445 strcat(path
, filename
);
1447 if ( extension
[0] ) {
1448 if ( extension
[0] != '.' ) {
1451 strcat(path
,extension
);
1456 TRACE("CRTDLL__makepath returns %s\n",path
);
1459 /*********************************************************************
1460 * _getcwd (CRTDLL.120)
1462 CHAR
* __cdecl
CRTDLL__getcwd(LPSTR buf
, INT size
)
1469 if (size
< 0) /* allocate as big as nescessary */
1470 len
=GetCurrentDirectoryA(1,test
) + 1;
1471 if(!(buf
= CRTDLL_malloc(len
)))
1473 /* set error to OutOfRange */
1478 if(!(len
=GetCurrentDirectoryA(len
,buf
)))
1484 /* set error to ERANGE */
1485 TRACE("CRTDLL_getcwd buffer to small\n");
1492 /*********************************************************************
1493 * _getdcwd (CRTDLL.121)
1495 CHAR
* __cdecl
CRTDLL__getdcwd(INT drive
,LPSTR buf
, INT size
)
1500 FIXME("(\"%c:\",%s,%d)\n",drive
+'A',buf
,size
);
1503 if (size
< 0) /* allocate as big as nescessary */
1504 len
=GetCurrentDirectoryA(1,test
) + 1;
1505 if(!(buf
= CRTDLL_malloc(len
)))
1507 /* set error to OutOfRange */
1512 if(!(len
=GetCurrentDirectoryA(len
,buf
)))
1518 /* set error to ERANGE */
1519 TRACE("buffer to small\n");
1526 /*********************************************************************
1527 * _getdrive (CRTDLL.124)
1529 * Return current drive, 1 for A, 2 for B
1531 INT __cdecl
CRTDLL__getdrive(VOID
)
1533 return DRIVE_GetCurrentDrive() + 1;
1536 /*********************************************************************
1537 * _mkdir (CRTDLL.234)
1539 INT __cdecl
CRTDLL__mkdir(LPCSTR newdir
)
1541 if (!CreateDirectoryA(newdir
,NULL
))
1546 /*********************************************************************
1547 * remove (CRTDLL.448)
1549 INT __cdecl
CRTDLL_remove(LPCSTR file
)
1551 if (!DeleteFileA(file
))
1556 /*********************************************************************
1557 * _errno (CRTDLL.52)
1558 * Yes, this is a function.
1560 LPINT __cdecl
CRTDLL__errno()
1562 static int crtdllerrno
;
1564 /* FIXME: we should set the error at the failing function call time */
1565 crtdllerrno
= LastErrorToErrno(GetLastError());
1566 return &crtdllerrno
;
1569 /*********************************************************************
1570 * _tempnam (CRTDLL.305)
1573 LPSTR __cdecl
CRTDLL__tempnam(LPCSTR dir
, LPCSTR prefix
)
1577 DOS_FULL_NAME tempname
;
1579 if ((ret
= tempnam(dir
,prefix
))==NULL
) {
1580 WARN("Unable to get unique filename\n");
1583 if (!DOSFS_GetFullName(ret
,FALSE
,&tempname
))
1585 TRACE("Wrong path?\n");
1589 if ((ret
= CRTDLL_malloc(strlen(tempname
.short_name
)+1)) == NULL
) {
1590 WARN("CRTDL_malloc for shortname failed\n");
1593 if ((ret
= strcpy(ret
,tempname
.short_name
)) == NULL
) {
1594 WARN("Malloc for shortname failed\n");
1598 TRACE("dir %s prefix %s got %s\n",
1603 /*********************************************************************
1604 * tmpnam (CRTDLL.490)
1606 * lcclnk from lcc-win32 relies on a terminating dot in the name returned
1609 LPSTR __cdecl
CRTDLL_tmpnam(LPSTR s
)
1613 if ((ret
=tmpnam(s
))== NULL
) {
1614 WARN("Unable to get unique filename\n");
1617 if (!DOSFS_GetFullName(ret
,FALSE
,&CRTDLL_tmpname
))
1619 TRACE("Wrong path?\n");
1622 strcat(CRTDLL_tmpname
.short_name
,".");
1623 TRACE("for buf %p got %s\n",
1624 s
,CRTDLL_tmpname
.short_name
);
1625 TRACE("long got %s\n",
1626 CRTDLL_tmpname
.long_name
);
1628 return strcpy(s
,CRTDLL_tmpname
.short_name
);
1630 return CRTDLL_tmpname
.short_name
;
1634 /*********************************************************************
1635 * _itoa (CRTDLL.165)
1637 LPSTR __cdecl
CRTDLL__itoa(INT x
,LPSTR buf
,INT buflen
)
1639 wsnprintfA(buf
,buflen
,"%d",x
);
1644 typedef VOID (*sig_handler_type
)(VOID
);
1646 /*********************************************************************
1647 * signal (CRTDLL.455)
1649 void * __cdecl
CRTDLL_signal(int sig
, sig_handler_type ptr
)
1651 FIXME("(%d %p):stub.\n", sig
, ptr
);
1655 /*********************************************************************
1656 * _sleep (CRTDLL.267)
1658 VOID __cdecl
CRTDLL__sleep(unsigned long timeout
)
1660 TRACE("CRTDLL__sleep for %ld milliseconds\n",timeout
);
1661 Sleep((timeout
)?timeout
:1);
1664 /*********************************************************************
1665 * getenv (CRTDLL.437)
1667 LPSTR __cdecl
CRTDLL_getenv(const char *name
)
1669 LPSTR environ
= GetEnvironmentStringsA();
1670 LPSTR pp
,pos
= NULL
;
1671 unsigned int length
;
1673 for (pp
= environ
; (*pp
); pp
= pp
+ strlen(pp
) +1)
1675 pos
=strchr(pp
,'=');
1679 length
= strlen(pp
);
1680 if (!strncmp(pp
,name
,length
)) break;
1685 TRACE("got %s\n",pp
);
1687 FreeEnvironmentStringsA( environ
);
1691 /*********************************************************************
1692 * _mbsrchr (CRTDLL.223)
1694 LPSTR __cdecl
CRTDLL__mbsrchr(LPSTR s
,CHAR x
) {
1695 /* FIXME: handle multibyte strings */
1696 return strrchr(s
,x
);
1699 /*********************************************************************
1700 * __dllonexit (CRTDLL.25)
1702 VOID __cdecl
CRTDLL___dllonexit ()
1707 /*********************************************************************
1708 * _strdate (CRTDLL.283)
1710 LPSTR __cdecl
CRTDLL__strdate (LPSTR date
)
1711 { FIXME("%p stub\n", date
);
1715 /*********************************************************************
1716 * _strtime (CRTDLL.299)
1718 LPSTR __cdecl
CRTDLL__strtime (LPSTR date
)
1719 { FIXME("%p stub\n", date
);
1723 /*********************************************************************
1724 * _except_handler2 (CRTDLL.78)
1726 INT __cdecl
CRTDLL__except_handler2 (
1727 PEXCEPTION_RECORD rec
,
1728 PEXCEPTION_FRAME frame
,
1730 PEXCEPTION_FRAME
*dispatcher
)
1732 FIXME ("exception %lx flags=%lx at %p handler=%p %p %p stub\n",
1733 rec
->ExceptionCode
, rec
->ExceptionFlags
, rec
->ExceptionAddress
,
1734 frame
->Handler
, context
, dispatcher
);
1735 return ExceptionContinueSearch
;