Fixed "Display Combination" call.
[wine/hacks.git] / misc / crtdll.c
blobad0db60f7fee4971537b3679833c87f2e647793c
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 "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"
54 /* windows.h RAND_MAX is smaller than normal RAND_MAX */
55 #define CRTDLL_RAND_MAX 0x7fff
57 static DOS_FULL_NAME CRTDLL_tmpname;
59 UINT CRTDLL_argc_dll; /* CRTDLL.23 */
60 LPSTR *CRTDLL_argv_dll; /* CRTDLL.24 */
61 LPSTR CRTDLL_acmdln_dll; /* CRTDLL.38 */
62 UINT CRTDLL_basemajor_dll; /* CRTDLL.42 */
63 UINT CRTDLL_baseminor_dll; /* CRTDLL.43 */
64 UINT CRTDLL_baseversion_dll; /* CRTDLL.44 */
65 UINT CRTDLL_commode_dll; /* CRTDLL.59 */
66 LPSTR CRTDLL_environ_dll; /* CRTDLL.75 */
67 UINT CRTDLL_fmode_dll; /* CRTDLL.104 */
68 UINT CRTDLL_osmajor_dll; /* CRTDLL.241 */
69 UINT CRTDLL_osminor_dll; /* CRTDLL.242 */
70 UINT CRTDLL_osmode_dll; /* CRTDLL.243 */
71 UINT CRTDLL_osver_dll; /* CRTDLL.244 */
72 UINT CRTDLL_osversion_dll; /* CRTDLL.245 */
73 UINT CRTDLL_winmajor_dll; /* CRTDLL.329 */
74 UINT CRTDLL_winminor_dll; /* CRTDLL.330 */
75 UINT CRTDLL_winver_dll; /* CRTDLL.331 */
77 /* FIXME: structure layout is obviously not correct */
78 typedef struct
80 HANDLE handle;
81 int pad[7];
82 } CRTDLL_FILE;
84 CRTDLL_FILE CRTDLL_iob[3];
86 static CRTDLL_FILE * const CRTDLL_stdin = &CRTDLL_iob[0];
87 static CRTDLL_FILE * const CRTDLL_stdout = &CRTDLL_iob[1];
88 static CRTDLL_FILE * const CRTDLL_stderr = &CRTDLL_iob[2];
90 typedef VOID (*new_handler_type)(VOID);
92 static new_handler_type new_handler;
94 #if defined(__GNUC__) && defined(__i386__)
95 #define USING_REAL_FPU
96 #define DO_FPU(x,y) __asm__ __volatile__( x " %0;fwait" : "=m" (y) : )
97 #define POP_FPU(x) DO_FPU("fstpl",x)
98 #endif
100 /*********************************************************************
101 * _GetMainArgs (CRTDLL.022)
103 DWORD __cdecl CRTDLL__GetMainArgs(LPDWORD argc,LPSTR **argv,
104 LPSTR *environ,DWORD flag)
106 char *cmdline;
107 char **xargv;
108 int xargc,i,afterlastspace;
109 DWORD version;
111 TRACE(crtdll,"(%p,%p,%p,%ld).\n",
112 argc,argv,environ,flag
114 CRTDLL_acmdln_dll = cmdline = HEAP_strdupA( GetProcessHeap(), 0,
115 GetCommandLineA() );
116 TRACE(crtdll,"got '%s'\n", cmdline);
118 version = GetVersion();
119 CRTDLL_osver_dll = version >> 16;
120 CRTDLL_winminor_dll = version & 0xFF;
121 CRTDLL_winmajor_dll = (version>>8) & 0xFF;
122 CRTDLL_baseversion_dll = version >> 16;
123 CRTDLL_winver_dll = ((version >> 8) & 0xFF) + ((version & 0xFF) << 8);
124 CRTDLL_baseminor_dll = (version >> 16) & 0xFF;
125 CRTDLL_basemajor_dll = (version >> 24) & 0xFF;
126 CRTDLL_osversion_dll = version & 0xFFFF;
127 CRTDLL_osminor_dll = version & 0xFF;
128 CRTDLL_osmajor_dll = (version>>8) & 0xFF;
130 /* missing threading init */
132 i=0;xargv=NULL;xargc=0;afterlastspace=0;
133 while (cmdline[i]) {
134 if (cmdline[i]==' ') {
135 xargv=(char**)HeapReAlloc( GetProcessHeap(), 0, xargv,
136 sizeof(char*)*(++xargc));
137 cmdline[i]='\0';
138 xargv[xargc-1] = HEAP_strdupA( GetProcessHeap(), 0,
139 cmdline+afterlastspace);
140 i++;
141 while (cmdline[i]==' ')
142 i++;
143 if (cmdline[i])
144 afterlastspace=i;
145 } else
146 i++;
148 xargv=(char**)HeapReAlloc( GetProcessHeap(), 0, xargv,
149 sizeof(char*)*(++xargc));
150 cmdline[i]='\0';
151 xargv[xargc-1] = HEAP_strdupA( GetProcessHeap(), 0,
152 cmdline+afterlastspace);
153 CRTDLL_argc_dll = xargc;
154 *argc = xargc;
155 CRTDLL_argv_dll = xargv;
156 *argv = xargv;
158 TRACE(crtdll,"found %d arguments\n",
159 CRTDLL_argc_dll);
160 CRTDLL_environ_dll = *environ = GetEnvironmentStringsA();
161 return 0;
165 typedef void (*_INITTERMFUN)();
167 /* fixme: move to header */
168 struct find_t
169 { unsigned attrib;
170 time_t time_create; /* -1 when not avaiable */
171 time_t time_access; /* -1 when not avaiable */
172 time_t time_write;
173 unsigned long size; /* fixme: 64 bit ??*/
174 char name[260];
176 /*********************************************************************
177 * _findfirst (CRTDLL.099)
179 * BUGS
180 * Unimplemented
182 DWORD __cdecl CRTDLL__findfirst(LPCSTR fname, struct find_t * x2)
184 FIXME(crtdll, ":(%s,%p): stub\n",fname,x2);
185 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
186 return FALSE;
189 /*********************************************************************
190 * _findnext (CRTDLL.100)
192 * BUGS
193 * Unimplemented
195 INT __cdecl CRTDLL__findnext(DWORD hand, struct find_t * x2)
197 FIXME(crtdll, ":(%ld,%p): stub\n",hand,x2);
198 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
199 return FALSE;
202 /*********************************************************************
203 * _fstat (CRTDLL.111)
205 * BUGS
206 * Unimplemented
208 int __cdecl CRTDLL__fstat(int file, struct stat* buf)
210 FIXME(crtdll, ":(%d,%p): stub\n",file,buf);
211 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
212 return FALSE;
215 /*********************************************************************
216 * _initterm (CRTDLL.135)
218 DWORD __cdecl CRTDLL__initterm(_INITTERMFUN *start,_INITTERMFUN *end)
220 _INITTERMFUN *current;
222 TRACE(crtdll,"(%p,%p)\n",start,end);
223 current=start;
224 while (current<end) {
225 if (*current) (*current)();
226 current++;
228 return 0;
231 /*********************************************************************
232 * _fdopen (CRTDLL.91)
234 CRTDLL_FILE * __cdecl CRTDLL__fdopen(INT handle, LPCSTR mode)
236 CRTDLL_FILE *file;
238 switch (handle)
240 case 0:
241 file = CRTDLL_stdin;
242 if (!file->handle) file->handle = GetStdHandle( STD_INPUT_HANDLE );
243 break;
244 case 1:
245 file = CRTDLL_stdout;
246 if (!file->handle) file->handle = GetStdHandle( STD_OUTPUT_HANDLE );
247 break;
248 case 2:
249 file=CRTDLL_stderr;
250 if (!file->handle) file->handle = GetStdHandle( STD_ERROR_HANDLE );
251 break;
252 default:
253 file = HeapAlloc( GetProcessHeap(), 0, sizeof(*file) );
254 file->handle = handle;
255 break;
257 TRACE(crtdll, "open handle %d mode %s got file %p\n",
258 handle, mode, file);
259 return file;
263 /*******************************************************************
264 * _global_unwind2 (CRTDLL.129)
266 void __cdecl CRTDLL__global_unwind2( PEXCEPTION_FRAME frame )
268 RtlUnwind( frame, 0, NULL, 0 );
271 /*******************************************************************
272 * _local_unwind2 (CRTDLL.173)
274 void __cdecl CRTDLL__local_unwind2( PEXCEPTION_FRAME endframe, DWORD nr )
276 TRACE(crtdll,"(%p,%ld)\n",endframe,nr);
278 /*********************************************************************
279 * _read (CRTDLL.256)
281 * BUGS
282 * Unimplemented
284 INT __cdecl CRTDLL__read(INT fd, LPVOID buf, UINT count)
286 FIXME(crtdll,":(%d,%p,%d): stub\n",fd,buf,count);
287 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
288 return FALSE;
291 /*********************************************************************
292 * fopen (CRTDLL.372)
294 CRTDLL_FILE * __cdecl CRTDLL_fopen(LPCSTR path, LPCSTR mode)
296 CRTDLL_FILE *file = NULL;
297 HFILE handle;
298 #if 0
299 DOS_FULL_NAME full_name;
301 if (!DOSFS_GetFullName( path, FALSE, &full_name )) {
302 WARN(crtdll, "file %s bad name\n",path);
303 return 0;
306 file=fopen(full_name.long_name ,mode);
307 #endif
308 DWORD access = 0, creation = 0;
310 if ((strchr(mode,'r')&&strchr(mode,'a'))||
311 (strchr(mode,'r')&&strchr(mode,'w'))||
312 (strchr(mode,'w')&&strchr(mode,'a')))
313 return NULL;
315 if (mode[0] == 'r')
317 access = GENERIC_READ;
318 creation = OPEN_EXISTING;
319 if (mode[1] == '+') access |= GENERIC_WRITE;
321 else if (mode[0] == 'w')
323 access = GENERIC_WRITE;
324 creation = CREATE_ALWAYS;
325 if (mode[1] == '+') access |= GENERIC_READ;
327 else if (mode[0] == 'a')
329 /* FIXME: there is no O_APPEND in CreateFile, should emulate it */
330 access = GENERIC_WRITE;
331 creation = OPEN_ALWAYS;
332 if (mode[1] == '+') access |= GENERIC_READ;
334 /* FIXME: should handle text/binary mode */
336 if ((handle = CreateFileA( path, access, FILE_SHARE_READ | FILE_SHARE_WRITE,
337 NULL, creation, FILE_ATTRIBUTE_NORMAL,
338 -1 )) != INVALID_HANDLE_VALUE)
340 file = HeapAlloc( GetProcessHeap(), 0, sizeof(*file) );
341 file->handle = handle;
343 TRACE(crtdll, "file %s mode %s got handle %d file %p\n",
344 path,mode,handle,file);
345 return file;
348 /*********************************************************************
349 * fread (CRTDLL.377)
351 DWORD __cdecl CRTDLL_fread(LPVOID ptr, INT size, INT nmemb, CRTDLL_FILE *file)
353 #if 0
354 int i=0;
355 void *temp=ptr;
357 /* If we would honour CR/LF <-> LF translation, we could do it like this.
358 We should keep track of all files opened, and probably files with \
359 known binary extensions must be unchanged */
360 while ( (i < (nmemb*size)) && (ret==1)) {
361 ret=fread(temp,1,1,file);
362 TRACE(crtdll, "got %c 0x%02x ret %d\n",
363 (isalpha(*(unsigned char*)temp))? *(unsigned char*)temp:
364 ' ',*(unsigned char*)temp, ret);
365 if (*(unsigned char*)temp != 0xd) { /* skip CR */
366 temp++;
367 i++;
369 else
370 TRACE(crtdll, "skipping ^M\n");
372 TRACE(crtdll, "0x%08x items of size %d from file %p to %p\n",
373 nmemb,size,file,ptr,);
374 if(i!=nmemb)
375 WARN(crtdll, " failed!\n");
377 return i;
378 #else
379 DWORD ret;
381 TRACE(crtdll, "0x%08x items of size %d from file %p to %p\n",
382 nmemb,size,file,ptr);
383 if (!ReadFile( file->handle, ptr, size * nmemb, &ret, NULL ))
384 WARN(crtdll, " failed!\n");
386 return ret / size;
387 #endif
389 /*********************************************************************
390 * freopen (CRTDLL.379)
392 * BUGS
393 * Unimplemented
395 DWORD __cdecl CRTDLL_freopen(LPCSTR path, LPCSTR mode, LPVOID stream)
397 FIXME(crtdll, ":(%s,%s,%p): stub\n", path, mode, stream);
398 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
399 return FALSE;
402 /*********************************************************************
403 * fscanf (CRTDLL.381)
405 INT __cdecl CRTDLL_fscanf( CRTDLL_FILE *stream, LPSTR format, ... )
407 #if 0
408 va_list valist;
409 INT res;
411 va_start( valist, format );
412 #ifdef HAVE_VFSCANF
413 res = vfscanf( xlat_file_ptr(stream), format, valist );
414 #endif
415 va_end( valist );
416 return res;
417 #endif
418 FIXME(crtdll,"broken\n");
419 return 0;
422 /*********************************************************************
423 * fseek (CRTDLL.382)
425 LONG __cdecl CRTDLL_fseek( CRTDLL_FILE *file, LONG offset, INT whence)
427 TRACE(crtdll, "file %p to 0x%08lx pos %s\n",
428 file,offset,(whence==SEEK_SET)?"SEEK_SET":
429 (whence==SEEK_CUR)?"SEEK_CUR":
430 (whence==SEEK_END)?"SEEK_END":"UNKNOWN");
431 if (SetFilePointer( file->handle, offset, NULL, whence ) != 0xffffffff)
432 return 0;
433 WARN(crtdll, " failed!\n");
434 return -1;
437 /*********************************************************************
438 * fsetpos (CRTDLL.383)
440 INT __cdecl CRTDLL_fsetpos( CRTDLL_FILE *file, INT *pos )
442 TRACE(crtdll, "file %p pos %d\n", file, *pos );
443 return CRTDLL_fseek(file, *pos, SEEK_SET);
446 /*********************************************************************
447 * ftell (CRTDLL.384)
449 LONG __cdecl CRTDLL_ftell( CRTDLL_FILE *file )
451 return SetFilePointer( file->handle, 0, NULL, SEEK_CUR );
454 /*********************************************************************
455 * fwrite (CRTDLL.386)
457 DWORD __cdecl CRTDLL_fwrite( LPVOID ptr, INT size, INT nmemb, CRTDLL_FILE *file )
459 DWORD ret;
461 TRACE(crtdll, "0x%08x items of size %d to file %p from %p\n",
462 nmemb,size,file,ptr);
463 if (!WriteFile( file->handle, ptr, size * nmemb, &ret, NULL ))
464 WARN(crtdll, " failed!\n");
465 return ret / size;
468 /*********************************************************************
469 * setbuf (CRTDLL.452)
471 INT __cdecl CRTDLL_setbuf(CRTDLL_FILE *file, LPSTR buf)
473 TRACE(crtdll, "(file %p buf %p)\n", file, buf);
474 /* this doesn't work:"void value not ignored as it ought to be"
475 return setbuf(file,buf);
477 /* FIXME: no buffering for now */
478 return 0;
481 /*********************************************************************
482 * _open_osfhandle (CRTDLL.240)
484 HFILE __cdecl CRTDLL__open_osfhandle(LONG osfhandle, INT flags)
486 HFILE handle;
488 switch (osfhandle) {
489 case STD_INPUT_HANDLE :
490 case 0 :
491 handle=0;
492 break;
493 case STD_OUTPUT_HANDLE:
494 case 1:
495 handle=1;
496 break;
497 case STD_ERROR_HANDLE:
498 case 2:
499 handle=2;
500 break;
501 default:
502 return (-1);
504 TRACE(crtdll, "(handle %08lx,flags %d) return %d\n",
505 osfhandle,flags,handle);
506 return handle;
510 /*********************************************************************
511 * srand (CRTDLL.460)
513 void __cdecl CRTDLL_srand(DWORD seed)
515 /* FIXME: should of course be thread? process? local */
516 srand(seed);
519 /*********************************************************************
520 * vfprintf (CRTDLL.373)
522 INT __cdecl CRTDLL_vfprintf( CRTDLL_FILE *file, LPSTR format, va_list args )
524 char buffer[1024]; /* FIXME... */
526 vsprintf( buffer, format, args );
527 return CRTDLL_fwrite( buffer, 1, strlen(buffer), file );
530 /*********************************************************************
531 * fprintf (CRTDLL.373)
533 INT __cdecl CRTDLL_fprintf( CRTDLL_FILE *file, LPSTR format, ... )
535 va_list valist;
536 INT res;
538 va_start( valist, format );
539 res = CRTDLL_vfprintf( file, format, valist );
540 va_end( valist );
541 return res;
544 /*********************************************************************
545 * time (CRTDLL.488)
547 time_t __cdecl CRTDLL_time(time_t *timeptr)
549 time_t curtime = time(NULL);
551 if (timeptr)
552 *timeptr = curtime;
553 return curtime;
556 /*********************************************************************
557 * (CRTDLL.350)
559 clock_t __cdecl CRTDLL_clock(void)
561 struct tms alltimes;
562 clock_t res;
564 times(&alltimes);
565 res = alltimes.tms_utime + alltimes.tms_stime+
566 alltimes.tms_cutime + alltimes.tms_cstime;
567 /* Fixme: We need some symbolic representation
568 for (Hostsystem_)CLOCKS_PER_SEC
569 and (Emulated_system_)CLOCKS_PER_SEC
570 10 holds only for Windows/Linux_i86)
572 return 10*res;
575 /*********************************************************************
576 * _isatty (CRTDLL.137)
578 BOOL __cdecl CRTDLL__isatty(DWORD x)
580 TRACE(crtdll,"(%ld)\n",x);
581 return TRUE;
584 /*********************************************************************
585 * _write (CRTDLL.332)
587 INT __cdecl CRTDLL__write(INT fd,LPCVOID buf,UINT count)
589 INT len=0;
591 if (fd == -1)
592 len = -1;
593 else if (fd<=2)
594 len = (UINT)write(fd,buf,(LONG)count);
595 else
596 len = _lwrite(fd,buf,count);
597 TRACE(crtdll,"%d/%d byte to dfh %d from %p,\n",
598 len,count,fd,buf);
599 return len;
603 /*********************************************************************
604 * _cexit (CRTDLL.49)
606 * FIXME: What the heck is the difference between
607 * FIXME _c_exit (CRTDLL.47)
608 * FIXME _cexit (CRTDLL.49)
609 * FIXME _exit (CRTDLL.87)
610 * FIXME exit (CRTDLL.359)
612 * atexit-processing comes to mind -- MW.
615 void __cdecl CRTDLL__cexit(INT ret)
617 TRACE(crtdll,"(%d)\n",ret);
618 ExitProcess(ret);
622 /*********************************************************************
623 * exit (CRTDLL.359)
625 void __cdecl CRTDLL_exit(DWORD ret)
627 TRACE(crtdll,"(%ld)\n",ret);
628 ExitProcess(ret);
632 /*********************************************************************
633 * _abnormal_termination (CRTDLL.36)
635 INT __cdecl CRTDLL__abnormal_termination(void)
637 TRACE(crtdll,"(void)\n");
638 return 0;
642 /*********************************************************************
643 * _access (CRTDLL.37)
645 INT __cdecl CRTDLL__access(LPCSTR filename, INT mode)
647 DWORD attr = GetFileAttributesA(filename);
649 if (attr == -1)
651 if (GetLastError() == ERROR_INVALID_ACCESS)
652 errno = EACCES;
653 else
654 errno = ENOENT;
655 return -1;
658 if ((attr & FILE_ATTRIBUTE_READONLY) && (mode & W_OK))
660 errno = EACCES;
661 return -1;
663 else
664 return 0;
668 /*********************************************************************
669 * fflush (CRTDLL.365)
671 INT __cdecl CRTDLL_fflush( CRTDLL_FILE *file )
673 return FlushFileBuffers( file->handle ) ? 0 : -1;
677 /*********************************************************************
678 * rand (CRTDLL.446)
680 INT __cdecl CRTDLL_rand()
682 return (rand() & CRTDLL_RAND_MAX);
686 /*********************************************************************
687 * putchar (CRTDLL.442)
689 void __cdecl CRTDLL_putchar( INT x )
691 putchar(x);
695 /*********************************************************************
696 * fputc (CRTDLL.374)
698 INT __cdecl CRTDLL_fputc( INT c, CRTDLL_FILE *file )
700 char ch = (char)c;
701 DWORD res;
702 TRACE(crtdll, "%c to file %p\n",c,file);
703 if (!WriteFile( file->handle, &ch, 1, &res, NULL )) return -1;
704 return c;
708 /*********************************************************************
709 * fputs (CRTDLL.375)
711 INT __cdecl CRTDLL_fputs( LPCSTR s, CRTDLL_FILE *file )
713 DWORD res;
714 TRACE(crtdll, "%s to file %p\n",s,file);
715 if (!WriteFile( file->handle, s, strlen(s), &res, NULL )) return -1;
716 return res;
720 /*********************************************************************
721 * puts (CRTDLL.443)
723 INT __cdecl CRTDLL_puts(LPCSTR s)
725 TRACE(crtdll, "%s \n",s);
726 return puts(s);
730 /*********************************************************************
731 * putc (CRTDLL.441)
733 INT __cdecl CRTDLL_putc( INT c, CRTDLL_FILE *file )
735 return CRTDLL_fputc( c, file );
738 /*********************************************************************
739 * fgetc (CRTDLL.366)
741 INT __cdecl CRTDLL_fgetc( CRTDLL_FILE *file )
743 DWORD res;
744 char ch;
745 if (!ReadFile( file->handle, &ch, 1, &res, NULL )) return -1;
746 if (res != 1) return -1;
747 return ch;
751 /*********************************************************************
752 * getc (CRTDLL.388)
754 INT __cdecl CRTDLL_getc( CRTDLL_FILE *file )
756 return CRTDLL_fgetc( file );
760 /*********************************************************************
761 * fgets (CRTDLL.368)
763 CHAR* __cdecl CRTDLL_fgets( LPSTR s, INT size, CRTDLL_FILE *file )
765 int cc;
766 LPSTR buf_start = s;
768 /* BAD, for the whole WINE process blocks... just done this way to test
769 * windows95's ftp.exe.
772 for(cc = CRTDLL_fgetc(file); cc != EOF && cc != '\n'; cc = CRTDLL_fgetc(file))
773 if (cc != '\r')
775 if (--size <= 0) break;
776 *s++ = (char)cc;
779 *s = '\0';
781 TRACE(crtdll,"got '%s'\n", buf_start);
782 return buf_start;
786 /*********************************************************************
787 * gets (CRTDLL.391)
789 LPSTR __cdecl CRTDLL_gets(LPSTR buf)
791 int cc;
792 LPSTR buf_start = buf;
794 /* BAD, for the whole WINE process blocks... just done this way to test
795 * windows95's ftp.exe.
798 for(cc = fgetc(stdin); cc != EOF && cc != '\n'; cc = fgetc(stdin))
799 if(cc != '\r') *buf++ = (char)cc;
801 *buf = '\0';
803 TRACE(crtdll,"got '%s'\n", buf_start);
804 return buf_start;
808 /*********************************************************************
809 * _rotl (CRTDLL.259)
811 UINT __cdecl CRTDLL__rotl(UINT x,INT shift)
813 unsigned int ret = (x >> shift)|( x >>((sizeof(x))-shift));
815 TRACE(crtdll, "got 0x%08x rot %d ret 0x%08x\n",
816 x,shift,ret);
817 return ret;
820 /*********************************************************************
821 * _lrotl (CRTDLL.176)
823 DWORD __cdecl CRTDLL__lrotl(DWORD x,INT shift)
825 unsigned long ret = (x >> shift)|( x >>((sizeof(x))-shift));
827 TRACE(crtdll, "got 0x%08lx rot %d ret 0x%08lx\n",
828 x,shift,ret);
829 return ret;
834 /*********************************************************************
835 * _mbsicmp (CRTDLL.204)
837 int __cdecl CRTDLL__mbsicmp(unsigned char *x,unsigned char *y)
839 do {
840 if (!*x)
841 return !!*y;
842 if (!*y)
843 return !!*x;
844 /* FIXME: MBCS handling... */
845 if (*x!=*y)
846 return 1;
847 x++;
848 y++;
849 } while (1);
853 /*********************************************************************
854 * _mbsinc (CRTDLL.205)
856 unsigned char * __cdecl CRTDLL__mbsinc(unsigned char *x)
858 /* FIXME: mbcs */
859 return x++;
863 /*********************************************************************
864 * vsprintf (CRTDLL.500)
866 INT __cdecl CRTDLL_vsprintf( LPSTR buffer, LPCSTR spec, va_list args )
868 return wvsprintfA( buffer, spec, args );
871 /*********************************************************************
872 * vswprintf (CRTDLL.501)
874 INT __cdecl CRTDLL_vswprintf( LPWSTR buffer, LPCWSTR spec, va_list args )
876 return wvsprintfW( buffer, spec, args );
879 /*********************************************************************
880 * _mbscpy (CRTDLL.200)
882 unsigned char* __cdecl CRTDLL__mbscpy(unsigned char *x,unsigned char *y)
884 TRACE(crtdll,"CRTDLL_mbscpy %s and %s\n",x,y);
885 return strcpy(x,y);
889 /*********************************************************************
890 * _mbscat (CRTDLL.197)
892 unsigned char* __cdecl CRTDLL__mbscat(unsigned char *x,unsigned char *y)
894 return strcat(x,y);
898 /*********************************************************************
899 * _strcmpi (CRTDLL.282) (CRTDLL.287)
901 INT __cdecl CRTDLL__strcmpi( LPCSTR s1, LPCSTR s2 )
903 return lstrcmpiA( s1, s2 );
907 /*********************************************************************
908 * _strnicmp (CRTDLL.293)
910 INT __cdecl CRTDLL__strnicmp( LPCSTR s1, LPCSTR s2, INT n )
912 return lstrncmpiA( s1, s2, n );
916 /*********************************************************************
917 * _strlwr (CRTDLL.293)
919 * convert a string in place to lowercase
921 LPSTR __cdecl CRTDLL__strlwr(LPSTR x)
923 unsigned char *y =x;
925 TRACE(crtdll, "CRTDLL_strlwr got %s\n", x);
926 while (*y) {
927 if ((*y > 0x40) && (*y< 0x5b))
928 *y = *y + 0x20;
929 y++;
931 TRACE(crtdll, " returned %s\n", x);
933 return x;
936 /*********************************************************************
937 * system (CRTDLL.485)
939 INT __cdecl CRTDLL_system(LPSTR x)
941 #define SYSBUF_LENGTH 1500
942 char buffer[SYSBUF_LENGTH];
943 unsigned char *y = x;
944 unsigned char *bp;
945 int i;
947 sprintf( buffer, "%s \"", Options.argv0 );
948 bp = buffer + strlen(buffer);
949 i = strlen(buffer) + strlen(x) +2;
951 /* Calculate needed buffer size to prevent overflow. */
952 while (*y) {
953 if (*y =='\\') i++;
954 y++;
956 /* If buffer too short, exit. */
957 if (i > SYSBUF_LENGTH) {
958 TRACE(crtdll,"_system buffer to small\n");
959 return 127;
962 y =x;
964 while (*y) {
965 *bp = *y;
966 bp++; y++;
967 if (*(y-1) =='\\') *bp++ = '\\';
969 /* Remove spaces from end of string. */
970 while (*(y-1) == ' ') {
971 bp--;y--;
973 *bp++ = '"';
974 *bp = 0;
975 TRACE(crtdll, "_system got '%s', executing '%s'\n",x,buffer);
977 return system(buffer);
980 /*********************************************************************
981 * _strupr (CRTDLL.300)
983 LPSTR __cdecl CRTDLL__strupr(LPSTR x)
985 LPSTR y=x;
987 while (*y) {
988 *y=toupper(*y);
989 y++;
991 return x;
994 /*********************************************************************
995 * _wcsupr (CRTDLL.328)
997 LPWSTR __cdecl CRTDLL__wcsupr(LPWSTR x)
999 LPWSTR y=x;
1001 while (*y) {
1002 *y=towupper(*y);
1003 y++;
1005 return x;
1008 /*********************************************************************
1009 * _wcslwr (CRTDLL.323)
1011 LPWSTR __cdecl CRTDLL__wcslwr(LPWSTR x)
1013 LPWSTR y=x;
1015 while (*y) {
1016 *y=towlower(*y);
1017 y++;
1019 return x;
1023 /*********************************************************************
1024 * longjmp (CRTDLL.426)
1026 VOID __cdecl CRTDLL_longjmp(jmp_buf env, int val)
1028 FIXME(crtdll,"CRTDLL_longjmp semistup, expect crash\n");
1029 return longjmp(env, val);
1032 /*********************************************************************
1033 * malloc (CRTDLL.427)
1035 VOID* __cdecl CRTDLL_malloc(DWORD size)
1037 return HeapAlloc(GetProcessHeap(),0,size);
1040 /*********************************************************************
1041 * new (CRTDLL.001)
1043 VOID* __cdecl CRTDLL_new(DWORD size)
1045 VOID* result;
1046 if(!(result = HeapAlloc(GetProcessHeap(),0,size)) && new_handler)
1047 (*new_handler)();
1048 return result;
1051 /*********************************************************************
1052 * set_new_handler(CRTDLL.003)
1054 new_handler_type __cdecl CRTDLL_set_new_handler(new_handler_type func)
1056 new_handler_type old_handler = new_handler;
1057 new_handler = func;
1058 return old_handler;
1061 /*********************************************************************
1062 * calloc (CRTDLL.350)
1064 VOID* __cdecl CRTDLL_calloc(DWORD size, DWORD count)
1066 return HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, size * count );
1069 /*********************************************************************
1070 * realloc (CRTDLL.447)
1072 VOID* __cdecl CRTDLL_realloc( VOID *ptr, DWORD size )
1074 return HeapReAlloc( GetProcessHeap(), 0, ptr, size );
1077 /*********************************************************************
1078 * free (CRTDLL.427)
1080 VOID __cdecl CRTDLL_free(LPVOID ptr)
1082 HeapFree(GetProcessHeap(),0,ptr);
1085 /*********************************************************************
1086 * delete (CRTDLL.002)
1088 VOID __cdecl CRTDLL_delete(VOID* ptr)
1090 HeapFree(GetProcessHeap(),0,ptr);
1093 /*********************************************************************
1094 * _strdup (CRTDLL.285)
1096 LPSTR __cdecl CRTDLL__strdup(LPCSTR ptr)
1098 return HEAP_strdupA(GetProcessHeap(),0,ptr);
1101 /*********************************************************************
1102 * _wcsdup (CRTDLL.320)
1104 LPWSTR __cdecl CRTDLL__wcsdup(LPCWSTR ptr)
1106 return HEAP_strdupW(GetProcessHeap(),0,ptr);
1109 /*********************************************************************
1110 * fclose (CRTDLL.362)
1112 INT __cdecl CRTDLL_fclose( CRTDLL_FILE *file )
1114 TRACE(crtdll,"%p\n", file );
1115 if (!CloseHandle( file->handle )) return -1;
1116 HeapFree( GetProcessHeap(), 0, file );
1117 return 0;
1120 /*********************************************************************
1121 * _unlink (CRTDLL.315)
1123 INT __cdecl CRTDLL__unlink(LPCSTR pathname)
1125 int ret=0;
1126 DOS_FULL_NAME full_name;
1128 if (!DOSFS_GetFullName( pathname, FALSE, &full_name )) {
1129 WARN(crtdll, "CRTDLL_unlink file %s bad name\n",pathname);
1130 return EOF;
1133 ret=unlink(full_name.long_name);
1134 TRACE(crtdll,"(%s unix %s)\n",
1135 pathname,full_name.long_name);
1136 if(ret)
1137 WARN(crtdll, " Failed!\n");
1139 return ret;
1142 /*********************************************************************
1143 * rename (CRTDLL.449)
1145 INT __cdecl CRTDLL_rename(LPCSTR oldpath,LPCSTR newpath)
1147 BOOL ok = MoveFileExA( oldpath, newpath, MOVEFILE_REPLACE_EXISTING );
1148 return ok ? 0 : -1;
1152 /*********************************************************************
1153 * _stat (CRTDLL.280)
1156 struct win_stat
1158 UINT16 win_st_dev;
1159 UINT16 win_st_ino;
1160 UINT16 win_st_mode;
1161 INT16 win_st_nlink;
1162 INT16 win_st_uid;
1163 INT16 win_st_gid;
1164 UINT win_st_rdev;
1165 INT win_st_size;
1166 INT win_st_atime;
1167 INT win_st_mtime;
1168 INT win_st_ctime;
1171 int __cdecl CRTDLL__stat(const char * filename, struct win_stat * buf)
1173 int ret=0;
1174 DOS_FULL_NAME full_name;
1175 struct stat mystat;
1177 if (!DOSFS_GetFullName( filename, TRUE, &full_name ))
1179 WARN(crtdll, "CRTDLL__stat filename %s bad name\n",filename);
1180 return -1;
1182 ret=stat(full_name.long_name,&mystat);
1183 TRACE(crtdll,"CRTDLL__stat %s\n", filename);
1184 if(ret)
1185 WARN(crtdll, " Failed!\n");
1187 /* FIXME: should check what Windows returns */
1189 buf->win_st_dev = mystat.st_dev;
1190 buf->win_st_ino = mystat.st_ino;
1191 buf->win_st_mode = mystat.st_mode;
1192 buf->win_st_nlink = mystat.st_nlink;
1193 buf->win_st_uid = mystat.st_uid;
1194 buf->win_st_gid = mystat.st_gid;
1195 buf->win_st_rdev = mystat.st_rdev;
1196 buf->win_st_size = mystat.st_size;
1197 buf->win_st_atime = mystat.st_atime;
1198 buf->win_st_mtime = mystat.st_mtime;
1199 buf->win_st_ctime = mystat.st_ctime;
1200 return ret;
1203 /*********************************************************************
1204 * _open (CRTDLL.239)
1206 HFILE __cdecl CRTDLL__open(LPCSTR path,INT flags)
1208 DWORD access = 0, creation = 0;
1209 HFILE ret;
1211 /* FIXME:
1212 the flags in lcc's header differ from the ones in Linux, e.g.
1213 Linux: define O_APPEND 02000 (= 0x400)
1214 lcc: define _O_APPEND 0x0008
1215 so here a scheme to translate them
1216 Probably lcc is wrong here, but at least a hack to get is going
1218 switch(flags & 3)
1220 case O_RDONLY: access |= GENERIC_READ; break;
1221 case O_WRONLY: access |= GENERIC_WRITE; break;
1222 case O_RDWR: access |= GENERIC_WRITE | GENERIC_READ; break;
1225 if (flags & 0x0100) /* O_CREAT */
1227 if (flags & 0x0400) /* O_EXCL */
1228 creation = CREATE_NEW;
1229 else if (flags & 0x0200) /* O_TRUNC */
1230 creation = CREATE_ALWAYS;
1231 else
1232 creation = OPEN_ALWAYS;
1234 else /* no O_CREAT */
1236 if (flags & 0x0200) /* O_TRUNC */
1237 creation = TRUNCATE_EXISTING;
1238 else
1239 creation = OPEN_EXISTING;
1241 if (flags & 0x0008) /* O_APPEND */
1242 FIXME(crtdll, "O_APPEND not supported\n" );
1243 if (flags & 0xf0f4)
1244 TRACE(crtdll,"CRTDLL_open file unsupported flags 0x%04x\n",flags);
1245 /* End Fixme */
1247 ret = CreateFileA( path, access, FILE_SHARE_READ | FILE_SHARE_WRITE,
1248 NULL, creation, FILE_ATTRIBUTE_NORMAL, -1 );
1249 TRACE(crtdll,"CRTDLL_open file %s mode 0x%04x got handle %d\n", path,flags,ret);
1250 return ret;
1253 /*********************************************************************
1254 * _close (CRTDLL.57)
1256 INT __cdecl CRTDLL__close(HFILE fd)
1258 int ret=_lclose(fd);
1260 TRACE(crtdll,"(%d)\n",fd);
1261 if(ret)
1262 WARN(crtdll, " Failed!\n");
1264 return ret;
1267 /*********************************************************************
1268 * feof (CRTDLL.363)
1270 INT __cdecl CRTDLL_feof( CRTDLL_FILE *file )
1272 FIXME(crtdll,"stub\n" );
1273 return 0;
1276 /*********************************************************************
1277 * setlocale (CRTDLL.453)
1279 LPSTR __cdecl CRTDLL_setlocale(INT category,LPCSTR locale)
1281 LPSTR categorystr;
1283 switch (category) {
1284 case CRTDLL_LC_ALL: categorystr="LC_ALL";break;
1285 case CRTDLL_LC_COLLATE: categorystr="LC_COLLATE";break;
1286 case CRTDLL_LC_CTYPE: categorystr="LC_CTYPE";break;
1287 case CRTDLL_LC_MONETARY: categorystr="LC_MONETARY";break;
1288 case CRTDLL_LC_NUMERIC: categorystr="LC_NUMERIC";break;
1289 case CRTDLL_LC_TIME: categorystr="LC_TIME";break;
1290 default: categorystr = "UNKNOWN?";break;
1292 FIXME(crtdll,"(%s,%s),stub!\n",categorystr,locale);
1293 return "C";
1296 /*********************************************************************
1297 * wcscat (CRTDLL.503)
1299 LPWSTR __cdecl CRTDLL_wcscat( LPWSTR s1, LPCWSTR s2 )
1301 return lstrcatW( s1, s2 );
1304 /*********************************************************************
1305 * wcschr (CRTDLL.504)
1307 LPWSTR __cdecl CRTDLL_wcschr(LPCWSTR str,WCHAR xchar)
1309 LPCWSTR s;
1311 s=str;
1312 do {
1313 if (*s==xchar)
1314 return (LPWSTR)s;
1315 } while (*s++);
1316 return NULL;
1319 /*********************************************************************
1320 * wcscmp (CRTDLL.505)
1322 INT __cdecl CRTDLL_wcscmp( LPCWSTR s1, LPCWSTR s2 )
1324 return lstrcmpW( s1, s2 );
1327 /*********************************************************************
1328 * wcscpy (CRTDLL.507)
1330 LPWSTR __cdecl CRTDLL_wcscpy( LPWSTR s1, LPCWSTR s2 )
1332 return lstrcpyW( s1, s2 );
1335 /*********************************************************************
1336 * wcscspn (CRTDLL.508)
1338 INT __cdecl CRTDLL_wcscspn(LPWSTR str,LPWSTR reject)
1340 LPWSTR s,t;
1342 s=str;
1343 do {
1344 t=reject;
1345 while (*t) { if (*t==*s) break;t++;}
1346 if (*t) break;
1347 s++;
1348 } while (*s);
1349 return s-str; /* nr of wchars */
1352 /*********************************************************************
1353 * wcslen (CRTDLL.510)
1355 INT __cdecl CRTDLL_wcslen( LPCWSTR s )
1357 return lstrlenW( s );
1360 /*********************************************************************
1361 * wcsncat (CRTDLL.511)
1363 LPWSTR __cdecl CRTDLL_wcsncat( LPWSTR s1, LPCWSTR s2, INT n )
1365 return lstrcatnW( s1, s2, n );
1368 /*********************************************************************
1369 * wcsncmp (CRTDLL.512)
1371 INT __cdecl CRTDLL_wcsncmp( LPCWSTR s1, LPCWSTR s2, INT n )
1373 return lstrncmpW( s1, s2, n );
1376 /*********************************************************************
1377 * wcsncpy (CRTDLL.513)
1379 LPWSTR __cdecl CRTDLL_wcsncpy( LPWSTR s1, LPCWSTR s2, INT n )
1381 return lstrcpynW( s1, s2, n );
1384 /*********************************************************************
1385 * wcsspn (CRTDLL.516)
1387 INT __cdecl CRTDLL_wcsspn(LPWSTR str,LPWSTR accept)
1389 LPWSTR s,t;
1391 s=str;
1392 do {
1393 t=accept;
1394 while (*t) { if (*t==*s) break;t++;}
1395 if (!*t) break;
1396 s++;
1397 } while (*s);
1398 return s-str; /* nr of wchars */
1401 /*********************************************************************
1402 * _wcsicmp (CRTDLL.321)
1404 DWORD __cdecl CRTDLL__wcsicmp( LPCWSTR s1, LPCWSTR s2 )
1406 return lstrcmpiW( s1, s2 );
1409 /*********************************************************************
1410 * _wcsicoll (CRTDLL.322)
1412 DWORD __cdecl CRTDLL__wcsicoll(LPCWSTR a1,LPCWSTR a2)
1414 /* FIXME: handle collates */
1415 return lstrcmpiW(a1,a2);
1418 /*********************************************************************
1419 * _wcsnicmp (CRTDLL.324)
1421 DWORD __cdecl CRTDLL__wcsnicmp( LPCWSTR s1, LPCWSTR s2, INT len )
1423 return lstrncmpiW( s1, s2, len );
1426 /*********************************************************************
1427 * wcscoll (CRTDLL.506)
1429 DWORD __cdecl CRTDLL_wcscoll(LPWSTR a1,LPWSTR a2)
1431 /* FIXME: handle collates */
1432 return lstrcmpW(a1,a2);
1435 /*********************************************************************
1436 * _wcsrev (CRTDLL.326)
1438 VOID __cdecl CRTDLL__wcsrev(LPWSTR s) {
1439 LPWSTR e;
1441 e=s;
1442 while (*e)
1443 e++;
1444 while (s<e) {
1445 WCHAR a;
1447 a=*s;*s=*e;*e=a;
1448 s++;e--;
1452 /*********************************************************************
1453 * wcsstr (CRTDLL.517)
1455 LPWSTR __cdecl CRTDLL_wcsstr(LPWSTR s,LPWSTR b)
1457 LPWSTR x,y,c;
1459 x=s;
1460 while (*x) {
1461 if (*x==*b) {
1462 y=x;c=b;
1463 while (*y && *c && *y==*c) { c++;y++; }
1464 if (!*c)
1465 return x;
1467 x++;
1469 return NULL;
1472 /*********************************************************************
1473 * wcstombs (CRTDLL.521)
1475 INT __cdecl CRTDLL_wcstombs( LPSTR dst, LPCWSTR src, INT len )
1477 lstrcpynWtoA( dst, src, len );
1478 return strlen(dst); /* FIXME: is this right? */
1481 /*********************************************************************
1482 * wcsrchr (CRTDLL.515)
1484 LPWSTR __cdecl CRTDLL_wcsrchr(LPWSTR str,WCHAR xchar)
1486 LPWSTR s;
1488 s=str+lstrlenW(str);
1489 do {
1490 if (*s==xchar)
1491 return s;
1492 s--;
1493 } while (s>=str);
1494 return NULL;
1497 /*********************************************************************
1498 * _setmode (CRTDLL.265)
1499 * FIXME: At present we ignore the request to translate CR/LF to LF.
1501 * We allways translate when we read with fgets, we never do with fread
1504 INT __cdecl CRTDLL__setmode( INT fh,INT mode)
1506 /* FIXME */
1507 #define O_TEXT 0x4000
1508 #define O_BINARY 0x8000
1510 FIXME(crtdll, "on fhandle %d mode %s, STUB.\n",
1511 fh,(mode=O_TEXT)?"O_TEXT":
1512 (mode=O_BINARY)?"O_BINARY":"UNKNOWN");
1513 return -1;
1516 /*********************************************************************
1517 * _fpreset (CRTDLL.107)
1519 VOID __cdecl CRTDLL__fpreset(void)
1521 FIXME(crtdll," STUB.\n");
1524 /*********************************************************************
1525 * atexit (CRTDLL.345)
1527 INT __cdecl CRTDLL_atexit(LPVOID x)
1529 FIXME(crtdll,"(%p), STUB.\n",x);
1530 return 0; /* successful */
1533 /*********************************************************************
1534 * mblen (CRTDLL.428)
1535 * FIXME: check multibyte support
1537 WCHAR __cdecl CRTDLL_mblen(CHAR *mb,INT size)
1540 int ret=1;
1542 if (!mb)
1543 ret = 0;
1544 else if ((size<1)||(!*(mb+1)))
1545 ret = -1;
1546 else if (!(*mb))
1547 ret =0;
1549 TRACE(crtdll,"CRTDLL_mlen %s for max %d bytes ret %d\n",mb,size,ret);
1551 return ret;
1554 /*********************************************************************
1555 * mbstowcs (CRTDLL.429)
1556 * FIXME: check multibyte support
1558 INT __cdecl CRTDLL_mbstowcs(LPWSTR wcs, LPCSTR mbs, INT size)
1561 /* Slightly modified lstrcpynAtoW functions from memory/strings.c
1562 * We need the number of characters transfered
1563 * FIXME: No multibyte support yet
1566 LPWSTR p = wcs;
1567 LPCSTR src= mbs;
1568 int ret, n=size;
1570 while ((n-- > 0) && *src) {
1571 *p++ = (WCHAR)(unsigned char)*src++;
1573 p++;
1574 ret = (p -wcs);
1576 TRACE(crtdll,"CRTDLL_mbstowcs %s for %d chars put %d wchars\n",
1577 mbs,size,ret);
1578 return ret;
1581 /*********************************************************************
1582 * mbtowc (CRTDLL.430)
1583 * FIXME: check multibyte support
1585 WCHAR __cdecl CRTDLL_mbtowc(WCHAR* wc,CHAR* mb,INT size)
1587 int ret;
1589 if (!mb)
1590 ret = 0;
1591 else if (!wc)
1592 ret =-1;
1593 else
1594 if ( (ret = mblen(mb,size)) != -1 )
1596 if (ret <= sizeof(char))
1597 *wc = (WCHAR) ((unsigned char)*mb);
1598 else
1599 ret= -1;
1601 else
1602 ret = -1;
1604 TRACE(crtdll,"CRTDLL_mbtowc %s for %d chars\n",mb,size);
1606 return ret;
1609 /*********************************************************************
1610 * _isctype (CRTDLL.138)
1612 BOOL __cdecl CRTDLL__isctype(CHAR x,CHAR type)
1614 if ((type & CRTDLL_SPACE) && isspace(x))
1615 return TRUE;
1616 if ((type & CRTDLL_PUNCT) && ispunct(x))
1617 return TRUE;
1618 if ((type & CRTDLL_LOWER) && islower(x))
1619 return TRUE;
1620 if ((type & CRTDLL_UPPER) && isupper(x))
1621 return TRUE;
1622 if ((type & CRTDLL_ALPHA) && isalpha(x))
1623 return TRUE;
1624 if ((type & CRTDLL_DIGIT) && isdigit(x))
1625 return TRUE;
1626 if ((type & CRTDLL_CONTROL) && iscntrl(x))
1627 return TRUE;
1628 /* check CRTDLL_LEADBYTE */
1629 return FALSE;
1632 /*********************************************************************
1633 * _chdrive (CRTDLL.52)
1635 * newdir [I] drive to change to, A=1
1638 BOOL __cdecl CRTDLL__chdrive(INT newdrive)
1640 /* FIXME: generates errnos */
1641 return DRIVE_SetCurrentDrive(newdrive-1);
1644 /*********************************************************************
1645 * _chdir (CRTDLL.51)
1647 INT __cdecl CRTDLL__chdir(LPCSTR newdir)
1649 if (!SetCurrentDirectoryA(newdir))
1650 return 1;
1651 return 0;
1654 /*********************************************************************
1655 * _fullpath (CRTDLL.114)
1657 LPSTR __cdecl CRTDLL__fullpath(LPSTR buf, LPCSTR name, INT size)
1659 DOS_FULL_NAME full_name;
1661 if (!buf)
1663 size = 256;
1664 if(!(buf = CRTDLL_malloc(size))) return NULL;
1666 if (!DOSFS_GetFullName( name, FALSE, &full_name )) return NULL;
1667 lstrcpynA(buf,full_name.short_name,size);
1668 TRACE(crtdll,"CRTDLL_fullpath got %s\n",buf);
1669 return buf;
1672 /*********************************************************************
1673 * _splitpath (CRTDLL.279)
1675 VOID __cdecl CRTDLL__splitpath(LPCSTR path, LPSTR drive, LPSTR directory, LPSTR filename, LPSTR extension )
1677 /* drive includes :
1678 directory includes leading and trailing (forward and backward slashes)
1679 filename without dot and slashes
1680 extension with leading dot
1682 char * drivechar,*dirchar,*namechar;
1684 TRACE(crtdll,"CRTDLL__splitpath got %s\n",path);
1686 drivechar = strchr(path,':');
1687 dirchar = strrchr(path,'/');
1688 namechar = strrchr(path,'\\');
1689 dirchar = MAX(dirchar,namechar);
1690 if (dirchar)
1691 namechar = strrchr(dirchar,'.');
1692 else
1693 namechar = strrchr(path,'.');
1696 if (drive)
1698 *drive = 0x00;
1699 if (drivechar)
1701 strncat(drive,path,drivechar-path+1);
1702 path = drivechar+1;
1705 if (directory)
1707 *directory = 0x00;
1708 if (dirchar)
1710 strncat(directory,path,dirchar-path+1);
1711 path = dirchar+1;
1714 if (filename)
1716 *filename = 0x00;
1717 if (namechar)
1719 strncat(filename,path,namechar-path);
1720 if (extension)
1722 *extension = 0x00;
1723 strcat(extension,namechar);
1728 TRACE(crtdll,"CRTDLL__splitpath found %s %s %s %s\n",drive,directory,filename,extension);
1733 /*********************************************************************
1734 * _makepath (CRTDLL.182)
1737 VOID __cdecl CRTDLL__makepath(LPSTR path, LPCSTR drive,
1738 LPCSTR directory, LPCSTR filename,
1739 LPCSTR extension )
1741 char ch;
1742 TRACE(crtdll,"CRTDLL__makepath got %s %s %s %s\n", drive, directory,
1743 filename, extension);
1745 if ( !path )
1746 return;
1748 path[0] = 0;
1749 if ( drive )
1750 if ( drive[0] ) {
1751 sprintf(path, "%c:", drive[0]);
1753 if ( directory )
1754 if ( directory[0] ) {
1755 strcat(path, directory);
1756 ch = path[strlen(path)-1];
1757 if (ch != '/' && ch != '\\')
1758 strcat(path,"\\");
1760 if ( filename )
1761 if ( filename[0] ) {
1762 strcat(path, filename);
1763 if ( extension ) {
1764 if ( extension[0] ) {
1765 if ( extension[0] != '.' ) {
1766 strcat(path,".");
1768 strcat(path,extension);
1773 TRACE(crtdll,"CRTDLL__makepath returns %s\n",path);
1776 /*********************************************************************
1777 * _getcwd (CRTDLL.120)
1779 CHAR* __cdecl CRTDLL__getcwd(LPSTR buf, INT size)
1781 char test[1];
1782 int len;
1784 len = size;
1785 if (!buf) {
1786 if (size < 0) /* allocate as big as nescessary */
1787 len =GetCurrentDirectoryA(1,test) + 1;
1788 if(!(buf = CRTDLL_malloc(len)))
1790 /* set error to OutOfRange */
1791 return( NULL );
1794 size = len;
1795 if(!(len =GetCurrentDirectoryA(len,buf)))
1797 return NULL;
1799 if (len > size)
1801 /* set error to ERANGE */
1802 TRACE(crtdll,"CRTDLL_getcwd buffer to small\n");
1803 return NULL;
1805 return buf;
1809 /*********************************************************************
1810 * _getdcwd (CRTDLL.121)
1812 CHAR* __cdecl CRTDLL__getdcwd(INT drive,LPSTR buf, INT size)
1814 char test[1];
1815 int len;
1817 FIXME(crtdll,"(\"%c:\",%s,%d)\n",drive+'A',buf,size);
1818 len = size;
1819 if (!buf) {
1820 if (size < 0) /* allocate as big as nescessary */
1821 len =GetCurrentDirectoryA(1,test) + 1;
1822 if(!(buf = CRTDLL_malloc(len)))
1824 /* set error to OutOfRange */
1825 return( NULL );
1828 size = len;
1829 if(!(len =GetCurrentDirectoryA(len,buf)))
1831 return NULL;
1833 if (len > size)
1835 /* set error to ERANGE */
1836 TRACE(crtdll,"buffer to small\n");
1837 return NULL;
1839 return buf;
1843 /*********************************************************************
1844 * _getdrive (CRTDLL.124)
1846 * Return current drive, 1 for A, 2 for B
1848 INT __cdecl CRTDLL__getdrive(VOID)
1850 return DRIVE_GetCurrentDrive() + 1;
1853 /*********************************************************************
1854 * _mkdir (CRTDLL.234)
1856 INT __cdecl CRTDLL__mkdir(LPCSTR newdir)
1858 if (!CreateDirectoryA(newdir,NULL))
1859 return -1;
1860 return 0;
1863 /*********************************************************************
1864 * remove (CRTDLL.448)
1866 INT __cdecl CRTDLL_remove(LPCSTR file)
1868 if (!DeleteFileA(file))
1869 return -1;
1870 return 0;
1873 /*********************************************************************
1874 * _errno (CRTDLL.52)
1875 * Yes, this is a function.
1877 LPINT __cdecl CRTDLL__errno()
1879 static int crtdllerrno;
1881 /* FIXME: we should set the error at the failing function call time */
1882 crtdllerrno = LastErrorToErrno(GetLastError());
1883 return &crtdllerrno;
1886 /*********************************************************************
1887 * _tempnam (CRTDLL.305)
1890 LPSTR __cdecl CRTDLL__tempnam(LPCSTR dir, LPCSTR prefix)
1893 char *ret;
1894 DOS_FULL_NAME tempname;
1896 if ((ret = tempnam(dir,prefix))==NULL) {
1897 WARN(crtdll, "Unable to get unique filename\n");
1898 return NULL;
1900 if (!DOSFS_GetFullName(ret,FALSE,&tempname))
1902 TRACE(crtdll, "Wrong path?\n");
1903 return NULL;
1905 free(ret);
1906 if ((ret = CRTDLL_malloc(strlen(tempname.short_name)+1)) == NULL) {
1907 WARN(crtdll, "CRTDL_malloc for shortname failed\n");
1908 return NULL;
1910 if ((ret = strcpy(ret,tempname.short_name)) == NULL) {
1911 WARN(crtdll, "Malloc for shortname failed\n");
1912 return NULL;
1915 TRACE(crtdll,"dir %s prefix %s got %s\n",
1916 dir,prefix,ret);
1917 return ret;
1920 /*********************************************************************
1921 * tmpnam (CRTDLL.490)
1923 * lcclnk from lcc-win32 relies on a terminating dot in the name returned
1926 LPSTR __cdecl CRTDLL_tmpnam(LPSTR s)
1928 char *ret;
1930 if ((ret =tmpnam(s))== NULL) {
1931 WARN(crtdll, "Unable to get unique filename\n");
1932 return NULL;
1934 if (!DOSFS_GetFullName(ret,FALSE,&CRTDLL_tmpname))
1936 TRACE(crtdll, "Wrong path?\n");
1937 return NULL;
1939 strcat(CRTDLL_tmpname.short_name,".");
1940 TRACE(crtdll,"for buf %p got %s\n",
1941 s,CRTDLL_tmpname.short_name);
1942 TRACE(crtdll,"long got %s\n",
1943 CRTDLL_tmpname.long_name);
1944 if ( s != NULL)
1945 return strcpy(s,CRTDLL_tmpname.short_name);
1946 else
1947 return CRTDLL_tmpname.short_name;
1951 /*********************************************************************
1952 * _itoa (CRTDLL.165)
1954 LPSTR __cdecl CRTDLL__itoa(INT x,LPSTR buf,INT buflen)
1956 wsnprintfA(buf,buflen,"%d",x);
1957 return buf;
1960 /*********************************************************************
1961 * _ltoa (CRTDLL.180)
1963 LPSTR __cdecl CRTDLL__ltoa(long x,LPSTR buf,INT radix)
1965 switch(radix) {
1966 case 2: FIXME(crtdll, "binary format not implemented !\n");
1967 break;
1968 case 8: wsnprintfA(buf,0x80,"%o",x);
1969 break;
1970 case 10: wsnprintfA(buf,0x80,"%d",x);
1971 break;
1972 case 16: wsnprintfA(buf,0x80,"%x",x);
1973 break;
1974 default: FIXME(crtdll, "radix %d not implemented !\n", radix);
1976 return buf;
1979 /*********************************************************************
1980 * _ultoa (CRTDLL.311)
1982 LPSTR __cdecl CRTDLL__ultoa(long x,LPSTR buf,INT radix)
1984 switch(radix) {
1985 case 2: FIXME(crtdll, "binary format not implemented !\n");
1986 break;
1987 case 8: wsnprintfA(buf,0x80,"%lo",x);
1988 break;
1989 case 10: wsnprintfA(buf,0x80,"%ld",x);
1990 break;
1991 case 16: wsnprintfA(buf,0x80,"%lx",x);
1992 break;
1993 default: FIXME(crtdll, "radix %d not implemented !\n", radix);
1995 return buf;
1998 typedef VOID (*sig_handler_type)(VOID);
2000 /*********************************************************************
2001 * signal (CRTDLL.455)
2003 VOID __cdecl CRTDLL_signal(int sig, sig_handler_type ptr)
2005 FIXME(crtdll, "(%d %p):stub.\n", sig, ptr);
2008 /*********************************************************************
2009 * _ftol (CRTDLL.113)
2011 #ifdef USING_REAL_FPU
2012 LONG __cdecl CRTDLL__ftol(void) {
2013 /* don't just do DO_FPU("fistp",retval), because the rounding
2014 * mode must also be set to "round towards zero"... */
2015 double fl;
2016 POP_FPU(fl);
2017 return (LONG)fl;
2019 #else
2020 LONG __cdecl CRTDLL__ftol(double fl) {
2021 FIXME(crtdll,"should be register function\n");
2022 return (LONG)fl;
2024 #endif
2026 /*********************************************************************
2027 * _CIpow (CRTDLL.14)
2029 #ifdef USING_REAL_FPU
2030 LONG __cdecl CRTDLL__CIpow(void) {
2031 double x,y;
2032 POP_FPU(y);
2033 POP_FPU(x);
2034 FIXME(crtdll,"(%f,%f): argument order unknown! Please report!\n",x,y);
2035 return pow(x,y);
2037 #else
2038 LONG __cdecl CRTDLL__CIpow(double x,double y) {
2039 FIXME(crtdll,"should be register function\n");
2040 return pow(x,y);
2042 #endif
2044 /*********************************************************************
2045 * _sleep (CRTDLL.267)
2047 VOID __cdecl CRTDLL__sleep(unsigned long timeout)
2049 TRACE(crtdll,"CRTDLL__sleep for %ld milliseconds\n",timeout);
2050 Sleep((timeout)?timeout:1);
2053 /*********************************************************************
2054 * getenv (CRTDLL.437)
2056 LPSTR __cdecl CRTDLL_getenv(const char *name)
2058 LPSTR environ = GetEnvironmentStringsA();
2059 LPSTR pp,pos = NULL;
2060 unsigned int length;
2062 for (pp = environ; (*pp); pp = pp + strlen(pp) +1)
2064 pos =strchr(pp,'=');
2065 if (pos)
2066 length = pos -pp;
2067 else
2068 length = strlen(pp);
2069 if (!strncmp(pp,name,length)) break;
2071 if ((pp)&& (pos))
2073 pp = pos+1;
2074 TRACE(crtdll,"got %s\n",pp);
2076 FreeEnvironmentStringsA( environ );
2077 return pp;
2080 /*********************************************************************
2081 * _mbsrchr (CRTDLL.223)
2083 LPSTR __cdecl CRTDLL__mbsrchr(LPSTR s,CHAR x) {
2084 /* FIXME: handle multibyte strings */
2085 return strrchr(s,x);
2088 /*********************************************************************
2089 * _memicmp (CRTDLL.233)(NTDLL.868)
2090 * A stringcompare, without \0 check
2091 * RETURNS
2092 * -1:if first string is alphabetically before second string
2093 * 1:if second '' '' '' '' first ''
2094 * 0:if both are equal.
2096 INT __cdecl CRTDLL__memicmp(
2097 LPCSTR s1, /* [in] first string */
2098 LPCSTR s2, /* [in] second string */
2099 DWORD len /* [in] length to compare */
2100 ) {
2101 int i;
2103 for (i=0;i<len;i++) {
2104 if (tolower(s1[i])<tolower(s2[i]))
2105 return -1;
2106 if (tolower(s1[i])>tolower(s2[i]))
2107 return 1;
2109 return 0;
2111 /*********************************************************************
2112 * __dllonexit (CRTDLL.25)
2114 VOID __cdecl CRTDLL__dllonexit ()
2116 FIXME(crtdll,"stub\n");
2119 /*********************************************************************
2120 * wcstok (CRTDLL.519)
2121 * Like strtok, but for wide character strings. s is modified, yes.
2123 LPWSTR __cdecl CRTDLL_wcstok(LPWSTR s,LPCWSTR delim) {
2124 static LPWSTR nexttok = NULL;
2125 LPWSTR x,ret;
2127 if (!s)
2128 s = nexttok;
2129 if (!s)
2130 return NULL;
2131 x = s;
2132 while (*x && !CRTDLL_wcschr(delim,*x))
2133 x++;
2134 ret = nexttok;
2135 if (*x) {
2136 *x='\0';
2137 nexttok = x+1;
2138 } else
2139 nexttok = NULL;
2140 return ret;
2143 /*********************************************************************
2144 * wcstol (CRTDLL.520)
2145 * Like strtol, but for wide character strings.
2147 INT __cdecl CRTDLL_wcstol(LPWSTR s,LPWSTR *end,INT base) {
2148 LPSTR sA = HEAP_strdupWtoA(GetProcessHeap(),0,s),endA;
2149 INT ret = strtol(sA,&endA,base);
2151 HeapFree(GetProcessHeap(),0,sA);
2152 if (end) *end = s+(endA-sA); /* pointer magic checked. */
2153 return ret;
2155 /*********************************************************************
2156 * strdate (CRTDLL.283)
2158 LPSTR __cdecl CRTDLL__strdate (LPSTR date)
2159 { FIXME (crtdll,"%p stub\n", date);
2160 return 0;
2163 /*********************************************************************
2164 * strtime (CRTDLL.299)
2166 LPSTR __cdecl CRTDLL__strtime (LPSTR date)
2167 { FIXME (crtdll,"%p stub\n", date);
2168 return 0;