Work-around for compiler bug.
[wine/gsoc_dplay.git] / misc / crtdll.c
blobe1ced3e1341cf80700e20325fa308dce6de2632e
1 /*
2 * The C RunTime DLL
3 *
4 * Implements C run-time functionality as known from UNIX.
6 * Copyright 1996,1998 Marcus Meissner
7 * Copyright 1996 Jukka Iivonen
8 * Copyright 1997 Uwe Bonnes
9 */
12 Unresolved issues Uwe Bonnes 970904:
13 - Handling of Binary/Text Files is crude. If in doubt, use fromdos or recode
14 - Arguments in crtdll.spec for functions with double argument
15 - system-call calls another wine process, but without debugging arguments
16 and uses the first wine executable in the path
17 - tested with ftp://ftp.remcomp.com/pub/remcomp/lcc-win32.zip, a C-Compiler
18 for Win32, based on lcc, from Jacob Navia
19 AJ 990101:
20 - needs a proper stdio emulation based on Win32 file handles
21 - should set CRTDLL errno from GetLastError() in every function
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
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 "ntddk.h"
45 #include "debugtools.h"
46 #include "module.h"
47 #include "heap.h"
48 #include "crtdll.h"
49 #include "drive.h"
50 #include "file.h"
51 #include "options.h"
52 #include "winnls.h"
54 DEFAULT_DEBUG_CHANNEL(crtdll)
56 /* windows.h RAND_MAX is smaller than normal RAND_MAX */
57 #define CRTDLL_RAND_MAX 0x7fff
59 static DOS_FULL_NAME CRTDLL_tmpname;
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 */
79 /* FIXME: structure layout is obviously not correct */
80 typedef struct
82 HANDLE handle;
83 int pad[7];
84 } CRTDLL_FILE;
86 CRTDLL_FILE CRTDLL_iob[3];
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];
92 typedef VOID (*new_handler_type)(VOID);
94 static new_handler_type new_handler;
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
102 CRTDLL_FILE * __cdecl CRTDLL__fdopen(INT handle, LPCSTR mode);
104 /*********************************************************************
105 * CRTDLL_MainInit (CRTDLL.init)
107 BOOL WINAPI CRTDLL_Init(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
109 TRACE("(0x%08x,%ld,%p)\n",hinstDLL,fdwReason,lpvReserved);
111 if (fdwReason == DLL_PROCESS_ATTACH) {
112 CRTDLL__fdopen(0,"r");
113 CRTDLL__fdopen(1,"w");
114 CRTDLL__fdopen(2,"w");
116 return TRUE;
119 /*********************************************************************
120 * _GetMainArgs (CRTDLL.022)
122 DWORD __cdecl CRTDLL__GetMainArgs(LPDWORD argc,LPSTR **argv,
123 LPSTR *environ,DWORD flag)
125 char *cmdline;
126 char **xargv;
127 int xargc,i,afterlastspace;
128 DWORD version;
130 TRACE("(%p,%p,%p,%ld).\n",
131 argc,argv,environ,flag
133 CRTDLL_acmdln_dll = cmdline = HEAP_strdupA( GetProcessHeap(), 0,
134 GetCommandLineA() );
135 TRACE("got '%s'\n", cmdline);
137 version = GetVersion();
138 CRTDLL_osver_dll = version >> 16;
139 CRTDLL_winminor_dll = version & 0xFF;
140 CRTDLL_winmajor_dll = (version>>8) & 0xFF;
141 CRTDLL_baseversion_dll = version >> 16;
142 CRTDLL_winver_dll = ((version >> 8) & 0xFF) + ((version & 0xFF) << 8);
143 CRTDLL_baseminor_dll = (version >> 16) & 0xFF;
144 CRTDLL_basemajor_dll = (version >> 24) & 0xFF;
145 CRTDLL_osversion_dll = version & 0xFFFF;
146 CRTDLL_osminor_dll = version & 0xFF;
147 CRTDLL_osmajor_dll = (version>>8) & 0xFF;
149 /* missing threading init */
151 i=0;xargv=NULL;xargc=0;afterlastspace=0;
152 while (cmdline[i]) {
153 if (cmdline[i]==' ') {
154 xargv=(char**)HeapReAlloc( GetProcessHeap(), 0, xargv,
155 sizeof(char*)*(++xargc));
156 cmdline[i]='\0';
157 xargv[xargc-1] = HEAP_strdupA( GetProcessHeap(), 0,
158 cmdline+afterlastspace);
159 i++;
160 while (cmdline[i]==' ')
161 i++;
162 if (cmdline[i])
163 afterlastspace=i;
164 } else
165 i++;
167 xargv=(char**)HeapReAlloc( GetProcessHeap(), 0, xargv,
168 sizeof(char*)*(++xargc));
169 cmdline[i]='\0';
170 xargv[xargc-1] = HEAP_strdupA( GetProcessHeap(), 0,
171 cmdline+afterlastspace);
172 CRTDLL_argc_dll = xargc;
173 *argc = xargc;
174 CRTDLL_argv_dll = xargv;
175 *argv = xargv;
177 TRACE("found %d arguments\n",
178 CRTDLL_argc_dll);
179 CRTDLL_environ_dll = *environ = GetEnvironmentStringsA();
180 return 0;
184 typedef void (*_INITTERMFUN)();
186 /* fixme: move to header */
187 struct find_t
188 { unsigned attrib;
189 time_t time_create; /* -1 when not avaiable */
190 time_t time_access; /* -1 when not avaiable */
191 time_t time_write;
192 unsigned long size; /* fixme: 64 bit ??*/
193 char name[260];
195 /*********************************************************************
196 * _findfirst (CRTDLL.099)
198 * BUGS
199 * Unimplemented
201 DWORD __cdecl CRTDLL__findfirst(LPCSTR fname, struct find_t * x2)
203 FIXME(":(%s,%p): stub\n",fname,x2);
204 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
205 return FALSE;
208 /*********************************************************************
209 * _findnext (CRTDLL.100)
211 * BUGS
212 * Unimplemented
214 INT __cdecl CRTDLL__findnext(DWORD hand, struct find_t * x2)
216 FIXME(":(%ld,%p): stub\n",hand,x2);
217 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
218 return FALSE;
221 /*********************************************************************
222 * _fstat (CRTDLL.111)
224 * BUGS
225 * Unimplemented
227 int __cdecl CRTDLL__fstat(int file, struct stat* buf)
229 FIXME(":(%d,%p): stub\n",file,buf);
230 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
231 return FALSE;
234 /*********************************************************************
235 * _initterm (CRTDLL.135)
237 DWORD __cdecl CRTDLL__initterm(_INITTERMFUN *start,_INITTERMFUN *end)
239 _INITTERMFUN *current;
241 TRACE("(%p,%p)\n",start,end);
242 current=start;
243 while (current<end) {
244 if (*current) (*current)();
245 current++;
247 return 0;
250 CRTDLL_FILE * __cdecl CRTDLL__fsopen(LPCSTR x, LPCSTR y, INT z) {
251 FIXME("(%s,%s,%d),stub!\n",x,y,z);
252 return NULL;
254 /*********************************************************************
255 * _fdopen (CRTDLL.91)
257 CRTDLL_FILE * __cdecl CRTDLL__fdopen(INT handle, LPCSTR mode)
259 CRTDLL_FILE *file;
261 switch (handle)
263 case 0:
264 file = CRTDLL_stdin;
265 if (!file->handle) file->handle = GetStdHandle( STD_INPUT_HANDLE );
266 break;
267 case 1:
268 file = CRTDLL_stdout;
269 if (!file->handle) file->handle = GetStdHandle( STD_OUTPUT_HANDLE );
270 break;
271 case 2:
272 file=CRTDLL_stderr;
273 if (!file->handle) file->handle = GetStdHandle( STD_ERROR_HANDLE );
274 break;
275 default:
276 file = HeapAlloc( GetProcessHeap(), 0, sizeof(*file) );
277 file->handle = handle;
278 break;
280 TRACE("open handle %d mode %s got file %p\n",
281 handle, mode, file);
282 return file;
286 /*******************************************************************
287 * _global_unwind2 (CRTDLL.129)
289 void __cdecl CRTDLL__global_unwind2( PEXCEPTION_FRAME frame )
291 RtlUnwind( frame, 0, NULL, 0 );
294 /*******************************************************************
295 * _local_unwind2 (CRTDLL.173)
297 void __cdecl CRTDLL__local_unwind2( PEXCEPTION_FRAME endframe, DWORD nr )
299 TRACE("(%p,%ld)\n",endframe,nr);
301 /*********************************************************************
302 * _read (CRTDLL.256)
304 * BUGS
305 * Unimplemented
307 INT __cdecl CRTDLL__read(INT fd, LPVOID buf, UINT count)
309 FIXME(":(%d,%p,%d): stub\n",fd,buf,count);
310 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
311 return FALSE;
314 /*********************************************************************
315 * fopen (CRTDLL.372)
317 CRTDLL_FILE * __cdecl CRTDLL_fopen(LPCSTR path, LPCSTR mode)
319 CRTDLL_FILE *file = NULL;
320 HFILE handle;
321 #if 0
322 DOS_FULL_NAME full_name;
324 if (!DOSFS_GetFullName( path, FALSE, &full_name )) {
325 WARN("file %s bad name\n",path);
326 return 0;
329 file=fopen(full_name.long_name ,mode);
330 #endif
331 DWORD access = 0, creation = 0;
333 if ((strchr(mode,'r')&&strchr(mode,'a'))||
334 (strchr(mode,'r')&&strchr(mode,'w'))||
335 (strchr(mode,'w')&&strchr(mode,'a')))
336 return NULL;
338 if (mode[0] == 'r')
340 access = GENERIC_READ;
341 creation = OPEN_EXISTING;
342 if (mode[1] == '+') access |= GENERIC_WRITE;
344 else if (mode[0] == 'w')
346 access = GENERIC_WRITE;
347 creation = CREATE_ALWAYS;
348 if (mode[1] == '+') access |= GENERIC_READ;
350 else if (mode[0] == 'a')
352 /* FIXME: there is no O_APPEND in CreateFile, should emulate it */
353 access = GENERIC_WRITE;
354 creation = OPEN_ALWAYS;
355 if (mode[1] == '+') access |= GENERIC_READ;
357 /* FIXME: should handle text/binary mode */
359 if ((handle = CreateFileA( path, access, FILE_SHARE_READ | FILE_SHARE_WRITE,
360 NULL, creation, FILE_ATTRIBUTE_NORMAL,
361 -1 )) != INVALID_HANDLE_VALUE)
363 file = HeapAlloc( GetProcessHeap(), 0, sizeof(*file) );
364 file->handle = handle;
366 TRACE("file %s mode %s got handle %d file %p\n",
367 path,mode,handle,file);
368 return file;
371 /*********************************************************************
372 * fread (CRTDLL.377)
374 DWORD __cdecl CRTDLL_fread(LPVOID ptr, INT size, INT nmemb, CRTDLL_FILE *file)
376 #if 0
377 int i=0;
378 void *temp=ptr;
380 /* If we would honour CR/LF <-> LF translation, we could do it like this.
381 We should keep track of all files opened, and probably files with \
382 known binary extensions must be unchanged */
383 while ( (i < (nmemb*size)) && (ret==1)) {
384 ret=fread(temp,1,1,file);
385 TRACE("got %c 0x%02x ret %d\n",
386 (isalpha(*(unsigned char*)temp))? *(unsigned char*)temp:
387 ' ',*(unsigned char*)temp, ret);
388 if (*(unsigned char*)temp != 0xd) { /* skip CR */
389 temp++;
390 i++;
392 else
393 TRACE("skipping ^M\n");
395 TRACE("0x%08x items of size %d from file %p to %p\n",
396 nmemb,size,file,ptr,);
397 if(i!=nmemb)
398 WARN(" failed!\n");
400 return i;
401 #else
402 DWORD ret;
404 TRACE("0x%08x items of size %d from file %p to %p\n",
405 nmemb,size,file,ptr);
406 if (!ReadFile( file->handle, ptr, size * nmemb, &ret, NULL ))
407 WARN(" failed!\n");
409 return ret / size;
410 #endif
412 /*********************************************************************
413 * freopen (CRTDLL.379)
415 * BUGS
416 * Unimplemented
418 DWORD __cdecl CRTDLL_freopen(LPCSTR path, LPCSTR mode, LPVOID stream)
420 FIXME(":(%s,%s,%p): stub\n", path, mode, stream);
421 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
422 return FALSE;
425 /*********************************************************************
426 * fscanf (CRTDLL.381)
428 INT __cdecl CRTDLL_fscanf( CRTDLL_FILE *stream, LPSTR format, ... )
430 #if 0
431 va_list valist;
432 INT res;
434 va_start( valist, format );
435 #ifdef HAVE_VFSCANF
436 res = vfscanf( xlat_file_ptr(stream), format, valist );
437 #endif
438 va_end( valist );
439 return res;
440 #endif
441 FIXME("broken\n");
442 return 0;
445 /*********************************************************************
446 * fseek (CRTDLL.382)
448 LONG __cdecl CRTDLL_fseek( CRTDLL_FILE *file, LONG offset, INT whence)
450 TRACE("file %p to 0x%08lx pos %s\n",
451 file,offset,(whence==SEEK_SET)?"SEEK_SET":
452 (whence==SEEK_CUR)?"SEEK_CUR":
453 (whence==SEEK_END)?"SEEK_END":"UNKNOWN");
454 if (SetFilePointer( file->handle, offset, NULL, whence ) != 0xffffffff)
455 return 0;
456 WARN(" failed!\n");
457 return -1;
460 /*********************************************************************
461 * fsetpos (CRTDLL.383)
463 INT __cdecl CRTDLL_fsetpos( CRTDLL_FILE *file, INT *pos )
465 TRACE("file %p pos %d\n", file, *pos );
466 return CRTDLL_fseek(file, *pos, SEEK_SET);
469 /*********************************************************************
470 * ftell (CRTDLL.384)
472 LONG __cdecl CRTDLL_ftell( CRTDLL_FILE *file )
474 return SetFilePointer( file->handle, 0, NULL, SEEK_CUR );
477 /*********************************************************************
478 * fwrite (CRTDLL.386)
480 DWORD __cdecl CRTDLL_fwrite( LPVOID ptr, INT size, INT nmemb, CRTDLL_FILE *file )
482 DWORD ret;
484 TRACE("0x%08x items of size %d to file %p(%d) from %p\n",
485 nmemb,size,file,file-(CRTDLL_FILE*)CRTDLL_iob,ptr);
488 if (!WriteFile( file->handle, ptr, size * nmemb, &ret, NULL ))
489 WARN(" failed!\n");
490 return ret / size;
493 /*********************************************************************
494 * setbuf (CRTDLL.452)
496 INT __cdecl CRTDLL_setbuf(CRTDLL_FILE *file, LPSTR buf)
498 TRACE("(file %p buf %p)\n", file, buf);
499 /* this doesn't work:"void value not ignored as it ought to be"
500 return setbuf(file,buf);
502 /* FIXME: no buffering for now */
503 return 0;
506 /*********************************************************************
507 * _open_osfhandle (CRTDLL.240)
509 HFILE __cdecl CRTDLL__open_osfhandle(LONG osfhandle, INT flags)
511 HFILE handle;
513 switch (osfhandle) {
514 case STD_INPUT_HANDLE :
515 case 0 :
516 handle=0;
517 break;
518 case STD_OUTPUT_HANDLE:
519 case 1:
520 handle=1;
521 break;
522 case STD_ERROR_HANDLE:
523 case 2:
524 handle=2;
525 break;
526 default:
527 return (-1);
529 TRACE("(handle %08lx,flags %d) return %d\n",
530 osfhandle,flags,handle);
531 return handle;
535 /*********************************************************************
536 * srand (CRTDLL.460)
538 void __cdecl CRTDLL_srand(DWORD seed)
540 /* FIXME: should of course be thread? process? local */
541 srand(seed);
544 /*********************************************************************
545 * vfprintf (CRTDLL.373)
547 INT __cdecl CRTDLL_vfprintf( CRTDLL_FILE *file, LPSTR format, va_list args )
549 char buffer[2048]; /* FIXME... */
551 vsprintf( buffer, format, args );
552 return CRTDLL_fwrite( buffer, 1, strlen(buffer), file );
555 /*********************************************************************
556 * fprintf (CRTDLL.373)
558 INT __cdecl CRTDLL_fprintf( CRTDLL_FILE *file, LPSTR format, ... )
560 va_list valist;
561 INT res;
563 va_start( valist, format );
564 res = CRTDLL_vfprintf( file, format, valist );
565 va_end( valist );
566 return res;
569 /*********************************************************************
570 * time (CRTDLL.488)
572 time_t __cdecl CRTDLL_time(time_t *timeptr)
574 time_t curtime = time(NULL);
576 if (timeptr)
577 *timeptr = curtime;
578 return curtime;
581 /*********************************************************************
582 * (CRTDLL.350)
584 clock_t __cdecl CRTDLL_clock(void)
586 struct tms alltimes;
587 clock_t res;
589 times(&alltimes);
590 res = alltimes.tms_utime + alltimes.tms_stime+
591 alltimes.tms_cutime + alltimes.tms_cstime;
592 /* Fixme: We need some symbolic representation
593 for (Hostsystem_)CLOCKS_PER_SEC
594 and (Emulated_system_)CLOCKS_PER_SEC
595 10 holds only for Windows/Linux_i86)
597 return 10*res;
600 /*********************************************************************
601 * _isatty (CRTDLL.137)
603 BOOL __cdecl CRTDLL__isatty(DWORD x)
605 TRACE("(%ld)\n",x);
606 return TRUE;
609 /*********************************************************************
610 * _write (CRTDLL.332)
612 INT __cdecl CRTDLL__write(INT fd,LPCVOID buf,UINT count)
614 INT len=0;
616 if (fd == -1)
617 len = -1;
618 else if (fd<=2)
619 len = (UINT)write(fd,buf,(LONG)count);
620 else
621 len = _lwrite(fd,buf,count);
622 TRACE("%d/%d byte to dfh %d from %p,\n",
623 len,count,fd,buf);
624 return len;
628 /*********************************************************************
629 * _cexit (CRTDLL.49)
631 * FIXME: What the heck is the difference between
632 * FIXME _c_exit (CRTDLL.47)
633 * FIXME _cexit (CRTDLL.49)
634 * FIXME _exit (CRTDLL.87)
635 * FIXME exit (CRTDLL.359)
637 * atexit-processing comes to mind -- MW.
640 void __cdecl CRTDLL__cexit(INT ret)
642 TRACE("(%d)\n",ret);
643 ExitProcess(ret);
647 /*********************************************************************
648 * exit (CRTDLL.359)
650 void __cdecl CRTDLL_exit(DWORD ret)
652 TRACE("(%ld)\n",ret);
653 ExitProcess(ret);
657 /*********************************************************************
658 * _abnormal_termination (CRTDLL.36)
660 INT __cdecl CRTDLL__abnormal_termination(void)
662 TRACE("(void)\n");
663 return 0;
667 /*********************************************************************
668 * _access (CRTDLL.37)
670 INT __cdecl CRTDLL__access(LPCSTR filename, INT mode)
672 DWORD attr = GetFileAttributesA(filename);
674 if (attr == -1)
676 if (GetLastError() == ERROR_INVALID_ACCESS)
677 errno = EACCES;
678 else
679 errno = ENOENT;
680 return -1;
683 if ((attr & FILE_ATTRIBUTE_READONLY) && (mode & W_OK))
685 errno = EACCES;
686 return -1;
688 else
689 return 0;
693 /*********************************************************************
694 * fflush (CRTDLL.365)
696 INT __cdecl CRTDLL_fflush( CRTDLL_FILE *file )
698 return FlushFileBuffers( file->handle ) ? 0 : -1;
702 /*********************************************************************
703 * rand (CRTDLL.446)
705 INT __cdecl CRTDLL_rand()
707 return (rand() & CRTDLL_RAND_MAX);
711 /*********************************************************************
712 * putchar (CRTDLL.442)
714 void __cdecl CRTDLL_putchar( INT x )
716 putchar(x);
720 /*********************************************************************
721 * fputc (CRTDLL.374)
723 INT __cdecl CRTDLL_fputc( INT c, CRTDLL_FILE *file )
725 char ch = (char)c;
726 DWORD res;
727 TRACE("%c to file %p\n",c,file);
728 if (!WriteFile( file->handle, &ch, 1, &res, NULL )) return -1;
729 return c;
733 /*********************************************************************
734 * fputs (CRTDLL.375)
736 INT __cdecl CRTDLL_fputs( LPCSTR s, CRTDLL_FILE *file )
738 DWORD res;
739 TRACE("%s to file %p\n",s,file);
740 if (!WriteFile( file->handle, s, strlen(s), &res, NULL )) return -1;
741 return res;
745 /*********************************************************************
746 * puts (CRTDLL.443)
748 INT __cdecl CRTDLL_puts(LPCSTR s)
750 TRACE("%s \n",s);
751 return puts(s);
755 /*********************************************************************
756 * putc (CRTDLL.441)
758 INT __cdecl CRTDLL_putc( INT c, CRTDLL_FILE *file )
760 return CRTDLL_fputc( c, file );
763 /*********************************************************************
764 * fgetc (CRTDLL.366)
766 INT __cdecl CRTDLL_fgetc( CRTDLL_FILE *file )
768 DWORD res;
769 char ch;
770 if (!ReadFile( file->handle, &ch, 1, &res, NULL )) return -1;
771 if (res != 1) return -1;
772 return ch;
776 /*********************************************************************
777 * getc (CRTDLL.388)
779 INT __cdecl CRTDLL_getc( CRTDLL_FILE *file )
781 return CRTDLL_fgetc( file );
785 /*********************************************************************
786 * fgets (CRTDLL.368)
788 CHAR* __cdecl CRTDLL_fgets( LPSTR s, INT size, CRTDLL_FILE *file )
790 int cc;
791 LPSTR buf_start = s;
793 /* BAD, for the whole WINE process blocks... just done this way to test
794 * windows95's ftp.exe.
797 for(cc = CRTDLL_fgetc(file); cc != EOF && cc != '\n'; cc = CRTDLL_fgetc(file))
798 if (cc != '\r')
800 if (--size <= 0) break;
801 *s++ = (char)cc;
804 *s = '\0';
806 TRACE("got '%s'\n", buf_start);
807 return buf_start;
811 /*********************************************************************
812 * gets (CRTDLL.391)
814 LPSTR __cdecl CRTDLL_gets(LPSTR buf)
816 int cc;
817 LPSTR buf_start = buf;
819 /* BAD, for the whole WINE process blocks... just done this way to test
820 * windows95's ftp.exe.
823 for(cc = fgetc(stdin); cc != EOF && cc != '\n'; cc = fgetc(stdin))
824 if(cc != '\r') *buf++ = (char)cc;
826 *buf = '\0';
828 TRACE("got '%s'\n", buf_start);
829 return buf_start;
833 /*********************************************************************
834 * _rotl (CRTDLL.259)
836 UINT __cdecl CRTDLL__rotl(UINT x,INT shift)
838 unsigned int ret = (x >> shift)|( x >>((sizeof(x))-shift));
840 TRACE("got 0x%08x rot %d ret 0x%08x\n",
841 x,shift,ret);
842 return ret;
845 /*********************************************************************
846 * _lrotl (CRTDLL.176)
848 DWORD __cdecl CRTDLL__lrotl(DWORD x,INT shift)
850 unsigned long ret = (x >> shift)|( x >>((sizeof(x))-shift));
852 TRACE("got 0x%08lx rot %d ret 0x%08lx\n",
853 x,shift,ret);
854 return ret;
859 /*********************************************************************
860 * _mbsicmp (CRTDLL.204)
862 int __cdecl CRTDLL__mbsicmp(unsigned char *x,unsigned char *y)
864 do {
865 if (!*x)
866 return !!*y;
867 if (!*y)
868 return !!*x;
869 /* FIXME: MBCS handling... */
870 if (*x!=*y)
871 return 1;
872 x++;
873 y++;
874 } while (1);
878 /*********************************************************************
879 * _mbsinc (CRTDLL.205)
881 unsigned char * __cdecl CRTDLL__mbsinc(unsigned char *x)
883 /* FIXME: mbcs */
884 return x++;
888 /*********************************************************************
889 * vsprintf (CRTDLL.500)
891 INT __cdecl CRTDLL_vsprintf( LPSTR buffer, LPCSTR spec, va_list args )
893 return wvsprintfA( buffer, spec, args );
896 /*********************************************************************
897 * vswprintf (CRTDLL.501)
899 INT __cdecl CRTDLL_vswprintf( LPWSTR buffer, LPCWSTR spec, va_list args )
901 return wvsprintfW( buffer, spec, args );
904 /*********************************************************************
905 * _mbscpy (CRTDLL.200)
907 unsigned char* __cdecl CRTDLL__mbscpy(unsigned char *x,unsigned char *y)
909 TRACE("CRTDLL_mbscpy %s and %s\n",x,y);
910 return strcpy(x,y);
914 /*********************************************************************
915 * _mbscat (CRTDLL.197)
917 unsigned char* __cdecl CRTDLL__mbscat(unsigned char *x,unsigned char *y)
919 return strcat(x,y);
923 /*********************************************************************
924 * _strcmpi (CRTDLL.282) (CRTDLL.287)
926 INT __cdecl CRTDLL__strcmpi( LPCSTR s1, LPCSTR s2 )
928 return lstrcmpiA( s1, s2 );
932 /*********************************************************************
933 * _strnicmp (CRTDLL.293)
935 INT __cdecl CRTDLL__strnicmp( LPCSTR s1, LPCSTR s2, INT n )
937 return lstrncmpiA( s1, s2, n );
941 /*********************************************************************
942 * _strlwr (CRTDLL.293)
944 * convert a string in place to lowercase
946 LPSTR __cdecl CRTDLL__strlwr(LPSTR x)
948 unsigned char *y =x;
950 TRACE("CRTDLL_strlwr got %s\n", x);
951 while (*y) {
952 if ((*y > 0x40) && (*y< 0x5b))
953 *y = *y + 0x20;
954 y++;
956 TRACE(" returned %s\n", x);
958 return x;
961 /*********************************************************************
962 * system (CRTDLL.485)
964 INT __cdecl CRTDLL_system(LPSTR x)
966 #define SYSBUF_LENGTH 1500
967 char buffer[SYSBUF_LENGTH];
968 unsigned char *y = x;
969 unsigned char *bp;
970 int i;
972 sprintf( buffer, "%s \"", Options.argv0 );
973 bp = buffer + strlen(buffer);
974 i = strlen(buffer) + strlen(x) +2;
976 /* Calculate needed buffer size to prevent overflow. */
977 while (*y) {
978 if (*y =='\\') i++;
979 y++;
981 /* If buffer too short, exit. */
982 if (i > SYSBUF_LENGTH) {
983 TRACE("_system buffer to small\n");
984 return 127;
987 y =x;
989 while (*y) {
990 *bp = *y;
991 bp++; y++;
992 if (*(y-1) =='\\') *bp++ = '\\';
994 /* Remove spaces from end of string. */
995 while (*(y-1) == ' ') {
996 bp--;y--;
998 *bp++ = '"';
999 *bp = 0;
1000 TRACE("_system got '%s', executing '%s'\n",x,buffer);
1002 return system(buffer);
1005 /*********************************************************************
1006 * _strupr (CRTDLL.300)
1008 LPSTR __cdecl CRTDLL__strupr(LPSTR x)
1010 LPSTR y=x;
1012 while (*y) {
1013 *y=toupper(*y);
1014 y++;
1016 return x;
1019 /*********************************************************************
1020 * _wcsupr (CRTDLL.328)
1022 LPWSTR __cdecl CRTDLL__wcsupr(LPWSTR x)
1024 LPWSTR y=x;
1026 while (*y) {
1027 *y=towupper(*y);
1028 y++;
1030 return x;
1033 /*********************************************************************
1034 * _wcslwr (CRTDLL.323)
1036 LPWSTR __cdecl CRTDLL__wcslwr(LPWSTR x)
1038 LPWSTR y=x;
1040 while (*y) {
1041 *y=towlower(*y);
1042 y++;
1044 return x;
1048 /*********************************************************************
1049 * longjmp (CRTDLL.426)
1051 VOID __cdecl CRTDLL_longjmp(jmp_buf env, int val)
1053 FIXME("CRTDLL_longjmp semistup, expect crash\n");
1054 longjmp(env, val);
1057 /*********************************************************************
1058 * malloc (CRTDLL.427)
1060 VOID* __cdecl CRTDLL_malloc(DWORD size)
1062 return HeapAlloc(GetProcessHeap(),0,size);
1065 /*********************************************************************
1066 * new (CRTDLL.001)
1068 VOID* __cdecl CRTDLL_new(DWORD size)
1070 VOID* result;
1071 if(!(result = HeapAlloc(GetProcessHeap(),0,size)) && new_handler)
1072 (*new_handler)();
1073 return result;
1076 /*********************************************************************
1077 * set_new_handler(CRTDLL.003)
1079 new_handler_type __cdecl CRTDLL_set_new_handler(new_handler_type func)
1081 new_handler_type old_handler = new_handler;
1082 new_handler = func;
1083 return old_handler;
1086 /*********************************************************************
1087 * calloc (CRTDLL.350)
1089 VOID* __cdecl CRTDLL_calloc(DWORD size, DWORD count)
1091 return HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, size * count );
1094 /*********************************************************************
1095 * realloc (CRTDLL.447)
1097 VOID* __cdecl CRTDLL_realloc( VOID *ptr, DWORD size )
1099 return HeapReAlloc( GetProcessHeap(), 0, ptr, size );
1102 /*********************************************************************
1103 * free (CRTDLL.427)
1105 VOID __cdecl CRTDLL_free(LPVOID ptr)
1107 HeapFree(GetProcessHeap(),0,ptr);
1110 /*********************************************************************
1111 * delete (CRTDLL.002)
1113 VOID __cdecl CRTDLL_delete(VOID* ptr)
1115 HeapFree(GetProcessHeap(),0,ptr);
1118 /*********************************************************************
1119 * _strdup (CRTDLL.285)
1121 LPSTR __cdecl CRTDLL__strdup(LPCSTR ptr)
1123 return HEAP_strdupA(GetProcessHeap(),0,ptr);
1126 /*********************************************************************
1127 * _wcsdup (CRTDLL.320)
1129 LPWSTR __cdecl CRTDLL__wcsdup(LPCWSTR ptr)
1131 return HEAP_strdupW(GetProcessHeap(),0,ptr);
1134 /*********************************************************************
1135 * fclose (CRTDLL.362)
1137 INT __cdecl CRTDLL_fclose( CRTDLL_FILE *file )
1139 TRACE("%p\n", file );
1140 if (!CloseHandle( file->handle )) return -1;
1141 HeapFree( GetProcessHeap(), 0, file );
1142 return 0;
1145 /*********************************************************************
1146 * _unlink (CRTDLL.315)
1148 INT __cdecl CRTDLL__unlink(LPCSTR pathname)
1150 int ret=0;
1151 DOS_FULL_NAME full_name;
1153 if (!DOSFS_GetFullName( pathname, FALSE, &full_name )) {
1154 WARN("CRTDLL_unlink file %s bad name\n",pathname);
1155 return EOF;
1158 ret=unlink(full_name.long_name);
1159 TRACE("(%s unix %s)\n",
1160 pathname,full_name.long_name);
1161 if(ret)
1162 WARN(" Failed!\n");
1164 return ret;
1167 /*********************************************************************
1168 * rename (CRTDLL.449)
1170 INT __cdecl CRTDLL_rename(LPCSTR oldpath,LPCSTR newpath)
1172 BOOL ok = MoveFileExA( oldpath, newpath, MOVEFILE_REPLACE_EXISTING );
1173 return ok ? 0 : -1;
1177 /*********************************************************************
1178 * _stat (CRTDLL.280)
1181 struct win_stat
1183 UINT16 win_st_dev;
1184 UINT16 win_st_ino;
1185 UINT16 win_st_mode;
1186 INT16 win_st_nlink;
1187 INT16 win_st_uid;
1188 INT16 win_st_gid;
1189 UINT win_st_rdev;
1190 INT win_st_size;
1191 INT win_st_atime;
1192 INT win_st_mtime;
1193 INT win_st_ctime;
1196 int __cdecl CRTDLL__stat(const char * filename, struct win_stat * buf)
1198 int ret=0;
1199 DOS_FULL_NAME full_name;
1200 struct stat mystat;
1202 if (!DOSFS_GetFullName( filename, TRUE, &full_name ))
1204 WARN("CRTDLL__stat filename %s bad name\n",filename);
1205 return -1;
1207 ret=stat(full_name.long_name,&mystat);
1208 TRACE("CRTDLL__stat %s\n", filename);
1209 if(ret)
1210 WARN(" Failed!\n");
1212 /* FIXME: should check what Windows returns */
1214 buf->win_st_dev = mystat.st_dev;
1215 buf->win_st_ino = mystat.st_ino;
1216 buf->win_st_mode = mystat.st_mode;
1217 buf->win_st_nlink = mystat.st_nlink;
1218 buf->win_st_uid = mystat.st_uid;
1219 buf->win_st_gid = mystat.st_gid;
1220 buf->win_st_rdev = mystat.st_rdev;
1221 buf->win_st_size = mystat.st_size;
1222 buf->win_st_atime = mystat.st_atime;
1223 buf->win_st_mtime = mystat.st_mtime;
1224 buf->win_st_ctime = mystat.st_ctime;
1225 return ret;
1228 /*********************************************************************
1229 * _open (CRTDLL.239)
1231 HFILE __cdecl CRTDLL__open(LPCSTR path,INT flags)
1233 DWORD access = 0, creation = 0;
1234 HFILE ret;
1236 /* FIXME:
1237 the flags in lcc's header differ from the ones in Linux, e.g.
1238 Linux: define O_APPEND 02000 (= 0x400)
1239 lcc: define _O_APPEND 0x0008
1240 so here a scheme to translate them
1241 Probably lcc is wrong here, but at least a hack to get is going
1243 switch(flags & 3)
1245 case O_RDONLY: access |= GENERIC_READ; break;
1246 case O_WRONLY: access |= GENERIC_WRITE; break;
1247 case O_RDWR: access |= GENERIC_WRITE | GENERIC_READ; break;
1250 if (flags & 0x0100) /* O_CREAT */
1252 if (flags & 0x0400) /* O_EXCL */
1253 creation = CREATE_NEW;
1254 else if (flags & 0x0200) /* O_TRUNC */
1255 creation = CREATE_ALWAYS;
1256 else
1257 creation = OPEN_ALWAYS;
1259 else /* no O_CREAT */
1261 if (flags & 0x0200) /* O_TRUNC */
1262 creation = TRUNCATE_EXISTING;
1263 else
1264 creation = OPEN_EXISTING;
1266 if (flags & 0x0008) /* O_APPEND */
1267 FIXME("O_APPEND not supported\n" );
1268 if (flags & 0xf0f4)
1269 TRACE("CRTDLL_open file unsupported flags 0x%04x\n",flags);
1270 /* End Fixme */
1272 ret = CreateFileA( path, access, FILE_SHARE_READ | FILE_SHARE_WRITE,
1273 NULL, creation, FILE_ATTRIBUTE_NORMAL, -1 );
1274 TRACE("CRTDLL_open file %s mode 0x%04x got handle %d\n", path,flags,ret);
1275 return ret;
1278 /*********************************************************************
1279 * _close (CRTDLL.57)
1281 INT __cdecl CRTDLL__close(HFILE fd)
1283 int ret=_lclose(fd);
1285 TRACE("(%d)\n",fd);
1286 if(ret)
1287 WARN(" Failed!\n");
1289 return ret;
1292 /*********************************************************************
1293 * feof (CRTDLL.363)
1295 INT __cdecl CRTDLL_feof( CRTDLL_FILE *file )
1297 FIXME("stub\n" );
1298 return 0;
1301 /*********************************************************************
1302 * setlocale (CRTDLL.453)
1304 LPSTR __cdecl CRTDLL_setlocale(INT category,LPCSTR locale)
1306 LPSTR categorystr;
1308 switch (category) {
1309 case CRTDLL_LC_ALL: categorystr="LC_ALL";break;
1310 case CRTDLL_LC_COLLATE: categorystr="LC_COLLATE";break;
1311 case CRTDLL_LC_CTYPE: categorystr="LC_CTYPE";break;
1312 case CRTDLL_LC_MONETARY: categorystr="LC_MONETARY";break;
1313 case CRTDLL_LC_NUMERIC: categorystr="LC_NUMERIC";break;
1314 case CRTDLL_LC_TIME: categorystr="LC_TIME";break;
1315 default: categorystr = "UNKNOWN?";break;
1317 FIXME("(%s,%s),stub!\n",categorystr,locale);
1318 return "C";
1321 /*********************************************************************
1322 * wcscat (CRTDLL.503)
1324 LPWSTR __cdecl CRTDLL_wcscat( LPWSTR s1, LPCWSTR s2 )
1326 return lstrcatW( s1, s2 );
1329 /*********************************************************************
1330 * wcschr (CRTDLL.504)
1332 LPWSTR __cdecl CRTDLL_wcschr(LPCWSTR str,WCHAR xchar)
1334 LPCWSTR s;
1336 s=str;
1337 do {
1338 if (*s==xchar)
1339 return (LPWSTR)s;
1340 } while (*s++);
1341 return NULL;
1344 /*********************************************************************
1345 * wcscmp (CRTDLL.505)
1347 INT __cdecl CRTDLL_wcscmp( LPCWSTR s1, LPCWSTR s2 )
1349 return lstrcmpW( s1, s2 );
1352 /*********************************************************************
1353 * wcscpy (CRTDLL.507)
1355 LPWSTR __cdecl CRTDLL_wcscpy( LPWSTR s1, LPCWSTR s2 )
1357 return lstrcpyW( s1, s2 );
1360 /*********************************************************************
1361 * wcscspn (CRTDLL.508)
1363 INT __cdecl CRTDLL_wcscspn(LPWSTR str,LPWSTR reject)
1365 LPWSTR s,t;
1367 s=str;
1368 do {
1369 t=reject;
1370 while (*t) { if (*t==*s) break;t++;}
1371 if (*t) break;
1372 s++;
1373 } while (*s);
1374 return s-str; /* nr of wchars */
1377 /*********************************************************************
1378 * wcslen (CRTDLL.510)
1380 INT __cdecl CRTDLL_wcslen( LPCWSTR s )
1382 return lstrlenW( s );
1385 /*********************************************************************
1386 * wcsncat (CRTDLL.511)
1388 LPWSTR __cdecl CRTDLL_wcsncat( LPWSTR s1, LPCWSTR s2, INT n )
1390 return lstrcatnW( s1, s2, n );
1393 /*********************************************************************
1394 * wcsncmp (CRTDLL.512)
1396 INT __cdecl CRTDLL_wcsncmp( LPCWSTR s1, LPCWSTR s2, INT n )
1398 return lstrncmpW( s1, s2, n );
1401 /*********************************************************************
1402 * wcsncpy (CRTDLL.513)
1404 LPWSTR __cdecl CRTDLL_wcsncpy( LPWSTR s1, LPCWSTR s2, INT n )
1406 return lstrcpynW( s1, s2, n );
1409 /*********************************************************************
1410 * wcsspn (CRTDLL.516)
1412 INT __cdecl CRTDLL_wcsspn(LPWSTR str,LPWSTR accept)
1414 LPWSTR s,t;
1416 s=str;
1417 do {
1418 t=accept;
1419 while (*t) { if (*t==*s) break;t++;}
1420 if (!*t) break;
1421 s++;
1422 } while (*s);
1423 return s-str; /* nr of wchars */
1426 /*********************************************************************
1427 * _wcsicmp (CRTDLL.321)
1429 DWORD __cdecl CRTDLL__wcsicmp( LPCWSTR s1, LPCWSTR s2 )
1431 return lstrcmpiW( s1, s2 );
1434 /*********************************************************************
1435 * _wcsicoll (CRTDLL.322)
1437 DWORD __cdecl CRTDLL__wcsicoll(LPCWSTR a1,LPCWSTR a2)
1439 /* FIXME: handle collates */
1440 return lstrcmpiW(a1,a2);
1443 /*********************************************************************
1444 * _wcsnicmp (CRTDLL.324)
1446 DWORD __cdecl CRTDLL__wcsnicmp( LPCWSTR s1, LPCWSTR s2, INT len )
1448 return lstrncmpiW( s1, s2, len );
1451 /*********************************************************************
1452 * wcscoll (CRTDLL.506)
1454 DWORD __cdecl CRTDLL_wcscoll(LPWSTR a1,LPWSTR a2)
1456 /* FIXME: handle collates */
1457 return lstrcmpW(a1,a2);
1460 /*********************************************************************
1461 * _wcsrev (CRTDLL.326)
1463 VOID __cdecl CRTDLL__wcsrev(LPWSTR s) {
1464 LPWSTR e;
1466 e=s;
1467 while (*e)
1468 e++;
1469 while (s<e) {
1470 WCHAR a;
1472 a=*s;*s=*e;*e=a;
1473 s++;e--;
1477 /*********************************************************************
1478 * wcsstr (CRTDLL.517)
1480 LPWSTR __cdecl CRTDLL_wcsstr(LPWSTR s,LPWSTR b)
1482 LPWSTR x,y,c;
1484 x=s;
1485 while (*x) {
1486 if (*x==*b) {
1487 y=x;c=b;
1488 while (*y && *c && *y==*c) { c++;y++; }
1489 if (!*c)
1490 return x;
1492 x++;
1494 return NULL;
1497 /*********************************************************************
1498 * wcstombs (CRTDLL.521)
1500 INT __cdecl CRTDLL_wcstombs( LPSTR dst, LPCWSTR src, INT len )
1502 lstrcpynWtoA( dst, src, len );
1503 return strlen(dst); /* FIXME: is this right? */
1506 /*********************************************************************
1507 * wcsrchr (CRTDLL.515)
1509 LPWSTR __cdecl CRTDLL_wcsrchr(LPWSTR str,WCHAR xchar)
1511 LPWSTR s;
1513 s=str+lstrlenW(str);
1514 do {
1515 if (*s==xchar)
1516 return s;
1517 s--;
1518 } while (s>=str);
1519 return NULL;
1522 /*********************************************************************
1523 * _setmode (CRTDLL.265)
1524 * FIXME: At present we ignore the request to translate CR/LF to LF.
1526 * We allways translate when we read with fgets, we never do with fread
1529 INT __cdecl CRTDLL__setmode( INT fh,INT mode)
1531 /* FIXME */
1532 #define O_TEXT 0x4000
1533 #define O_BINARY 0x8000
1535 FIXME("on fhandle %d mode %s, STUB.\n",
1536 fh,(mode=O_TEXT)?"O_TEXT":
1537 (mode=O_BINARY)?"O_BINARY":"UNKNOWN");
1538 return -1;
1541 /*********************************************************************
1542 * _fpreset (CRTDLL.107)
1544 VOID __cdecl CRTDLL__fpreset(void)
1546 FIXME(" STUB.\n");
1549 /*********************************************************************
1550 * atexit (CRTDLL.345)
1552 INT __cdecl CRTDLL_atexit(LPVOID x)
1554 FIXME("(%p), STUB.\n",x);
1555 return 0; /* successful */
1558 /*********************************************************************
1559 * mblen (CRTDLL.428)
1560 * FIXME: check multibyte support
1562 WCHAR __cdecl CRTDLL_mblen(CHAR *mb,INT size)
1565 int ret=1;
1567 if (!mb)
1568 ret = 0;
1569 else if ((size<1)||(!*(mb+1)))
1570 ret = -1;
1571 else if (!(*mb))
1572 ret =0;
1574 TRACE("CRTDLL_mlen %s for max %d bytes ret %d\n",mb,size,ret);
1576 return ret;
1579 /*********************************************************************
1580 * mbstowcs (CRTDLL.429)
1581 * FIXME: check multibyte support
1583 INT __cdecl CRTDLL_mbstowcs(LPWSTR wcs, LPCSTR mbs, INT size)
1586 /* Slightly modified lstrcpynAtoW functions from memory/strings.c
1587 * We need the number of characters transfered
1588 * FIXME: No multibyte support yet
1591 LPWSTR p = wcs;
1592 LPCSTR src= mbs;
1593 int ret, n=size;
1595 while ((n-- > 0) && *src) {
1596 *p++ = (WCHAR)(unsigned char)*src++;
1598 p++;
1599 ret = (p -wcs);
1601 TRACE("CRTDLL_mbstowcs %s for %d chars put %d wchars\n",
1602 mbs,size,ret);
1603 return ret;
1606 /*********************************************************************
1607 * mbtowc (CRTDLL.430)
1608 * FIXME: check multibyte support
1610 WCHAR __cdecl CRTDLL_mbtowc(WCHAR* wc,CHAR* mb,INT size)
1612 int ret;
1614 if (!mb)
1615 ret = 0;
1616 else if (!wc)
1617 ret =-1;
1618 else
1619 if ( (ret = mblen(mb,size)) != -1 )
1621 if (ret <= sizeof(char))
1622 *wc = (WCHAR) ((unsigned char)*mb);
1623 else
1624 ret= -1;
1626 else
1627 ret = -1;
1629 TRACE("CRTDLL_mbtowc %s for %d chars\n",mb,size);
1631 return ret;
1634 /*********************************************************************
1635 * _isctype (CRTDLL.138)
1637 BOOL __cdecl CRTDLL__isctype(CHAR x,CHAR type)
1639 if ((type & CRTDLL_SPACE) && isspace(x))
1640 return TRUE;
1641 if ((type & CRTDLL_PUNCT) && ispunct(x))
1642 return TRUE;
1643 if ((type & CRTDLL_LOWER) && islower(x))
1644 return TRUE;
1645 if ((type & CRTDLL_UPPER) && isupper(x))
1646 return TRUE;
1647 if ((type & CRTDLL_ALPHA) && isalpha(x))
1648 return TRUE;
1649 if ((type & CRTDLL_DIGIT) && isdigit(x))
1650 return TRUE;
1651 if ((type & CRTDLL_CONTROL) && iscntrl(x))
1652 return TRUE;
1653 /* check CRTDLL_LEADBYTE */
1654 return FALSE;
1657 /*********************************************************************
1658 * _chdrive (CRTDLL.52)
1660 * newdir [I] drive to change to, A=1
1663 BOOL __cdecl CRTDLL__chdrive(INT newdrive)
1665 /* FIXME: generates errnos */
1666 return DRIVE_SetCurrentDrive(newdrive-1);
1669 /*********************************************************************
1670 * _chdir (CRTDLL.51)
1672 INT __cdecl CRTDLL__chdir(LPCSTR newdir)
1674 if (!SetCurrentDirectoryA(newdir))
1675 return 1;
1676 return 0;
1679 /*********************************************************************
1680 * _fullpath (CRTDLL.114)
1682 LPSTR __cdecl CRTDLL__fullpath(LPSTR buf, LPCSTR name, INT size)
1684 DOS_FULL_NAME full_name;
1686 if (!buf)
1688 size = 256;
1689 if(!(buf = CRTDLL_malloc(size))) return NULL;
1691 if (!DOSFS_GetFullName( name, FALSE, &full_name )) return NULL;
1692 lstrcpynA(buf,full_name.short_name,size);
1693 TRACE("CRTDLL_fullpath got %s\n",buf);
1694 return buf;
1697 /*********************************************************************
1698 * _splitpath (CRTDLL.279)
1700 VOID __cdecl CRTDLL__splitpath(LPCSTR path, LPSTR drive, LPSTR directory, LPSTR filename, LPSTR extension )
1702 /* drive includes :
1703 directory includes leading and trailing (forward and backward slashes)
1704 filename without dot and slashes
1705 extension with leading dot
1707 char * drivechar,*dirchar,*namechar;
1709 TRACE("CRTDLL__splitpath got %s\n",path);
1711 drivechar = strchr(path,':');
1712 dirchar = strrchr(path,'/');
1713 namechar = strrchr(path,'\\');
1714 dirchar = MAX(dirchar,namechar);
1715 if (dirchar)
1716 namechar = strrchr(dirchar,'.');
1717 else
1718 namechar = strrchr(path,'.');
1721 if (drive)
1723 *drive = 0x00;
1724 if (drivechar)
1726 strncat(drive,path,drivechar-path+1);
1727 path = drivechar+1;
1730 if (directory)
1732 *directory = 0x00;
1733 if (dirchar)
1735 strncat(directory,path,dirchar-path+1);
1736 path = dirchar+1;
1739 if (filename)
1741 *filename = 0x00;
1742 if (namechar)
1744 strncat(filename,path,namechar-path);
1745 if (extension)
1747 *extension = 0x00;
1748 strcat(extension,namechar);
1753 TRACE("CRTDLL__splitpath found %s %s %s %s\n",drive,directory,filename,extension);
1758 /*********************************************************************
1759 * _makepath (CRTDLL.182)
1762 VOID __cdecl CRTDLL__makepath(LPSTR path, LPCSTR drive,
1763 LPCSTR directory, LPCSTR filename,
1764 LPCSTR extension )
1766 char ch;
1767 TRACE("CRTDLL__makepath got %s %s %s %s\n", drive, directory,
1768 filename, extension);
1770 if ( !path )
1771 return;
1773 path[0] = 0;
1774 if ( drive )
1775 if ( drive[0] ) {
1776 sprintf(path, "%c:", drive[0]);
1778 if ( directory )
1779 if ( directory[0] ) {
1780 strcat(path, directory);
1781 ch = path[strlen(path)-1];
1782 if (ch != '/' && ch != '\\')
1783 strcat(path,"\\");
1785 if ( filename )
1786 if ( filename[0] ) {
1787 strcat(path, filename);
1788 if ( extension ) {
1789 if ( extension[0] ) {
1790 if ( extension[0] != '.' ) {
1791 strcat(path,".");
1793 strcat(path,extension);
1798 TRACE("CRTDLL__makepath returns %s\n",path);
1801 /*********************************************************************
1802 * _getcwd (CRTDLL.120)
1804 CHAR* __cdecl CRTDLL__getcwd(LPSTR buf, INT size)
1806 char test[1];
1807 int len;
1809 len = size;
1810 if (!buf) {
1811 if (size < 0) /* allocate as big as nescessary */
1812 len =GetCurrentDirectoryA(1,test) + 1;
1813 if(!(buf = CRTDLL_malloc(len)))
1815 /* set error to OutOfRange */
1816 return( NULL );
1819 size = len;
1820 if(!(len =GetCurrentDirectoryA(len,buf)))
1822 return NULL;
1824 if (len > size)
1826 /* set error to ERANGE */
1827 TRACE("CRTDLL_getcwd buffer to small\n");
1828 return NULL;
1830 return buf;
1834 /*********************************************************************
1835 * _getdcwd (CRTDLL.121)
1837 CHAR* __cdecl CRTDLL__getdcwd(INT drive,LPSTR buf, INT size)
1839 char test[1];
1840 int len;
1842 FIXME("(\"%c:\",%s,%d)\n",drive+'A',buf,size);
1843 len = size;
1844 if (!buf) {
1845 if (size < 0) /* allocate as big as nescessary */
1846 len =GetCurrentDirectoryA(1,test) + 1;
1847 if(!(buf = CRTDLL_malloc(len)))
1849 /* set error to OutOfRange */
1850 return( NULL );
1853 size = len;
1854 if(!(len =GetCurrentDirectoryA(len,buf)))
1856 return NULL;
1858 if (len > size)
1860 /* set error to ERANGE */
1861 TRACE("buffer to small\n");
1862 return NULL;
1864 return buf;
1868 /*********************************************************************
1869 * _getdrive (CRTDLL.124)
1871 * Return current drive, 1 for A, 2 for B
1873 INT __cdecl CRTDLL__getdrive(VOID)
1875 return DRIVE_GetCurrentDrive() + 1;
1878 /*********************************************************************
1879 * _mkdir (CRTDLL.234)
1881 INT __cdecl CRTDLL__mkdir(LPCSTR newdir)
1883 if (!CreateDirectoryA(newdir,NULL))
1884 return -1;
1885 return 0;
1888 /*********************************************************************
1889 * remove (CRTDLL.448)
1891 INT __cdecl CRTDLL_remove(LPCSTR file)
1893 if (!DeleteFileA(file))
1894 return -1;
1895 return 0;
1898 /*********************************************************************
1899 * _errno (CRTDLL.52)
1900 * Yes, this is a function.
1902 LPINT __cdecl CRTDLL__errno()
1904 static int crtdllerrno;
1906 /* FIXME: we should set the error at the failing function call time */
1907 crtdllerrno = LastErrorToErrno(GetLastError());
1908 return &crtdllerrno;
1911 /*********************************************************************
1912 * _tempnam (CRTDLL.305)
1915 LPSTR __cdecl CRTDLL__tempnam(LPCSTR dir, LPCSTR prefix)
1918 char *ret;
1919 DOS_FULL_NAME tempname;
1921 if ((ret = tempnam(dir,prefix))==NULL) {
1922 WARN("Unable to get unique filename\n");
1923 return NULL;
1925 if (!DOSFS_GetFullName(ret,FALSE,&tempname))
1927 TRACE("Wrong path?\n");
1928 return NULL;
1930 free(ret);
1931 if ((ret = CRTDLL_malloc(strlen(tempname.short_name)+1)) == NULL) {
1932 WARN("CRTDL_malloc for shortname failed\n");
1933 return NULL;
1935 if ((ret = strcpy(ret,tempname.short_name)) == NULL) {
1936 WARN("Malloc for shortname failed\n");
1937 return NULL;
1940 TRACE("dir %s prefix %s got %s\n",
1941 dir,prefix,ret);
1942 return ret;
1945 /*********************************************************************
1946 * tmpnam (CRTDLL.490)
1948 * lcclnk from lcc-win32 relies on a terminating dot in the name returned
1951 LPSTR __cdecl CRTDLL_tmpnam(LPSTR s)
1953 char *ret;
1955 if ((ret =tmpnam(s))== NULL) {
1956 WARN("Unable to get unique filename\n");
1957 return NULL;
1959 if (!DOSFS_GetFullName(ret,FALSE,&CRTDLL_tmpname))
1961 TRACE("Wrong path?\n");
1962 return NULL;
1964 strcat(CRTDLL_tmpname.short_name,".");
1965 TRACE("for buf %p got %s\n",
1966 s,CRTDLL_tmpname.short_name);
1967 TRACE("long got %s\n",
1968 CRTDLL_tmpname.long_name);
1969 if ( s != NULL)
1970 return strcpy(s,CRTDLL_tmpname.short_name);
1971 else
1972 return CRTDLL_tmpname.short_name;
1976 /*********************************************************************
1977 * _itoa (CRTDLL.165)
1979 LPSTR __cdecl CRTDLL__itoa(INT x,LPSTR buf,INT buflen)
1981 wsnprintfA(buf,buflen,"%d",x);
1982 return buf;
1985 /*********************************************************************
1986 * _ltoa (CRTDLL.180)
1988 LPSTR __cdecl CRTDLL__ltoa(long x,LPSTR buf,INT radix)
1990 switch(radix) {
1991 case 2: FIXME("binary format not implemented !\n");
1992 break;
1993 case 8: wsnprintfA(buf,0x80,"%o",x);
1994 break;
1995 case 10: wsnprintfA(buf,0x80,"%d",x);
1996 break;
1997 case 16: wsnprintfA(buf,0x80,"%x",x);
1998 break;
1999 default: FIXME("radix %d not implemented !\n", radix);
2001 return buf;
2004 /*********************************************************************
2005 * _ultoa (CRTDLL.311)
2007 LPSTR __cdecl CRTDLL__ultoa(long x,LPSTR buf,INT radix)
2009 switch(radix) {
2010 case 2: FIXME("binary format not implemented !\n");
2011 break;
2012 case 8: wsnprintfA(buf,0x80,"%lo",x);
2013 break;
2014 case 10: wsnprintfA(buf,0x80,"%ld",x);
2015 break;
2016 case 16: wsnprintfA(buf,0x80,"%lx",x);
2017 break;
2018 default: FIXME("radix %d not implemented !\n", radix);
2020 return buf;
2023 typedef VOID (*sig_handler_type)(VOID);
2025 /*********************************************************************
2026 * signal (CRTDLL.455)
2028 VOID __cdecl CRTDLL_signal(int sig, sig_handler_type ptr)
2030 FIXME("(%d %p):stub.\n", sig, ptr);
2033 /*********************************************************************
2034 * _ftol (CRTDLL.113)
2036 #ifdef USING_REAL_FPU
2037 LONG __cdecl CRTDLL__ftol(void) {
2038 /* don't just do DO_FPU("fistp",retval), because the rounding
2039 * mode must also be set to "round towards zero"... */
2040 double fl;
2041 POP_FPU(fl);
2042 return (LONG)fl;
2044 #else
2045 LONG __cdecl CRTDLL__ftol(double fl) {
2046 FIXME("should be register function\n");
2047 return (LONG)fl;
2049 #endif
2051 /*********************************************************************
2052 * _CIpow (CRTDLL.14)
2054 #ifdef USING_REAL_FPU
2055 LONG __cdecl CRTDLL__CIpow(void) {
2056 double x,y;
2057 POP_FPU(y);
2058 POP_FPU(x);
2059 return pow(x,y);
2061 #else
2062 LONG __cdecl CRTDLL__CIpow(double x,double y) {
2063 FIXME("should be register function\n");
2064 return pow(x,y);
2066 #endif
2068 /*********************************************************************
2069 * _sleep (CRTDLL.267)
2071 VOID __cdecl CRTDLL__sleep(unsigned long timeout)
2073 TRACE("CRTDLL__sleep for %ld milliseconds\n",timeout);
2074 Sleep((timeout)?timeout:1);
2077 /*********************************************************************
2078 * getenv (CRTDLL.437)
2080 LPSTR __cdecl CRTDLL_getenv(const char *name)
2082 LPSTR environ = GetEnvironmentStringsA();
2083 LPSTR pp,pos = NULL;
2084 unsigned int length;
2086 for (pp = environ; (*pp); pp = pp + strlen(pp) +1)
2088 pos =strchr(pp,'=');
2089 if (pos)
2090 length = pos -pp;
2091 else
2092 length = strlen(pp);
2093 if (!strncmp(pp,name,length)) break;
2095 if ((pp)&& (pos))
2097 pp = pos+1;
2098 TRACE("got %s\n",pp);
2100 FreeEnvironmentStringsA( environ );
2101 return pp;
2104 /*********************************************************************
2105 * _mbsrchr (CRTDLL.223)
2107 LPSTR __cdecl CRTDLL__mbsrchr(LPSTR s,CHAR x) {
2108 /* FIXME: handle multibyte strings */
2109 return strrchr(s,x);
2112 /*********************************************************************
2113 * _memicmp (CRTDLL.233)(NTDLL.868)
2114 * A stringcompare, without \0 check
2115 * RETURNS
2116 * -1:if first string is alphabetically before second string
2117 * 1:if second '' '' '' '' first ''
2118 * 0:if both are equal.
2120 INT __cdecl CRTDLL__memicmp(
2121 LPCSTR s1, /* [in] first string */
2122 LPCSTR s2, /* [in] second string */
2123 DWORD len /* [in] length to compare */
2124 ) {
2125 int i;
2127 for (i=0;i<len;i++) {
2128 if (tolower(s1[i])<tolower(s2[i]))
2129 return -1;
2130 if (tolower(s1[i])>tolower(s2[i]))
2131 return 1;
2133 return 0;
2135 /*********************************************************************
2136 * __dllonexit (CRTDLL.25)
2138 VOID __cdecl CRTDLL__dllonexit ()
2140 FIXME("stub\n");
2143 /*********************************************************************
2144 * wcstok (CRTDLL.519)
2145 * Like strtok, but for wide character strings. s is modified, yes.
2147 LPWSTR __cdecl CRTDLL_wcstok(LPWSTR s,LPCWSTR delim) {
2148 static LPWSTR nexttok = NULL;
2149 LPWSTR x,ret;
2151 if (!s)
2152 s = nexttok;
2153 if (!s)
2154 return NULL;
2155 x = s;
2156 while (*x && !CRTDLL_wcschr(delim,*x))
2157 x++;
2158 ret = nexttok;
2159 if (*x) {
2160 *x='\0';
2161 nexttok = x+1;
2162 } else
2163 nexttok = NULL;
2164 return ret;
2167 /*********************************************************************
2168 * wcstol (CRTDLL.520)
2169 * Like strtol, but for wide character strings.
2171 INT __cdecl CRTDLL_wcstol(LPWSTR s,LPWSTR *end,INT base) {
2172 LPSTR sA = HEAP_strdupWtoA(GetProcessHeap(),0,s),endA;
2173 INT ret = strtol(sA,&endA,base);
2175 HeapFree(GetProcessHeap(),0,sA);
2176 if (end) *end = s+(endA-sA); /* pointer magic checked. */
2177 return ret;
2179 /*********************************************************************
2180 * strdate (CRTDLL.283)
2182 LPSTR __cdecl CRTDLL__strdate (LPSTR date)
2183 { FIXME("%p stub\n", date);
2184 return 0;
2187 /*********************************************************************
2188 * strtime (CRTDLL.299)
2190 LPSTR __cdecl CRTDLL__strtime (LPSTR date)
2191 { FIXME("%p stub\n", date);
2192 return 0;