Hacked stdio functions to use Win32 file handles. Still needs a proper
[wine/wine-kai.git] / misc / crtdll.c
blobeb3e40aacf18a342f5b1ef885fb2e3d27fb6227e
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 "win.h"
42 #include "windows.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 static DOS_FULL_NAME CRTDLL_tmpname;
56 UINT32 CRTDLL_argc_dll; /* CRTDLL.23 */
57 LPSTR *CRTDLL_argv_dll; /* CRTDLL.24 */
58 LPSTR CRTDLL_acmdln_dll; /* CRTDLL.38 */
59 UINT32 CRTDLL_basemajor_dll; /* CRTDLL.42 */
60 UINT32 CRTDLL_baseminor_dll; /* CRTDLL.43 */
61 UINT32 CRTDLL_baseversion_dll; /* CRTDLL.44 */
62 UINT32 CRTDLL_commode_dll; /* CRTDLL.59 */
63 LPSTR CRTDLL_environ_dll; /* CRTDLL.75 */
64 UINT32 CRTDLL_fmode_dll; /* CRTDLL.104 */
65 UINT32 CRTDLL_osmajor_dll; /* CRTDLL.241 */
66 UINT32 CRTDLL_osminor_dll; /* CRTDLL.242 */
67 UINT32 CRTDLL_osmode_dll; /* CRTDLL.243 */
68 UINT32 CRTDLL_osver_dll; /* CRTDLL.244 */
69 UINT32 CRTDLL_osversion_dll; /* CRTDLL.245 */
70 UINT32 CRTDLL_winmajor_dll; /* CRTDLL.329 */
71 UINT32 CRTDLL_winminor_dll; /* CRTDLL.330 */
72 UINT32 CRTDLL_winver_dll; /* CRTDLL.331 */
74 /* FIXME: structure layout is obviously not correct */
75 typedef struct
77 HANDLE32 handle;
78 int pad[7];
79 } CRTDLL_FILE;
81 CRTDLL_FILE CRTDLL_iob[3];
83 static CRTDLL_FILE * const CRTDLL_stdin = &CRTDLL_iob[0];
84 static CRTDLL_FILE * const CRTDLL_stdout = &CRTDLL_iob[1];
85 static CRTDLL_FILE * const CRTDLL_stderr = &CRTDLL_iob[2];
87 typedef VOID (*new_handler_type)(VOID);
89 static new_handler_type new_handler;
91 /*********************************************************************
92 * _GetMainArgs (CRTDLL.022)
94 DWORD __cdecl CRTDLL__GetMainArgs(LPDWORD argc,LPSTR **argv,
95 LPSTR *environ,DWORD flag)
97 char *cmdline;
98 char **xargv;
99 int xargc,i,afterlastspace;
100 DWORD version;
102 TRACE(crtdll,"(%p,%p,%p,%ld).\n",
103 argc,argv,environ,flag
105 CRTDLL_acmdln_dll = cmdline = HEAP_strdupA( GetProcessHeap(), 0,
106 GetCommandLine32A() );
107 TRACE(crtdll,"got '%s'\n", cmdline);
109 version = GetVersion32();
110 CRTDLL_osver_dll = version >> 16;
111 CRTDLL_winminor_dll = version & 0xFF;
112 CRTDLL_winmajor_dll = (version>>8) & 0xFF;
113 CRTDLL_baseversion_dll = version >> 16;
114 CRTDLL_winver_dll = ((version >> 8) & 0xFF) + ((version & 0xFF) << 8);
115 CRTDLL_baseminor_dll = (version >> 16) & 0xFF;
116 CRTDLL_basemajor_dll = (version >> 24) & 0xFF;
117 CRTDLL_osversion_dll = version & 0xFFFF;
118 CRTDLL_osminor_dll = version & 0xFF;
119 CRTDLL_osmajor_dll = (version>>8) & 0xFF;
121 /* missing threading init */
123 i=0;xargv=NULL;xargc=0;afterlastspace=0;
124 while (cmdline[i]) {
125 if (cmdline[i]==' ') {
126 xargv=(char**)HeapReAlloc( GetProcessHeap(), 0, xargv,
127 sizeof(char*)*(++xargc));
128 cmdline[i]='\0';
129 xargv[xargc-1] = HEAP_strdupA( GetProcessHeap(), 0,
130 cmdline+afterlastspace);
131 i++;
132 while (cmdline[i]==' ')
133 i++;
134 if (cmdline[i])
135 afterlastspace=i;
136 } else
137 i++;
139 xargv=(char**)HeapReAlloc( GetProcessHeap(), 0, xargv,
140 sizeof(char*)*(++xargc));
141 cmdline[i]='\0';
142 xargv[xargc-1] = HEAP_strdupA( GetProcessHeap(), 0,
143 cmdline+afterlastspace);
144 CRTDLL_argc_dll = xargc;
145 *argc = xargc;
146 CRTDLL_argv_dll = xargv;
147 *argv = xargv;
149 TRACE(crtdll,"found %d arguments\n",
150 CRTDLL_argc_dll);
151 CRTDLL_environ_dll = *environ = GetEnvironmentStrings32A();
152 return 0;
156 typedef void (*_INITTERMFUN)();
158 /* fixme: move to header */
159 struct find_t
160 { unsigned attrib;
161 time_t time_create; /* -1 when not avaiable */
162 time_t time_access; /* -1 when not avaiable */
163 time_t time_write;
164 unsigned long size; /* fixme: 64 bit ??*/
165 char name[260];
167 /*********************************************************************
168 * _findfirst (CRTDLL.099)
170 * BUGS
171 * Unimplemented
173 DWORD __cdecl CRTDLL__findfirst(LPCSTR fname, struct find_t * x2)
175 FIXME(crtdll, ":(%s,%p): stub\n",fname,x2);
176 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
177 return FALSE;
180 /*********************************************************************
181 * _findnext (CRTDLL.100)
183 * BUGS
184 * Unimplemented
186 INT32 __cdecl CRTDLL__findnext(DWORD hand, struct find_t * x2)
188 FIXME(crtdll, ":(%ld,%p): stub\n",hand,x2);
189 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
190 return FALSE;
193 /*********************************************************************
194 * _fstat (CRTDLL.111)
196 * BUGS
197 * Unimplemented
199 int __cdecl CRTDLL__fstat(int file, struct stat* buf)
201 FIXME(crtdll, ":(%d,%p): stub\n",file,buf);
202 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
203 return FALSE;
206 /*********************************************************************
207 * _initterm (CRTDLL.135)
209 DWORD __cdecl CRTDLL__initterm(_INITTERMFUN *start,_INITTERMFUN *end)
211 _INITTERMFUN *current;
213 TRACE(crtdll,"(%p,%p)\n",start,end);
214 current=start;
215 while (current<end) {
216 if (*current) (*current)();
217 current++;
219 return 0;
222 /*********************************************************************
223 * _fdopen (CRTDLL.91)
225 CRTDLL_FILE * __cdecl CRTDLL__fdopen(INT32 handle, LPCSTR mode)
227 CRTDLL_FILE *file;
229 switch (handle)
231 case 0:
232 file = CRTDLL_stdin;
233 if (!file->handle) file->handle = GetStdHandle( STD_INPUT_HANDLE );
234 break;
235 case 1:
236 file = CRTDLL_stdout;
237 if (!file->handle) file->handle = GetStdHandle( STD_OUTPUT_HANDLE );
238 break;
239 case 2:
240 file=CRTDLL_stderr;
241 if (!file->handle) file->handle = GetStdHandle( STD_ERROR_HANDLE );
242 break;
243 default:
244 file = HeapAlloc( GetProcessHeap(), 0, sizeof(*file) );
245 file->handle = handle;
246 break;
248 TRACE(crtdll, "open handle %d mode %s got file %p\n",
249 handle, mode, file);
250 return file;
254 /*******************************************************************
255 * _global_unwind2 (CRTDLL.129)
257 void __cdecl CRTDLL__global_unwind2( PEXCEPTION_FRAME frame )
259 RtlUnwind( frame, 0, NULL, 0 );
262 /*******************************************************************
263 * _local_unwind2 (CRTDLL.173)
265 void __cdecl CRTDLL__local_unwind2( PEXCEPTION_FRAME endframe, DWORD nr )
267 TRACE(crtdll,"(%p,%ld)\n",endframe,nr);
269 /*********************************************************************
270 * _read (CRTDLL.256)
272 * BUGS
273 * Unimplemented
275 INT32 __cdecl CRTDLL__read(INT32 fd, LPVOID buf, UINT32 count)
277 FIXME(crtdll,":(%d,%p,%d): stub\n",fd,buf,count);
278 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
279 return FALSE;
282 /*********************************************************************
283 * fopen (CRTDLL.372)
285 CRTDLL_FILE * __cdecl CRTDLL_fopen(LPCSTR path, LPCSTR mode)
287 CRTDLL_FILE *file = NULL;
288 HFILE32 handle;
289 #if 0
290 DOS_FULL_NAME full_name;
292 if (!DOSFS_GetFullName( path, FALSE, &full_name )) {
293 WARN(crtdll, "file %s bad name\n",path);
294 return 0;
297 file=fopen(full_name.long_name ,mode);
298 #endif
299 INT32 flagmode=0;
301 if ((strchr(mode,'r')&&strchr(mode,'a'))||
302 (strchr(mode,'r')&&strchr(mode,'w'))||
303 (strchr(mode,'w')&&strchr(mode,'a')))
304 return NULL;
306 if (strstr(mode,"r+")) flagmode=O_RDWR;
307 else if (strchr(mode,'r')) flagmode = O_RDONLY;
308 else if (strstr(mode,"w+")) flagmode= O_RDWR | O_TRUNC | O_CREAT;
309 else if (strchr(mode,'w')) flagmode = O_WRONLY | O_TRUNC | O_CREAT;
310 else if (strstr(mode,"a+")) flagmode= O_RDWR | O_CREAT | O_APPEND;
311 else if (strchr(mode,'w')) flagmode = O_RDWR | O_CREAT | O_APPEND;
312 else if (strchr(mode,'b'))
313 TRACE(crtdll, "%s in BINARY mode\n",path);
315 if ((handle = FILE_Open(path, flagmode,0)) != INVALID_HANDLE_VALUE32)
317 file = HeapAlloc( GetProcessHeap(), 0, sizeof(*file) );
318 file->handle = handle;
320 TRACE(crtdll, "file %s mode %s got handle %d file %p\n",
321 path,mode,handle,file);
322 return file;
325 /*********************************************************************
326 * fread (CRTDLL.377)
328 DWORD __cdecl CRTDLL_fread(LPVOID ptr, INT32 size, INT32 nmemb, CRTDLL_FILE *file)
330 #if 0
331 int i=0;
332 void *temp=ptr;
334 /* If we would honour CR/LF <-> LF translation, we could do it like this.
335 We should keep track of all files opened, and probably files with \
336 known binary extensions must be unchanged */
337 while ( (i < (nmemb*size)) && (ret==1)) {
338 ret=fread(temp,1,1,file);
339 TRACE(crtdll, "got %c 0x%02x ret %d\n",
340 (isalpha(*(unsigned char*)temp))? *(unsigned char*)temp:
341 ' ',*(unsigned char*)temp, ret);
342 if (*(unsigned char*)temp != 0xd) { /* skip CR */
343 temp++;
344 i++;
346 else
347 TRACE(crtdll, "skipping ^M\n");
349 TRACE(crtdll, "0x%08x items of size %d from file %p to %p\n",
350 nmemb,size,file,ptr,);
351 if(i!=nmemb)
352 WARN(crtdll, " failed!\n");
354 return i;
355 #else
356 DWORD ret;
358 TRACE(crtdll, "0x%08x items of size %d from file %p to %p\n",
359 nmemb,size,file,ptr);
360 if (!ReadFile( file->handle, ptr, size * nmemb, &ret, NULL ))
361 WARN(crtdll, " failed!\n");
363 return ret / size;
364 #endif
366 /*********************************************************************
367 * freopen (CRTDLL.379)
369 * BUGS
370 * Unimplemented
372 DWORD __cdecl CRTDLL_freopen(LPCSTR path, LPCSTR mode, LPVOID stream)
374 FIXME(crtdll, ":(%s,%s,%p): stub\n", path, mode, stream);
375 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
376 return FALSE;
379 /*********************************************************************
380 * fscanf (CRTDLL.381)
382 INT32 __cdecl CRTDLL_fscanf( CRTDLL_FILE *stream, LPSTR format, ... )
384 #if 0
385 va_list valist;
386 INT32 res;
388 va_start( valist, format );
389 #ifdef HAVE_VFSCANF
390 res = vfscanf( xlat_file_ptr(stream), format, valist );
391 #endif
392 va_end( valist );
393 return res;
394 #endif
395 FIXME(crtdll,"broken\n");
396 return 0;
399 /*********************************************************************
400 * fseek (CRTDLL.382)
402 LONG __cdecl CRTDLL_fseek( CRTDLL_FILE *file, LONG offset, INT32 whence)
404 TRACE(crtdll, "file %p to 0x%08lx pos %s\n",
405 file,offset,(whence==SEEK_SET)?"SEEK_SET":
406 (whence==SEEK_CUR)?"SEEK_CUR":
407 (whence==SEEK_END)?"SEEK_END":"UNKNOWN");
408 if (SetFilePointer( file->handle, offset, NULL, whence ) != 0xffffffff)
409 return 0;
410 WARN(crtdll, " failed!\n");
411 return -1;
414 /*********************************************************************
415 * fsetpos (CRTDLL.383)
417 INT32 __cdecl CRTDLL_fsetpos( CRTDLL_FILE *file, INT32 *pos )
419 TRACE(crtdll, "file %p pos %d\n", file, *pos );
420 return CRTDLL_fseek(file, *pos, SEEK_SET);
423 /*********************************************************************
424 * ftell (CRTDLL.384)
426 LONG __cdecl CRTDLL_ftell( CRTDLL_FILE *file )
428 return SetFilePointer( file->handle, 0, NULL, SEEK_CUR );
431 /*********************************************************************
432 * fwrite (CRTDLL.386)
434 DWORD __cdecl CRTDLL_fwrite( LPVOID ptr, INT32 size, INT32 nmemb, CRTDLL_FILE *file )
436 DWORD ret;
438 TRACE(crtdll, "0x%08x items of size %d to file %p from %p\n",
439 nmemb,size,file,ptr);
440 if (!WriteFile( file->handle, ptr, size * nmemb, &ret, NULL ))
441 WARN(crtdll, " failed!\n");
442 return ret / size;
445 /*********************************************************************
446 * setbuf (CRTDLL.452)
448 INT32 __cdecl CRTDLL_setbuf(CRTDLL_FILE *file, LPSTR buf)
450 TRACE(crtdll, "(file %p buf %p)\n", file, buf);
451 /* this doesn't work:"void value not ignored as it ought to be"
452 return setbuf(file,buf);
454 /* FIXME: no buffering for now */
455 return 0;
458 /*********************************************************************
459 * _open_osfhandle (CRTDLL.240)
461 HFILE32 __cdecl CRTDLL__open_osfhandle(LONG osfhandle, INT32 flags)
463 HFILE32 handle;
465 switch (osfhandle) {
466 case STD_INPUT_HANDLE :
467 case 0 :
468 handle=0;
469 break;
470 case STD_OUTPUT_HANDLE:
471 case 1:
472 handle=1;
473 break;
474 case STD_ERROR_HANDLE:
475 case 2:
476 handle=2;
477 break;
478 default:
479 return (-1);
481 TRACE(crtdll, "(handle %08lx,flags %d) return %d\n",
482 osfhandle,flags,handle);
483 return handle;
487 /*********************************************************************
488 * srand (CRTDLL.460)
490 void __cdecl CRTDLL_srand(DWORD seed)
492 /* FIXME: should of course be thread? process? local */
493 srand(seed);
496 /*********************************************************************
497 * vfprintf (CRTDLL.373)
499 INT32 __cdecl CRTDLL_vfprintf( CRTDLL_FILE *file, LPSTR format, va_list args )
501 char buffer[1024]; /* FIXME... */
503 vsprintf( buffer, format, args );
504 return CRTDLL_fwrite( buffer, 1, strlen(buffer), file );
507 /*********************************************************************
508 * fprintf (CRTDLL.373)
510 INT32 __cdecl CRTDLL_fprintf( CRTDLL_FILE *file, LPSTR format, ... )
512 va_list valist;
513 INT32 res;
515 va_start( valist, format );
516 res = CRTDLL_vfprintf( file, format, valist );
517 va_end( valist );
518 return res;
521 /*********************************************************************
522 * time (CRTDLL.488)
524 time_t __cdecl CRTDLL_time(time_t *timeptr)
526 time_t curtime = time(NULL);
528 if (timeptr)
529 *timeptr = curtime;
530 return curtime;
533 /*********************************************************************
534 * (CRTDLL.350)
536 clock_t __cdecl CRTDLL_clock(void)
538 struct tms alltimes;
539 clock_t res;
541 times(&alltimes);
542 res = alltimes.tms_utime + alltimes.tms_stime+
543 alltimes.tms_cutime + alltimes.tms_cstime;
544 /* Fixme: We need some symbolic representation
545 for (Hostsystem_)CLOCKS_PER_SEC
546 and (Emulated_system_)CLOCKS_PER_SEC
547 10 holds only for Windows/Linux_i86)
549 return 10*res;
552 /*********************************************************************
553 * _isatty (CRTDLL.137)
555 BOOL32 __cdecl CRTDLL__isatty(DWORD x)
557 TRACE(crtdll,"(%ld)\n",x);
558 return TRUE;
561 /*********************************************************************
562 * _write (CRTDLL.332)
564 INT32 __cdecl CRTDLL__write(INT32 fd,LPCVOID buf,UINT32 count)
566 INT32 len=0;
568 if (fd == -1)
569 len = -1;
570 else if (fd<=2)
571 len = (UINT32)write(fd,buf,(LONG)count);
572 else
573 len = _lwrite32(fd,buf,count);
574 TRACE(crtdll,"%d/%d byte to dfh %d from %p,\n",
575 len,count,fd,buf);
576 return len;
580 /*********************************************************************
581 * _cexit (CRTDLL.49)
583 * FIXME: What the heck is the difference between
584 * FIXME _c_exit (CRTDLL.47)
585 * FIXME _cexit (CRTDLL.49)
586 * FIXME _exit (CRTDLL.87)
587 * FIXME exit (CRTDLL.359)
589 * atexit-processing comes to mind -- MW.
592 void __cdecl CRTDLL__cexit(INT32 ret)
594 TRACE(crtdll,"(%d)\n",ret);
595 ExitProcess(ret);
599 /*********************************************************************
600 * exit (CRTDLL.359)
602 void __cdecl CRTDLL_exit(DWORD ret)
604 TRACE(crtdll,"(%ld)\n",ret);
605 ExitProcess(ret);
609 /*********************************************************************
610 * _abnormal_termination (CRTDLL.36)
612 INT32 __cdecl CRTDLL__abnormal_termination(void)
614 TRACE(crtdll,"(void)\n");
615 return 0;
619 /*********************************************************************
620 * _access (CRTDLL.37)
622 INT32 __cdecl CRTDLL__access(LPCSTR filename, INT32 mode)
624 DWORD attr = GetFileAttributes32A(filename);
626 if (attr == -1)
628 if (GetLastError() == ERROR_INVALID_ACCESS)
629 errno = EACCES;
630 else
631 errno = ENOENT;
632 return -1;
635 if ((attr & FILE_ATTRIBUTE_READONLY) && (mode & W_OK))
637 errno = EACCES;
638 return -1;
640 else
641 return 0;
645 /*********************************************************************
646 * fflush (CRTDLL.365)
648 INT32 __cdecl CRTDLL_fflush( CRTDLL_FILE *file )
650 return FlushFileBuffers( file->handle ) ? 0 : -1;
654 /*********************************************************************
655 * rand (CRTDLL.446)
657 INT32 __cdecl CRTDLL_rand()
659 return rand();
663 /*********************************************************************
664 * putchar (CRTDLL.442)
666 void __cdecl CRTDLL_putchar( INT32 x )
668 putchar(x);
672 /*********************************************************************
673 * fputc (CRTDLL.374)
675 INT32 __cdecl CRTDLL_fputc( INT32 c, CRTDLL_FILE *file )
677 char ch = (char)c;
678 DWORD res;
679 TRACE(crtdll, "%c to file %p\n",c,file);
680 if (!WriteFile( file->handle, &ch, 1, &res, NULL )) return -1;
681 return c;
685 /*********************************************************************
686 * fputs (CRTDLL.375)
688 INT32 __cdecl CRTDLL_fputs( LPCSTR s, CRTDLL_FILE *file )
690 DWORD res;
691 TRACE(crtdll, "%s to file %p\n",s,file);
692 if (!WriteFile( file->handle, s, strlen(s), &res, NULL )) return -1;
693 return res;
697 /*********************************************************************
698 * puts (CRTDLL.443)
700 INT32 __cdecl CRTDLL_puts(LPCSTR s)
702 TRACE(crtdll, "%s \n",s);
703 return puts(s);
707 /*********************************************************************
708 * putc (CRTDLL.441)
710 INT32 __cdecl CRTDLL_putc( INT32 c, CRTDLL_FILE *file )
712 return CRTDLL_fputc( c, file );
715 /*********************************************************************
716 * fgetc (CRTDLL.366)
718 INT32 __cdecl CRTDLL_fgetc( CRTDLL_FILE *file )
720 DWORD res;
721 char ch;
722 if (!ReadFile( file->handle, &ch, 1, &res, NULL )) return -1;
723 return ch;
727 /*********************************************************************
728 * getc (CRTDLL.388)
730 INT32 __cdecl CRTDLL_getc( CRTDLL_FILE *file )
732 return CRTDLL_fgetc( file );
736 /*********************************************************************
737 * fgets (CRTDLL.368)
739 CHAR* __cdecl CRTDLL_fgets( LPSTR s, INT32 size, CRTDLL_FILE *file )
741 int cc;
742 LPSTR buf_start = s;
744 /* BAD, for the whole WINE process blocks... just done this way to test
745 * windows95's ftp.exe.
748 for(cc = CRTDLL_fgetc(file); cc != EOF && cc != '\n'; cc = CRTDLL_fgetc(file))
749 if (cc != '\r')
751 if (--size <= 0) break;
752 *s++ = (char)cc;
755 *s = '\0';
757 TRACE(crtdll,"got '%s'\n", buf_start);
758 return buf_start;
762 /*********************************************************************
763 * gets (CRTDLL.391)
765 LPSTR __cdecl CRTDLL_gets(LPSTR buf)
767 int cc;
768 LPSTR buf_start = buf;
770 /* BAD, for the whole WINE process blocks... just done this way to test
771 * windows95's ftp.exe.
774 for(cc = fgetc(stdin); cc != EOF && cc != '\n'; cc = fgetc(stdin))
775 if(cc != '\r') *buf++ = (char)cc;
777 *buf = '\0';
779 TRACE(crtdll,"got '%s'\n", buf_start);
780 return buf_start;
784 /*********************************************************************
785 * _rotl (CRTDLL.259)
787 UINT32 __cdecl CRTDLL__rotl(UINT32 x,INT32 shift)
789 unsigned int ret = (x >> shift)|( x >>((sizeof(x))-shift));
791 TRACE(crtdll, "got 0x%08x rot %d ret 0x%08x\n",
792 x,shift,ret);
793 return ret;
796 /*********************************************************************
797 * _lrotl (CRTDLL.176)
799 DWORD __cdecl CRTDLL__lrotl(DWORD x,INT32 shift)
801 unsigned long ret = (x >> shift)|( x >>((sizeof(x))-shift));
803 TRACE(crtdll, "got 0x%08lx rot %d ret 0x%08lx\n",
804 x,shift,ret);
805 return ret;
810 /*********************************************************************
811 * _mbsicmp (CRTDLL.204)
813 int __cdecl CRTDLL__mbsicmp(unsigned char *x,unsigned char *y)
815 do {
816 if (!*x)
817 return !!*y;
818 if (!*y)
819 return !!*x;
820 /* FIXME: MBCS handling... */
821 if (*x!=*y)
822 return 1;
823 x++;
824 y++;
825 } while (1);
829 /*********************************************************************
830 * _mbsinc (CRTDLL.205)
832 unsigned char * __cdecl CRTDLL__mbsinc(unsigned char *x)
834 /* FIXME: mbcs */
835 return x++;
839 /*********************************************************************
840 * vsprintf (CRTDLL.500)
842 INT32 __cdecl CRTDLL_vsprintf( LPSTR buffer, LPCSTR spec, va_list args )
844 return wvsprintf32A( buffer, spec, args );
847 /*********************************************************************
848 * vswprintf (CRTDLL.501)
850 INT32 __cdecl CRTDLL_vswprintf( LPWSTR buffer, LPCWSTR spec, va_list args )
852 return wvsprintf32W( buffer, spec, args );
855 /*********************************************************************
856 * _mbscpy (CRTDLL.200)
858 unsigned char* __cdecl CRTDLL__mbscpy(unsigned char *x,unsigned char *y)
860 TRACE(crtdll,"CRTDLL_mbscpy %s and %s\n",x,y);
861 return strcpy(x,y);
865 /*********************************************************************
866 * _mbscat (CRTDLL.197)
868 unsigned char* __cdecl CRTDLL__mbscat(unsigned char *x,unsigned char *y)
870 return strcat(x,y);
874 /*********************************************************************
875 * _strcmpi (CRTDLL.282) (CRTDLL.287)
877 INT32 __cdecl CRTDLL__strcmpi( LPCSTR s1, LPCSTR s2 )
879 return lstrcmpi32A( s1, s2 );
883 /*********************************************************************
884 * _strnicmp (CRTDLL.293)
886 INT32 __cdecl CRTDLL__strnicmp( LPCSTR s1, LPCSTR s2, INT32 n )
888 return lstrncmpi32A( s1, s2, n );
892 /*********************************************************************
893 * _strlwr (CRTDLL.293)
895 * convert a string in place to lowercase
897 LPSTR __cdecl CRTDLL__strlwr(LPSTR x)
899 unsigned char *y =x;
901 TRACE(crtdll, "CRTDLL_strlwr got %s\n", x);
902 while (*y) {
903 if ((*y > 0x40) && (*y< 0x5b))
904 *y = *y + 0x20;
905 y++;
907 TRACE(crtdll, " returned %s\n", x);
909 return x;
912 /*********************************************************************
913 * system (CRTDLL.485)
915 INT32 __cdecl CRTDLL_system(LPSTR x)
917 #define SYSBUF_LENGTH 1500
918 char buffer[SYSBUF_LENGTH];
919 unsigned char *y = x;
920 unsigned char *bp;
921 int i;
923 sprintf( buffer, "%s \"", Options.argv0 );
924 bp = buffer + strlen(buffer);
925 i = strlen(buffer) + strlen(x) +2;
927 /* Calculate needed buffer size to prevent overflow. */
928 while (*y) {
929 if (*y =='\\') i++;
930 y++;
932 /* If buffer too short, exit. */
933 if (i > SYSBUF_LENGTH) {
934 TRACE(crtdll,"_system buffer to small\n");
935 return 127;
938 y =x;
940 while (*y) {
941 *bp = *y;
942 bp++; y++;
943 if (*(y-1) =='\\') *bp++ = '\\';
945 /* Remove spaces from end of string. */
946 while (*(y-1) == ' ') {
947 bp--;y--;
949 *bp++ = '"';
950 *bp = 0;
951 TRACE(crtdll, "_system got '%s', executing '%s'\n",x,buffer);
953 return system(buffer);
956 /*********************************************************************
957 * _strupr (CRTDLL.300)
959 LPSTR __cdecl CRTDLL__strupr(LPSTR x)
961 LPSTR y=x;
963 while (*y) {
964 *y=toupper(*y);
965 y++;
967 return x;
970 /*********************************************************************
971 * _wcsupr (CRTDLL.328)
973 LPWSTR __cdecl CRTDLL__wcsupr(LPWSTR x)
975 LPWSTR y=x;
977 while (*y) {
978 *y=towupper(*y);
979 y++;
981 return x;
984 /*********************************************************************
985 * _wcslwr (CRTDLL.323)
987 LPWSTR __cdecl CRTDLL__wcslwr(LPWSTR x)
989 LPWSTR y=x;
991 while (*y) {
992 *y=towlower(*y);
993 y++;
995 return x;
999 /*********************************************************************
1000 * longjmp (CRTDLL.426)
1002 VOID __cdecl CRTDLL_longjmp(jmp_buf env, int val)
1004 FIXME(crtdll,"CRTDLL_longjmp semistup, expect crash\n");
1005 return longjmp(env, val);
1008 /*********************************************************************
1009 * malloc (CRTDLL.427)
1011 VOID* __cdecl CRTDLL_malloc(DWORD size)
1013 return HeapAlloc(GetProcessHeap(),0,size);
1016 /*********************************************************************
1017 * new (CRTDLL.001)
1019 VOID* __cdecl CRTDLL_new(DWORD size)
1021 VOID* result;
1022 if(!(result = HeapAlloc(GetProcessHeap(),0,size)) && new_handler)
1023 (*new_handler)();
1024 return result;
1027 /*********************************************************************
1028 * set_new_handler(CRTDLL.003)
1030 new_handler_type __cdecl CRTDLL_set_new_handler(new_handler_type func)
1032 new_handler_type old_handler = new_handler;
1033 new_handler = func;
1034 return old_handler;
1037 /*********************************************************************
1038 * calloc (CRTDLL.350)
1040 VOID* __cdecl CRTDLL_calloc(DWORD size, DWORD count)
1042 return HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, size * count );
1045 /*********************************************************************
1046 * realloc (CRTDLL.447)
1048 VOID* __cdecl CRTDLL_realloc( VOID *ptr, DWORD size )
1050 return HeapReAlloc( GetProcessHeap(), 0, ptr, size );
1053 /*********************************************************************
1054 * free (CRTDLL.427)
1056 VOID __cdecl CRTDLL_free(LPVOID ptr)
1058 HeapFree(GetProcessHeap(),0,ptr);
1061 /*********************************************************************
1062 * delete (CRTDLL.002)
1064 VOID __cdecl CRTDLL_delete(VOID* ptr)
1066 HeapFree(GetProcessHeap(),0,ptr);
1069 /*********************************************************************
1070 * _strdup (CRTDLL.285)
1072 LPSTR __cdecl CRTDLL__strdup(LPCSTR ptr)
1074 return HEAP_strdupA(GetProcessHeap(),0,ptr);
1077 /*********************************************************************
1078 * _wcsdup (CRTDLL.320)
1080 LPWSTR __cdecl CRTDLL__wcsdup(LPCWSTR ptr)
1082 return HEAP_strdupW(GetProcessHeap(),0,ptr);
1085 /*********************************************************************
1086 * fclose (CRTDLL.362)
1088 INT32 __cdecl CRTDLL_fclose( CRTDLL_FILE *file )
1090 TRACE(crtdll,"%p\n", file );
1091 if (!CloseHandle( file->handle )) return -1;
1092 HeapFree( GetProcessHeap(), 0, file );
1093 return 0;
1096 /*********************************************************************
1097 * _unlink (CRTDLL.315)
1099 INT32 __cdecl CRTDLL__unlink(LPCSTR pathname)
1101 int ret=0;
1102 DOS_FULL_NAME full_name;
1104 if (!DOSFS_GetFullName( pathname, FALSE, &full_name )) {
1105 WARN(crtdll, "CRTDLL_unlink file %s bad name\n",pathname);
1106 return EOF;
1109 ret=unlink(full_name.long_name);
1110 TRACE(crtdll,"(%s unix %s)\n",
1111 pathname,full_name.long_name);
1112 if(ret)
1113 WARN(crtdll, " Failed!\n");
1115 return ret;
1118 /*********************************************************************
1119 * rename (CRTDLL.449)
1121 INT32 __cdecl CRTDLL_rename(LPCSTR oldpath,LPCSTR newpath)
1123 BOOL32 ok = MoveFileEx32A( oldpath, newpath, MOVEFILE_REPLACE_EXISTING );
1124 return ok ? 0 : -1;
1128 /*********************************************************************
1129 * _stat (CRTDLL.280)
1132 struct win_stat
1134 UINT16 win_st_dev;
1135 UINT16 win_st_ino;
1136 UINT16 win_st_mode;
1137 INT16 win_st_nlink;
1138 INT16 win_st_uid;
1139 INT16 win_st_gid;
1140 UINT32 win_st_rdev;
1141 INT32 win_st_size;
1142 INT32 win_st_atime;
1143 INT32 win_st_mtime;
1144 INT32 win_st_ctime;
1147 int __cdecl CRTDLL__stat(const char * filename, struct win_stat * buf)
1149 int ret=0;
1150 DOS_FULL_NAME full_name;
1151 struct stat mystat;
1153 if (!DOSFS_GetFullName( filename, TRUE, &full_name ))
1155 WARN(crtdll, "CRTDLL__stat filename %s bad name\n",filename);
1156 return -1;
1158 ret=stat(full_name.long_name,&mystat);
1159 TRACE(crtdll,"CRTDLL__stat %s\n", filename);
1160 if(ret)
1161 WARN(crtdll, " Failed!\n");
1163 /* FIXME: should check what Windows returns */
1165 buf->win_st_dev = mystat.st_dev;
1166 buf->win_st_ino = mystat.st_ino;
1167 buf->win_st_mode = mystat.st_mode;
1168 buf->win_st_nlink = mystat.st_nlink;
1169 buf->win_st_uid = mystat.st_uid;
1170 buf->win_st_gid = mystat.st_gid;
1171 buf->win_st_rdev = mystat.st_rdev;
1172 buf->win_st_size = mystat.st_size;
1173 buf->win_st_atime = mystat.st_atime;
1174 buf->win_st_mtime = mystat.st_mtime;
1175 buf->win_st_ctime = mystat.st_ctime;
1176 return ret;
1179 /*********************************************************************
1180 * _open (CRTDLL.239)
1182 HFILE32 __cdecl CRTDLL__open(LPCSTR path,INT32 flags)
1184 HFILE32 ret=0;
1185 int wineflags=0;
1187 /* FIXME:
1188 the flags in lcc's header differ from the ones in Linux, e.g.
1189 Linux: define O_APPEND 02000 (= 0x400)
1190 lcc: define _O_APPEND 0x0008
1191 so here a scheme to translate them
1192 Probably lcc is wrong here, but at least a hack to get is going
1194 wineflags = (flags & 3);
1195 if (flags & 0x0008 ) wineflags |= O_APPEND;
1196 if (flags & 0x0100 ) wineflags |= O_CREAT;
1197 if (flags & 0x0200 ) wineflags |= O_TRUNC;
1198 if (flags & 0x0400 ) wineflags |= O_EXCL;
1199 if (flags & 0xf0f4 )
1200 TRACE(crtdll,"CRTDLL_open file unsupported flags 0x%04x\n",flags);
1201 /* End Fixme */
1203 ret = FILE_Open(path,wineflags,0);
1204 TRACE(crtdll,"CRTDLL_open file %s mode 0x%04x (lccmode 0x%04x) got dfh %d\n",
1205 path,wineflags,flags,ret);
1206 return ret;
1209 /*********************************************************************
1210 * _close (CRTDLL.57)
1212 INT32 __cdecl CRTDLL__close(HFILE32 fd)
1214 int ret=_lclose32(fd);
1216 TRACE(crtdll,"(%d)\n",fd);
1217 if(ret)
1218 WARN(crtdll, " Failed!\n");
1220 return ret;
1223 /*********************************************************************
1224 * feof (CRTDLL.363)
1226 INT32 __cdecl CRTDLL_feof( CRTDLL_FILE *file )
1228 FIXME(crtdll,"stub\n" );
1229 return 0;
1232 /*********************************************************************
1233 * setlocale (CRTDLL.453)
1235 LPSTR __cdecl CRTDLL_setlocale(INT32 category,LPCSTR locale)
1237 LPSTR categorystr;
1239 switch (category) {
1240 case CRTDLL_LC_ALL: categorystr="LC_ALL";break;
1241 case CRTDLL_LC_COLLATE: categorystr="LC_COLLATE";break;
1242 case CRTDLL_LC_CTYPE: categorystr="LC_CTYPE";break;
1243 case CRTDLL_LC_MONETARY: categorystr="LC_MONETARY";break;
1244 case CRTDLL_LC_NUMERIC: categorystr="LC_NUMERIC";break;
1245 case CRTDLL_LC_TIME: categorystr="LC_TIME";break;
1246 default: categorystr = "UNKNOWN?";break;
1248 FIXME(crtdll,"(%s,%s),stub!\n",categorystr,locale);
1249 return "C";
1252 /*********************************************************************
1253 * wcscat (CRTDLL.503)
1255 LPWSTR __cdecl CRTDLL_wcscat( LPWSTR s1, LPCWSTR s2 )
1257 return lstrcat32W( s1, s2 );
1260 /*********************************************************************
1261 * wcschr (CRTDLL.504)
1263 LPWSTR __cdecl CRTDLL_wcschr(LPCWSTR str,WCHAR xchar)
1265 LPCWSTR s;
1267 s=str;
1268 do {
1269 if (*s==xchar)
1270 return (LPWSTR)s;
1271 } while (*s++);
1272 return NULL;
1275 /*********************************************************************
1276 * wcscmp (CRTDLL.505)
1278 INT32 __cdecl CRTDLL_wcscmp( LPCWSTR s1, LPCWSTR s2 )
1280 return lstrcmp32W( s1, s2 );
1283 /*********************************************************************
1284 * wcscpy (CRTDLL.507)
1286 LPWSTR __cdecl CRTDLL_wcscpy( LPWSTR s1, LPCWSTR s2 )
1288 return lstrcpy32W( s1, s2 );
1291 /*********************************************************************
1292 * wcscspn (CRTDLL.508)
1294 INT32 __cdecl CRTDLL_wcscspn(LPWSTR str,LPWSTR reject)
1296 LPWSTR s,t;
1298 s=str;
1299 do {
1300 t=reject;
1301 while (*t) { if (*t==*s) break;t++;}
1302 if (*t) break;
1303 s++;
1304 } while (*s);
1305 return s-str; /* nr of wchars */
1308 /*********************************************************************
1309 * wcslen (CRTDLL.510)
1311 INT32 __cdecl CRTDLL_wcslen( LPCWSTR s )
1313 return lstrlen32W( s );
1316 /*********************************************************************
1317 * wcsncat (CRTDLL.511)
1319 LPWSTR __cdecl CRTDLL_wcsncat( LPWSTR s1, LPCWSTR s2, INT32 n )
1321 return lstrcatn32W( s1, s2, n );
1324 /*********************************************************************
1325 * wcsncmp (CRTDLL.512)
1327 INT32 __cdecl CRTDLL_wcsncmp( LPCWSTR s1, LPCWSTR s2, INT32 n )
1329 return lstrncmp32W( s1, s2, n );
1332 /*********************************************************************
1333 * wcsncpy (CRTDLL.513)
1335 LPWSTR __cdecl CRTDLL_wcsncpy( LPWSTR s1, LPCWSTR s2, INT32 n )
1337 return lstrcpyn32W( s1, s2, n );
1340 /*********************************************************************
1341 * wcsspn (CRTDLL.516)
1343 INT32 __cdecl CRTDLL_wcsspn(LPWSTR str,LPWSTR accept)
1345 LPWSTR s,t;
1347 s=str;
1348 do {
1349 t=accept;
1350 while (*t) { if (*t==*s) break;t++;}
1351 if (!*t) break;
1352 s++;
1353 } while (*s);
1354 return s-str; /* nr of wchars */
1357 /*********************************************************************
1358 * _wcsicmp (CRTDLL.321)
1360 DWORD __cdecl CRTDLL__wcsicmp( LPCWSTR s1, LPCWSTR s2 )
1362 return lstrcmpi32W( s1, s2 );
1365 /*********************************************************************
1366 * _wcsicoll (CRTDLL.322)
1368 DWORD __cdecl CRTDLL__wcsicoll(LPCWSTR a1,LPCWSTR a2)
1370 /* FIXME: handle collates */
1371 return lstrcmpi32W(a1,a2);
1374 /*********************************************************************
1375 * _wcsnicmp (CRTDLL.324)
1377 DWORD __cdecl CRTDLL__wcsnicmp( LPCWSTR s1, LPCWSTR s2, INT32 len )
1379 return lstrncmpi32W( s1, s2, len );
1382 /*********************************************************************
1383 * wcscoll (CRTDLL.506)
1385 DWORD __cdecl CRTDLL_wcscoll(LPWSTR a1,LPWSTR a2)
1387 /* FIXME: handle collates */
1388 return lstrcmp32W(a1,a2);
1391 /*********************************************************************
1392 * _wcsrev (CRTDLL.326)
1394 VOID __cdecl CRTDLL__wcsrev(LPWSTR s) {
1395 LPWSTR e;
1397 e=s;
1398 while (*e)
1399 e++;
1400 while (s<e) {
1401 WCHAR a;
1403 a=*s;*s=*e;*e=a;
1404 s++;e--;
1408 /*********************************************************************
1409 * wcsstr (CRTDLL.517)
1411 LPWSTR __cdecl CRTDLL_wcsstr(LPWSTR s,LPWSTR b)
1413 LPWSTR x,y,c;
1415 x=s;
1416 while (*x) {
1417 if (*x==*b) {
1418 y=x;c=b;
1419 while (*y && *c && *y==*c) { c++;y++; }
1420 if (!*c)
1421 return x;
1423 x++;
1425 return NULL;
1428 /*********************************************************************
1429 * wcstombs (CRTDLL.521)
1431 INT32 __cdecl CRTDLL_wcstombs( LPSTR dst, LPCWSTR src, INT32 len )
1433 lstrcpynWtoA( dst, src, len );
1434 return strlen(dst); /* FIXME: is this right? */
1437 /*********************************************************************
1438 * wcsrchr (CRTDLL.515)
1440 LPWSTR __cdecl CRTDLL_wcsrchr(LPWSTR str,WCHAR xchar)
1442 LPWSTR s;
1444 s=str+lstrlen32W(str);
1445 do {
1446 if (*s==xchar)
1447 return s;
1448 s--;
1449 } while (s>=str);
1450 return NULL;
1453 /*********************************************************************
1454 * _setmode (CRTDLL.265)
1455 * FIXME: At present we ignore the request to translate CR/LF to LF.
1457 * We allways translate when we read with fgets, we never do with fread
1460 INT32 __cdecl CRTDLL__setmode( INT32 fh,INT32 mode)
1462 /* FIXME */
1463 #define O_TEXT 0x4000
1464 #define O_BINARY 0x8000
1466 FIXME(crtdll, "on fhandle %d mode %s, STUB.\n",
1467 fh,(mode=O_TEXT)?"O_TEXT":
1468 (mode=O_BINARY)?"O_BINARY":"UNKNOWN");
1469 return -1;
1472 /*********************************************************************
1473 * _fpreset (CRTDLL.107)
1475 VOID __cdecl CRTDLL__fpreset(void)
1477 FIXME(crtdll," STUB.\n");
1480 /*********************************************************************
1481 * atexit (CRTDLL.345)
1483 INT32 __cdecl CRTDLL_atexit(LPVOID x)
1485 FIXME(crtdll,"(%p), STUB.\n",x);
1486 return 0; /* successful */
1489 /*********************************************************************
1490 * mblen (CRTDLL.428)
1491 * FIXME: check multibyte support
1493 WCHAR __cdecl CRTDLL_mblen(CHAR *mb,INT32 size)
1496 int ret=1;
1498 if (!mb)
1499 ret = 0;
1500 else if ((size<1)||(!*(mb+1)))
1501 ret = -1;
1502 else if (!(*mb))
1503 ret =0;
1505 TRACE(crtdll,"CRTDLL_mlen %s for max %d bytes ret %d\n",mb,size,ret);
1507 return ret;
1510 /*********************************************************************
1511 * mbstowcs (CRTDLL.429)
1512 * FIXME: check multibyte support
1514 INT32 __cdecl CRTDLL_mbstowcs(LPWSTR wcs, LPCSTR mbs, INT32 size)
1517 /* Slightly modified lstrcpynAtoW functions from memory/strings.c
1518 * We need the number of characters transfered
1519 * FIXME: No multibyte support yet
1522 LPWSTR p = wcs;
1523 LPCSTR src= mbs;
1524 int ret, n=size;
1526 while ((n-- > 0) && *src) {
1527 *p++ = (WCHAR)(unsigned char)*src++;
1529 p++;
1530 ret = (p -wcs);
1532 TRACE(crtdll,"CRTDLL_mbstowcs %s for %d chars put %d wchars\n",
1533 mbs,size,ret);
1534 return ret;
1537 /*********************************************************************
1538 * mbtowc (CRTDLL.430)
1539 * FIXME: check multibyte support
1541 WCHAR __cdecl CRTDLL_mbtowc(WCHAR* wc,CHAR* mb,INT32 size)
1543 int ret;
1545 if (!mb)
1546 ret = 0;
1547 else if (!wc)
1548 ret =-1;
1549 else
1550 if ( (ret = mblen(mb,size)) != -1 )
1552 if (ret <= sizeof(char))
1553 *wc = (WCHAR) ((unsigned char)*mb);
1554 else
1555 ret= -1;
1557 else
1558 ret = -1;
1560 TRACE(crtdll,"CRTDLL_mbtowc %s for %d chars\n",mb,size);
1562 return ret;
1565 /*********************************************************************
1566 * _isctype (CRTDLL.138)
1568 BOOL32 __cdecl CRTDLL__isctype(CHAR x,CHAR type)
1570 if ((type & CRTDLL_SPACE) && isspace(x))
1571 return TRUE;
1572 if ((type & CRTDLL_PUNCT) && ispunct(x))
1573 return TRUE;
1574 if ((type & CRTDLL_LOWER) && islower(x))
1575 return TRUE;
1576 if ((type & CRTDLL_UPPER) && isupper(x))
1577 return TRUE;
1578 if ((type & CRTDLL_ALPHA) && isalpha(x))
1579 return TRUE;
1580 if ((type & CRTDLL_DIGIT) && isdigit(x))
1581 return TRUE;
1582 if ((type & CRTDLL_CONTROL) && iscntrl(x))
1583 return TRUE;
1584 /* check CRTDLL_LEADBYTE */
1585 return FALSE;
1588 /*********************************************************************
1589 * _chdrive (CRTDLL.52)
1591 * newdir [I] drive to change to, A=1
1594 BOOL32 __cdecl CRTDLL__chdrive(INT32 newdrive)
1596 /* FIXME: generates errnos */
1597 return DRIVE_SetCurrentDrive(newdrive-1);
1600 /*********************************************************************
1601 * _chdir (CRTDLL.51)
1603 INT32 __cdecl CRTDLL__chdir(LPCSTR newdir)
1605 if (!SetCurrentDirectory32A(newdir))
1606 return 1;
1607 return 0;
1610 /*********************************************************************
1611 * _fullpath (CRTDLL.114)
1613 LPSTR __cdecl CRTDLL__fullpath(LPSTR buf, LPCSTR name, INT32 size)
1615 DOS_FULL_NAME full_name;
1617 if (!buf)
1619 size = 256;
1620 if(!(buf = CRTDLL_malloc(size))) return NULL;
1622 if (!DOSFS_GetFullName( name, FALSE, &full_name )) return NULL;
1623 lstrcpyn32A(buf,full_name.short_name,size);
1624 TRACE(crtdll,"CRTDLL_fullpath got %s\n",buf);
1625 return buf;
1628 /*********************************************************************
1629 * _splitpath (CRTDLL.279)
1631 VOID __cdecl CRTDLL__splitpath(LPCSTR path, LPSTR drive, LPSTR directory, LPSTR filename, LPSTR extension )
1633 /* drive includes :
1634 directory includes leading and trailing (forward and backward slashes)
1635 filename without dot and slashes
1636 extension with leading dot
1638 char * drivechar,*dirchar,*namechar;
1640 TRACE(crtdll,"CRTDLL__splitpath got %s\n",path);
1642 drivechar = strchr(path,':');
1643 dirchar = strrchr(path,'/');
1644 namechar = strrchr(path,'\\');
1645 dirchar = MAX(dirchar,namechar);
1646 if (dirchar)
1647 namechar = strrchr(dirchar,'.');
1648 else
1649 namechar = strrchr(path,'.');
1652 if (drive)
1654 *drive = 0x00;
1655 if (drivechar)
1657 strncat(drive,path,drivechar-path+1);
1658 path = drivechar+1;
1661 if (directory)
1663 *directory = 0x00;
1664 if (dirchar)
1666 strncat(directory,path,dirchar-path+1);
1667 path = dirchar+1;
1670 if (filename)
1672 *filename = 0x00;
1673 if (namechar)
1675 strncat(filename,path,namechar-path);
1676 if (extension)
1678 *extension = 0x00;
1679 strcat(extension,namechar);
1684 TRACE(crtdll,"CRTDLL__splitpath found %s %s %s %s\n",drive,directory,filename,extension);
1688 /*********************************************************************
1689 * _getcwd (CRTDLL.120)
1691 CHAR* __cdecl CRTDLL__getcwd(LPSTR buf, INT32 size)
1693 char test[1];
1694 int len;
1696 len = size;
1697 if (!buf) {
1698 if (size < 0) /* allocate as big as nescessary */
1699 len =GetCurrentDirectory32A(1,test) + 1;
1700 if(!(buf = CRTDLL_malloc(len)))
1702 /* set error to OutOfRange */
1703 return( NULL );
1706 size = len;
1707 if(!(len =GetCurrentDirectory32A(len,buf)))
1709 return NULL;
1711 if (len > size)
1713 /* set error to ERANGE */
1714 TRACE(crtdll,"CRTDLL_getcwd buffer to small\n");
1715 return NULL;
1717 return buf;
1721 /*********************************************************************
1722 * _getdcwd (CRTDLL.121)
1724 CHAR* __cdecl CRTDLL__getdcwd(INT32 drive,LPSTR buf, INT32 size)
1726 char test[1];
1727 int len;
1729 FIXME(crtdll,"(\"%c:\",%s,%d)\n",drive+'A',buf,size);
1730 len = size;
1731 if (!buf) {
1732 if (size < 0) /* allocate as big as nescessary */
1733 len =GetCurrentDirectory32A(1,test) + 1;
1734 if(!(buf = CRTDLL_malloc(len)))
1736 /* set error to OutOfRange */
1737 return( NULL );
1740 size = len;
1741 if(!(len =GetCurrentDirectory32A(len,buf)))
1743 return NULL;
1745 if (len > size)
1747 /* set error to ERANGE */
1748 TRACE(crtdll,"buffer to small\n");
1749 return NULL;
1751 return buf;
1755 /*********************************************************************
1756 * _getdrive (CRTDLL.124)
1758 * Return current drive, 1 for A, 2 for B
1760 INT32 __cdecl CRTDLL__getdrive(VOID)
1762 return DRIVE_GetCurrentDrive() + 1;
1765 /*********************************************************************
1766 * _mkdir (CRTDLL.234)
1768 INT32 __cdecl CRTDLL__mkdir(LPCSTR newdir)
1770 if (!CreateDirectory32A(newdir,NULL))
1771 return -1;
1772 return 0;
1775 /*********************************************************************
1776 * remove (CRTDLL.448)
1778 INT32 __cdecl CRTDLL_remove(LPCSTR file)
1780 if (!DeleteFile32A(file))
1781 return -1;
1782 return 0;
1785 /*********************************************************************
1786 * _errno (CRTDLL.52)
1787 * Yes, this is a function.
1789 LPINT32 __cdecl CRTDLL__errno()
1791 static int crtdllerrno;
1793 /* FIXME: we should set the error at the failing function call time */
1794 crtdllerrno = LastErrorToErrno(GetLastError());
1795 return &crtdllerrno;
1798 /*********************************************************************
1799 * _tempnam (CRTDLL.305)
1802 LPSTR __cdecl CRTDLL__tempnam(LPCSTR dir, LPCSTR prefix)
1805 char *ret;
1806 DOS_FULL_NAME tempname;
1808 if ((ret = tempnam(dir,prefix))==NULL) {
1809 WARN(crtdll, "Unable to get unique filename\n");
1810 return NULL;
1812 if (!DOSFS_GetFullName(ret,FALSE,&tempname))
1814 TRACE(crtdll, "Wrong path?\n");
1815 return NULL;
1817 free(ret);
1818 if ((ret = CRTDLL_malloc(strlen(tempname.short_name)+1)) == NULL) {
1819 WARN(crtdll, "CRTDL_malloc for shortname failed\n");
1820 return NULL;
1822 if ((ret = strcpy(ret,tempname.short_name)) == NULL) {
1823 WARN(crtdll, "Malloc for shortname failed\n");
1824 return NULL;
1827 TRACE(crtdll,"dir %s prefix %s got %s\n",
1828 dir,prefix,ret);
1829 return ret;
1832 /*********************************************************************
1833 * tmpnam (CRTDLL.490)
1835 * lcclnk from lcc-win32 relies on a terminating dot in the name returned
1838 LPSTR __cdecl CRTDLL_tmpnam(LPSTR s)
1840 char *ret;
1842 if ((ret =tmpnam(s))== NULL) {
1843 WARN(crtdll, "Unable to get unique filename\n");
1844 return NULL;
1846 if (!DOSFS_GetFullName(ret,FALSE,&CRTDLL_tmpname))
1848 TRACE(crtdll, "Wrong path?\n");
1849 return NULL;
1851 strcat(CRTDLL_tmpname.short_name,".");
1852 TRACE(crtdll,"for buf %p got %s\n",
1853 s,CRTDLL_tmpname.short_name);
1854 TRACE(crtdll,"long got %s\n",
1855 CRTDLL_tmpname.long_name);
1856 if ( s != NULL)
1857 return strcpy(s,CRTDLL_tmpname.short_name);
1858 else
1859 return CRTDLL_tmpname.short_name;
1863 /*********************************************************************
1864 * _itoa (CRTDLL.165)
1866 LPSTR __cdecl CRTDLL__itoa(INT32 x,LPSTR buf,INT32 buflen)
1868 wsnprintf32A(buf,buflen,"%d",x);
1869 return buf;
1872 /*********************************************************************
1873 * _ltoa (CRTDLL.180)
1875 LPSTR __cdecl CRTDLL__ltoa(long x,LPSTR buf,INT32 radix)
1877 switch(radix) {
1878 case 2: FIXME(crtdll, "binary format not implemented !\n");
1879 break;
1880 case 8: wsnprintf32A(buf,0x80,"%o",x);
1881 break;
1882 case 10: wsnprintf32A(buf,0x80,"%d",x);
1883 break;
1884 case 16: wsnprintf32A(buf,0x80,"%x",x);
1885 break;
1886 default: FIXME(crtdll, "radix %d not implemented !\n", radix);
1888 return buf;
1891 /*********************************************************************
1892 * _ultoa (CRTDLL.311)
1894 LPSTR __cdecl CRTDLL__ultoa(long x,LPSTR buf,INT32 radix)
1896 switch(radix) {
1897 case 2: FIXME(crtdll, "binary format not implemented !\n");
1898 break;
1899 case 8: wsnprintf32A(buf,0x80,"%lo",x);
1900 break;
1901 case 10: wsnprintf32A(buf,0x80,"%ld",x);
1902 break;
1903 case 16: wsnprintf32A(buf,0x80,"%lx",x);
1904 break;
1905 default: FIXME(crtdll, "radix %d not implemented !\n", radix);
1907 return buf;
1910 typedef VOID (*sig_handler_type)(VOID);
1912 /*********************************************************************
1913 * signal (CRTDLL.455)
1915 VOID __cdecl CRTDLL_signal(int sig, sig_handler_type ptr)
1917 FIXME(crtdll, "(%d %p):stub.\n", sig, ptr);
1920 /*********************************************************************
1921 * _ftol (CRTDLL.113)
1923 LONG __cdecl CRTDLL__ftol(double fl) {
1924 return (LONG)fl;
1926 /*********************************************************************
1927 * _sleep (CRTDLL.267)
1929 VOID __cdecl CRTDLL__sleep(unsigned long timeout)
1931 TRACE(crtdll,"CRTDLL__sleep for %ld milliseconds\n",timeout);
1932 Sleep((timeout)?timeout:1);
1935 /*********************************************************************
1936 * getenv (CRTDLL.437)
1938 LPSTR __cdecl CRTDLL_getenv(const char *name)
1940 LPSTR environ = GetEnvironmentStrings32A();
1941 LPSTR pp,pos = NULL;
1942 unsigned int length;
1944 for (pp = environ; (*pp); pp = pp + strlen(pp) +1)
1946 pos =strchr(pp,'=');
1947 if (pos)
1948 length = pos -pp;
1949 else
1950 length = strlen(pp);
1951 if (!strncmp(pp,name,length)) break;
1953 if ((pp)&& (pos))
1955 pp = pos+1;
1956 TRACE(crtdll,"got %s\n",pp);
1958 FreeEnvironmentStrings32A( environ );
1959 return pp;
1962 /*********************************************************************
1963 * _mbsrchr (CRTDLL.223)
1965 LPSTR __cdecl CRTDLL__mbsrchr(LPSTR s,CHAR x) {
1966 /* FIXME: handle multibyte strings */
1967 return strrchr(s,x);
1970 /*********************************************************************
1971 * _memicmp (CRTDLL.233)(NTDLL.868)
1972 * A stringcompare, without \0 check
1973 * RETURNS
1974 * -1:if first string is alphabetically before second string
1975 * 1:if second '' '' '' '' first ''
1976 * 0:if both are equal.
1978 INT32 __cdecl CRTDLL__memicmp(
1979 LPCSTR s1, /* [in] first string */
1980 LPCSTR s2, /* [in] second string */
1981 DWORD len /* [in] length to compare */
1982 ) {
1983 int i;
1985 for (i=0;i<len;i++) {
1986 if (tolower(s1[i])<tolower(s2[i]))
1987 return -1;
1988 if (tolower(s1[i])>tolower(s2[i]))
1989 return 1;
1991 return 0;
1993 /*********************************************************************
1994 * __dllonexit (CRTDLL.25)
1996 VOID __cdecl CRTDLL__dllonexit ()
1998 FIXME(crtdll,"stub\n");
2001 /*********************************************************************
2002 * wcstok (CRTDLL.519)
2003 * Like strtok, but for wide character strings. s is modified, yes.
2005 LPWSTR CRTDLL_wcstok(LPWSTR s,LPCWSTR delim) {
2006 static LPWSTR nexttok = NULL;
2007 LPWSTR x,ret;
2009 if (!s)
2010 s = nexttok;
2011 if (!s)
2012 return NULL;
2013 x = s;
2014 while (*x && !CRTDLL_wcschr(delim,*x))
2015 x++;
2016 ret = nexttok;
2017 if (*x) {
2018 *x='\0';
2019 nexttok = x+1;
2020 } else
2021 nexttok = NULL;
2022 return ret;
2025 /*********************************************************************
2026 * wcstol (CRTDLL.520)
2027 * Like strtol, but for wide character strings.
2029 INT32 CRTDLL_wcstol(LPWSTR s,LPWSTR *end,INT32 base) {
2030 LPSTR sA = HEAP_strdupWtoA(GetProcessHeap(),0,s),endA;
2031 INT32 ret = strtol(sA,&endA,base);
2033 HeapFree(GetProcessHeap(),0,sA);
2034 if (end) *end = s+(endA-sA); /* pointer magic checked. */
2035 return ret;
2037 /*********************************************************************
2038 * strdate (CRTDLL.283)
2040 LPSTR __cdecl CRTDLL__strdate (LPSTR date)
2041 { FIXME (crtdll,"%p stub\n", date);
2042 return 0;
2045 /*********************************************************************
2046 * strtime (CRTDLL.299)
2048 LPSTR __cdecl CRTDLL__strtime (LPSTR date)
2049 { FIXME (crtdll,"%p stub\n", date);
2050 return 0;