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