Added beginnings of server-side file handling.
[wine/multimedia.git] / misc / crtdll.c
blobc142b7efbf43df153c57a75451d6dae86f5b5411
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
21 /* NOTE: This file also implements the wcs* functions. They _ARE_ in
22 * the newer Linux libcs, but use 4 byte wide characters, so are unusable,
23 * since we need 2 byte wide characters. - Marcus Meissner, 981031
26 /* FIXME: all the file handling is hopelessly broken -- AJ */
28 #include <errno.h>
29 #include <stdlib.h>
30 #include <stdarg.h>
31 #include <string.h>
32 #include <sys/stat.h>
33 #include <sys/times.h>
34 #include <unistd.h>
35 #include <time.h>
36 #include <ctype.h>
37 #include <math.h>
38 #include <fcntl.h>
39 #include <setjmp.h>
40 #include "win.h"
41 #include "windows.h"
42 #include "winerror.h"
43 #include "debug.h"
44 #include "module.h"
45 #include "heap.h"
46 #include "crtdll.h"
47 #include "drive.h"
48 #include "file.h"
49 #include "except.h"
50 #include "options.h"
51 #include "winnls.h"
53 static DOS_FULL_NAME CRTDLL_tmpname;
55 UINT32 CRTDLL_argc_dll; /* CRTDLL.23 */
56 LPSTR *CRTDLL_argv_dll; /* CRTDLL.24 */
57 LPSTR CRTDLL_acmdln_dll; /* CRTDLL.38 */
58 UINT32 CRTDLL_basemajor_dll; /* CRTDLL.42 */
59 UINT32 CRTDLL_baseminor_dll; /* CRTDLL.43 */
60 UINT32 CRTDLL_baseversion_dll; /* CRTDLL.44 */
61 UINT32 CRTDLL_commode_dll; /* CRTDLL.59 */
62 LPSTR CRTDLL_environ_dll; /* CRTDLL.75 */
63 UINT32 CRTDLL_fmode_dll; /* CRTDLL.104 */
64 UINT32 CRTDLL_osmajor_dll; /* CRTDLL.241 */
65 UINT32 CRTDLL_osminor_dll; /* CRTDLL.242 */
66 UINT32 CRTDLL_osmode_dll; /* CRTDLL.243 */
67 UINT32 CRTDLL_osver_dll; /* CRTDLL.244 */
68 UINT32 CRTDLL_osversion_dll; /* CRTDLL.245 */
69 UINT32 CRTDLL_winmajor_dll; /* CRTDLL.329 */
70 UINT32 CRTDLL_winminor_dll; /* CRTDLL.330 */
71 UINT32 CRTDLL_winver_dll; /* CRTDLL.331 */
73 BYTE CRTDLL_iob[32*3]; /* FIXME */
75 typedef VOID (*new_handler_type)(VOID);
77 static new_handler_type new_handler;
79 /*********************************************************************
80 * _GetMainArgs (CRTDLL.022)
82 DWORD __cdecl CRTDLL__GetMainArgs(LPDWORD argc,LPSTR **argv,
83 LPSTR *environ,DWORD flag)
85 char *cmdline;
86 char **xargv;
87 int xargc,i,afterlastspace;
88 DWORD version;
90 TRACE(crtdll,"(%p,%p,%p,%ld).\n",
91 argc,argv,environ,flag
93 CRTDLL_acmdln_dll = cmdline = HEAP_strdupA( GetProcessHeap(), 0,
94 GetCommandLine32A() );
95 TRACE(crtdll,"got '%s'\n", cmdline);
97 version = GetVersion32();
98 CRTDLL_osver_dll = version >> 16;
99 CRTDLL_winminor_dll = version & 0xFF;
100 CRTDLL_winmajor_dll = (version>>8) & 0xFF;
101 CRTDLL_baseversion_dll = version >> 16;
102 CRTDLL_winver_dll = ((version >> 8) & 0xFF) + ((version & 0xFF) << 8);
103 CRTDLL_baseminor_dll = (version >> 16) & 0xFF;
104 CRTDLL_basemajor_dll = (version >> 24) & 0xFF;
105 CRTDLL_osversion_dll = version & 0xFFFF;
106 CRTDLL_osminor_dll = version & 0xFF;
107 CRTDLL_osmajor_dll = (version>>8) & 0xFF;
109 /* missing threading init */
111 i=0;xargv=NULL;xargc=0;afterlastspace=0;
112 while (cmdline[i]) {
113 if (cmdline[i]==' ') {
114 xargv=(char**)HeapReAlloc( GetProcessHeap(), 0, xargv,
115 sizeof(char*)*(++xargc));
116 cmdline[i]='\0';
117 xargv[xargc-1] = HEAP_strdupA( GetProcessHeap(), 0,
118 cmdline+afterlastspace);
119 i++;
120 while (cmdline[i]==' ')
121 i++;
122 if (cmdline[i])
123 afterlastspace=i;
124 } else
125 i++;
127 xargv=(char**)HeapReAlloc( GetProcessHeap(), 0, xargv,
128 sizeof(char*)*(++xargc));
129 cmdline[i]='\0';
130 xargv[xargc-1] = HEAP_strdupA( GetProcessHeap(), 0,
131 cmdline+afterlastspace);
132 CRTDLL_argc_dll = xargc;
133 *argc = xargc;
134 CRTDLL_argv_dll = xargv;
135 *argv = xargv;
137 TRACE(crtdll,"found %d arguments\n",
138 CRTDLL_argc_dll);
139 CRTDLL_environ_dll = *environ = GetEnvironmentStrings32A();
140 return 0;
144 typedef void (*_INITTERMFUN)();
146 /* fixme: move to header */
147 struct find_t
148 { unsigned attrib;
149 time_t time_create; /* -1 when not avaiable */
150 time_t time_access; /* -1 when not avaiable */
151 time_t time_write;
152 unsigned long size; /* fixme: 64 bit ??*/
153 char name[260];
155 /*********************************************************************
156 * _findfirst (CRTDLL.099)
158 * BUGS
159 * Unimplemented
161 DWORD __cdecl CRTDLL__findfirst(LPCSTR fname, struct find_t * x2)
163 FIXME(crtdll, ":(%s,%p): stub\n",fname,x2);
164 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
165 return FALSE;
168 /*********************************************************************
169 * _findnext (CRTDLL.100)
171 * BUGS
172 * Unimplemented
174 INT32 __cdecl CRTDLL__findnext(DWORD hand, struct find_t * x2)
176 FIXME(crtdll, ":(%ld,%p): stub\n",hand,x2);
177 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
178 return FALSE;
181 /*********************************************************************
182 * _fstat (CRTDLL.111)
184 * BUGS
185 * Unimplemented
187 int __cdecl CRTDLL__fstat(int file, struct stat* buf)
189 FIXME(crtdll, ":(%d,%p): stub\n",file,buf);
190 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
191 return FALSE;
194 /*********************************************************************
195 * _initterm (CRTDLL.135)
197 DWORD __cdecl CRTDLL__initterm(_INITTERMFUN *start,_INITTERMFUN *end)
199 _INITTERMFUN *current;
201 TRACE(crtdll,"(%p,%p)\n",start,end);
202 current=start;
203 while (current<end) {
204 if (*current) (*current)();
205 current++;
207 return 0;
210 /*********************************************************************
211 * _fdopen (CRTDLL.91)
213 DWORD __cdecl CRTDLL__fdopen(INT32 handle, LPCSTR mode)
215 FILE *file;
217 switch (handle)
219 case 0 : file=stdin;
220 break;
221 case 1 : file=stdout;
222 break;
223 case 2 : file=stderr;
224 break;
225 default:
226 file=fdopen(handle,mode);
228 TRACE(crtdll, "open handle %d mode %s got file %p\n",
229 handle, mode, file);
230 return (DWORD)file;
233 static FILE *xlat_file_ptr(void *ptr)
235 unsigned long dif;
237 /* CRT sizeof(FILE) == 32 */
238 dif = ((char *)ptr - (char *)CRTDLL_iob) / 32;
239 switch(dif)
241 case 0: return stdin;
242 case 1: return stdout;
243 case 2: return stderr;
245 return (FILE*)ptr;
248 /*******************************************************************
249 * _global_unwind2 (CRTDLL.129)
251 void __cdecl CRTDLL__global_unwind2( PEXCEPTION_FRAME frame )
253 RtlUnwind( frame, 0, NULL, 0 );
256 /*******************************************************************
257 * _local_unwind2 (CRTDLL.173)
259 void __cdecl CRTDLL__local_unwind2( PEXCEPTION_FRAME endframe, DWORD nr )
261 TRACE(crtdll,"(%p,%ld)\n",endframe,nr);
263 /*********************************************************************
264 * _read (CRTDLL.256)
266 * BUGS
267 * Unimplemented
269 INT32 __cdecl CRTDLL__read(INT32 fd, LPVOID buf, UINT32 count)
271 FIXME(crtdll,":(%d,%p,%d): stub\n",fd,buf,count);
272 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
273 return FALSE;
276 /*********************************************************************
277 * fopen (CRTDLL.372)
279 DWORD __cdecl CRTDLL_fopen(LPCSTR path, LPCSTR mode)
281 FILE *file;
282 HFILE32 dos_fildes;
283 #if 0
284 DOS_FULL_NAME full_name;
286 if (!DOSFS_GetFullName( path, FALSE, &full_name )) {
287 WARN(crtdll, "file %s bad name\n",path);
288 return 0;
291 file=fopen(full_name.long_name ,mode);
292 #endif
293 INT32 flagmode=0;
294 int unix_fildes=0;
296 if ((strchr(mode,'r')&&strchr(mode,'a'))||
297 (strchr(mode,'r')&&strchr(mode,'w'))||
298 (strchr(mode,'w')&&strchr(mode,'a')))
299 return 0;
301 if (strstr(mode,"r+")) flagmode=O_RDWR;
302 else if (strchr(mode,'r')) flagmode = O_RDONLY;
303 else if (strstr(mode,"w+")) flagmode= O_RDWR | O_TRUNC | O_CREAT;
304 else if (strchr(mode,'w')) flagmode = O_WRONLY | O_TRUNC | O_CREAT;
305 else if (strstr(mode,"a+")) flagmode= O_RDWR | O_CREAT | O_APPEND;
306 else if (strchr(mode,'w')) flagmode = O_RDWR | O_CREAT | O_APPEND;
307 else if (strchr(mode,'b'))
308 TRACE(crtdll, "%s in BINARY mode\n",path);
310 dos_fildes=FILE_Open(path, flagmode,0);
311 unix_fildes=FILE_GetUnixHandle(dos_fildes,0);
312 file = fdopen(unix_fildes,mode);
314 TRACE(crtdll, "file %s mode %s got ufh %d dfh %d file %p\n",
315 path,mode,unix_fildes,dos_fildes,file);
316 return (DWORD)file;
319 /*********************************************************************
320 * fread (CRTDLL.377)
322 DWORD __cdecl CRTDLL_fread(LPVOID ptr, INT32 size, INT32 nmemb, LPVOID vfile)
324 size_t ret=1;
325 FILE *file=xlat_file_ptr(vfile);
326 #if 0
327 int i=0;
328 void *temp=ptr;
330 /* If we would honour CR/LF <-> LF translation, we could do it like this.
331 We should keep track of all files opened, and probably files with \
332 known binary extensions must be unchanged */
333 while ( (i < (nmemb*size)) && (ret==1)) {
334 ret=fread(temp,1,1,file);
335 TRACE(crtdll, "got %c 0x%02x ret %d\n",
336 (isalpha(*(unsigned char*)temp))? *(unsigned char*)temp:
337 ' ',*(unsigned char*)temp, ret);
338 if (*(unsigned char*)temp != 0xd) { /* skip CR */
339 temp++;
340 i++;
342 else
343 TRACE(crtdll, "skipping ^M\n");
345 TRACE(crtdll, "0x%08x items of size %d from file %p to %p\n",
346 nmemb,size,file,ptr,);
347 if(i!=nmemb)
348 WARN(crtdll, " failed!\n");
350 return i;
351 #else
353 ret=fread(ptr,size,nmemb,file);
354 TRACE(crtdll, "0x%08x items of size %d from file %p to %p\n",
355 nmemb,size,file,ptr);
356 if(ret!=nmemb)
357 WARN(crtdll, " failed!\n");
359 return ret;
360 #endif
362 /*********************************************************************
363 * freopen (CRTDLL.379)
365 * BUGS
366 * Unimplemented
368 DWORD __cdecl CRTDLL_freopen(LPCSTR path, LPCSTR mode, LPVOID stream)
370 FIXME(crtdll, ":(%s,%s,%p): stub\n", path, mode, stream);
371 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
372 return FALSE;
375 /*********************************************************************
376 * fscanf (CRTDLL.381)
378 INT32 __cdecl CRTDLL_fscanf( LPVOID stream, LPSTR format, ... )
380 va_list valist;
381 INT32 res;
383 va_start( valist, format );
384 #ifdef HAVE_VFSCANF
385 res = vfscanf( xlat_file_ptr(stream), format, valist );
386 #endif
387 va_end( valist );
388 return res;
391 /*********************************************************************
392 * fseek (CRTDLL.382)
394 LONG __cdecl CRTDLL_fseek(LPVOID stream, LONG offset, INT32 whence)
396 long ret;
398 ret=fseek(xlat_file_ptr(stream),offset,whence);
399 TRACE(crtdll, "file %p to 0x%08lx pos %s\n",
400 stream,offset,(whence==SEEK_SET)?"SEEK_SET":
401 (whence==SEEK_CUR)?"SEEK_CUR":
402 (whence==SEEK_END)?"SEEK_END":"UNKNOWN");
403 if(ret)
404 WARN(crtdll, " failed!\n");
406 return ret;
409 /*********************************************************************
410 * fsetpos (CRTDLL.383)
412 INT32 __cdecl CRTDLL_fsetpos(LPVOID stream, fpos_t *pos)
414 TRACE(crtdll, "file %p\n", stream);
415 return fseek(xlat_file_ptr(stream), *pos, SEEK_SET);
418 /*********************************************************************
419 * ftell (CRTDLL.384)
421 LONG __cdecl CRTDLL_ftell(LPVOID stream)
423 long ret;
425 ret=ftell(xlat_file_ptr(stream));
426 TRACE(crtdll, "file %p at 0x%08lx\n",
427 stream,ret);
428 return ret;
431 /*********************************************************************
432 * fwrite (CRTDLL.386)
434 DWORD __cdecl CRTDLL_fwrite(LPVOID ptr, INT32 size, INT32 nmemb, LPVOID vfile)
436 size_t ret;
437 FILE *file=xlat_file_ptr(vfile);
439 ret=fwrite(ptr,size,nmemb,file);
440 TRACE(crtdll, "0x%08x items of size %d from %p to file %p\n",
441 nmemb,size,ptr,file);
442 if(ret!=nmemb)
443 WARN(crtdll, " Failed!\n");
445 return ret;
448 /*********************************************************************
449 * setbuf (CRTDLL.452)
451 INT32 __cdecl CRTDLL_setbuf(LPVOID file, LPSTR buf)
453 TRACE(crtdll, "(file %p buf %p)\n", file, buf);
454 /* this doesn't work:"void value not ignored as it ought to be"
455 return setbuf(file,buf);
457 setbuf(xlat_file_ptr(file),buf);
458 return 0;
461 /*********************************************************************
462 * _open_osfhandle (CRTDLL.240)
464 HFILE32 __cdecl CRTDLL__open_osfhandle(LONG osfhandle, INT32 flags)
466 HFILE32 handle;
468 switch (osfhandle) {
469 case STD_INPUT_HANDLE :
470 case 0 :
471 handle=0;
472 break;
473 case STD_OUTPUT_HANDLE:
474 case 1:
475 handle=1;
476 break;
477 case STD_ERROR_HANDLE:
478 case 2:
479 handle=2;
480 break;
481 default:
482 return (-1);
484 TRACE(crtdll, "(handle %08lx,flags %d) return %d\n",
485 osfhandle,flags,handle);
486 return handle;
490 /*********************************************************************
491 * srand (CRTDLL.460)
493 void __cdecl CRTDLL_srand(DWORD seed)
495 /* FIXME: should of course be thread? process? local */
496 srand(seed);
499 /*********************************************************************
500 * fprintf (CRTDLL.373)
502 INT32 __cdecl CRTDLL_fprintf( FILE *file, LPSTR format, ... )
504 va_list valist;
505 INT32 res;
507 va_start( valist, format );
508 res = vfprintf( xlat_file_ptr(file), format, valist );
509 va_end( valist );
510 return res;
513 /*********************************************************************
514 * vfprintf (CRTDLL.373)
516 INT32 __cdecl CRTDLL_vfprintf( FILE *file, LPSTR format, va_list args )
518 return vfprintf( xlat_file_ptr(file), format, args );
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(LPVOID stream)
650 int ret;
652 ret = fflush(xlat_file_ptr(stream));
653 TRACE(crtdll,"%p returnd %d\n",stream,ret);
654 if(ret)
655 WARN(crtdll, " Failed!\n");
657 return ret;
661 /*********************************************************************
662 * gets (CRTDLL.391)
664 LPSTR __cdecl CRTDLL_gets(LPSTR buf)
666 int cc;
667 LPSTR buf_start = buf;
669 /* BAD, for the whole WINE process blocks... just done this way to test
670 * windows95's ftp.exe.
673 for(cc = fgetc(stdin); cc != EOF && cc != '\n'; cc = fgetc(stdin))
674 if(cc != '\r') *buf++ = (char)cc;
676 *buf = '\0';
678 TRACE(crtdll,"got '%s'\n", buf_start);
679 return buf_start;
683 /*********************************************************************
684 * rand (CRTDLL.446)
686 INT32 __cdecl CRTDLL_rand()
688 return rand();
692 /*********************************************************************
693 * putchar (CRTDLL.442)
695 void __cdecl CRTDLL_putchar( INT32 x )
697 putchar(x);
701 /*********************************************************************
702 * fputc (CRTDLL.374)
704 INT32 __cdecl CRTDLL_fputc( INT32 c, FILE *stream )
706 TRACE(crtdll, "%c to file %p\n",c,stream);
707 return fputc(c,xlat_file_ptr(stream));
711 /*********************************************************************
712 * fputs (CRTDLL.375)
714 INT32 __cdecl CRTDLL_fputs( LPCSTR s, FILE *stream )
716 TRACE(crtdll, "%s to file %p\n",s,stream);
717 return fputs(s,xlat_file_ptr(stream));
721 /*********************************************************************
722 * puts (CRTDLL.443)
724 INT32 __cdecl CRTDLL_puts(LPCSTR s)
726 TRACE(crtdll, "%s \n",s);
727 return puts(s);
731 /*********************************************************************
732 * putc (CRTDLL.441)
734 INT32 __cdecl CRTDLL_putc(INT32 c, FILE *stream)
736 TRACE(crtdll, " %c to file %p\n",c,stream);
737 return fputc(c,xlat_file_ptr(stream));
739 /*********************************************************************
740 * fgetc (CRTDLL.366)
742 INT32 __cdecl CRTDLL_fgetc( FILE *stream )
744 int ret= fgetc(xlat_file_ptr(stream));
745 TRACE(crtdll, "got %d\n",ret);
746 return ret;
750 /*********************************************************************
751 * getc (CRTDLL.388)
753 INT32 __cdecl CRTDLL_getc( FILE *stream )
755 int ret= fgetc(xlat_file_ptr(stream));
756 TRACE(crtdll, "got %d\n",ret);
757 return ret;
760 /*********************************************************************
761 * _rotl (CRTDLL.259)
763 UINT32 __cdecl CRTDLL__rotl(UINT32 x,INT32 shift)
765 unsigned int ret = (x >> shift)|( x >>((sizeof(x))-shift));
767 TRACE(crtdll, "got 0x%08x rot %d ret 0x%08x\n",
768 x,shift,ret);
769 return ret;
772 /*********************************************************************
773 * _lrotl (CRTDLL.176)
775 DWORD __cdecl CRTDLL__lrotl(DWORD x,INT32 shift)
777 unsigned long ret = (x >> shift)|( x >>((sizeof(x))-shift));
779 TRACE(crtdll, "got 0x%08lx rot %d ret 0x%08lx\n",
780 x,shift,ret);
781 return ret;
786 /*********************************************************************
787 * fgets (CRTDLL.368)
789 CHAR* __cdecl CRTDLL_fgets(LPSTR s,INT32 size, LPVOID stream)
791 char * ret;
792 char * control_M;
794 ret=fgets(s, size,xlat_file_ptr(stream));
795 /*FIXME: Control with CRTDLL_setmode */
796 control_M= strrchr(s,'\r');
797 /*delete CR if we read a DOS File */
798 if (control_M)
800 *control_M='\n';
801 *(control_M+1)=0;
803 TRACE(crtdll, "got %s for %d chars from file %p\n",
804 s,size,stream);
805 if(ret)
806 WARN(crtdll, " Failed!\n");
808 return ret;
812 /*********************************************************************
813 * _mbsicmp (CRTDLL.204)
815 int __cdecl CRTDLL__mbsicmp(unsigned char *x,unsigned char *y)
817 do {
818 if (!*x)
819 return !!*y;
820 if (!*y)
821 return !!*x;
822 /* FIXME: MBCS handling... */
823 if (*x!=*y)
824 return 1;
825 x++;
826 y++;
827 } while (1);
831 /*********************************************************************
832 * _mbsinc (CRTDLL.205)
834 unsigned char * __cdecl CRTDLL__mbsinc(unsigned char *x)
836 /* FIXME: mbcs */
837 return x++;
841 /*********************************************************************
842 * vsprintf (CRTDLL.500)
844 INT32 __cdecl CRTDLL_vsprintf( LPSTR buffer, LPCSTR spec, va_list args )
846 return wvsprintf32A( buffer, spec, args );
849 /*********************************************************************
850 * vswprintf (CRTDLL.501)
852 INT32 __cdecl CRTDLL_vswprintf( LPWSTR buffer, LPCWSTR spec, va_list args )
854 return wvsprintf32W( buffer, spec, args );
857 /*********************************************************************
858 * _mbscpy (CRTDLL.200)
860 unsigned char* __cdecl CRTDLL__mbscpy(unsigned char *x,unsigned char *y)
862 TRACE(crtdll,"CRTDLL_mbscpy %s and %s\n",x,y);
863 return strcpy(x,y);
867 /*********************************************************************
868 * _mbscat (CRTDLL.197)
870 unsigned char* __cdecl CRTDLL__mbscat(unsigned char *x,unsigned char *y)
872 return strcat(x,y);
876 /*********************************************************************
877 * _strcmpi (CRTDLL.282) (CRTDLL.287)
879 INT32 __cdecl CRTDLL__strcmpi( LPCSTR s1, LPCSTR s2 )
881 return lstrcmpi32A( s1, s2 );
885 /*********************************************************************
886 * _strnicmp (CRTDLL.293)
888 INT32 __cdecl CRTDLL__strnicmp( LPCSTR s1, LPCSTR s2, INT32 n )
890 return lstrncmpi32A( s1, s2, n );
894 /*********************************************************************
895 * _strlwr (CRTDLL.293)
897 * convert a string in place to lowercase
899 LPSTR __cdecl CRTDLL__strlwr(LPSTR x)
901 unsigned char *y =x;
903 TRACE(crtdll, "CRTDLL_strlwr got %s\n", x);
904 while (*y) {
905 if ((*y > 0x40) && (*y< 0x5b))
906 *y = *y + 0x20;
907 y++;
909 TRACE(crtdll, " returned %s\n", x);
911 return x;
914 /*********************************************************************
915 * system (CRTDLL.485)
917 INT32 __cdecl CRTDLL_system(LPSTR x)
919 #define SYSBUF_LENGTH 1500
920 char buffer[SYSBUF_LENGTH];
921 unsigned char *y = x;
922 unsigned char *bp;
923 int i;
925 sprintf( buffer, "%s \"", Options.argv0 );
926 bp = buffer + strlen(buffer);
927 i = strlen(buffer) + strlen(x) +2;
929 /* Calculate needed buffer size to prevent overflow. */
930 while (*y) {
931 if (*y =='\\') i++;
932 y++;
934 /* If buffer too short, exit. */
935 if (i > SYSBUF_LENGTH) {
936 TRACE(crtdll,"_system buffer to small\n");
937 return 127;
940 y =x;
942 while (*y) {
943 *bp = *y;
944 bp++; y++;
945 if (*(y-1) =='\\') *bp++ = '\\';
947 /* Remove spaces from end of string. */
948 while (*(y-1) == ' ') {
949 bp--;y--;
951 *bp++ = '"';
952 *bp = 0;
953 TRACE(crtdll, "_system got '%s', executing '%s'\n",x,buffer);
955 return system(buffer);
958 /*********************************************************************
959 * _strupr (CRTDLL.300)
961 LPSTR __cdecl CRTDLL__strupr(LPSTR x)
963 LPSTR y=x;
965 while (*y) {
966 *y=toupper(*y);
967 y++;
969 return x;
972 /*********************************************************************
973 * _wcsupr (CRTDLL.328)
975 LPWSTR __cdecl CRTDLL__wcsupr(LPWSTR x)
977 LPWSTR y=x;
979 while (*y) {
980 *y=towupper(*y);
981 y++;
983 return x;
986 /*********************************************************************
987 * _wcslwr (CRTDLL.323)
989 LPWSTR __cdecl CRTDLL__wcslwr(LPWSTR x)
991 LPWSTR y=x;
993 while (*y) {
994 *y=towlower(*y);
995 y++;
997 return x;
1001 /*********************************************************************
1002 * longjmp (CRTDLL.426)
1004 VOID __cdecl CRTDLL_longjmp(jmp_buf env, int val)
1006 FIXME(crtdll,"CRTDLL_longjmp semistup, expect crash\n");
1007 return longjmp(env, val);
1010 /*********************************************************************
1011 * malloc (CRTDLL.427)
1013 VOID* __cdecl CRTDLL_malloc(DWORD size)
1015 return HeapAlloc(GetProcessHeap(),0,size);
1018 /*********************************************************************
1019 * new (CRTDLL.001)
1021 VOID* __cdecl CRTDLL_new(DWORD size)
1023 VOID* result;
1024 if(!(result = HeapAlloc(GetProcessHeap(),0,size)) && new_handler)
1025 (*new_handler)();
1026 return result;
1029 /*********************************************************************
1030 * set_new_handler(CRTDLL.003)
1032 new_handler_type __cdecl CRTDLL_set_new_handler(new_handler_type func)
1034 new_handler_type old_handler = new_handler;
1035 new_handler = func;
1036 return old_handler;
1039 /*********************************************************************
1040 * calloc (CRTDLL.350)
1042 VOID* __cdecl CRTDLL_calloc(DWORD size, DWORD count)
1044 return HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, size * count );
1047 /*********************************************************************
1048 * realloc (CRTDLL.447)
1050 VOID* __cdecl CRTDLL_realloc( VOID *ptr, DWORD size )
1052 return HeapReAlloc( GetProcessHeap(), 0, ptr, size );
1055 /*********************************************************************
1056 * free (CRTDLL.427)
1058 VOID __cdecl CRTDLL_free(LPVOID ptr)
1060 HeapFree(GetProcessHeap(),0,ptr);
1063 /*********************************************************************
1064 * delete (CRTDLL.002)
1066 VOID __cdecl CRTDLL_delete(VOID* ptr)
1068 HeapFree(GetProcessHeap(),0,ptr);
1071 /*********************************************************************
1072 * _strdup (CRTDLL.285)
1074 LPSTR __cdecl CRTDLL__strdup(LPCSTR ptr)
1076 return HEAP_strdupA(GetProcessHeap(),0,ptr);
1079 /*********************************************************************
1080 * _wcsdup (CRTDLL.320)
1082 LPWSTR __cdecl CRTDLL__wcsdup(LPCWSTR ptr)
1084 return HEAP_strdupW(GetProcessHeap(),0,ptr);
1087 /*********************************************************************
1088 * fclose (CRTDLL.362)
1090 INT32 __cdecl CRTDLL_fclose( FILE *stream )
1092 int unix_handle;
1093 HFILE32 dos_handle=1;
1094 HFILE32 ret=EOF;
1096 stream=xlat_file_ptr(stream);
1097 unix_handle=fileno(stream);
1099 if (unix_handle<4) ret= fclose(stream);
1100 else {
1101 int h;
1102 while((h = FILE_GetUnixHandle(dos_handle,0)) != unix_handle)
1104 close(h);
1105 dos_handle++;
1107 fclose(stream);
1108 ret = _lclose32( dos_handle);
1110 TRACE(crtdll,"(%p) ufh %d dfh %d\n",
1111 stream,unix_handle,dos_handle);
1113 if(ret)
1114 WARN(crtdll, " Failed!\n");
1116 return ret;
1119 /*********************************************************************
1120 * _unlink (CRTDLL.315)
1122 INT32 __cdecl CRTDLL__unlink(LPCSTR pathname)
1124 int ret=0;
1125 DOS_FULL_NAME full_name;
1127 if (!DOSFS_GetFullName( pathname, FALSE, &full_name )) {
1128 WARN(crtdll, "CRTDLL_unlink file %s bad name\n",pathname);
1129 return EOF;
1132 ret=unlink(full_name.long_name);
1133 TRACE(crtdll,"(%s unix %s)\n",
1134 pathname,full_name.long_name);
1135 if(ret)
1136 WARN(crtdll, " Failed!\n");
1138 return ret;
1141 /*********************************************************************
1142 * rename (CRTDLL.449)
1144 INT32 __cdecl CRTDLL_rename(LPCSTR oldpath,LPCSTR newpath)
1146 BOOL32 ok = MoveFileEx32A( oldpath, newpath, MOVEFILE_REPLACE_EXISTING );
1147 return ok ? 0 : -1;
1151 /*********************************************************************
1152 * _stat (CRTDLL.280)
1155 struct win_stat
1157 UINT16 win_st_dev;
1158 UINT16 win_st_ino;
1159 UINT16 win_st_mode;
1160 INT16 win_st_nlink;
1161 INT16 win_st_uid;
1162 INT16 win_st_gid;
1163 UINT32 win_st_rdev;
1164 INT32 win_st_size;
1165 INT32 win_st_atime;
1166 INT32 win_st_mtime;
1167 INT32 win_st_ctime;
1170 int __cdecl CRTDLL__stat(const char * filename, struct win_stat * buf)
1172 int ret=0;
1173 DOS_FULL_NAME full_name;
1174 struct stat mystat;
1176 if (!DOSFS_GetFullName( filename, TRUE, &full_name ))
1178 WARN(crtdll, "CRTDLL__stat filename %s bad name\n",filename);
1179 return -1;
1181 ret=stat(full_name.long_name,&mystat);
1182 TRACE(crtdll,"CRTDLL__stat %s\n", filename);
1183 if(ret)
1184 WARN(crtdll, " Failed!\n");
1186 /* FIXME: should check what Windows returns */
1188 buf->win_st_dev = mystat.st_dev;
1189 buf->win_st_ino = mystat.st_ino;
1190 buf->win_st_mode = mystat.st_mode;
1191 buf->win_st_nlink = mystat.st_nlink;
1192 buf->win_st_uid = mystat.st_uid;
1193 buf->win_st_gid = mystat.st_gid;
1194 buf->win_st_rdev = mystat.st_rdev;
1195 buf->win_st_size = mystat.st_size;
1196 buf->win_st_atime = mystat.st_atime;
1197 buf->win_st_mtime = mystat.st_mtime;
1198 buf->win_st_ctime = mystat.st_ctime;
1199 return ret;
1202 /*********************************************************************
1203 * _open (CRTDLL.239)
1205 HFILE32 __cdecl CRTDLL__open(LPCSTR path,INT32 flags)
1207 HFILE32 ret=0;
1208 int wineflags=0;
1210 /* FIXME:
1211 the flags in lcc's header differ from the ones in Linux, e.g.
1212 Linux: define O_APPEND 02000 (= 0x400)
1213 lcc: define _O_APPEND 0x0008
1214 so here a scheme to translate them
1215 Probably lcc is wrong here, but at least a hack to get is going
1217 wineflags = (flags & 3);
1218 if (flags & 0x0008 ) wineflags |= O_APPEND;
1219 if (flags & 0x0100 ) wineflags |= O_CREAT;
1220 if (flags & 0x0200 ) wineflags |= O_TRUNC;
1221 if (flags & 0x0400 ) wineflags |= O_EXCL;
1222 if (flags & 0xf0f4 )
1223 TRACE(crtdll,"CRTDLL_open file unsupported flags 0x%04x\n",flags);
1224 /* End Fixme */
1226 ret = FILE_Open(path,wineflags,0);
1227 TRACE(crtdll,"CRTDLL_open file %s mode 0x%04x (lccmode 0x%04x) got dfh %d\n",
1228 path,wineflags,flags,ret);
1229 return ret;
1232 /*********************************************************************
1233 * _close (CRTDLL.57)
1235 INT32 __cdecl CRTDLL__close(HFILE32 fd)
1237 int ret=_lclose32(fd);
1239 TRACE(crtdll,"(%d)\n",fd);
1240 if(ret)
1241 WARN(crtdll, " Failed!\n");
1243 return ret;
1246 /*********************************************************************
1247 * feof (CRTDLL.363)
1249 INT32 __cdecl CRTDLL_feof( FILE *stream )
1251 int ret;
1253 ret=feof(xlat_file_ptr(stream));
1254 TRACE(crtdll,"(%p) %s\n",stream,(ret)?"true":"false");
1255 return ret;
1258 /*********************************************************************
1259 * setlocale (CRTDLL.453)
1261 LPSTR __cdecl CRTDLL_setlocale(INT32 category,LPCSTR locale)
1263 LPSTR categorystr;
1265 switch (category) {
1266 case CRTDLL_LC_ALL: categorystr="LC_ALL";break;
1267 case CRTDLL_LC_COLLATE: categorystr="LC_COLLATE";break;
1268 case CRTDLL_LC_CTYPE: categorystr="LC_CTYPE";break;
1269 case CRTDLL_LC_MONETARY: categorystr="LC_MONETARY";break;
1270 case CRTDLL_LC_NUMERIC: categorystr="LC_NUMERIC";break;
1271 case CRTDLL_LC_TIME: categorystr="LC_TIME";break;
1272 default: categorystr = "UNKNOWN?";break;
1274 FIXME(crtdll,"(%s,%s),stub!\n",categorystr,locale);
1275 return "C";
1278 /*********************************************************************
1279 * wcscat (CRTDLL.503)
1281 LPWSTR __cdecl CRTDLL_wcscat( LPWSTR s1, LPCWSTR s2 )
1283 return lstrcat32W( s1, s2 );
1286 /*********************************************************************
1287 * wcschr (CRTDLL.504)
1289 LPWSTR __cdecl CRTDLL_wcschr(LPCWSTR str,WCHAR xchar)
1291 LPCWSTR s;
1293 s=str;
1294 do {
1295 if (*s==xchar)
1296 return (LPWSTR)s;
1297 } while (*s++);
1298 return NULL;
1301 /*********************************************************************
1302 * wcscmp (CRTDLL.505)
1304 INT32 __cdecl CRTDLL_wcscmp( LPCWSTR s1, LPCWSTR s2 )
1306 return lstrcmp32W( s1, s2 );
1309 /*********************************************************************
1310 * wcscpy (CRTDLL.507)
1312 LPWSTR __cdecl CRTDLL_wcscpy( LPWSTR s1, LPCWSTR s2 )
1314 return lstrcpy32W( s1, s2 );
1317 /*********************************************************************
1318 * wcscspn (CRTDLL.508)
1320 INT32 __cdecl CRTDLL_wcscspn(LPWSTR str,LPWSTR reject)
1322 LPWSTR s,t;
1324 s=str;
1325 do {
1326 t=reject;
1327 while (*t) { if (*t==*s) break;t++;}
1328 if (*t) break;
1329 s++;
1330 } while (*s);
1331 return s-str; /* nr of wchars */
1334 /*********************************************************************
1335 * wcslen (CRTDLL.510)
1337 INT32 __cdecl CRTDLL_wcslen( LPCWSTR s )
1339 return lstrlen32W( s );
1342 /*********************************************************************
1343 * wcsncat (CRTDLL.511)
1345 LPWSTR __cdecl CRTDLL_wcsncat( LPWSTR s1, LPCWSTR s2, INT32 n )
1347 return lstrcatn32W( s1, s2, n );
1350 /*********************************************************************
1351 * wcsncmp (CRTDLL.512)
1353 INT32 __cdecl CRTDLL_wcsncmp( LPCWSTR s1, LPCWSTR s2, INT32 n )
1355 return lstrncmp32W( s1, s2, n );
1358 /*********************************************************************
1359 * wcsncpy (CRTDLL.513)
1361 LPWSTR __cdecl CRTDLL_wcsncpy( LPWSTR s1, LPCWSTR s2, INT32 n )
1363 return lstrcpyn32W( s1, s2, n );
1366 /*********************************************************************
1367 * wcsspn (CRTDLL.516)
1369 INT32 __cdecl CRTDLL_wcsspn(LPWSTR str,LPWSTR accept)
1371 LPWSTR s,t;
1373 s=str;
1374 do {
1375 t=accept;
1376 while (*t) { if (*t==*s) break;t++;}
1377 if (!*t) break;
1378 s++;
1379 } while (*s);
1380 return s-str; /* nr of wchars */
1383 /*********************************************************************
1384 * _wcsicmp (CRTDLL.321)
1386 DWORD __cdecl CRTDLL__wcsicmp( LPCWSTR s1, LPCWSTR s2 )
1388 return lstrcmpi32W( s1, s2 );
1391 /*********************************************************************
1392 * _wcsicoll (CRTDLL.322)
1394 DWORD __cdecl CRTDLL__wcsicoll(LPCWSTR a1,LPCWSTR a2)
1396 /* FIXME: handle collates */
1397 return lstrcmpi32W(a1,a2);
1400 /*********************************************************************
1401 * _wcsnicmp (CRTDLL.324)
1403 DWORD __cdecl CRTDLL__wcsnicmp( LPCWSTR s1, LPCWSTR s2, INT32 len )
1405 return lstrncmpi32W( s1, s2, len );
1408 /*********************************************************************
1409 * wcscoll (CRTDLL.506)
1411 DWORD __cdecl CRTDLL_wcscoll(LPWSTR a1,LPWSTR a2)
1413 /* FIXME: handle collates */
1414 return lstrcmp32W(a1,a2);
1417 /*********************************************************************
1418 * _wcsrev (CRTDLL.326)
1420 VOID __cdecl CRTDLL__wcsrev(LPWSTR s) {
1421 LPWSTR e;
1423 e=s;
1424 while (*e)
1425 e++;
1426 while (s<e) {
1427 WCHAR a;
1429 a=*s;*s=*e;*e=a;
1430 s++;e--;
1434 /*********************************************************************
1435 * wcsstr (CRTDLL.517)
1437 LPWSTR __cdecl CRTDLL_wcsstr(LPWSTR s,LPWSTR b)
1439 LPWSTR x,y,c;
1441 x=s;
1442 while (*x) {
1443 if (*x==*b) {
1444 y=x;c=b;
1445 while (*y && *c && *y==*c) { c++;y++; }
1446 if (!*c)
1447 return x;
1449 x++;
1451 return NULL;
1454 /*********************************************************************
1455 * wcstombs (CRTDLL.521)
1457 INT32 __cdecl CRTDLL_wcstombs( LPSTR dst, LPCWSTR src, INT32 len )
1459 lstrcpynWtoA( dst, src, len );
1460 return strlen(dst); /* FIXME: is this right? */
1463 /*********************************************************************
1464 * wcsrchr (CRTDLL.515)
1466 LPWSTR __cdecl CRTDLL_wcsrchr(LPWSTR str,WCHAR xchar)
1468 LPWSTR s;
1470 s=str+lstrlen32W(str);
1471 do {
1472 if (*s==xchar)
1473 return s;
1474 s--;
1475 } while (s>=str);
1476 return NULL;
1479 /*********************************************************************
1480 * _setmode (CRTDLL.265)
1481 * FIXME: At present we ignore the request to translate CR/LF to LF.
1483 * We allways translate when we read with fgets, we never do with fread
1486 INT32 __cdecl CRTDLL__setmode( INT32 fh,INT32 mode)
1488 /* FIXME */
1489 #define O_TEXT 0x4000
1490 #define O_BINARY 0x8000
1492 FIXME(crtdll, "on fhandle %d mode %s, STUB.\n",
1493 fh,(mode=O_TEXT)?"O_TEXT":
1494 (mode=O_BINARY)?"O_BINARY":"UNKNOWN");
1495 return -1;
1498 /*********************************************************************
1499 * _fpreset (CRTDLL.107)
1501 VOID __cdecl CRTDLL__fpreset(void)
1503 FIXME(crtdll," STUB.\n");
1506 /*********************************************************************
1507 * atexit (CRTDLL.345)
1509 INT32 __cdecl CRTDLL_atexit(LPVOID x)
1511 FIXME(crtdll,"(%p), STUB.\n",x);
1512 return 0; /* successful */
1515 /*********************************************************************
1516 * mblen (CRTDLL.428)
1517 * FIXME: check multibyte support
1519 WCHAR __cdecl CRTDLL_mblen(CHAR *mb,INT32 size)
1522 int ret=1;
1524 if (!mb)
1525 ret = 0;
1526 else if ((size<1)||(!*(mb+1)))
1527 ret = -1;
1528 else if (!(*mb))
1529 ret =0;
1531 TRACE(crtdll,"CRTDLL_mlen %s for max %d bytes ret %d\n",mb,size,ret);
1533 return ret;
1536 /*********************************************************************
1537 * mbstowcs (CRTDLL.429)
1538 * FIXME: check multibyte support
1540 INT32 __cdecl CRTDLL_mbstowcs(LPWSTR wcs, LPCSTR mbs, INT32 size)
1543 /* Slightly modified lstrcpynAtoW functions from memory/strings.c
1544 * We need the number of characters transfered
1545 * FIXME: No multibyte support yet
1548 LPWSTR p = wcs;
1549 LPCSTR src= mbs;
1550 int ret, n=size;
1552 while ((n-- > 0) && *src) {
1553 *p++ = (WCHAR)(unsigned char)*src++;
1555 p++;
1556 ret = (p -wcs);
1558 TRACE(crtdll,"CRTDLL_mbstowcs %s for %d chars put %d wchars\n",
1559 mbs,size,ret);
1560 return ret;
1563 /*********************************************************************
1564 * mbtowc (CRTDLL.430)
1565 * FIXME: check multibyte support
1567 WCHAR __cdecl CRTDLL_mbtowc(WCHAR* wc,CHAR* mb,INT32 size)
1569 int ret;
1571 if (!mb)
1572 ret = 0;
1573 else if (!wc)
1574 ret =-1;
1575 else
1576 if ( (ret = mblen(mb,size)) != -1 )
1578 if (ret <= sizeof(char))
1579 *wc = (WCHAR) ((unsigned char)*mb);
1580 else
1581 ret= -1;
1583 else
1584 ret = -1;
1586 TRACE(crtdll,"CRTDLL_mbtowc %s for %d chars\n",mb,size);
1588 return ret;
1591 /*********************************************************************
1592 * _isctype (CRTDLL.138)
1594 BOOL32 __cdecl CRTDLL__isctype(CHAR x,CHAR type)
1596 if ((type & CRTDLL_SPACE) && isspace(x))
1597 return TRUE;
1598 if ((type & CRTDLL_PUNCT) && ispunct(x))
1599 return TRUE;
1600 if ((type & CRTDLL_LOWER) && islower(x))
1601 return TRUE;
1602 if ((type & CRTDLL_UPPER) && isupper(x))
1603 return TRUE;
1604 if ((type & CRTDLL_ALPHA) && isalpha(x))
1605 return TRUE;
1606 if ((type & CRTDLL_DIGIT) && isdigit(x))
1607 return TRUE;
1608 if ((type & CRTDLL_CONTROL) && iscntrl(x))
1609 return TRUE;
1610 /* check CRTDLL_LEADBYTE */
1611 return FALSE;
1614 /*********************************************************************
1615 * _chdrive (CRTDLL.52)
1617 * newdir [I] drive to change to, A=1
1620 BOOL32 __cdecl CRTDLL__chdrive(INT32 newdrive)
1622 /* FIXME: generates errnos */
1623 return DRIVE_SetCurrentDrive(newdrive-1);
1626 /*********************************************************************
1627 * _chdir (CRTDLL.51)
1629 INT32 __cdecl CRTDLL__chdir(LPCSTR newdir)
1631 if (!SetCurrentDirectory32A(newdir))
1632 return 1;
1633 return 0;
1636 /*********************************************************************
1637 * _fullpath (CRTDLL.114)
1639 LPSTR __cdecl CRTDLL__fullpath(LPSTR buf, LPCSTR name, INT32 size)
1641 DOS_FULL_NAME full_name;
1643 if (!buf)
1645 size = 256;
1646 if(!(buf = CRTDLL_malloc(size))) return NULL;
1648 if (!DOSFS_GetFullName( name, FALSE, &full_name )) return NULL;
1649 lstrcpyn32A(buf,full_name.short_name,size);
1650 TRACE(crtdll,"CRTDLL_fullpath got %s\n",buf);
1651 return buf;
1654 /*********************************************************************
1655 * _splitpath (CRTDLL.279)
1657 VOID __cdecl CRTDLL__splitpath(LPCSTR path, LPSTR drive, LPSTR directory, LPSTR filename, LPSTR extension )
1659 /* drive includes :
1660 directory includes leading and trailing (forward and backward slashes)
1661 filename without dot and slashes
1662 extension with leading dot
1664 char * drivechar,*dirchar,*namechar;
1666 TRACE(crtdll,"CRTDLL__splitpath got %s\n",path);
1668 drivechar = strchr(path,':');
1669 dirchar = strrchr(path,'/');
1670 namechar = strrchr(path,'\\');
1671 dirchar = MAX(dirchar,namechar);
1672 if (dirchar)
1673 namechar = strrchr(dirchar,'.');
1674 else
1675 namechar = strrchr(path,'.');
1678 if (drive)
1680 *drive = 0x00;
1681 if (drivechar)
1683 strncat(drive,path,drivechar-path+1);
1684 path = drivechar+1;
1687 if (directory)
1689 *directory = 0x00;
1690 if (dirchar)
1692 strncat(directory,path,dirchar-path+1);
1693 path = dirchar+1;
1696 if (filename)
1698 *filename = 0x00;
1699 if (namechar)
1701 strncat(filename,path,namechar-path);
1702 if (extension)
1704 *extension = 0x00;
1705 strcat(extension,namechar);
1710 TRACE(crtdll,"CRTDLL__splitpath found %s %s %s %s\n",drive,directory,filename,extension);
1714 /*********************************************************************
1715 * _getcwd (CRTDLL.120)
1717 CHAR* __cdecl CRTDLL__getcwd(LPSTR buf, INT32 size)
1719 char test[1];
1720 int len;
1722 len = size;
1723 if (!buf) {
1724 if (size < 0) /* allocate as big as nescessary */
1725 len =GetCurrentDirectory32A(1,test) + 1;
1726 if(!(buf = CRTDLL_malloc(len)))
1728 /* set error to OutOfRange */
1729 return( NULL );
1732 size = len;
1733 if(!(len =GetCurrentDirectory32A(len,buf)))
1735 return NULL;
1737 if (len > size)
1739 /* set error to ERANGE */
1740 TRACE(crtdll,"CRTDLL_getcwd buffer to small\n");
1741 return NULL;
1743 return buf;
1747 /*********************************************************************
1748 * _getdcwd (CRTDLL.121)
1750 CHAR* __cdecl CRTDLL__getdcwd(INT32 drive,LPSTR buf, INT32 size)
1752 char test[1];
1753 int len;
1755 FIXME(crtdll,"(\"%c:\",%s,%d)\n",drive+'A',buf,size);
1756 len = size;
1757 if (!buf) {
1758 if (size < 0) /* allocate as big as nescessary */
1759 len =GetCurrentDirectory32A(1,test) + 1;
1760 if(!(buf = CRTDLL_malloc(len)))
1762 /* set error to OutOfRange */
1763 return( NULL );
1766 size = len;
1767 if(!(len =GetCurrentDirectory32A(len,buf)))
1769 return NULL;
1771 if (len > size)
1773 /* set error to ERANGE */
1774 TRACE(crtdll,"buffer to small\n");
1775 return NULL;
1777 return buf;
1781 /*********************************************************************
1782 * _getdrive (CRTDLL.124)
1784 * Return current drive, 1 for A, 2 for B
1786 INT32 __cdecl CRTDLL__getdrive(VOID)
1788 return DRIVE_GetCurrentDrive() + 1;
1791 /*********************************************************************
1792 * _mkdir (CRTDLL.234)
1794 INT32 __cdecl CRTDLL__mkdir(LPCSTR newdir)
1796 if (!CreateDirectory32A(newdir,NULL))
1797 return -1;
1798 return 0;
1801 /*********************************************************************
1802 * remove (CRTDLL.448)
1804 INT32 __cdecl CRTDLL_remove(LPCSTR file)
1806 if (!DeleteFile32A(file))
1807 return -1;
1808 return 0;
1811 /*********************************************************************
1812 * _errno (CRTDLL.52)
1813 * Yes, this is a function.
1815 LPINT32 __cdecl CRTDLL__errno()
1817 static int crtdllerrno;
1819 /* FIXME: we should set the error at the failing function call time */
1820 crtdllerrno = LastErrorToErrno(GetLastError());
1821 return &crtdllerrno;
1824 /*********************************************************************
1825 * _tempnam (CRTDLL.305)
1828 LPSTR __cdecl CRTDLL__tempnam(LPCSTR dir, LPCSTR prefix)
1831 char *ret;
1832 DOS_FULL_NAME tempname;
1834 if ((ret = tempnam(dir,prefix))==NULL) {
1835 WARN(crtdll, "Unable to get unique filename\n");
1836 return NULL;
1838 if (!DOSFS_GetFullName(ret,FALSE,&tempname))
1840 TRACE(crtdll, "Wrong path?\n");
1841 return NULL;
1843 free(ret);
1844 if ((ret = CRTDLL_malloc(strlen(tempname.short_name)+1)) == NULL) {
1845 WARN(crtdll, "CRTDL_malloc for shortname failed\n");
1846 return NULL;
1848 if ((ret = strcpy(ret,tempname.short_name)) == NULL) {
1849 WARN(crtdll, "Malloc for shortname failed\n");
1850 return NULL;
1853 TRACE(crtdll,"dir %s prefix %s got %s\n",
1854 dir,prefix,ret);
1855 return ret;
1858 /*********************************************************************
1859 * tmpnam (CRTDLL.490)
1861 * lcclnk from lcc-win32 relies on a terminating dot in the name returned
1864 LPSTR __cdecl CRTDLL_tmpnam(LPSTR s)
1866 char *ret;
1868 if ((ret =tmpnam(s))== NULL) {
1869 WARN(crtdll, "Unable to get unique filename\n");
1870 return NULL;
1872 if (!DOSFS_GetFullName(ret,FALSE,&CRTDLL_tmpname))
1874 TRACE(crtdll, "Wrong path?\n");
1875 return NULL;
1877 strcat(CRTDLL_tmpname.short_name,".");
1878 TRACE(crtdll,"for buf %p got %s\n",
1879 s,CRTDLL_tmpname.short_name);
1880 TRACE(crtdll,"long got %s\n",
1881 CRTDLL_tmpname.long_name);
1882 if ( s != NULL)
1883 return strcpy(s,CRTDLL_tmpname.short_name);
1884 else
1885 return CRTDLL_tmpname.short_name;
1889 /*********************************************************************
1890 * _itoa (CRTDLL.165)
1892 LPSTR __cdecl CRTDLL__itoa(INT32 x,LPSTR buf,INT32 buflen)
1894 wsnprintf32A(buf,buflen,"%d",x);
1895 return buf;
1898 /*********************************************************************
1899 * _ltoa (CRTDLL.180)
1901 LPSTR __cdecl CRTDLL__ltoa(long x,LPSTR buf,INT32 radix)
1903 switch(radix) {
1904 case 2: FIXME(crtdll, "binary format not implemented !\n");
1905 break;
1906 case 8: wsnprintf32A(buf,0x80,"%o",x);
1907 break;
1908 case 10: wsnprintf32A(buf,0x80,"%d",x);
1909 break;
1910 case 16: wsnprintf32A(buf,0x80,"%x",x);
1911 break;
1912 default: FIXME(crtdll, "radix %d not implemented !\n", radix);
1914 return buf;
1917 /*********************************************************************
1918 * _ultoa (CRTDLL.311)
1920 LPSTR __cdecl CRTDLL__ultoa(long x,LPSTR buf,INT32 radix)
1922 switch(radix) {
1923 case 2: FIXME(crtdll, "binary format not implemented !\n");
1924 break;
1925 case 8: wsnprintf32A(buf,0x80,"%lo",x);
1926 break;
1927 case 10: wsnprintf32A(buf,0x80,"%ld",x);
1928 break;
1929 case 16: wsnprintf32A(buf,0x80,"%lx",x);
1930 break;
1931 default: FIXME(crtdll, "radix %d not implemented !\n", radix);
1933 return buf;
1936 typedef VOID (*sig_handler_type)(VOID);
1938 /*********************************************************************
1939 * signal (CRTDLL.455)
1941 VOID __cdecl CRTDLL_signal(int sig, sig_handler_type ptr)
1943 FIXME(crtdll, "(%d %p):stub.\n", sig, ptr);
1946 /*********************************************************************
1947 * _ftol (CRTDLL.113)
1949 LONG __cdecl CRTDLL__ftol(double fl) {
1950 return (LONG)fl;
1952 /*********************************************************************
1953 * _sleep (CRTDLL.267)
1955 VOID __cdecl CRTDLL__sleep(unsigned long timeout)
1957 TRACE(crtdll,"CRTDLL__sleep for %ld milliseconds\n",timeout);
1958 Sleep((timeout)?timeout:1);
1961 /*********************************************************************
1962 * getenv (CRTDLL.437)
1964 LPSTR __cdecl CRTDLL_getenv(const char *name)
1966 LPSTR environ = GetEnvironmentStrings32A();
1967 LPSTR pp,pos = NULL;
1968 unsigned int length;
1970 for (pp = environ; (*pp); pp = pp + strlen(pp) +1)
1972 pos =strchr(pp,'=');
1973 if (pos)
1974 length = pos -pp;
1975 else
1976 length = strlen(pp);
1977 if (!strncmp(pp,name,length)) break;
1979 if ((pp)&& (pos))
1981 pp = pos+1;
1982 TRACE(crtdll,"got %s\n",pp);
1984 FreeEnvironmentStrings32A( environ );
1985 return pp;
1988 /*********************************************************************
1989 * _mbsrchr (CRTDLL.223)
1991 LPSTR __cdecl CRTDLL__mbsrchr(LPSTR s,CHAR x) {
1992 /* FIXME: handle multibyte strings */
1993 return strrchr(s,x);
1996 /*********************************************************************
1997 * _memicmp (CRTDLL.233)(NTDLL.868)
1998 * A stringcompare, without \0 check
1999 * RETURNS
2000 * -1:if first string is alphabetically before second string
2001 * 1:if second '' '' '' '' first ''
2002 * 0:if both are equal.
2004 INT32 __cdecl CRTDLL__memicmp(
2005 LPCSTR s1, /* [in] first string */
2006 LPCSTR s2, /* [in] second string */
2007 DWORD len /* [in] length to compare */
2008 ) {
2009 int i;
2011 for (i=0;i<len;i++) {
2012 if (tolower(s1[i])<tolower(s2[i]))
2013 return -1;
2014 if (tolower(s1[i])>tolower(s2[i]))
2015 return 1;
2017 return 0;
2019 /*********************************************************************
2020 * __dllonexit (CRTDLL.25)
2022 VOID __cdecl CRTDLL__dllonexit ()
2024 FIXME(crtdll,"stub\n");
2027 /*********************************************************************
2028 * wcstok (CRTDLL.519)
2029 * Like strtok, but for wide character strings. s is modified, yes.
2031 LPWSTR CRTDLL_wcstok(LPWSTR s,LPCWSTR delim) {
2032 static LPWSTR nexttok = NULL;
2033 LPWSTR x,ret;
2035 if (!s)
2036 s = nexttok;
2037 if (!s)
2038 return NULL;
2039 x = s;
2040 while (*x && !CRTDLL_wcschr(delim,*x))
2041 x++;
2042 ret = nexttok;
2043 if (*x) {
2044 *x='\0';
2045 nexttok = x+1;
2046 } else
2047 nexttok = NULL;
2048 return ret;
2051 /*********************************************************************
2052 * wcstol (CRTDLL.520)
2053 * Like strtol, but for wide character strings.
2055 INT32 CRTDLL_wcstol(LPWSTR s,LPWSTR *end,INT32 base) {
2056 LPSTR sA = HEAP_strdupWtoA(GetProcessHeap(),0,s),endA;
2057 INT32 ret = strtol(sA,&endA,base);
2059 HeapFree(GetProcessHeap(),0,sA);
2060 if (end) *end = s+(endA-sA); /* pointer magic checked. */
2061 return ret;
2063 /*********************************************************************
2064 * strdate (CRTDLL.283)
2066 LPSTR __cdecl CRTDLL__strdate (LPSTR date)
2067 { FIXME (crtdll,"%p stub\n", date);
2068 return 0;
2071 /*********************************************************************
2072 * strtime (CRTDLL.299)
2074 LPSTR __cdecl CRTDLL__strtime (LPSTR date)
2075 { FIXME (crtdll,"%p stub\n", date);
2076 return 0;