Bug fixes.
[wine] / misc / crtdll.c
1 /*
2  * The C RunTime DLL
3  * 
4  * Implements C run-time functionality as known from UNIX.
5  *
6  * Copyright 1996,1998 Marcus Meissner
7  * Copyright 1996 Jukka Iivonen
8  * Copyright 1997 Uwe Bonnes
9  */
10
11 /*
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
19 AJ 990101:
20 - needs a proper stdio emulation based on Win32 file handles
21 - should set CRTDLL errno from GetLastError() in every function
22 */
23
24 /* NOTE: This file also implements the wcs* functions. They _ARE_ in 
25  * the newer Linux libcs, but use 4 byte wide characters, so are unusable,
26  * since we need 2 byte wide characters. - Marcus Meissner, 981031
27  */
28
29 #include <errno.h>
30 #include <stdlib.h>
31 #include <stdarg.h>
32 #include <string.h>
33 #include <sys/stat.h>
34 #include <sys/times.h>
35 #include <unistd.h>
36 #include <time.h>
37 #include <ctype.h>
38 #include <math.h>
39 #include <fcntl.h>
40 #include <setjmp.h>
41 #include "winbase.h"
42 #include "winuser.h"
43 #include "winerror.h"
44 #include "debug.h"
45 #include "module.h"
46 #include "heap.h"
47 #include "crtdll.h"
48 #include "drive.h"
49 #include "file.h"
50 #include "except.h"
51 #include "options.h"
52 #include "winnls.h"
53
54 DEFAULT_DEBUG_CHANNEL(crtdll)
55
56 /* windows.h RAND_MAX is smaller than normal RAND_MAX */
57 #define CRTDLL_RAND_MAX         0x7fff 
58
59 static DOS_FULL_NAME CRTDLL_tmpname;
60
61 UINT CRTDLL_argc_dll;         /* CRTDLL.23 */
62 LPSTR *CRTDLL_argv_dll;         /* CRTDLL.24 */
63 LPSTR  CRTDLL_acmdln_dll;       /* CRTDLL.38 */
64 UINT CRTDLL_basemajor_dll;    /* CRTDLL.42 */
65 UINT CRTDLL_baseminor_dll;    /* CRTDLL.43 */
66 UINT CRTDLL_baseversion_dll;  /* CRTDLL.44 */
67 UINT CRTDLL_commode_dll;      /* CRTDLL.59 */
68 LPSTR  CRTDLL_environ_dll;      /* CRTDLL.75 */
69 UINT CRTDLL_fmode_dll;        /* CRTDLL.104 */
70 UINT CRTDLL_osmajor_dll;      /* CRTDLL.241 */
71 UINT CRTDLL_osminor_dll;      /* CRTDLL.242 */
72 UINT CRTDLL_osmode_dll;       /* CRTDLL.243 */
73 UINT CRTDLL_osver_dll;        /* CRTDLL.244 */
74 UINT CRTDLL_osversion_dll;    /* CRTDLL.245 */
75 UINT CRTDLL_winmajor_dll;     /* CRTDLL.329 */
76 UINT CRTDLL_winminor_dll;     /* CRTDLL.330 */
77 UINT CRTDLL_winver_dll;       /* CRTDLL.331 */
78
79 /* FIXME: structure layout is obviously not correct */
80 typedef struct
81 {
82     HANDLE handle;
83     int      pad[7];
84 } CRTDLL_FILE;
85
86 CRTDLL_FILE CRTDLL_iob[3];
87
88 static CRTDLL_FILE * const CRTDLL_stdin  = &CRTDLL_iob[0];
89 static CRTDLL_FILE * const CRTDLL_stdout = &CRTDLL_iob[1];
90 static CRTDLL_FILE * const CRTDLL_stderr = &CRTDLL_iob[2];
91
92 typedef VOID (*new_handler_type)(VOID);
93
94 static new_handler_type new_handler;
95
96 #if defined(__GNUC__) && defined(__i386__)
97 #define USING_REAL_FPU
98 #define DO_FPU(x,y) __asm__ __volatile__( x " %0;fwait" : "=m" (y) : )
99 #define POP_FPU(x) DO_FPU("fstpl",x)
100 #endif
101
102 /*********************************************************************
103  *                  _GetMainArgs  (CRTDLL.022)
104  */
105 DWORD __cdecl CRTDLL__GetMainArgs(LPDWORD argc,LPSTR **argv,
106                                 LPSTR *environ,DWORD flag)
107 {
108         char *cmdline;
109         char  **xargv;
110         int     xargc,i,afterlastspace;
111         DWORD   version;
112
113         TRACE(crtdll,"(%p,%p,%p,%ld).\n",
114                 argc,argv,environ,flag
115         );
116         CRTDLL_acmdln_dll = cmdline = HEAP_strdupA( GetProcessHeap(), 0,
117                                                     GetCommandLineA() );
118         TRACE(crtdll,"got '%s'\n", cmdline);
119
120         version = GetVersion();
121         CRTDLL_osver_dll       = version >> 16;
122         CRTDLL_winminor_dll    = version & 0xFF;
123         CRTDLL_winmajor_dll    = (version>>8) & 0xFF;
124         CRTDLL_baseversion_dll = version >> 16;
125         CRTDLL_winver_dll      = ((version >> 8) & 0xFF) + ((version & 0xFF) << 8);
126         CRTDLL_baseminor_dll   = (version >> 16) & 0xFF;
127         CRTDLL_basemajor_dll   = (version >> 24) & 0xFF;
128         CRTDLL_osversion_dll   = version & 0xFFFF;
129         CRTDLL_osminor_dll     = version & 0xFF;
130         CRTDLL_osmajor_dll     = (version>>8) & 0xFF;
131
132         /* missing threading init */
133
134         i=0;xargv=NULL;xargc=0;afterlastspace=0;
135         while (cmdline[i]) {
136                 if (cmdline[i]==' ') {
137                         xargv=(char**)HeapReAlloc( GetProcessHeap(), 0, xargv,
138                                                    sizeof(char*)*(++xargc));
139                         cmdline[i]='\0';
140                         xargv[xargc-1] = HEAP_strdupA( GetProcessHeap(), 0,
141                                                        cmdline+afterlastspace);
142                         i++;
143                         while (cmdline[i]==' ')
144                                 i++;
145                         if (cmdline[i])
146                                 afterlastspace=i;
147                 } else
148                         i++;
149         }
150         xargv=(char**)HeapReAlloc( GetProcessHeap(), 0, xargv,
151                                    sizeof(char*)*(++xargc));
152         cmdline[i]='\0';
153         xargv[xargc-1] = HEAP_strdupA( GetProcessHeap(), 0,
154                                        cmdline+afterlastspace);
155         CRTDLL_argc_dll = xargc;
156         *argc           = xargc;
157         CRTDLL_argv_dll = xargv;
158         *argv           = xargv;
159
160         TRACE(crtdll,"found %d arguments\n",
161                 CRTDLL_argc_dll);
162         CRTDLL_environ_dll = *environ = GetEnvironmentStringsA();
163         return 0;
164 }
165
166
167 typedef void (*_INITTERMFUN)();
168
169 /* fixme: move to header */
170 struct find_t 
171 {   unsigned    attrib;
172     time_t      time_create;    /* -1 when not avaiable */
173     time_t      time_access;    /* -1 when not avaiable */
174     time_t      time_write;
175     unsigned long       size;   /* fixme: 64 bit ??*/
176     char        name[260];
177 };
178  /*********************************************************************
179  *                  _findfirst    (CRTDLL.099)
180  * 
181  * BUGS
182  *   Unimplemented
183  */
184 DWORD __cdecl CRTDLL__findfirst(LPCSTR fname,  struct find_t * x2)
185 {
186   FIXME(crtdll, ":(%s,%p): stub\n",fname,x2);
187   SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
188   return FALSE;
189 }
190
191 /*********************************************************************
192  *                  _findnext     (CRTDLL.100)
193  * 
194  * BUGS
195  *   Unimplemented
196  */
197 INT __cdecl CRTDLL__findnext(DWORD hand, struct find_t * x2)
198 {
199   FIXME(crtdll, ":(%ld,%p): stub\n",hand,x2);
200   SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
201   return FALSE;
202 }
203
204 /*********************************************************************
205  *                  _fstat        (CRTDLL.111)
206  * 
207  * BUGS
208  *   Unimplemented
209  */
210 int __cdecl CRTDLL__fstat(int file, struct stat* buf)
211 {
212   FIXME(crtdll, ":(%d,%p): stub\n",file,buf);
213   SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
214   return FALSE;
215 }
216
217 /*********************************************************************
218  *                  _initterm     (CRTDLL.135)
219  */
220 DWORD __cdecl CRTDLL__initterm(_INITTERMFUN *start,_INITTERMFUN *end)
221 {
222         _INITTERMFUN    *current;
223
224         TRACE(crtdll,"(%p,%p)\n",start,end);
225         current=start;
226         while (current<end) {
227                 if (*current) (*current)();
228                 current++;
229         }
230         return 0;
231 }
232
233 /*********************************************************************
234  *                  _fdopen     (CRTDLL.91)
235  */
236 CRTDLL_FILE * __cdecl CRTDLL__fdopen(INT handle, LPCSTR mode)
237 {
238     CRTDLL_FILE *file;
239
240     switch (handle) 
241     {
242     case 0:
243         file = CRTDLL_stdin;
244         if (!file->handle) file->handle = GetStdHandle( STD_INPUT_HANDLE );
245         break;
246     case 1:
247         file = CRTDLL_stdout;
248         if (!file->handle) file->handle = GetStdHandle( STD_OUTPUT_HANDLE );
249         break;
250     case 2:
251         file=CRTDLL_stderr;
252         if (!file->handle) file->handle = GetStdHandle( STD_ERROR_HANDLE );
253         break;
254     default:
255         file = HeapAlloc( GetProcessHeap(), 0, sizeof(*file) );
256         file->handle = handle;
257         break;
258     }
259   TRACE(crtdll, "open handle %d mode %s  got file %p\n",
260                handle, mode, file);
261   return file;
262 }
263
264
265 /*******************************************************************
266  *         _global_unwind2  (CRTDLL.129)
267  */
268 void __cdecl CRTDLL__global_unwind2( PEXCEPTION_FRAME frame )
269 {
270     RtlUnwind( frame, 0, NULL, 0 );
271 }
272
273 /*******************************************************************
274  *         _local_unwind2  (CRTDLL.173)
275  */
276 void __cdecl CRTDLL__local_unwind2( PEXCEPTION_FRAME endframe, DWORD nr )
277 {
278     TRACE(crtdll,"(%p,%ld)\n",endframe,nr);
279 }
280 /*********************************************************************
281  *                  _read     (CRTDLL.256)
282  * 
283  * BUGS
284  *   Unimplemented
285  */
286 INT __cdecl CRTDLL__read(INT fd, LPVOID buf, UINT count)
287 {
288   FIXME(crtdll,":(%d,%p,%d): stub\n",fd,buf,count);
289   SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
290   return FALSE;
291 }
292
293 /*********************************************************************
294  *                  fopen     (CRTDLL.372)
295  */
296 CRTDLL_FILE * __cdecl CRTDLL_fopen(LPCSTR path, LPCSTR mode)
297 {
298   CRTDLL_FILE *file = NULL;
299   HFILE handle;
300 #if 0
301   DOS_FULL_NAME full_name;
302   
303   if (!DOSFS_GetFullName( path, FALSE, &full_name )) {
304     WARN(crtdll, "file %s bad name\n",path);
305    return 0;
306   }
307   
308   file=fopen(full_name.long_name ,mode);
309 #endif
310   DWORD access = 0, creation = 0;
311
312   if ((strchr(mode,'r')&&strchr(mode,'a'))||
313       (strchr(mode,'r')&&strchr(mode,'w'))||
314       (strchr(mode,'w')&&strchr(mode,'a')))
315     return NULL;
316        
317   if (mode[0] == 'r')
318   {
319       access = GENERIC_READ;
320       creation = OPEN_EXISTING;
321       if (mode[1] == '+') access |= GENERIC_WRITE;
322   }
323   else if (mode[0] == 'w')
324   {
325       access = GENERIC_WRITE;
326       creation = CREATE_ALWAYS;
327       if (mode[1] == '+') access |= GENERIC_READ;
328   }
329   else if (mode[0] == 'a')
330   {
331       /* FIXME: there is no O_APPEND in CreateFile, should emulate it */
332       access = GENERIC_WRITE;
333       creation = OPEN_ALWAYS;
334       if (mode[1] == '+') access |= GENERIC_READ;
335   }
336   /* FIXME: should handle text/binary mode */
337
338   if ((handle = CreateFileA( path, access, FILE_SHARE_READ | FILE_SHARE_WRITE,
339                                NULL, creation, FILE_ATTRIBUTE_NORMAL,
340                                -1 )) != INVALID_HANDLE_VALUE)
341   {
342       file = HeapAlloc( GetProcessHeap(), 0, sizeof(*file) );
343       file->handle = handle;
344   }
345   TRACE(crtdll, "file %s mode %s got handle %d file %p\n",
346                  path,mode,handle,file);
347   return file;
348 }
349
350 /*********************************************************************
351  *                  fread     (CRTDLL.377)
352  */
353 DWORD __cdecl CRTDLL_fread(LPVOID ptr, INT size, INT nmemb, CRTDLL_FILE *file)
354 {
355 #if 0
356   int i=0;
357   void *temp=ptr;
358
359   /* If we would honour CR/LF <-> LF translation, we could do it like this.
360      We should keep track of all files opened, and probably files with \
361      known binary extensions must be unchanged */
362   while ( (i < (nmemb*size)) && (ret==1)) {
363     ret=fread(temp,1,1,file);
364     TRACE(crtdll, "got %c 0x%02x ret %d\n",
365                  (isalpha(*(unsigned char*)temp))? *(unsigned char*)temp:
366                  ' ',*(unsigned char*)temp, ret);
367     if (*(unsigned char*)temp != 0xd) { /* skip CR */
368       temp++;
369       i++;
370     }
371     else
372       TRACE(crtdll, "skipping ^M\n");
373   }
374   TRACE(crtdll, "0x%08x items of size %d from file %p to %p\n",
375                nmemb,size,file,ptr,);
376   if(i!=nmemb)
377     WARN(crtdll, " failed!\n");
378
379   return i;
380 #else
381   DWORD ret;
382
383   TRACE(crtdll, "0x%08x items of size %d from file %p to %p\n",
384                nmemb,size,file,ptr);
385   if (!ReadFile( file->handle, ptr, size * nmemb, &ret, NULL ))
386       WARN(crtdll, " failed!\n");
387
388   return ret / size;
389 #endif
390 }
391 /*********************************************************************
392  *                  freopen    (CRTDLL.379)
393  * 
394  * BUGS
395  *   Unimplemented
396  */
397 DWORD __cdecl CRTDLL_freopen(LPCSTR path, LPCSTR mode, LPVOID stream)
398 {
399   FIXME(crtdll, ":(%s,%s,%p): stub\n", path, mode, stream);
400   SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
401   return FALSE;
402 }
403
404 /*********************************************************************
405  *                  fscanf     (CRTDLL.381)
406  */
407 INT __cdecl CRTDLL_fscanf( CRTDLL_FILE *stream, LPSTR format, ... )
408 {
409 #if 0
410     va_list valist;
411     INT res;
412
413     va_start( valist, format );
414 #ifdef HAVE_VFSCANF
415     res = vfscanf( xlat_file_ptr(stream), format, valist );
416 #endif
417     va_end( valist );
418     return res;
419 #endif
420     FIXME(crtdll,"broken\n");
421     return 0;
422 }
423
424 /*********************************************************************
425  *                  fseek     (CRTDLL.382)
426  */
427 LONG __cdecl CRTDLL_fseek( CRTDLL_FILE *file, LONG offset, INT whence)
428 {
429   TRACE(crtdll, "file %p to 0x%08lx pos %s\n",
430         file,offset,(whence==SEEK_SET)?"SEEK_SET":
431         (whence==SEEK_CUR)?"SEEK_CUR":
432         (whence==SEEK_END)?"SEEK_END":"UNKNOWN");
433   if (SetFilePointer( file->handle, offset, NULL, whence ) != 0xffffffff)
434       return 0;
435   WARN(crtdll, " failed!\n");
436   return -1;
437 }
438   
439 /*********************************************************************
440  *                  fsetpos     (CRTDLL.383)
441  */
442 INT __cdecl CRTDLL_fsetpos( CRTDLL_FILE *file, INT *pos )
443 {
444     TRACE(crtdll, "file %p pos %d\n", file, *pos );
445     return CRTDLL_fseek(file, *pos, SEEK_SET);
446 }
447
448 /*********************************************************************
449  *                  ftell     (CRTDLL.384)
450  */
451 LONG __cdecl CRTDLL_ftell( CRTDLL_FILE *file )
452 {
453     return SetFilePointer( file->handle, 0, NULL, SEEK_CUR );
454 }
455   
456 /*********************************************************************
457  *                  fwrite     (CRTDLL.386)
458  */
459 DWORD __cdecl CRTDLL_fwrite( LPVOID ptr, INT size, INT nmemb, CRTDLL_FILE *file )
460 {
461     DWORD ret;
462
463     TRACE(crtdll, "0x%08x items of size %d to file %p from %p\n",
464           nmemb,size,file,ptr);
465     if (!WriteFile( file->handle, ptr, size * nmemb, &ret, NULL ))
466         WARN(crtdll, " failed!\n");
467     return ret / size;
468 }
469
470 /*********************************************************************
471  *                  setbuf     (CRTDLL.452)
472  */
473 INT __cdecl CRTDLL_setbuf(CRTDLL_FILE *file, LPSTR buf)
474 {
475   TRACE(crtdll, "(file %p buf %p)\n", file, buf);
476   /* this doesn't work:"void value not ignored as it ought to be" 
477   return setbuf(file,buf); 
478   */
479   /* FIXME: no buffering for now */
480   return 0;
481 }
482
483 /*********************************************************************
484  *                  _open_osfhandle         (CRTDLL.240)
485  */
486 HFILE __cdecl CRTDLL__open_osfhandle(LONG osfhandle, INT flags)
487 {
488 HFILE handle;
489  
490         switch (osfhandle) {
491         case STD_INPUT_HANDLE :
492         case 0 :
493           handle=0;
494           break;
495         case STD_OUTPUT_HANDLE:
496         case 1:
497           handle=1;
498           break;
499         case STD_ERROR_HANDLE:
500         case 2:
501           handle=2;
502           break;
503         default:
504           return (-1);
505         }
506         TRACE(crtdll, "(handle %08lx,flags %d) return %d\n",
507                      osfhandle,flags,handle);
508         return handle;
509         
510 }
511
512 /*********************************************************************
513  *                  srand         (CRTDLL.460)
514  */
515 void __cdecl CRTDLL_srand(DWORD seed)
516 {
517         /* FIXME: should of course be thread? process? local */
518         srand(seed);
519 }
520
521 /*********************************************************************
522  *                  vfprintf       (CRTDLL.373)
523  */
524 INT __cdecl CRTDLL_vfprintf( CRTDLL_FILE *file, LPSTR format, va_list args )
525 {
526     char buffer[1024];  /* FIXME... */
527
528     vsprintf( buffer, format, args );
529     return CRTDLL_fwrite( buffer, 1, strlen(buffer), file );
530 }
531
532 /*********************************************************************
533  *                  fprintf       (CRTDLL.373)
534  */
535 INT __cdecl CRTDLL_fprintf( CRTDLL_FILE *file, LPSTR format, ... )
536 {
537     va_list valist;
538     INT res;
539
540     va_start( valist, format );
541     res = CRTDLL_vfprintf( file, format, valist );
542     va_end( valist );
543     return res;
544 }
545
546 /*********************************************************************
547  *                  time          (CRTDLL.488)
548  */
549 time_t __cdecl CRTDLL_time(time_t *timeptr)
550 {
551         time_t  curtime = time(NULL);
552
553         if (timeptr)
554                 *timeptr = curtime;
555         return curtime;
556 }
557
558 /*********************************************************************
559  *                            (CRTDLL.350)
560  */
561 clock_t __cdecl CRTDLL_clock(void)
562 {
563         struct tms alltimes;
564         clock_t res;
565
566         times(&alltimes);
567         res = alltimes.tms_utime + alltimes.tms_stime+
568                alltimes.tms_cutime + alltimes.tms_cstime;
569         /* Fixme: We need some symbolic representation
570            for (Hostsystem_)CLOCKS_PER_SEC 
571            and (Emulated_system_)CLOCKS_PER_SEC
572            10 holds only for Windows/Linux_i86)
573            */
574         return 10*res;
575 }
576
577 /*********************************************************************
578  *                  _isatty       (CRTDLL.137)
579  */
580 BOOL __cdecl CRTDLL__isatty(DWORD x)
581 {
582         TRACE(crtdll,"(%ld)\n",x);
583         return TRUE;
584 }
585
586 /*********************************************************************
587  *                  _write        (CRTDLL.332)
588  */
589 INT __cdecl CRTDLL__write(INT fd,LPCVOID buf,UINT count)
590 {
591         INT len=0;
592
593         if (fd == -1)
594           len = -1;
595         else if (fd<=2)
596           len = (UINT)write(fd,buf,(LONG)count);
597         else
598           len = _lwrite(fd,buf,count);
599         TRACE(crtdll,"%d/%d byte to dfh %d from %p,\n",
600                        len,count,fd,buf);
601         return len;
602 }
603
604
605 /*********************************************************************
606  *                  _cexit          (CRTDLL.49)
607  *
608  *  FIXME: What the heck is the difference between 
609  *  FIXME           _c_exit         (CRTDLL.47)
610  *  FIXME           _cexit          (CRTDLL.49)
611  *  FIXME           _exit           (CRTDLL.87)
612  *  FIXME           exit            (CRTDLL.359)
613  *
614  * atexit-processing comes to mind -- MW.
615  *
616  */
617 void __cdecl CRTDLL__cexit(INT ret)
618 {
619         TRACE(crtdll,"(%d)\n",ret);
620         ExitProcess(ret);
621 }
622
623
624 /*********************************************************************
625  *                  exit          (CRTDLL.359)
626  */
627 void __cdecl CRTDLL_exit(DWORD ret)
628 {
629         TRACE(crtdll,"(%ld)\n",ret);
630         ExitProcess(ret);
631 }
632
633
634 /*********************************************************************
635  *                  _abnormal_termination          (CRTDLL.36)
636  */
637 INT __cdecl CRTDLL__abnormal_termination(void)
638 {
639         TRACE(crtdll,"(void)\n");
640         return 0;
641 }
642
643
644 /*********************************************************************
645  *                  _access          (CRTDLL.37)
646  */
647 INT __cdecl CRTDLL__access(LPCSTR filename, INT mode)
648 {
649     DWORD attr = GetFileAttributesA(filename);
650
651     if (attr == -1)
652     {
653         if (GetLastError() == ERROR_INVALID_ACCESS)
654             errno = EACCES;
655         else
656             errno = ENOENT;
657         return -1;
658     }
659
660     if ((attr & FILE_ATTRIBUTE_READONLY) && (mode & W_OK))
661     {
662         errno = EACCES;
663         return -1;
664     }
665     else
666         return 0;
667 }
668
669
670 /*********************************************************************
671  *                  fflush        (CRTDLL.365)
672  */
673 INT __cdecl CRTDLL_fflush( CRTDLL_FILE *file )
674 {
675     return FlushFileBuffers( file->handle ) ? 0 : -1;
676 }
677
678
679 /*********************************************************************
680  *                  rand          (CRTDLL.446)
681  */
682 INT __cdecl CRTDLL_rand()
683 {
684     return (rand() & CRTDLL_RAND_MAX); 
685 }
686
687
688 /*********************************************************************
689  *                  putchar       (CRTDLL.442)
690  */
691 void __cdecl CRTDLL_putchar( INT x )
692 {
693     putchar(x);
694 }
695
696
697 /*********************************************************************
698  *                  fputc       (CRTDLL.374)
699  */
700 INT __cdecl CRTDLL_fputc( INT c, CRTDLL_FILE *file )
701 {
702     char ch = (char)c;
703     DWORD res;
704     TRACE(crtdll, "%c to file %p\n",c,file);
705     if (!WriteFile( file->handle, &ch, 1, &res, NULL )) return -1;
706     return c;
707 }
708
709
710 /*********************************************************************
711  *                  fputs       (CRTDLL.375)
712  */
713 INT __cdecl CRTDLL_fputs( LPCSTR s, CRTDLL_FILE *file )
714 {
715     DWORD res;
716     TRACE(crtdll, "%s to file %p\n",s,file);
717     if (!WriteFile( file->handle, s, strlen(s), &res, NULL )) return -1;
718     return res;
719 }
720
721
722 /*********************************************************************
723  *                  puts       (CRTDLL.443)
724  */
725 INT __cdecl CRTDLL_puts(LPCSTR s)
726 {
727     TRACE(crtdll, "%s \n",s);
728     return puts(s);
729 }
730
731
732 /*********************************************************************
733  *                  putc       (CRTDLL.441)
734  */
735 INT __cdecl CRTDLL_putc( INT c, CRTDLL_FILE *file )
736 {
737     return CRTDLL_fputc( c, file );
738 }
739
740 /*********************************************************************
741  *                  fgetc       (CRTDLL.366)
742  */
743 INT __cdecl CRTDLL_fgetc( CRTDLL_FILE *file )
744 {
745     DWORD res;
746     char ch;
747     if (!ReadFile( file->handle, &ch, 1, &res, NULL )) return -1;
748     if (res != 1) return -1;
749     return ch;
750 }
751
752
753 /*********************************************************************
754  *                  getc       (CRTDLL.388)
755  */
756 INT __cdecl CRTDLL_getc( CRTDLL_FILE *file )
757 {
758     return CRTDLL_fgetc( file );
759 }
760
761
762 /*********************************************************************
763  *                  fgets       (CRTDLL.368)
764  */
765 CHAR* __cdecl CRTDLL_fgets( LPSTR s, INT size, CRTDLL_FILE *file )
766 {
767     int    cc;
768     LPSTR  buf_start = s;
769
770     /* BAD, for the whole WINE process blocks... just done this way to test
771      * windows95's ftp.exe.
772      */
773
774     for(cc = CRTDLL_fgetc(file); cc != EOF && cc != '\n'; cc = CRTDLL_fgetc(file))
775         if (cc != '\r')
776         {
777             if (--size <= 0) break;
778             *s++ = (char)cc;
779         }
780
781     *s = '\0';
782
783     TRACE(crtdll,"got '%s'\n", buf_start);
784     return buf_start;
785 }
786
787
788 /*********************************************************************
789  *                  gets          (CRTDLL.391)
790  */
791 LPSTR __cdecl CRTDLL_gets(LPSTR buf)
792 {
793     int    cc;
794     LPSTR  buf_start = buf;
795
796     /* BAD, for the whole WINE process blocks... just done this way to test
797      * windows95's ftp.exe.
798      */
799
800     for(cc = fgetc(stdin); cc != EOF && cc != '\n'; cc = fgetc(stdin))
801         if(cc != '\r') *buf++ = (char)cc;
802
803     *buf = '\0';
804
805     TRACE(crtdll,"got '%s'\n", buf_start);
806     return buf_start;
807 }
808
809
810 /*********************************************************************
811  *                  _rotl          (CRTDLL.259)
812  */
813 UINT __cdecl CRTDLL__rotl(UINT x,INT shift)
814 {
815    unsigned int ret = (x >> shift)|( x >>((sizeof(x))-shift));
816
817    TRACE(crtdll, "got 0x%08x rot %d ret 0x%08x\n",
818                   x,shift,ret);
819    return ret;
820     
821 }
822 /*********************************************************************
823  *                  _lrotl          (CRTDLL.176)
824  */
825 DWORD __cdecl CRTDLL__lrotl(DWORD x,INT shift)
826 {
827    unsigned long ret = (x >> shift)|( x >>((sizeof(x))-shift));
828
829    TRACE(crtdll, "got 0x%08lx rot %d ret 0x%08lx\n",
830                   x,shift,ret);
831    return ret;
832     
833 }
834
835
836 /*********************************************************************
837  *                  _mbsicmp      (CRTDLL.204)
838  */
839 int __cdecl CRTDLL__mbsicmp(unsigned char *x,unsigned char *y)
840 {
841     do {
842         if (!*x)
843             return !!*y;
844         if (!*y)
845             return !!*x;
846         /* FIXME: MBCS handling... */
847         if (*x!=*y)
848             return 1;
849         x++;
850         y++;
851     } while (1);
852 }
853
854
855 /*********************************************************************
856  *                  _mbsinc       (CRTDLL.205)
857  */
858 unsigned char * __cdecl CRTDLL__mbsinc(unsigned char *x)
859 {
860     /* FIXME: mbcs */
861     return x++;
862 }
863
864
865 /*********************************************************************
866  *                  vsprintf      (CRTDLL.500)
867  */
868 INT __cdecl CRTDLL_vsprintf( LPSTR buffer, LPCSTR spec, va_list args )
869 {
870     return wvsprintfA( buffer, spec, args );
871 }
872
873 /*********************************************************************
874  *                  vswprintf      (CRTDLL.501)
875  */
876 INT __cdecl CRTDLL_vswprintf( LPWSTR buffer, LPCWSTR spec, va_list args )
877 {
878     return wvsprintfW( buffer, spec, args );
879 }
880
881 /*********************************************************************
882  *                  _mbscpy       (CRTDLL.200)
883  */
884 unsigned char* __cdecl CRTDLL__mbscpy(unsigned char *x,unsigned char *y)
885 {
886     TRACE(crtdll,"CRTDLL_mbscpy %s and %s\n",x,y);
887     return strcpy(x,y);
888 }
889
890
891 /*********************************************************************
892  *                  _mbscat       (CRTDLL.197)
893  */
894 unsigned char* __cdecl CRTDLL__mbscat(unsigned char *x,unsigned char *y)
895 {
896     return strcat(x,y);
897 }
898
899
900 /*********************************************************************
901  *                  _strcmpi   (CRTDLL.282) (CRTDLL.287)
902  */
903 INT __cdecl CRTDLL__strcmpi( LPCSTR s1, LPCSTR s2 )
904 {
905     return lstrcmpiA( s1, s2 );
906 }
907
908
909 /*********************************************************************
910  *                  _strnicmp   (CRTDLL.293)
911  */
912 INT __cdecl CRTDLL__strnicmp( LPCSTR s1, LPCSTR s2, INT n )
913 {
914     return lstrncmpiA( s1, s2, n );
915 }
916
917
918 /*********************************************************************
919  *                  _strlwr      (CRTDLL.293)
920  *
921  * convert a string in place to lowercase 
922  */
923 LPSTR __cdecl CRTDLL__strlwr(LPSTR x)
924 {
925   unsigned char *y =x;
926   
927   TRACE(crtdll, "CRTDLL_strlwr got %s\n", x);
928   while (*y) {
929     if ((*y > 0x40) && (*y< 0x5b))
930       *y = *y + 0x20;
931     y++;
932   }
933   TRACE(crtdll, "   returned %s\n", x);
934                  
935   return x;
936 }
937
938 /*********************************************************************
939  *                  system       (CRTDLL.485)
940  */
941 INT __cdecl CRTDLL_system(LPSTR x)
942 {
943 #define SYSBUF_LENGTH 1500
944   char buffer[SYSBUF_LENGTH];
945   unsigned char *y = x;
946   unsigned char *bp;
947   int i;
948
949   sprintf( buffer, "%s \"", Options.argv0 );
950   bp = buffer + strlen(buffer);
951   i = strlen(buffer) + strlen(x) +2;
952
953   /* Calculate needed buffer size to prevent overflow.  */
954   while (*y) {
955     if (*y =='\\') i++;
956     y++;
957   }
958   /* If buffer too short, exit.  */
959   if (i > SYSBUF_LENGTH) {
960     TRACE(crtdll,"_system buffer to small\n");
961     return 127;
962   }
963   
964   y =x;
965
966   while (*y) {
967     *bp = *y;
968     bp++; y++;
969     if (*(y-1) =='\\') *bp++ = '\\';
970   }
971   /* Remove spaces from end of string.  */
972   while (*(y-1) == ' ') {
973     bp--;y--;
974   }
975   *bp++ = '"';
976   *bp = 0;
977   TRACE(crtdll, "_system got '%s', executing '%s'\n",x,buffer);
978
979   return system(buffer);
980 }
981
982 /*********************************************************************
983  *                  _strupr       (CRTDLL.300)
984  */
985 LPSTR __cdecl CRTDLL__strupr(LPSTR x)
986 {
987         LPSTR   y=x;
988
989         while (*y) {
990                 *y=toupper(*y);
991                 y++;
992         }
993         return x;
994 }
995
996 /*********************************************************************
997  *                  _wcsupr       (CRTDLL.328)
998  */
999 LPWSTR __cdecl CRTDLL__wcsupr(LPWSTR x)
1000 {
1001         LPWSTR  y=x;
1002
1003         while (*y) {
1004                 *y=towupper(*y);
1005                 y++;
1006         }
1007         return x;
1008 }
1009
1010 /*********************************************************************
1011  *                  _wcslwr       (CRTDLL.323)
1012  */
1013 LPWSTR __cdecl CRTDLL__wcslwr(LPWSTR x)
1014 {
1015         LPWSTR  y=x;
1016
1017         while (*y) {
1018                 *y=towlower(*y);
1019                 y++;
1020         }
1021         return x;
1022 }
1023
1024
1025 /*********************************************************************
1026  *                  longjmp        (CRTDLL.426)
1027  */
1028 VOID __cdecl CRTDLL_longjmp(jmp_buf env, int val)
1029 {
1030     FIXME(crtdll,"CRTDLL_longjmp semistup, expect crash\n");
1031     longjmp(env, val);
1032 }
1033
1034 /*********************************************************************
1035  *                  malloc        (CRTDLL.427)
1036  */
1037 VOID* __cdecl CRTDLL_malloc(DWORD size)
1038 {
1039     return HeapAlloc(GetProcessHeap(),0,size);
1040 }
1041
1042 /*********************************************************************
1043  *                  new           (CRTDLL.001)
1044  */
1045 VOID* __cdecl CRTDLL_new(DWORD size)
1046 {
1047     VOID* result;
1048     if(!(result = HeapAlloc(GetProcessHeap(),0,size)) && new_handler)
1049         (*new_handler)();
1050     return result;
1051 }
1052
1053 /*********************************************************************
1054  *                  set_new_handler(CRTDLL.003)
1055  */
1056 new_handler_type __cdecl CRTDLL_set_new_handler(new_handler_type func)
1057 {
1058     new_handler_type old_handler = new_handler;
1059     new_handler = func;
1060     return old_handler;
1061 }
1062
1063 /*********************************************************************
1064  *                  calloc        (CRTDLL.350)
1065  */
1066 VOID* __cdecl CRTDLL_calloc(DWORD size, DWORD count)
1067 {
1068     return HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, size * count );
1069 }
1070
1071 /*********************************************************************
1072  *                  realloc        (CRTDLL.447)
1073  */
1074 VOID* __cdecl CRTDLL_realloc( VOID *ptr, DWORD size )
1075 {
1076     return HeapReAlloc( GetProcessHeap(), 0, ptr, size );
1077 }
1078
1079 /*********************************************************************
1080  *                  free          (CRTDLL.427)
1081  */
1082 VOID __cdecl CRTDLL_free(LPVOID ptr)
1083 {
1084     HeapFree(GetProcessHeap(),0,ptr);
1085 }
1086
1087 /*********************************************************************
1088  *                  delete       (CRTDLL.002)
1089  */
1090 VOID __cdecl CRTDLL_delete(VOID* ptr)
1091 {
1092     HeapFree(GetProcessHeap(),0,ptr);
1093 }
1094
1095 /*********************************************************************
1096  *                  _strdup          (CRTDLL.285)
1097  */
1098 LPSTR __cdecl CRTDLL__strdup(LPCSTR ptr)
1099 {
1100     return HEAP_strdupA(GetProcessHeap(),0,ptr);
1101 }
1102
1103 /*********************************************************************
1104  *                  _wcsdup          (CRTDLL.320)
1105  */
1106 LPWSTR __cdecl CRTDLL__wcsdup(LPCWSTR ptr)
1107 {
1108     return HEAP_strdupW(GetProcessHeap(),0,ptr);
1109 }
1110
1111 /*********************************************************************
1112  *                  fclose           (CRTDLL.362)
1113  */
1114 INT __cdecl CRTDLL_fclose( CRTDLL_FILE *file )
1115 {
1116     TRACE(crtdll,"%p\n", file );
1117     if (!CloseHandle( file->handle )) return -1;
1118     HeapFree( GetProcessHeap(), 0, file );
1119     return 0;
1120 }
1121
1122 /*********************************************************************
1123  *                  _unlink           (CRTDLL.315)
1124  */
1125 INT __cdecl CRTDLL__unlink(LPCSTR pathname)
1126 {
1127     int ret=0;
1128     DOS_FULL_NAME full_name;
1129
1130     if (!DOSFS_GetFullName( pathname, FALSE, &full_name )) {
1131       WARN(crtdll, "CRTDLL_unlink file %s bad name\n",pathname);
1132       return EOF;
1133     }
1134   
1135     ret=unlink(full_name.long_name);
1136     TRACE(crtdll,"(%s unix %s)\n",
1137                    pathname,full_name.long_name);
1138     if(ret)
1139       WARN(crtdll, " Failed!\n");
1140
1141     return ret;
1142 }
1143
1144 /*********************************************************************
1145  *                  rename           (CRTDLL.449)
1146  */
1147 INT __cdecl CRTDLL_rename(LPCSTR oldpath,LPCSTR newpath)
1148 {
1149     BOOL ok = MoveFileExA( oldpath, newpath, MOVEFILE_REPLACE_EXISTING );
1150     return ok ? 0 : -1;
1151 }
1152
1153
1154 /*********************************************************************
1155  *                  _stat          (CRTDLL.280)
1156  */
1157
1158 struct win_stat
1159 {
1160     UINT16 win_st_dev;
1161     UINT16 win_st_ino;
1162     UINT16 win_st_mode;
1163     INT16  win_st_nlink;
1164     INT16  win_st_uid;
1165     INT16  win_st_gid;
1166     UINT win_st_rdev;
1167     INT  win_st_size;
1168     INT  win_st_atime;
1169     INT  win_st_mtime;
1170     INT  win_st_ctime;
1171 };
1172
1173 int __cdecl CRTDLL__stat(const char * filename, struct win_stat * buf)
1174 {
1175     int ret=0;
1176     DOS_FULL_NAME full_name;
1177     struct stat mystat;
1178
1179     if (!DOSFS_GetFullName( filename, TRUE, &full_name ))
1180     {
1181       WARN(crtdll, "CRTDLL__stat filename %s bad name\n",filename);
1182       return -1;
1183     }
1184     ret=stat(full_name.long_name,&mystat);
1185     TRACE(crtdll,"CRTDLL__stat %s\n", filename);
1186     if(ret) 
1187       WARN(crtdll, " Failed!\n");
1188
1189     /* FIXME: should check what Windows returns */
1190
1191     buf->win_st_dev   = mystat.st_dev;
1192     buf->win_st_ino   = mystat.st_ino;
1193     buf->win_st_mode  = mystat.st_mode;
1194     buf->win_st_nlink = mystat.st_nlink;
1195     buf->win_st_uid   = mystat.st_uid;
1196     buf->win_st_gid   = mystat.st_gid;
1197     buf->win_st_rdev  = mystat.st_rdev;
1198     buf->win_st_size  = mystat.st_size;
1199     buf->win_st_atime = mystat.st_atime;
1200     buf->win_st_mtime = mystat.st_mtime;
1201     buf->win_st_ctime = mystat.st_ctime;
1202     return ret;
1203 }
1204
1205 /*********************************************************************
1206  *                  _open           (CRTDLL.239)
1207  */
1208 HFILE __cdecl CRTDLL__open(LPCSTR path,INT flags)
1209 {
1210     DWORD access = 0, creation = 0;
1211     HFILE ret;
1212     
1213     /* FIXME:
1214        the flags in lcc's header differ from the ones in Linux, e.g.
1215        Linux: define O_APPEND         02000   (= 0x400)
1216        lcc:  define _O_APPEND       0x0008  
1217        so here a scheme to translate them
1218        Probably lcc is wrong here, but at least a hack to get is going
1219        */
1220     switch(flags & 3)
1221     {
1222     case O_RDONLY: access |= GENERIC_READ; break;
1223     case O_WRONLY: access |= GENERIC_WRITE; break;
1224     case O_RDWR:   access |= GENERIC_WRITE | GENERIC_READ; break;
1225     }
1226
1227     if (flags & 0x0100) /* O_CREAT */
1228     {
1229         if (flags & 0x0400) /* O_EXCL */
1230             creation = CREATE_NEW;
1231         else if (flags & 0x0200) /* O_TRUNC */
1232             creation = CREATE_ALWAYS;
1233         else
1234             creation = OPEN_ALWAYS;
1235     }
1236     else  /* no O_CREAT */
1237     {
1238         if (flags & 0x0200) /* O_TRUNC */
1239             creation = TRUNCATE_EXISTING;
1240         else
1241             creation = OPEN_EXISTING;
1242     }
1243     if (flags & 0x0008) /* O_APPEND */
1244         FIXME(crtdll, "O_APPEND not supported\n" );
1245     if (flags & 0xf0f4) 
1246       TRACE(crtdll,"CRTDLL_open file unsupported flags 0x%04x\n",flags);
1247     /* End Fixme */
1248
1249     ret = CreateFileA( path, access, FILE_SHARE_READ | FILE_SHARE_WRITE,
1250                          NULL, creation, FILE_ATTRIBUTE_NORMAL, -1 );
1251     TRACE(crtdll,"CRTDLL_open file %s mode 0x%04x got handle %d\n", path,flags,ret);
1252     return ret;
1253 }
1254
1255 /*********************************************************************
1256  *                  _close           (CRTDLL.57)
1257  */
1258 INT __cdecl CRTDLL__close(HFILE fd)
1259 {
1260     int ret=_lclose(fd);
1261
1262     TRACE(crtdll,"(%d)\n",fd);
1263     if(ret)
1264       WARN(crtdll, " Failed!\n");
1265
1266     return ret;
1267 }
1268
1269 /*********************************************************************
1270  *                  feof           (CRTDLL.363)
1271  */
1272 INT __cdecl CRTDLL_feof( CRTDLL_FILE *file )
1273 {
1274     FIXME(crtdll,"stub\n" );
1275     return 0;
1276 }
1277
1278 /*********************************************************************
1279  *                  setlocale           (CRTDLL.453)
1280  */
1281 LPSTR __cdecl CRTDLL_setlocale(INT category,LPCSTR locale)
1282 {
1283         LPSTR categorystr;
1284
1285         switch (category) {
1286         case CRTDLL_LC_ALL: categorystr="LC_ALL";break;
1287         case CRTDLL_LC_COLLATE: categorystr="LC_COLLATE";break;
1288         case CRTDLL_LC_CTYPE: categorystr="LC_CTYPE";break;
1289         case CRTDLL_LC_MONETARY: categorystr="LC_MONETARY";break;
1290         case CRTDLL_LC_NUMERIC: categorystr="LC_NUMERIC";break;
1291         case CRTDLL_LC_TIME: categorystr="LC_TIME";break;
1292         default: categorystr = "UNKNOWN?";break;
1293         }
1294         FIXME(crtdll,"(%s,%s),stub!\n",categorystr,locale);
1295         return "C";
1296 }
1297
1298 /*********************************************************************
1299  *                  wcscat           (CRTDLL.503)
1300  */
1301 LPWSTR __cdecl CRTDLL_wcscat( LPWSTR s1, LPCWSTR s2 )
1302 {
1303     return lstrcatW( s1, s2 );
1304 }
1305
1306 /*********************************************************************
1307  *                  wcschr           (CRTDLL.504)
1308  */
1309 LPWSTR __cdecl CRTDLL_wcschr(LPCWSTR str,WCHAR xchar)
1310 {
1311         LPCWSTR s;
1312
1313         s=str;
1314         do {
1315                 if (*s==xchar)
1316                         return (LPWSTR)s;
1317         } while (*s++);
1318         return NULL;
1319 }
1320
1321 /*********************************************************************
1322  *                  wcscmp           (CRTDLL.505)
1323  */
1324 INT __cdecl CRTDLL_wcscmp( LPCWSTR s1, LPCWSTR s2 )
1325 {
1326     return lstrcmpW( s1, s2 );
1327 }
1328
1329 /*********************************************************************
1330  *                  wcscpy           (CRTDLL.507)
1331  */
1332 LPWSTR __cdecl CRTDLL_wcscpy( LPWSTR s1, LPCWSTR s2 )
1333 {
1334     return lstrcpyW( s1, s2 );
1335 }
1336
1337 /*********************************************************************
1338  *                  wcscspn           (CRTDLL.508)
1339  */
1340 INT __cdecl CRTDLL_wcscspn(LPWSTR str,LPWSTR reject)
1341 {
1342         LPWSTR  s,t;
1343
1344         s=str;
1345         do {
1346                 t=reject;
1347                 while (*t) { if (*t==*s) break;t++;}
1348                 if (*t) break;
1349                 s++;
1350         } while (*s);
1351         return s-str; /* nr of wchars */
1352 }
1353
1354 /*********************************************************************
1355  *                  wcslen           (CRTDLL.510)
1356  */
1357 INT __cdecl CRTDLL_wcslen( LPCWSTR s )
1358 {
1359     return lstrlenW( s );
1360 }
1361
1362 /*********************************************************************
1363  *                  wcsncat           (CRTDLL.511)
1364  */
1365 LPWSTR __cdecl CRTDLL_wcsncat( LPWSTR s1, LPCWSTR s2, INT n )
1366 {
1367     return lstrcatnW( s1, s2, n );
1368 }
1369
1370 /*********************************************************************
1371  *                  wcsncmp           (CRTDLL.512)
1372  */
1373 INT __cdecl CRTDLL_wcsncmp( LPCWSTR s1, LPCWSTR s2, INT n )
1374 {
1375     return lstrncmpW( s1, s2, n );
1376 }
1377
1378 /*********************************************************************
1379  *                  wcsncpy           (CRTDLL.513)
1380  */
1381 LPWSTR __cdecl CRTDLL_wcsncpy( LPWSTR s1, LPCWSTR s2, INT n )
1382 {
1383     return lstrcpynW( s1, s2, n );
1384 }
1385
1386 /*********************************************************************
1387  *                  wcsspn           (CRTDLL.516)
1388  */
1389 INT __cdecl CRTDLL_wcsspn(LPWSTR str,LPWSTR accept)
1390 {
1391         LPWSTR  s,t;
1392
1393         s=str;
1394         do {
1395                 t=accept;
1396                 while (*t) { if (*t==*s) break;t++;}
1397                 if (!*t) break;
1398                 s++;
1399         } while (*s);
1400         return s-str; /* nr of wchars */
1401 }
1402
1403 /*********************************************************************
1404  *                  _wcsicmp           (CRTDLL.321)
1405  */
1406 DWORD __cdecl CRTDLL__wcsicmp( LPCWSTR s1, LPCWSTR s2 )
1407 {
1408     return lstrcmpiW( s1, s2 );
1409 }
1410
1411 /*********************************************************************
1412  *                  _wcsicoll           (CRTDLL.322)
1413  */
1414 DWORD __cdecl CRTDLL__wcsicoll(LPCWSTR a1,LPCWSTR a2)
1415 {
1416     /* FIXME: handle collates */
1417     return lstrcmpiW(a1,a2);
1418 }
1419
1420 /*********************************************************************
1421  *                  _wcsnicmp           (CRTDLL.324)
1422  */
1423 DWORD __cdecl CRTDLL__wcsnicmp( LPCWSTR s1, LPCWSTR s2, INT len )
1424 {
1425     return lstrncmpiW( s1, s2, len );
1426 }
1427
1428 /*********************************************************************
1429  *                  wcscoll           (CRTDLL.506)
1430  */
1431 DWORD __cdecl CRTDLL_wcscoll(LPWSTR a1,LPWSTR a2)
1432 {
1433     /* FIXME: handle collates */
1434     return lstrcmpW(a1,a2);
1435 }
1436
1437 /*********************************************************************
1438  *                  _wcsrev           (CRTDLL.326)
1439  */
1440 VOID __cdecl CRTDLL__wcsrev(LPWSTR s) {
1441         LPWSTR  e;
1442
1443         e=s;
1444         while (*e)
1445                 e++;
1446         while (s<e) {
1447                 WCHAR   a;
1448
1449                 a=*s;*s=*e;*e=a;
1450                 s++;e--;
1451         }
1452 }
1453
1454 /*********************************************************************
1455  *                  wcsstr           (CRTDLL.517)
1456  */
1457 LPWSTR __cdecl CRTDLL_wcsstr(LPWSTR s,LPWSTR b)
1458 {
1459         LPWSTR  x,y,c;
1460
1461         x=s;
1462         while (*x) {
1463                 if (*x==*b) {
1464                         y=x;c=b;
1465                         while (*y && *c && *y==*c) { c++;y++; }
1466                         if (!*c)
1467                                 return x;
1468                 }
1469                 x++;
1470         }
1471         return NULL;
1472 }
1473
1474 /*********************************************************************
1475  *                  wcstombs   (CRTDLL.521)
1476  */
1477 INT __cdecl CRTDLL_wcstombs( LPSTR dst, LPCWSTR src, INT len )
1478 {
1479     lstrcpynWtoA( dst, src, len );
1480     return strlen(dst);  /* FIXME: is this right? */
1481 }
1482
1483 /*********************************************************************
1484  *                  wcsrchr           (CRTDLL.515)
1485  */
1486 LPWSTR __cdecl CRTDLL_wcsrchr(LPWSTR str,WCHAR xchar)
1487 {
1488         LPWSTR  s;
1489
1490         s=str+lstrlenW(str);
1491         do {
1492                 if (*s==xchar)
1493                         return s;
1494                 s--;
1495         } while (s>=str);
1496         return NULL;
1497 }
1498
1499 /*********************************************************************
1500  *                  _setmode           (CRTDLL.265)
1501  * FIXME: At present we ignore the request to translate CR/LF to LF.
1502  *
1503  * We allways translate when we read with fgets, we never do with fread
1504  *
1505  */
1506 INT __cdecl CRTDLL__setmode( INT fh,INT mode)
1507 {
1508         /* FIXME */
1509 #define O_TEXT     0x4000
1510 #define O_BINARY   0x8000
1511
1512         FIXME(crtdll, "on fhandle %d mode %s, STUB.\n",
1513                       fh,(mode=O_TEXT)?"O_TEXT":
1514                       (mode=O_BINARY)?"O_BINARY":"UNKNOWN");
1515         return -1;
1516 }
1517
1518 /*********************************************************************
1519  *                  _fpreset           (CRTDLL.107)
1520  */
1521 VOID __cdecl CRTDLL__fpreset(void)
1522 {
1523        FIXME(crtdll," STUB.\n");
1524 }
1525
1526 /*********************************************************************
1527  *                  atexit           (CRTDLL.345)
1528  */
1529 INT __cdecl CRTDLL_atexit(LPVOID x)
1530 {
1531         FIXME(crtdll,"(%p), STUB.\n",x);
1532         return 0; /* successful */
1533 }
1534
1535 /*********************************************************************
1536  *                  mblen          (CRTDLL.428)
1537  * FIXME: check multibyte support
1538  */
1539 WCHAR  __cdecl CRTDLL_mblen(CHAR *mb,INT size)
1540 {
1541
1542     int ret=1;
1543     
1544     if (!mb)
1545       ret = 0;
1546     else if ((size<1)||(!*(mb+1)))
1547       ret = -1;
1548     else if (!(*mb))
1549       ret =0;
1550       
1551     TRACE(crtdll,"CRTDLL_mlen %s for max %d bytes ret %d\n",mb,size,ret);
1552
1553     return ret;
1554 }
1555
1556 /*********************************************************************
1557  *                  mbstowcs           (CRTDLL.429)
1558  * FIXME: check multibyte support
1559  */
1560 INT __cdecl CRTDLL_mbstowcs(LPWSTR wcs, LPCSTR mbs, INT size)
1561 {
1562
1563 /* Slightly modified lstrcpynAtoW functions from memory/strings.c
1564  *  We need the number of characters transfered 
1565  *  FIXME: No multibyte support yet
1566  */
1567
1568     LPWSTR p = wcs;
1569     LPCSTR src= mbs;
1570     int ret, n=size;
1571
1572     while ((n-- > 0) && *src) {
1573       *p++ = (WCHAR)(unsigned char)*src++;
1574         }
1575     p++;
1576     ret = (p -wcs);
1577           
1578     TRACE(crtdll,"CRTDLL_mbstowcs %s for %d chars put %d wchars\n",
1579                    mbs,size,ret);
1580     return ret;
1581 }
1582
1583 /*********************************************************************
1584  *                  mbtowc           (CRTDLL.430)
1585  * FIXME: check multibyte support
1586  */
1587 WCHAR __cdecl CRTDLL_mbtowc(WCHAR* wc,CHAR* mb,INT size) 
1588 {
1589    int ret;
1590
1591    if (!mb)
1592      ret = 0;
1593    else if (!wc)
1594      ret =-1;
1595    else 
1596      if ( (ret = mblen(mb,size)) != -1 )
1597        {
1598          if (ret <= sizeof(char))
1599            *wc = (WCHAR) ((unsigned char)*mb);
1600         else
1601         ret=   -1;
1602         }   
1603      else
1604        ret = -1;
1605    
1606    TRACE(crtdll,"CRTDLL_mbtowc %s for %d chars\n",mb,size);
1607          
1608    return ret;
1609 }
1610
1611 /*********************************************************************
1612  *                  _isctype           (CRTDLL.138)
1613  */
1614 BOOL __cdecl CRTDLL__isctype(CHAR x,CHAR type)
1615 {
1616         if ((type & CRTDLL_SPACE) && isspace(x))
1617                 return TRUE;
1618         if ((type & CRTDLL_PUNCT) && ispunct(x))
1619                 return TRUE;
1620         if ((type & CRTDLL_LOWER) && islower(x))
1621                 return TRUE;
1622         if ((type & CRTDLL_UPPER) && isupper(x))
1623                 return TRUE;
1624         if ((type & CRTDLL_ALPHA) && isalpha(x))
1625                 return TRUE;
1626         if ((type & CRTDLL_DIGIT) && isdigit(x))
1627                 return TRUE;
1628         if ((type & CRTDLL_CONTROL) && iscntrl(x))
1629                 return TRUE;
1630         /* check CRTDLL_LEADBYTE */
1631         return FALSE;
1632 }
1633
1634 /*********************************************************************
1635  *                  _chdrive           (CRTDLL.52)
1636  *
1637  *  newdir      [I] drive to change to, A=1
1638  *
1639  */
1640 BOOL __cdecl CRTDLL__chdrive(INT newdrive)
1641 {
1642         /* FIXME: generates errnos */
1643         return DRIVE_SetCurrentDrive(newdrive-1);
1644 }
1645
1646 /*********************************************************************
1647  *                  _chdir           (CRTDLL.51)
1648  */
1649 INT __cdecl CRTDLL__chdir(LPCSTR newdir)
1650 {
1651         if (!SetCurrentDirectoryA(newdir))
1652                 return 1;
1653         return 0;
1654 }
1655
1656 /*********************************************************************
1657  *                  _fullpath           (CRTDLL.114)
1658  */
1659 LPSTR __cdecl CRTDLL__fullpath(LPSTR buf, LPCSTR name, INT size)
1660 {
1661   DOS_FULL_NAME full_name;
1662
1663   if (!buf)
1664   {
1665       size = 256;
1666       if(!(buf = CRTDLL_malloc(size))) return NULL;
1667   }
1668   if (!DOSFS_GetFullName( name, FALSE, &full_name )) return NULL;
1669   lstrcpynA(buf,full_name.short_name,size);
1670   TRACE(crtdll,"CRTDLL_fullpath got %s\n",buf);
1671   return buf;
1672 }
1673
1674 /*********************************************************************
1675  *                  _splitpath           (CRTDLL.279)
1676  */
1677 VOID __cdecl CRTDLL__splitpath(LPCSTR path, LPSTR drive, LPSTR directory, LPSTR filename, LPSTR extension )
1678 {
1679   /* drive includes :
1680      directory includes leading and trailing (forward and backward slashes)
1681      filename without dot and slashes
1682      extension with leading dot
1683      */
1684   char * drivechar,*dirchar,*namechar;
1685
1686   TRACE(crtdll,"CRTDLL__splitpath got %s\n",path);
1687
1688   drivechar  = strchr(path,':');
1689   dirchar    = strrchr(path,'/');
1690   namechar   = strrchr(path,'\\');
1691   dirchar = MAX(dirchar,namechar);
1692   if (dirchar)
1693     namechar   = strrchr(dirchar,'.');
1694   else
1695     namechar   = strrchr(path,'.');
1696   
1697   
1698   if (drive) 
1699     {
1700       *drive = 0x00;
1701       if (drivechar) 
1702         {
1703           strncat(drive,path,drivechar-path+1);
1704           path = drivechar+1;
1705         }
1706     }
1707   if (directory) 
1708     {
1709       *directory = 0x00;
1710       if (dirchar)
1711         {
1712           strncat(directory,path,dirchar-path+1);
1713           path = dirchar+1;
1714         }
1715     }
1716   if (filename)
1717     {
1718       *filename = 0x00;
1719       if (namechar)
1720         {
1721           strncat(filename,path,namechar-path);
1722           if (extension) 
1723             {
1724               *extension = 0x00;
1725               strcat(extension,namechar);
1726             }
1727         }
1728     }
1729
1730   TRACE(crtdll,"CRTDLL__splitpath found %s %s %s %s\n",drive,directory,filename,extension);
1731   
1732 }
1733
1734
1735 /*********************************************************************
1736  *                  _makepath           (CRTDLL.182)
1737  */
1738
1739 VOID __cdecl CRTDLL__makepath(LPSTR path, LPCSTR drive, 
1740                               LPCSTR directory, LPCSTR filename, 
1741                               LPCSTR extension )
1742 {
1743         char ch;
1744         TRACE(crtdll,"CRTDLL__makepath got %s %s %s %s\n", drive, directory, 
1745               filename, extension);
1746
1747         if ( !path )
1748                 return;
1749
1750         path[0] = 0;
1751         if ( drive ) 
1752                 if ( drive[0] ) {
1753                         sprintf(path, "%c:", drive[0]);
1754                 }
1755         if ( directory ) 
1756                 if ( directory[0] ) {
1757                         strcat(path, directory);
1758                         ch = path[strlen(path)-1];
1759                         if (ch != '/' && ch != '\\')
1760                                 strcat(path,"\\");
1761                 }
1762         if ( filename ) 
1763                 if ( filename[0] ) {
1764                         strcat(path, filename);
1765                         if ( extension ) {
1766                                 if ( extension[0] ) {
1767                                         if ( extension[0] != '.' ) {
1768                                                 strcat(path,".");
1769                                         } 
1770                                         strcat(path,extension);
1771                                 }
1772                         }
1773                 }
1774         
1775         TRACE(crtdll,"CRTDLL__makepath returns %s\n",path);  
1776 }
1777
1778 /*********************************************************************
1779  *                  _getcwd           (CRTDLL.120)
1780  */
1781 CHAR* __cdecl CRTDLL__getcwd(LPSTR buf, INT size)
1782 {
1783   char test[1];
1784   int len;
1785
1786   len = size;
1787   if (!buf) {
1788     if (size < 0) /* allocate as big as nescessary */
1789       len =GetCurrentDirectoryA(1,test) + 1;
1790     if(!(buf = CRTDLL_malloc(len)))
1791     {
1792         /* set error to OutOfRange */
1793         return( NULL );
1794     }
1795   }
1796   size = len;
1797   if(!(len =GetCurrentDirectoryA(len,buf)))
1798     {
1799       return NULL;
1800     }
1801   if (len > size)
1802     {
1803       /* set error to ERANGE */
1804       TRACE(crtdll,"CRTDLL_getcwd buffer to small\n");
1805       return NULL;
1806     }
1807   return buf;
1808
1809 }
1810
1811 /*********************************************************************
1812  *                  _getdcwd           (CRTDLL.121)
1813  */
1814 CHAR* __cdecl CRTDLL__getdcwd(INT drive,LPSTR buf, INT size)
1815 {
1816   char test[1];
1817   int len;
1818
1819   FIXME(crtdll,"(\"%c:\",%s,%d)\n",drive+'A',buf,size);
1820   len = size;
1821   if (!buf) {
1822     if (size < 0) /* allocate as big as nescessary */
1823       len =GetCurrentDirectoryA(1,test) + 1;
1824     if(!(buf = CRTDLL_malloc(len)))
1825     {
1826         /* set error to OutOfRange */
1827         return( NULL );
1828     }
1829   }
1830   size = len;
1831   if(!(len =GetCurrentDirectoryA(len,buf)))
1832     {
1833       return NULL;
1834     }
1835   if (len > size)
1836     {
1837       /* set error to ERANGE */
1838       TRACE(crtdll,"buffer to small\n");
1839       return NULL;
1840     }
1841   return buf;
1842
1843 }
1844
1845 /*********************************************************************
1846  *                  _getdrive           (CRTDLL.124)
1847  *
1848  *  Return current drive, 1 for A, 2 for B
1849  */
1850 INT __cdecl CRTDLL__getdrive(VOID)
1851 {
1852     return DRIVE_GetCurrentDrive() + 1;
1853 }
1854
1855 /*********************************************************************
1856  *                  _mkdir           (CRTDLL.234)
1857  */
1858 INT __cdecl CRTDLL__mkdir(LPCSTR newdir)
1859 {
1860         if (!CreateDirectoryA(newdir,NULL))
1861                 return -1;
1862         return 0;
1863 }
1864
1865 /*********************************************************************
1866  *                  remove           (CRTDLL.448)
1867  */
1868 INT __cdecl CRTDLL_remove(LPCSTR file)
1869 {
1870         if (!DeleteFileA(file))
1871                 return -1;
1872         return 0;
1873 }
1874
1875 /*********************************************************************
1876  *                  _errno           (CRTDLL.52)
1877  * Yes, this is a function.
1878  */
1879 LPINT __cdecl CRTDLL__errno()
1880 {
1881         static  int crtdllerrno;
1882         
1883         /* FIXME: we should set the error at the failing function call time */
1884         crtdllerrno = LastErrorToErrno(GetLastError());
1885         return &crtdllerrno;
1886 }
1887
1888 /*********************************************************************
1889  *                  _tempnam           (CRTDLL.305)
1890  * 
1891  */
1892 LPSTR __cdecl CRTDLL__tempnam(LPCSTR dir, LPCSTR prefix)
1893 {
1894
1895      char *ret;
1896      DOS_FULL_NAME tempname;
1897      
1898      if ((ret = tempnam(dir,prefix))==NULL) {
1899        WARN(crtdll, "Unable to get unique filename\n");
1900        return NULL;
1901      }
1902      if (!DOSFS_GetFullName(ret,FALSE,&tempname))
1903      {
1904        TRACE(crtdll, "Wrong path?\n");
1905        return NULL;
1906      }
1907      free(ret);
1908      if ((ret = CRTDLL_malloc(strlen(tempname.short_name)+1)) == NULL) {
1909          WARN(crtdll, "CRTDL_malloc for shortname failed\n");
1910          return NULL;
1911      }
1912      if ((ret = strcpy(ret,tempname.short_name)) == NULL) { 
1913        WARN(crtdll, "Malloc for shortname failed\n");
1914        return NULL;
1915      }
1916      
1917      TRACE(crtdll,"dir %s prefix %s got %s\n",
1918                     dir,prefix,ret);
1919      return ret;
1920
1921 }
1922 /*********************************************************************
1923  *                  tmpnam           (CRTDLL.490)
1924  *
1925  * lcclnk from lcc-win32 relies on a terminating dot in the name returned
1926  * 
1927  */
1928 LPSTR __cdecl CRTDLL_tmpnam(LPSTR s)
1929 {
1930      char *ret;
1931
1932      if ((ret =tmpnam(s))== NULL) {
1933        WARN(crtdll, "Unable to get unique filename\n");
1934        return NULL;
1935      }
1936      if (!DOSFS_GetFullName(ret,FALSE,&CRTDLL_tmpname))
1937      {
1938        TRACE(crtdll, "Wrong path?\n");
1939        return NULL;
1940      }
1941      strcat(CRTDLL_tmpname.short_name,".");
1942      TRACE(crtdll,"for buf %p got %s\n",
1943                     s,CRTDLL_tmpname.short_name);
1944      TRACE(crtdll,"long got %s\n",
1945                     CRTDLL_tmpname.long_name);
1946      if ( s != NULL) 
1947        return strcpy(s,CRTDLL_tmpname.short_name);
1948      else 
1949        return CRTDLL_tmpname.short_name;
1950
1951 }
1952
1953 /*********************************************************************
1954  *                  _itoa           (CRTDLL.165)
1955  */
1956 LPSTR  __cdecl CRTDLL__itoa(INT x,LPSTR buf,INT buflen)
1957 {
1958     wsnprintfA(buf,buflen,"%d",x);
1959     return buf;
1960 }
1961
1962 /*********************************************************************
1963  *                  _ltoa           (CRTDLL.180)
1964  */
1965 LPSTR  __cdecl CRTDLL__ltoa(long x,LPSTR buf,INT radix)
1966 {
1967     switch(radix) {
1968         case  2: FIXME(crtdll, "binary format not implemented !\n");
1969                  break;
1970         case  8: wsnprintfA(buf,0x80,"%o",x);
1971                  break;
1972         case 10: wsnprintfA(buf,0x80,"%d",x);
1973                  break;
1974         case 16: wsnprintfA(buf,0x80,"%x",x);
1975                  break;
1976         default: FIXME(crtdll, "radix %d not implemented !\n", radix);
1977     }
1978     return buf;
1979 }
1980
1981 /*********************************************************************
1982  *                  _ultoa           (CRTDLL.311)
1983  */
1984 LPSTR  __cdecl CRTDLL__ultoa(long x,LPSTR buf,INT radix)
1985 {
1986     switch(radix) {
1987         case  2: FIXME(crtdll, "binary format not implemented !\n");
1988                  break;
1989         case  8: wsnprintfA(buf,0x80,"%lo",x);
1990                  break;
1991         case 10: wsnprintfA(buf,0x80,"%ld",x);
1992                  break;
1993         case 16: wsnprintfA(buf,0x80,"%lx",x);
1994                  break;
1995         default: FIXME(crtdll, "radix %d not implemented !\n", radix);
1996     }
1997     return buf;
1998 }
1999
2000 typedef VOID (*sig_handler_type)(VOID);
2001
2002 /*********************************************************************
2003  *                  signal           (CRTDLL.455)
2004  */
2005 VOID __cdecl CRTDLL_signal(int sig, sig_handler_type ptr)
2006 {
2007     FIXME(crtdll, "(%d %p):stub.\n", sig, ptr);
2008 }
2009
2010 /*********************************************************************
2011  *                  _ftol            (CRTDLL.113)
2012  */
2013 #ifdef USING_REAL_FPU
2014 LONG __cdecl CRTDLL__ftol(void) {
2015         /* don't just do DO_FPU("fistp",retval), because the rounding
2016          * mode must also be set to "round towards zero"... */
2017         double fl;
2018         POP_FPU(fl);
2019         return (LONG)fl;
2020 }
2021 #else
2022 LONG __cdecl CRTDLL__ftol(double fl) {
2023         FIXME(crtdll,"should be register function\n");
2024         return (LONG)fl;
2025 }
2026 #endif
2027
2028 /*********************************************************************
2029  *                  _CIpow           (CRTDLL.14)
2030  */
2031 #ifdef USING_REAL_FPU
2032 LONG __cdecl CRTDLL__CIpow(void) {
2033         double x,y;
2034         POP_FPU(y);
2035         POP_FPU(x);
2036         return pow(x,y);
2037 }
2038 #else
2039 LONG __cdecl CRTDLL__CIpow(double x,double y) {
2040         FIXME(crtdll,"should be register function\n");
2041         return pow(x,y);
2042 }
2043 #endif
2044
2045 /*********************************************************************
2046  *                  _sleep           (CRTDLL.267)
2047  */
2048 VOID __cdecl CRTDLL__sleep(unsigned long timeout) 
2049 {
2050   TRACE(crtdll,"CRTDLL__sleep for %ld milliseconds\n",timeout);
2051   Sleep((timeout)?timeout:1);
2052 }
2053
2054 /*********************************************************************
2055  *                  getenv           (CRTDLL.437)
2056  */
2057 LPSTR __cdecl CRTDLL_getenv(const char *name) 
2058 {
2059      LPSTR environ = GetEnvironmentStringsA();
2060      LPSTR pp,pos = NULL;
2061      unsigned int length;
2062   
2063      for (pp = environ; (*pp); pp = pp + strlen(pp) +1)
2064        {
2065          pos =strchr(pp,'=');
2066          if (pos)
2067            length = pos -pp;
2068          else
2069            length = strlen(pp);
2070          if (!strncmp(pp,name,length)) break;
2071        }
2072      if ((pp)&& (pos)) 
2073        {
2074          pp = pos+1;
2075          TRACE(crtdll,"got %s\n",pp);
2076        }
2077      FreeEnvironmentStringsA( environ );
2078      return pp;
2079 }
2080
2081 /*********************************************************************
2082  *                  _mbsrchr           (CRTDLL.223)
2083  */
2084 LPSTR __cdecl CRTDLL__mbsrchr(LPSTR s,CHAR x) {
2085         /* FIXME: handle multibyte strings */
2086         return strrchr(s,x);
2087 }
2088
2089 /*********************************************************************
2090  *                  _memicmp           (CRTDLL.233)(NTDLL.868)
2091  * A stringcompare, without \0 check
2092  * RETURNS
2093  *      -1:if first string is alphabetically before second string
2094  *      1:if second ''    ''      ''          ''   first   ''
2095  *      0:if both are equal.
2096  */
2097 INT __cdecl CRTDLL__memicmp(
2098         LPCSTR s1,      /* [in] first string */
2099         LPCSTR s2,      /* [in] second string */
2100         DWORD len       /* [in] length to compare */
2101 ) { 
2102         int     i;
2103
2104         for (i=0;i<len;i++) {
2105                 if (tolower(s1[i])<tolower(s2[i]))
2106                         return -1;
2107                 if (tolower(s1[i])>tolower(s2[i]))
2108                         return  1;
2109         }
2110         return 0;
2111 }
2112 /*********************************************************************
2113  *                  __dllonexit           (CRTDLL.25)
2114  */
2115 VOID __cdecl CRTDLL__dllonexit ()
2116 {       
2117         FIXME(crtdll,"stub\n");
2118 }
2119
2120 /*********************************************************************
2121  *                  wcstok           (CRTDLL.519)
2122  * Like strtok, but for wide character strings. s is modified, yes.
2123  */
2124 LPWSTR __cdecl CRTDLL_wcstok(LPWSTR s,LPCWSTR delim) {
2125         static LPWSTR nexttok = NULL;
2126         LPWSTR  x,ret;
2127
2128         if (!s)
2129                 s = nexttok;
2130         if (!s) 
2131                 return NULL;
2132         x = s;
2133         while (*x && !CRTDLL_wcschr(delim,*x))
2134                 x++;
2135         ret = nexttok;
2136         if (*x) {
2137                 *x='\0';
2138                 nexttok = x+1;
2139         } else
2140                 nexttok = NULL;
2141         return ret;
2142 }
2143
2144 /*********************************************************************
2145  *                  wcstol           (CRTDLL.520)
2146  * Like strtol, but for wide character strings.
2147  */
2148 INT __cdecl CRTDLL_wcstol(LPWSTR s,LPWSTR *end,INT base) {
2149         LPSTR   sA = HEAP_strdupWtoA(GetProcessHeap(),0,s),endA;
2150         INT     ret = strtol(sA,&endA,base);
2151
2152         HeapFree(GetProcessHeap(),0,sA);
2153         if (end) *end = s+(endA-sA); /* pointer magic checked. */
2154         return ret;
2155 }
2156 /*********************************************************************
2157  *                  strdate           (CRTDLL.283)
2158  */
2159 LPSTR __cdecl CRTDLL__strdate (LPSTR date)
2160 {       FIXME (crtdll,"%p stub\n", date);
2161         return 0;
2162 }
2163
2164 /*********************************************************************
2165  *                  strtime           (CRTDLL.299)
2166  */
2167 LPSTR __cdecl CRTDLL__strtime (LPSTR date)
2168 {       FIXME (crtdll,"%p stub\n", date);
2169         return 0;
2170 }