Release 971012
[wine/multimedia.git] / misc / crtdll.c
blob91a8732b7793ab80782b89f95bd2bd9badff42bc
1 /*
2 * The C RunTime DLL
3 *
4 * Implements C run-time functionality as known from UNIX.
6 * Copyright 1996 Marcus Meissner
7 * Copyright 1996 Jukka Iivonen
8 * Copyright 1997 Uwe Bonnes
9 */
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
21 /* FIXME: all the file handling is hopelessly broken -- AJ */
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <stdarg.h>
26 #include <string.h>
27 #include <unistd.h>
28 #include <time.h>
29 #include <ctype.h>
30 #include <math.h>
31 #include <fcntl.h>
32 #include "win.h"
33 #include "windows.h"
34 #include "stddebug.h"
35 #include "debug.h"
36 #include "module.h"
37 #include "xmalloc.h"
38 #include "heap.h"
39 #include "crtdll.h"
40 #include "drive.h"
41 #include "file.h"
42 #include "except.h"
43 #include "options.h"
45 extern int FILE_GetUnixHandle( HFILE32 );
47 static DOS_FULL_NAME CRTDLL_tmpname;
49 extern INT32 WIN32_wsprintf32W( DWORD *args );
51 UINT32 CRTDLL_argc_dll; /* CRTDLL.23 */
52 LPSTR *CRTDLL_argv_dll; /* CRTDLL.24 */
53 LPSTR CRTDLL_acmdln_dll; /* CRTDLL.38 */
54 UINT32 CRTDLL_basemajor_dll; /* CRTDLL.42 */
55 UINT32 CRTDLL_baseminor_dll; /* CRTDLL.43 */
56 UINT32 CRTDLL_baseversion_dll; /* CRTDLL.44 */
57 UINT32 CRTDLL_commode_dll; /* CRTDLL.59 */
58 LPSTR CRTDLL_environ_dll; /* CRTDLL.75 */
59 UINT32 CRTDLL_fmode_dll; /* CRTDLL.104 */
60 UINT32 CRTDLL_osmajor_dll; /* CRTDLL.241 */
61 UINT32 CRTDLL_osminor_dll; /* CRTDLL.242 */
62 UINT32 CRTDLL_osmode_dll; /* CRTDLL.243 */
63 UINT32 CRTDLL_osver_dll; /* CRTDLL.244 */
64 UINT32 CRTDLL_osversion_dll; /* CRTDLL.245 */
65 UINT32 CRTDLL_winmajor_dll; /* CRTDLL.329 */
66 UINT32 CRTDLL_winminor_dll; /* CRTDLL.330 */
67 UINT32 CRTDLL_winver_dll; /* CRTDLL.331 */
69 typedef VOID (*new_handler_type)(VOID);
71 static new_handler_type new_handler;
73 /*********************************************************************
74 * _GetMainArgs (CRTDLL.022)
76 DWORD __cdecl CRTDLL__GetMainArgs(LPDWORD argc,LPSTR **argv,
77 LPSTR *environ,DWORD flag)
79 char *cmdline;
80 char **xargv;
81 int xargc,i,afterlastspace;
82 DWORD version;
84 dprintf_crtdll(stddeb,"CRTDLL__GetMainArgs(%p,%p,%p,%ld).\n",
85 argc,argv,environ,flag
87 CRTDLL_acmdln_dll = cmdline = xstrdup( GetCommandLine32A() );
88 dprintf_crtdll(stddeb,"CRTDLL__GetMainArgs got \"%s\"\n",
89 cmdline);
91 version = GetVersion32();
92 CRTDLL_osver_dll = version >> 16;
93 CRTDLL_winminor_dll = version & 0xFF;
94 CRTDLL_winmajor_dll = (version>>8) & 0xFF;
95 CRTDLL_baseversion_dll = version >> 16;
96 CRTDLL_winver_dll = ((version >> 8) & 0xFF) + ((version & 0xFF) << 8);
97 CRTDLL_baseminor_dll = (version >> 16) & 0xFF;
98 CRTDLL_basemajor_dll = (version >> 24) & 0xFF;
99 CRTDLL_osversion_dll = version & 0xFFFF;
100 CRTDLL_osminor_dll = version & 0xFF;
101 CRTDLL_osmajor_dll = (version>>8) & 0xFF;
103 /* missing threading init */
105 i=0;xargv=NULL;xargc=0;afterlastspace=0;
106 while (cmdline[i]) {
107 if (cmdline[i]==' ') {
108 xargv=(char**)xrealloc(xargv,sizeof(char*)*(++xargc));
109 cmdline[i]='\0';
110 xargv[xargc-1] = xstrdup(cmdline+afterlastspace);
111 i++;
112 while (cmdline[i]==' ')
113 i++;
114 if (cmdline[i])
115 afterlastspace=i;
116 } else
117 i++;
119 xargv=(char**)xrealloc(xargv,sizeof(char*)*(++xargc));
120 cmdline[i]='\0';
121 xargv[xargc-1] = xstrdup(cmdline+afterlastspace);
122 CRTDLL_argc_dll = xargc;
123 *argc = xargc;
124 CRTDLL_argv_dll = xargv;
125 *argv = xargv;
127 dprintf_crtdll(stddeb,"CRTDLL__GetMainArgs found %d arguments\n",
128 CRTDLL_argc_dll);
129 /* FIXME ... use real environment */
130 *environ = xmalloc(sizeof(LPSTR));
131 CRTDLL_environ_dll = *environ;
132 (*environ)[0] = NULL;
133 return 0;
137 typedef void (*_INITTERMFUN)();
139 /*********************************************************************
140 * _initterm (CRTDLL.135)
142 DWORD __cdecl CRTDLL__initterm(_INITTERMFUN *start,_INITTERMFUN *end)
144 _INITTERMFUN *current;
146 dprintf_crtdll(stddeb,"_initterm(%p,%p)\n",start,end);
147 current=start;
148 while (current<end) {
149 if (*current) (*current)();
150 current++;
152 return 0;
155 /*********************************************************************
156 * _fdopen (CRTDLL.91)
158 DWORD __cdecl CRTDLL__fdopen(INT32 handle, LPCSTR mode)
160 FILE *file;
162 switch (handle)
164 case 0 : file=stdin;
165 break;
166 case 1 : file=stdout;
167 break;
168 case 2 : file=stderr;
169 break;
170 default:
171 file=fdopen(handle,mode);
173 dprintf_crtdll(stddeb,
174 "CRTDLL_fdopen open handle %d mode %s got file %p\n",
175 handle, mode, file);
176 return (DWORD)file;
179 /*******************************************************************
180 * _global_unwind2 (CRTDLL.129)
182 void __cdecl CRTDLL__global_unwind2( CONTEXT *context )
184 /* Retrieve the arguments (args[0] is return addr, args[1] is first arg) */
185 DWORD *args = (DWORD *)ESP_reg(context);
186 RtlUnwind( (PEXCEPTION_FRAME)args[1], (LPVOID)EIP_reg(context),
187 NULL, 0, context );
190 /*******************************************************************
191 * _local_unwind2 (CRTDLL.173)
193 void __cdecl CRTDLL__local_unwind2( CONTEXT *context )
195 /* Retrieve the arguments (args[0] is return addr, args[1] is first arg) */
196 DWORD *args = (DWORD *)ESP_reg(context);
197 PEXCEPTION_FRAME endframe = (PEXCEPTION_FRAME)args[1];
198 DWORD nr = args[2];
199 fprintf(stderr,"CRTDLL__local_unwind2(%p,%ld)\n",endframe,nr);
202 /*********************************************************************
203 * fopen (CRTDLL.372)
205 DWORD __cdecl CRTDLL_fopen(LPCSTR path, LPCSTR mode)
207 FILE *file;
208 HFILE32 dos_fildes;
209 #if 0
210 DOS_FULL_NAME full_name;
212 if (!DOSFS_GetFullName( path, FALSE, &full_name )) {
213 dprintf_crtdll(stddeb,"CRTDLL_fopen file %s bad name\n",path);
214 return 0;
217 file=fopen(full_name.long_name ,mode);
218 #endif
219 INT32 flagmode=0;
220 int unix_fildes=0;
222 if ((strchr(mode,'r')&&strchr(mode,'a'))||
223 (strchr(mode,'r')&&strchr(mode,'w'))||
224 (strchr(mode,'w')&&strchr(mode,'a')))
225 return 0;
227 if (strstr(mode,"r+")) flagmode=O_RDWR;
228 else if (strchr(mode,'r')) flagmode = O_RDONLY;
229 else if (strstr(mode,"w+")) flagmode= O_RDWR | O_TRUNC | O_CREAT;
230 else if (strchr(mode,'w')) flagmode = O_WRONLY | O_TRUNC | O_CREAT;
231 else if (strstr(mode,"a+")) flagmode= O_RDWR | O_CREAT | O_APPEND;
232 else if (strchr(mode,'w')) flagmode = O_RDWR | O_CREAT | O_APPEND;
233 else if (strchr(mode,'b'))
234 dprintf_crtdll(stderr,
235 "CRTDLL_fopen %s in BINARY mode\n",path);
237 dos_fildes=FILE_Open(path, flagmode);
238 unix_fildes=FILE_GetUnixHandle(dos_fildes);
239 file = fdopen(unix_fildes,mode);
241 dprintf_crtdll(stddeb,
242 "CRTDLL_fopen file %s mode %s got ufh %d dfh %d file %p\n",
243 path,mode,unix_fildes,dos_fildes,file);
244 return (DWORD)file;
247 /*********************************************************************
248 * fread (CRTDLL.377)
250 DWORD __cdecl CRTDLL_fread(LPVOID ptr, INT32 size, INT32 nmemb, LPVOID file)
252 size_t ret=1;
253 #if 0
254 int i=0;
255 void *temp=ptr;
257 /* If we would honour CR/LF <-> LF translation, we could do it like this.
258 We should keep track of all files opened, and probably files with \
259 known binary extensions must be unchanged */
260 while ( (i < (nmemb*size)) && (ret==1)) {
261 ret=fread(temp,1,1,file);
262 dprintf_crtdll(stddeb,
263 "CRTDLL_fread got %c 0x%02x ret %d\n",
264 (isalpha(*(unsigned char*)temp))? *(unsigned char*)temp:
265 ' ',*(unsigned char*)temp, ret);
266 if (*(unsigned char*)temp != 0xd) { /* skip CR */
267 temp++;
268 i++;
270 else
271 dprintf_crtdll(stddeb, "CRTDLL_fread skipping ^M\n");
273 dprintf_crtdll(stddeb,
274 "CRTDLL_fread 0x%08x items of size %d from file %p to %p%s\n",
275 nmemb,size,file,ptr,(i!=nmemb)?" failed":"");
276 return i;
277 #else
279 ret=fread(ptr,size,nmemb,file);
280 dprintf_crtdll(stddeb,
281 "CRTDLL_fread 0x%08x items of size %d from file %p to %p%s\n",
282 nmemb,size,file,ptr,(ret!=nmemb)?" failed":"");
283 return ret;
284 #endif
287 /*********************************************************************
288 * fseek (CRTDLL.382)
290 LONG __cdecl CRTDLL_fseek(LPVOID stream, LONG offset, INT32 whence)
292 long ret;
294 ret=fseek(stream,offset,whence);
295 dprintf_crtdll(stddeb,
296 "CRTDLL_fseek file %p to 0x%08lx pos %s%s\n",
297 stream,offset,(whence==SEEK_SET)?"SEEK_SET":
298 (whence==SEEK_CUR)?"SEEK_CUR":
299 (whence==SEEK_END)?"SEEK_END":"UNKNOWN",
300 (ret)?"failed":"");
301 return ret;
304 /*********************************************************************
305 * ftell (CRTDLL.384)
307 LONG __cdecl CRTDLL_ftell(LPVOID stream)
309 long ret;
311 ret=ftell(stream);
312 dprintf_crtdll(stddeb,
313 "CRTDLL_ftell file %p at 0x%08lx\n",
314 stream,ret);
315 return ret;
318 /*********************************************************************
319 * fwrite (CRTDLL.386)
321 DWORD __cdecl CRTDLL_fwrite(LPVOID ptr, INT32 size, INT32 nmemb, LPVOID file)
323 size_t ret;
325 ret=fwrite(ptr,size,nmemb,file);
326 dprintf_crtdll(stddeb,
327 "CRTDLL_fwrite 0x%08x items of size %d from %p to file %p%s\n",
328 nmemb,size,ptr,file,(ret!=nmemb)?" failed":"");
329 return ret;
332 /*********************************************************************
333 * setbuf (CRTDLL.452)
335 INT32 __cdecl CRTDLL_setbuf(LPVOID file, LPSTR buf)
337 dprintf_crtdll(stddeb,
338 "CRTDLL_setbuf(file %p buf %p)\n",
339 file,buf);
340 /* this doesn't work:"void value not ignored as it ought to be"
341 return setbuf(file,buf);
343 setbuf(file,buf);
344 return 0;
347 /*********************************************************************
348 * _open_osfhandle (CRTDLL.240)
350 HFILE32 __cdecl CRTDLL__open_osfhandle(LONG osfhandle, INT32 flags)
352 HFILE32 handle;
354 switch (osfhandle) {
355 case STD_INPUT_HANDLE :
356 case 0 :
357 handle=0;
358 break;
359 case STD_OUTPUT_HANDLE:
360 case 1:
361 handle=1;
362 break;
363 case STD_ERROR_HANDLE:
364 case 2:
365 handle=2;
366 break;
367 default:
368 return (-1);
370 dprintf_crtdll(stddeb,
371 "CRTDLL_open_osfhandle(handle %08lx,flags %d) return %d\n",
372 osfhandle,flags,handle);
373 return handle;
377 /*********************************************************************
378 * srand (CRTDLL.460)
380 void __cdecl CRTDLL_srand(DWORD seed)
382 /* FIXME: should of course be thread? process? local */
383 srand(seed);
386 /*********************************************************************
387 * fprintf (CRTDLL.373)
389 INT32 __cdecl CRTDLL_fprintf( FILE *file, LPSTR format, ... )
391 va_list valist;
392 INT32 res;
394 va_start( valist, format );
395 res = vfprintf( file, format, valist );
396 va_end( valist );
397 return res;
400 /*********************************************************************
401 * vfprintf (CRTDLL.373)
403 INT32 __cdecl CRTDLL_vfprintf( FILE *file, LPSTR format, va_list args )
405 return vfprintf( file, format, args );
408 /*********************************************************************
409 * time (CRTDLL.488)
411 time_t __cdecl CRTDLL_time(time_t *timeptr)
413 time_t curtime = time(NULL);
415 if (timeptr)
416 *timeptr = curtime;
417 return curtime;
420 /*********************************************************************
421 * _isatty (CRTDLL.137)
423 BOOL32 __cdecl CRTDLL__isatty(DWORD x)
425 dprintf_crtdll(stderr,"CRTDLL__isatty(%ld)\n",x);
426 return TRUE;
429 /*********************************************************************
430 * _write (CRTDLL.332)
432 INT32 __cdecl CRTDLL__write(INT32 fd,LPCVOID buf,UINT32 count)
434 INT32 len=0;
436 if (fd == -1)
437 len = -1;
438 else if (fd<=2)
439 len = (UINT32)write(fd,buf,(LONG)len);
440 else
441 len = _lwrite32(fd,buf,count);
442 dprintf_crtdll(stddeb,"CRTDLL_write %d/%d byte to dfh %d from %p,\n",
443 len,count,fd,buf);
444 return len;
448 /*********************************************************************
449 * _cexit (CRTDLL.49)
451 * FIXME: What the heck is the difference between
452 * FIXME _c_exit (CRTDLL.47)
453 * FIXME _cexit (CRTDLL.49)
454 * FIXME _exit (CRTDLL.87)
455 * FIXME exit (CRTDLL.359)
458 void __cdecl CRTDLL__cexit(INT32 ret)
460 dprintf_crtdll(stddeb,"CRTDLL__cexit(%d)\n",ret);
461 ExitProcess(ret);
465 /*********************************************************************
466 * exit (CRTDLL.359)
468 void __cdecl CRTDLL_exit(DWORD ret)
470 dprintf_crtdll(stddeb,"CRTDLL_exit(%ld)\n",ret);
471 ExitProcess(ret);
475 /*********************************************************************
476 * fflush (CRTDLL.365)
478 INT32 __cdecl CRTDLL_fflush(LPVOID stream)
480 int ret;
482 ret = fflush(stream);
483 dprintf_crtdll(stddeb,"CRTDLL_fflush %p returnd %d %s\n",stream,ret,(ret)?"":" failed");
484 return ret;
488 /*********************************************************************
489 * gets (CRTDLL.391)
491 LPSTR __cdecl CRTDLL_gets(LPSTR buf)
493 /* BAD, for the whole WINE process blocks... just done this way to test
494 * windows95's ftp.exe.
496 return gets(buf);
500 /*********************************************************************
501 * rand (CRTDLL.446)
503 INT32 __cdecl CRTDLL_rand()
505 return rand();
509 /*********************************************************************
510 * putchar (CRTDLL.442)
512 void __cdecl CRTDLL_putchar( INT32 x )
514 putchar(x);
518 /*********************************************************************
519 * fputc (CRTDLL.374)
521 INT32 __cdecl CRTDLL_fputc( INT32 c, FILE *stream )
523 dprintf_crtdll(stddeb,
524 "CRTDLL_fputc %c to file %p\n",c,stream);
525 return fputc(c,stream);
529 /*********************************************************************
530 * fputs (CRTDLL.375)
532 INT32 __cdecl CRTDLL_fputs( LPCSTR s, FILE *stream )
534 dprintf_crtdll(stddeb,
535 "CRTDLL_fputs %s to file %p\n",s,stream);
536 return fputs(s,stream);
540 /*********************************************************************
541 * puts (CRTDLL.443)
543 INT32 __cdecl CRTDLL_puts(LPCSTR s)
545 dprintf_crtdll(stddeb,
546 "CRTDLL_fputs %s \n",s);
547 return puts(s);
551 /*********************************************************************
552 * putc (CRTDLL.441)
554 INT32 __cdecl CRTDLL_putc(INT32 c, FILE *stream)
556 dprintf_crtdll(stddeb,
557 "CRTDLL_putc %c to file %p\n",c,stream);
558 return fputc(c,stream);
560 /*********************************************************************
561 * fgetc (CRTDLL.366)
563 INT32 __cdecl CRTDLL_fgetc( FILE *stream )
565 int ret= fgetc(stream);
566 dprintf_crtdll(stddeb,
567 "CRTDLL_fgetc got %d\n",ret);
568 return ret;
572 /*********************************************************************
573 * getc (CRTDLL.388)
575 INT32 __cdecl CRTDLL_getc( FILE *stream )
577 int ret= fgetc(stream);
578 dprintf_crtdll(stddeb,
579 "CRTDLL_getc got %d\n",ret);
580 return ret;
583 /*********************************************************************
584 * _lrotl (CRTDLL.176)
586 DWORD __cdecl CRTDLL__lrotl(DWORD x,INT32 shift)
588 unsigned long ret = (x >> shift)|( x >>((sizeof(x))-shift));
590 dprintf_crtdll(stddeb,
591 "CRTDLL_lrotl got 0x%08lx rot %d ret 0x%08lx\n",
592 x,shift,ret);
593 return ret;
598 /*********************************************************************
599 * fgets (CRTDLL.368)
601 CHAR* __cdecl CRTDLL_fgets(LPSTR s,INT32 size, LPVOID stream)
603 char * ret;
604 char * control_M;
606 ret=fgets(s, size,stream);
607 /*FIXME: Control with CRTDLL_setmode */
608 control_M= strrchr(s,'\r');
609 /*delete CR if we read a DOS File */
610 if (control_M)
612 *control_M='\n';
613 *(control_M+1)=0;
615 dprintf_crtdll(stddeb,
616 "CRTDLL_fgets got %s for %d chars from file %p%s\n",
617 s,size,stream,(ret)?"":" failed");
618 return ret;
622 /*********************************************************************
623 * _mbsicmp (CRTDLL.204)
625 int __cdecl CRTDLL__mbsicmp(unsigned char *x,unsigned char *y)
627 do {
628 if (!*x)
629 return !!*y;
630 if (!*y)
631 return !!*x;
632 /* FIXME: MBCS handling... */
633 if (*x!=*y)
634 return 1;
635 x++;
636 y++;
637 } while (1);
641 /*********************************************************************
642 * _mbsinc (CRTDLL.205)
644 unsigned char * __cdecl CRTDLL__mbsinc(unsigned char *x)
646 /* FIXME: mbcs */
647 return x++;
651 /*********************************************************************
652 * vsprintf (CRTDLL.500)
654 INT32 __cdecl CRTDLL_vsprintf( LPSTR buffer, LPCSTR spec, va_list args )
656 return wvsprintf32A( buffer, spec, args );
659 /*********************************************************************
660 * vswprintf (CRTDLL.501)
662 INT32 __cdecl CRTDLL_vswprintf( LPWSTR buffer, LPCWSTR spec, va_list args )
664 return wvsprintf32W( buffer, spec, args );
667 /*********************************************************************
668 * _mbscpy (CRTDLL.200)
670 unsigned char* __cdecl CRTDLL__mbscpy(unsigned char *x,unsigned char *y)
672 dprintf_crtdll(stddeb,"CRTDLL_mbscpy %s and %s\n",x,y);
673 return strcpy(x,y);
677 /*********************************************************************
678 * _mbscat (CRTDLL.197)
680 unsigned char* __cdecl CRTDLL__mbscat(unsigned char *x,unsigned char *y)
682 return strcat(x,y);
686 /*********************************************************************
687 * _strcmpi (CRTDLL.282) (CRTDLL.287)
689 INT32 __cdecl CRTDLL__strcmpi( LPCSTR s1, LPCSTR s2 )
691 return lstrcmpi32A( s1, s2 );
695 /*********************************************************************
696 * _strnicmp (CRTDLL.293)
698 INT32 __cdecl CRTDLL__strnicmp( LPCSTR s1, LPCSTR s2, INT32 n )
700 return lstrncmpi32A( s1, s2, n );
704 /*********************************************************************
705 * _strlwr (CRTDLL.293)
707 * convert a string in place to lowercase
709 LPSTR CRTDLL__strlwr(LPSTR x)
711 unsigned char *y =x;
713 dprintf_crtdll(stddeb,
714 "CRTDLL_strlwr got %s",x);
715 while (*y) {
716 if ((*y > 0x40) && (*y< 0x5b))
717 *y = *y + 0x20;
718 y++;
720 dprintf_crtdll(stddeb," returned %s\n",x);
722 return x;
725 /*********************************************************************
726 * system (CRTDLL.485)
728 INT32 CRTDLL_system(LPSTR x)
730 #define SYSBUF_LENGTH 1500
731 char buffer[SYSBUF_LENGTH];
732 unsigned char *y = x;
733 unsigned char *bp;
734 int i;
736 sprintf( buffer, "%s \"", Options.argv0 );
737 bp = buffer + strlen(buffer);
738 i = strlen(buffer) + strlen(x) +2;
740 /* Calculate needed buffer size tp prevent overflow*/
741 while (*y) {
742 if (*y =='\\') i++;
743 y++;
745 /* if buffer to short, exit */
746 if (i > SYSBUF_LENGTH) {
747 dprintf_crtdll(stddeb,"_system buffer to small\n");
748 return 127;
751 y =x;
753 while (*y) {
754 *bp = *y;
755 bp++; y++;
756 if (*(y-1) =='\\') *bp++ = '\\';
758 /* remove spaces from end of string */
759 while (*(y-1) == ' ') {
760 bp--;y--;
762 *bp++ = '"';
763 *bp = 0;
764 dprintf_crtdll(stddeb,
765 "_system got \"%s\", executing \"%s\"\n",x,buffer);
767 return system(buffer);
770 /*********************************************************************
771 * _strupr (CRTDLL.300)
773 LPSTR __cdecl CRTDLL__strupr(LPSTR x)
775 LPSTR y=x;
777 while (*y) {
778 *y=toupper(*y);
779 y++;
781 return x;
784 /*********************************************************************
785 * _wcsupr (CRTDLL.328)
787 LPWSTR __cdecl CRTDLL__wcsupr(LPWSTR x)
789 LPWSTR y=x;
791 while (*y) {
792 *y=toupper(*y);
793 y++;
795 return x;
798 /*********************************************************************
799 * _wcslwr (CRTDLL.323)
801 LPWSTR __cdecl CRTDLL__wcslwr(LPWSTR x)
803 LPWSTR y=x;
805 while (*y) {
806 *y=tolower(*y);
807 y++;
809 return x;
813 /*********************************************************************
814 * malloc (CRTDLL.427)
816 VOID* __cdecl CRTDLL_malloc(DWORD size)
818 return HeapAlloc(GetProcessHeap(),0,size);
821 /*********************************************************************
822 * new (CRTDLL.001)
824 VOID* __cdecl CRTDLL_new(DWORD size)
826 VOID* result;
827 if(!(result = HeapAlloc(GetProcessHeap(),0,size)) && new_handler)
828 (*new_handler)();
829 return result;
832 /*********************************************************************
833 * set_new_handler(CRTDLL.003)
835 new_handler_type __cdecl CRTDLL_set_new_handler(new_handler_type func)
837 new_handler_type old_handler = new_handler;
838 new_handler = func;
839 return old_handler;
842 /*********************************************************************
843 * calloc (CRTDLL.350)
845 VOID* __cdecl CRTDLL_calloc(DWORD size, DWORD count)
847 return HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, size * count );
850 /*********************************************************************
851 * realloc (CRTDLL.447)
853 VOID* __cdecl CRTDLL_realloc( VOID *ptr, DWORD size )
855 return HeapReAlloc( GetProcessHeap(), 0, ptr, size );
858 /*********************************************************************
859 * free (CRTDLL.427)
861 VOID __cdecl CRTDLL_free(LPVOID ptr)
863 HeapFree(GetProcessHeap(),0,ptr);
866 /*********************************************************************
867 * delete (CRTDLL.002)
869 VOID __cdecl CRTDLL_delete(VOID* ptr)
871 HeapFree(GetProcessHeap(),0,ptr);
874 /*********************************************************************
875 * _strdup (CRTDLL.285)
877 LPSTR __cdecl CRTDLL__strdup(LPSTR ptr)
879 return HEAP_strdupA(GetProcessHeap(),0,ptr);
883 /*********************************************************************
884 * fclose (CRTDLL.362)
886 INT32 __cdecl CRTDLL_fclose( FILE *stream )
888 int unix_handle=fileno(stream);
889 HFILE32 dos_handle=1;
890 HFILE32 ret=EOF;
892 if (unix_handle<4) ret= fclose(stream);
893 else {
894 while(FILE_GetUnixHandle(dos_handle) != unix_handle) dos_handle++;
895 fclose(stream);
896 ret = _lclose32( dos_handle);
898 dprintf_crtdll(stddeb,"CRTDLL_fclose(%p) ufh %d dfh %d%s\n",
899 stream,unix_handle,dos_handle,(ret)?" failed":"");
900 return ret;
903 /*********************************************************************
904 * _unlink (CRTDLL.315)
906 INT32 __cdecl CRTDLL__unlink(LPCSTR pathname)
908 int ret=0;
909 DOS_FULL_NAME full_name;
911 if (!DOSFS_GetFullName( pathname, FALSE, &full_name )) {
912 dprintf_crtdll(stddeb,"CRTDLL_unlink file %s bad name\n",pathname);
913 return EOF;
916 ret=unlink(full_name.long_name);
917 dprintf_crtdll(stddeb,"CRTDLL_unlink(%s unix %s)%s\n",
918 pathname,full_name.long_name, (ret)?" failed":"");
919 return ret;
922 /*********************************************************************
923 * _open (CRTDLL.239)
925 HFILE32 __cdecl CRTDLL__open(LPCSTR path,INT32 flags)
927 HFILE32 ret=0;
928 int wineflags=0;
930 /* FIXME:
931 the flags in lcc's header differ from the ones in Linux, e.g.
932 Linux: define O_APPEND 02000 (= 0x400)
933 lcc: define _O_APPEND 0x0008
934 so here a scheme to translate them
935 Probably lcc is wrong here, but at least a hack to get is going
937 wineflags = (flags & 3);
938 if (flags & 0x0008 ) wineflags |= O_APPEND;
939 if (flags & 0x0100 ) wineflags |= O_CREAT;
940 if (flags & 0x0200 ) wineflags |= O_TRUNC;
941 if (flags & 0x0400 ) wineflags |= O_EXCL;
942 if (flags & 0xf0f4 )
943 dprintf_crtdll(stddeb,"CRTDLL_open file unsupported flags 0x%04x\n",flags);
944 /* End Fixme */
946 ret = FILE_Open(path,wineflags);
947 dprintf_crtdll(stddeb,"CRTDLL_open file %s mode 0x%04x (lccmode 0x%04x) got dfh %d\n",
948 path,wineflags,flags,ret);
949 return ret;
952 /*********************************************************************
953 * _close (CRTDLL.57)
955 INT32 __cdecl CRTDLL__close(HFILE32 fd)
957 int ret=_lclose32(fd);
959 dprintf_crtdll(stddeb,"CRTDLL_close(%d)%s\n",fd,(ret)?" failed":"");
960 return ret;
963 /*********************************************************************
964 * feof (CRTDLL.363)
966 INT32 __cdecl CRTDLL_feof( FILE *stream )
968 int ret;
970 ret=feof(stream);
971 dprintf_crtdll(stddeb,"CRTDLL_feof(%p) %s\n",stream,(ret)?"true":"false");
972 return ret;
975 /*********************************************************************
976 * setlocale (CRTDLL.453)
978 LPSTR __cdecl CRTDLL_setlocale(INT32 category,LPCSTR locale)
980 LPSTR categorystr;
982 switch (category) {
983 case CRTDLL_LC_ALL: categorystr="LC_ALL";break;
984 case CRTDLL_LC_COLLATE: categorystr="LC_COLLATE";break;
985 case CRTDLL_LC_CTYPE: categorystr="LC_CTYPE";break;
986 case CRTDLL_LC_MONETARY: categorystr="LC_MONETARY";break;
987 case CRTDLL_LC_NUMERIC: categorystr="LC_NUMERIC";break;
988 case CRTDLL_LC_TIME: categorystr="LC_TIME";break;
989 default: categorystr = "UNKNOWN?";break;
991 fprintf(stderr,"CRTDLL_setlocale(%s,%s),stub!\n",categorystr,locale);
992 return "C";
995 /*********************************************************************
996 * wcscat (CRTDLL.503)
998 LPWSTR __cdecl CRTDLL_wcscat( LPWSTR s1, LPCWSTR s2 )
1000 return lstrcat32W( s1, s2 );
1003 /*********************************************************************
1004 * wcschr (CRTDLL.504)
1006 LPWSTR __cdecl CRTDLL_wcschr(LPWSTR str,WCHAR xchar)
1008 LPWSTR s;
1010 s=str;
1011 do {
1012 if (*s==xchar)
1013 return s;
1014 } while (*s++);
1015 return NULL;
1018 /*********************************************************************
1019 * wcscmp (CRTDLL.505)
1021 INT32 __cdecl CRTDLL_wcscmp( LPCWSTR s1, LPCWSTR s2 )
1023 return lstrcmp32W( s1, s2 );
1026 /*********************************************************************
1027 * wcscpy (CRTDLL.507)
1029 LPWSTR __cdecl CRTDLL_wcscpy( LPWSTR s1, LPCWSTR s2 )
1031 return lstrcpy32W( s1, s2 );
1034 /*********************************************************************
1035 * wcscspn (CRTDLL.508)
1037 INT32 __cdecl CRTDLL_wcscspn(LPWSTR str,LPWSTR reject)
1039 LPWSTR s,t;
1041 s=str;
1042 do {
1043 t=reject;
1044 while (*t) { if (*t==*s) break;t++;}
1045 if (*t) break;
1046 s++;
1047 } while (*s);
1048 return s-str; /* nr of wchars */
1051 /*********************************************************************
1052 * wcslen (CRTDLL.510)
1054 INT32 __cdecl CRTDLL_wcslen( LPCWSTR s )
1056 return lstrlen32W( s );
1059 /*********************************************************************
1060 * wcsncat (CRTDLL.511)
1062 LPWSTR __cdecl CRTDLL_wcsncat( LPWSTR s1, LPCWSTR s2, INT32 n )
1064 return lstrcatn32W( s1, s2, n );
1067 /*********************************************************************
1068 * wcsncmp (CRTDLL.512)
1070 INT32 __cdecl CRTDLL_wcsncmp( LPCWSTR s1, LPCWSTR s2, INT32 n )
1072 return lstrncmp32W( s1, s2, n );
1075 /*********************************************************************
1076 * wcsncpy (CRTDLL.513)
1078 LPWSTR __cdecl CRTDLL_wcsncpy( LPWSTR s1, LPCWSTR s2, INT32 n )
1080 return lstrcpyn32W( s1, s2, n );
1083 /*********************************************************************
1084 * wcsspn (CRTDLL.516)
1086 INT32 __cdecl CRTDLL_wcsspn(LPWSTR str,LPWSTR accept)
1088 LPWSTR s,t;
1090 s=str;
1091 do {
1092 t=accept;
1093 while (*t) { if (*t==*s) break;t++;}
1094 if (!*t) break;
1095 s++;
1096 } while (*s);
1097 return s-str; /* nr of wchars */
1100 /*********************************************************************
1101 * towupper (CRTDLL.494)
1103 WCHAR __cdecl CRTDLL_towupper(WCHAR x)
1105 return (WCHAR)toupper((CHAR)x);
1108 /*********************************************************************
1109 * _wcsicmp (CRTDLL.321)
1111 DWORD __cdecl CRTDLL__wcsicmp( LPCWSTR s1, LPCWSTR s2 )
1113 return lstrcmpi32W( s1, s2 );
1116 /*********************************************************************
1117 * _wcsicoll (CRTDLL.322)
1119 DWORD __cdecl CRTDLL__wcsicoll(LPCWSTR a1,LPCWSTR a2)
1121 /* FIXME: handle collates */
1122 return lstrcmpi32W(a1,a2);
1125 /*********************************************************************
1126 * _wcsnicmp (CRTDLL.324)
1128 DWORD __cdecl CRTDLL__wcsnicmp( LPCWSTR s1, LPCWSTR s2, INT32 len )
1130 return lstrncmpi32W( s1, s2, len );
1133 /*********************************************************************
1134 * wcscoll (CRTDLL.506)
1136 DWORD __cdecl CRTDLL_wcscoll(LPWSTR a1,LPWSTR a2)
1138 /* FIXME: handle collates */
1139 return lstrcmp32W(a1,a2);
1142 /*********************************************************************
1143 * _wcsrev (CRTDLL.326)
1145 VOID __cdecl CRTDLL__wcsrev(LPWSTR s) {
1146 LPWSTR e;
1148 e=s;
1149 while (*e)
1150 e++;
1151 while (s<e) {
1152 WCHAR a;
1154 a=*s;*s=*e;*e=a;
1155 s++;e--;
1159 /*********************************************************************
1160 * wcsstr (CRTDLL.517)
1162 LPWSTR __cdecl CRTDLL_wcsstr(LPWSTR s,LPWSTR b)
1164 LPWSTR x,y,c;
1166 x=s;
1167 while (*x) {
1168 if (*x==*b) {
1169 y=x;c=b;
1170 while (*y && *c && *y==*c) { c++;y++; }
1171 if (!*c)
1172 return x;
1174 x++;
1176 return NULL;
1179 /*********************************************************************
1180 * wcstombs (CRTDLL.521)
1182 INT32 __cdecl CRTDLL_wcstombs( LPSTR dst, LPCWSTR src, INT32 len )
1184 lstrcpynWtoA( dst, src, len );
1185 return strlen(dst); /* FIXME: is this right? */
1188 /*********************************************************************
1189 * wcsrchr (CRTDLL.515)
1191 LPWSTR __cdecl CRTDLL_wcsrchr(LPWSTR str,WCHAR xchar)
1193 LPWSTR s;
1195 s=str+lstrlen32W(str);
1196 do {
1197 if (*s==xchar)
1198 return s;
1199 s--;
1200 } while (s>=str);
1201 return NULL;
1204 /*********************************************************************
1205 * _setmode (CRTDLL.265)
1206 * FIXME: At present we ignore the request to translate CR/LF to LF.
1208 * We allways translate when we read with fgets, we never do with fread
1211 INT32 __cdecl CRTDLL__setmode( INT32 fh,INT32 mode)
1213 /* FIXME */
1214 #define O_TEXT 0x4000
1215 #define O_BINARY 0x8000
1217 dprintf_crtdll(stddeb,
1218 "CRTDLL._setmode on fhandle %d mode %s, STUB.\n",
1219 fh,(mode=O_TEXT)?"O_TEXT":
1220 (mode=O_BINARY)?"O_BINARY":"UNKNOWN");
1221 return -1;
1224 /*********************************************************************
1225 * atexit (CRTDLL.345)
1227 INT32 __cdecl CRTDLL_atexit(LPVOID x)
1229 /* FIXME */
1230 fprintf(stdnimp,"CRTDLL.atexit(%p), STUB.\n",x);
1231 return 0; /* successful */
1234 /*********************************************************************
1235 * mblen (CRTDLL.428)
1236 * FIXME: check multibyte support
1238 WCHAR __cdecl CRTDLL_mblen(CHAR *mb,INT32 size)
1241 int ret=1;
1243 if (!mb)
1244 ret = 0;
1245 else if ((size<1)||(!*(mb+1)))
1246 ret = -1;
1247 else if (!(*mb))
1248 ret =0;
1250 dprintf_crtdll(stderr,"CRTDLL_mlen %s for max %d bytes ret %d\n",mb,size,ret);
1252 return ret;
1255 /*********************************************************************
1256 * mbstowcs (CRTDLL.429)
1257 * FIXME: check multibyte support
1259 INT32 __cdecl CRTDLL_mbstowcs(LPWSTR wcs, LPCSTR mbs, INT32 size)
1262 /* Slightly modified lstrcpynAtoW functions from memory/strings.c
1263 * We need the numberr of characters transfered
1264 * FIXME: No multibyte support yet
1267 LPWSTR p = wcs;
1268 LPCSTR src= mbs;
1269 int ret, n=size;
1271 while ((n-- > 0) && *src) {
1272 *p++ = (WCHAR)(unsigned char)*src++;
1274 p++;
1275 ret = (p -wcs);
1277 dprintf_crtdll(stddeb,"CRTDLL_mbstowcs %s for %d chars put %d wchars\n",
1278 mbs,size,ret);
1279 return ret;
1282 /*********************************************************************
1283 * mbtowc (CRTDLL.430)
1284 * FIXME: check multibyte support
1286 WCHAR __cdecl CRTDLL_mbtowc(WCHAR* wc,CHAR* mb,INT32 size)
1288 int ret;
1290 if (!mb)
1291 ret = 0;
1292 else if (!wc)
1293 ret =-1;
1294 else
1295 if ( (ret = mblen(mb,size)) != -1 )
1297 if (ret <= sizeof(char))
1298 *wc = (WCHAR) ((unsigned char)*mb);
1299 else
1300 ret= -1;
1302 else
1303 ret = -1;
1305 dprintf_crtdll(stderr,"CRTDLL_mbtowc %s for %d chars\n",mb,size);
1307 return ret;
1310 /*********************************************************************
1311 * _isctype (CRTDLL.138)
1313 BOOL32 __cdecl CRTDLL__isctype(CHAR x,CHAR type)
1315 if ((type & CRTDLL_SPACE) && isspace(x))
1316 return TRUE;
1317 if ((type & CRTDLL_PUNCT) && ispunct(x))
1318 return TRUE;
1319 if ((type & CRTDLL_LOWER) && islower(x))
1320 return TRUE;
1321 if ((type & CRTDLL_UPPER) && isupper(x))
1322 return TRUE;
1323 if ((type & CRTDLL_ALPHA) && isalpha(x))
1324 return TRUE;
1325 if ((type & CRTDLL_DIGIT) && isdigit(x))
1326 return TRUE;
1327 if ((type & CRTDLL_CONTROL) && iscntrl(x))
1328 return TRUE;
1329 /* check CRTDLL_LEADBYTE */
1330 return FALSE;
1333 /*********************************************************************
1334 * _chdrive (CRTDLL.52)
1336 BOOL32 __cdecl CRTDLL__chdrive(INT32 newdrive)
1338 /* FIXME: generates errnos */
1339 return DRIVE_SetCurrentDrive(newdrive);
1342 /*********************************************************************
1343 * _chdir (CRTDLL.51)
1345 INT32 __cdecl CRTDLL__chdir(LPCSTR newdir)
1347 if (!SetCurrentDirectory32A(newdir))
1348 return -1;
1349 return 0;
1352 /*********************************************************************
1353 * _getcwd (CRTDLL.120)
1355 CHAR* __cdecl CRTDLL__getcwd(LPSTR buf, INT32 size)
1357 DOS_FULL_NAME full_name;
1358 char *ret;
1360 dprintf_crtdll(stddeb,"CRTDLL_getcwd for buf %p size %d\n",
1361 buf,size);
1362 if (buf == NULL)
1364 dprintf_crtdll(stderr,"CRTDLL_getcwd malloc unsupported\n");
1365 printf("CRTDLL_getcwd malloc unsupported\n");
1366 return 0;
1368 ret = getcwd(buf,size);
1369 if (!DOSFS_GetFullName( buf, FALSE, &full_name ))
1371 dprintf_crtdll(stddeb,"CRTDLL_getcwd failed\n");
1372 return 0;
1374 if (strlen(full_name.short_name)>size)
1376 dprintf_crtdll(stddeb,"CRTDLL_getcwd string too long\n");
1377 return 0;
1379 ret=strcpy(buf,full_name.short_name);
1380 if (ret)
1381 dprintf_crtdll(stddeb,"CRTDLL_getcwd returned:%s\n",ret);
1382 return ret;
1385 /*********************************************************************
1386 * _mkdir (CRTDLL.234)
1388 INT32 __cdecl CRTDLL__mkdir(LPCSTR newdir)
1390 if (!CreateDirectory32A(newdir,NULL))
1391 return -1;
1392 return 0;
1395 /*********************************************************************
1396 * _errno (CRTDLL.52)
1397 * Yes, this is a function.
1399 LPINT32 __cdecl CRTDLL__errno()
1401 static int crtdllerrno;
1402 extern int LastErrorToErrno(DWORD);
1404 /* FIXME: we should set the error at the failing function call time */
1405 crtdllerrno = LastErrorToErrno(GetLastError());
1406 return &crtdllerrno;
1409 /*********************************************************************
1410 * _tempnam (CRTDLL.305)
1413 LPSTR __cdecl CRTDLL__tempnam(LPCSTR dir, LPCSTR prefix)
1416 char *ret;
1417 DOS_FULL_NAME tempname;
1419 if ((ret = tempnam(dir,prefix))==NULL) {
1420 dprintf_crtdll(stddeb,
1421 "CRTDLL_tempnam Unable to get unique filename\n");
1422 return NULL;
1424 if (!DOSFS_GetFullName(ret,FALSE,&tempname))
1426 dprintf_crtdll(stddeb,
1427 "CRTDLL_tempnam Wrong path?\n");
1428 return NULL;
1430 free(ret);
1431 if ((ret = CRTDLL_malloc(strlen(tempname.short_name)+1)) == NULL) {
1432 dprintf_crtdll(stddeb,
1433 "CRTDLL_tempnam CRTDL_malloc for shortname failed\n");
1434 return NULL;
1436 if ((ret = strcpy(ret,tempname.short_name)) == NULL) {
1437 dprintf_crtdll(stddeb,
1438 "CRTDLL_tempnam Malloc for shortname failed\n");
1439 return NULL;
1442 dprintf_crtdll(stddeb,"CRTDLL_tempnam dir %s prefix %s got %s\n",
1443 dir,prefix,ret);
1444 return ret;
1447 /*********************************************************************
1448 * tmpnam (CRTDLL.490)
1450 * lcclnk from lcc-win32 relies on a terminating dot in the name returned
1453 LPSTR __cdecl CRTDLL_tmpnam(LPSTR s)
1455 char *ret;
1457 if ((ret =tmpnam(s))== NULL) {
1458 dprintf_crtdll(stddeb,
1459 "CRTDLL_tmpnam Unable to get unique filename\n");
1460 return NULL;
1462 if (!DOSFS_GetFullName(ret,FALSE,&CRTDLL_tmpname))
1464 dprintf_crtdll(stddeb,
1465 "CRTDLL_tmpnam Wrong path?\n");
1466 return NULL;
1468 strcat(CRTDLL_tmpname.short_name,".");
1469 dprintf_crtdll(stddeb,"CRTDLL_tmpnam for buf %p got %s\n",
1470 s,CRTDLL_tmpname.short_name);
1471 dprintf_crtdll(stddeb,"CRTDLL_tmpnam long got %s\n",
1472 CRTDLL_tmpname.long_name);
1473 if ( s != NULL)
1474 return strcpy(s,CRTDLL_tmpname.short_name);
1475 else
1476 return CRTDLL_tmpname.short_name;
1480 /*********************************************************************
1481 * _itoa (CRTDLL.165)
1483 LPSTR __cdecl CRTDLL__itoa(INT32 x,LPSTR buf,INT32 buflen)
1485 wsnprintf32A(buf,buflen,"%d",x);
1486 return buf;
1489 typedef VOID (*sig_handler_type)(VOID);
1491 /*********************************************************************
1492 * signal (CRTDLL.455)
1494 VOID __cdecl CRTDLL_signal(int sig, sig_handler_type ptr)
1496 dprintf_crtdll(stddeb,"CRTDLL_signal %d %p: STUB!\n",sig,ptr);