Release 970804
[wine/multimedia.git] / misc / crtdll.c
blob0015d92dc88e036cb33c62a0bd970fc9c0916dcd
1 /*
2 * The C RunTime DLL
3 *
4 * Implements C run-time functionality as known from UNIX.
6 * Copyright 1996 Marcus Meissner
7 * Copyright 1996 Jukka Iivonen
8 */
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <stdarg.h>
13 #include <string.h>
14 #include <unistd.h>
15 #include <time.h>
16 #include <ctype.h>
17 #include <math.h>
18 #include "win.h"
19 #include "windows.h"
20 #include "stddebug.h"
21 #include "debug.h"
22 #include "module.h"
23 #include "xmalloc.h"
24 #include "heap.h"
25 #include "crtdll.h"
26 #include "drive.h"
28 extern INT32 WIN32_wsprintf32W( DWORD *args );
30 UINT32 CRTDLL_argc_dll; /* CRTDLL.23 */
31 LPSTR *CRTDLL_argv_dll; /* CRTDLL.24 */
32 LPSTR CRTDLL_acmdln_dll; /* CRTDLL.38 */
33 UINT32 CRTDLL_basemajor_dll; /* CRTDLL.42 */
34 UINT32 CRTDLL_baseminor_dll; /* CRTDLL.43 */
35 UINT32 CRTDLL_baseversion_dll; /* CRTDLL.44 */
36 LPSTR CRTDLL_environ_dll; /* CRTDLL.75 */
37 UINT32 CRTDLL_osmajor_dll; /* CRTDLL.241 */
38 UINT32 CRTDLL_osminor_dll; /* CRTDLL.242 */
39 UINT32 CRTDLL_osver_dll; /* CRTDLL.244 */
40 UINT32 CRTDLL_osversion_dll; /* CRTDLL.245 */
41 UINT32 CRTDLL_winmajor_dll; /* CRTDLL.329 */
42 UINT32 CRTDLL_winminor_dll; /* CRTDLL.330 */
43 UINT32 CRTDLL_winver_dll; /* CRTDLL.331 */
45 typedef VOID (*new_handler_type)(VOID);
47 static new_handler_type new_handler;
49 /*********************************************************************
50 * _GetMainArgs (CRTDLL.022)
52 DWORD
53 CRTDLL__GetMainArgs(LPDWORD argc,LPSTR **argv,LPSTR *environ,DWORD flag)
55 char *cmdline;
56 char **xargv;
57 int xargc,i,afterlastspace;
58 DWORD version;
60 dprintf_crtdll(stderr,"__GetMainArgs(%p,%p,%p,%ld).\n",
61 argc,argv,environ,flag
63 CRTDLL_acmdln_dll = cmdline = xstrdup( GetCommandLine32A() );
65 version = GetVersion32();
66 CRTDLL_osver_dll = version >> 16;
67 CRTDLL_winminor_dll = version & 0xFF;
68 CRTDLL_winmajor_dll = (version>>8) & 0xFF;
69 CRTDLL_baseversion_dll = version >> 16;
70 CRTDLL_winver_dll = ((version >> 8) & 0xFF) + ((version & 0xFF) << 8);
71 CRTDLL_baseminor_dll = (version >> 16) & 0xFF;
72 CRTDLL_basemajor_dll = (version >> 24) & 0xFF;
73 CRTDLL_osversion_dll = version & 0xFFFF;
74 CRTDLL_osminor_dll = version & 0xFF;
75 CRTDLL_osmajor_dll = (version>>8) & 0xFF;
77 /* missing threading init */
79 i=0;xargv=NULL;xargc=0;afterlastspace=0;
80 while (cmdline[i]) {
81 if (cmdline[i]==' ') {
82 xargv=(char**)xrealloc(xargv,sizeof(char*)*(++xargc));
83 cmdline[i]='\0';
84 xargv[xargc-1] = xstrdup(cmdline+afterlastspace);
85 i++;
86 while (cmdline[i]==' ')
87 i++;
88 if (cmdline[i])
89 afterlastspace=i;
90 } else
91 i++;
93 xargv=(char**)xrealloc(xargv,sizeof(char*)*(++xargc));
94 cmdline[i]='\0';
95 xargv[xargc-1] = xstrdup(cmdline+afterlastspace);
96 CRTDLL_argc_dll = xargc;
97 *argc = xargc;
98 CRTDLL_argv_dll = xargv;
99 *argv = xargv;
101 /* FIXME ... use real environment */
102 *environ = xmalloc(sizeof(LPSTR));
103 CRTDLL_environ_dll = *environ;
104 (*environ)[0] = NULL;
105 return 0;
108 typedef void (*_INITTERMFUN)();
110 /*********************************************************************
111 * _initterm (CRTDLL.135)
113 DWORD CRTDLL__initterm(_INITTERMFUN *start,_INITTERMFUN *end)
115 _INITTERMFUN *current;
117 dprintf_crtdll(stddeb,"_initterm(%p,%p)\n",start,end);
118 current=start;
119 while (current<end) {
120 if (*current) (*current)();
121 current++;
123 return 0;
126 /*********************************************************************
127 * srand (CRTDLL.460)
129 void CRTDLL_srand(DWORD seed)
131 /* FIXME: should of course be thread? process? local */
132 srand(seed);
135 /*********************************************************************
136 * fprintf (CRTDLL.373)
138 int CRTDLL_fprintf(DWORD *args)
140 /* FIXME: use args[0] */
141 /* CMF - This makes a BIG assumption about va_list */
142 return vfprintf(stderr, (LPSTR) args[1], (va_list) &args[2]);
145 /*********************************************************************
146 * printf (CRTDLL.440)
148 int CRTDLL_printf(DWORD *args)
150 /* CMF - This makes a BIG assumption about va_list */
151 return vfprintf(stdout, (LPSTR) args[0], (va_list) &args[1]);
154 /*********************************************************************
155 * sprintf (CRTDLL.458)
157 int CRTDLL_sprintf(DWORD *args)
159 /* CMF - This makes a BIG assumption about va_list */
160 return vsprintf((LPSTR) args[0], (LPSTR) args[1], (va_list) &args[2]);
163 /*********************************************************************
164 * time (CRTDLL.488)
166 time_t CRTDLL_time(time_t *timeptr)
168 time_t curtime = time(NULL);
170 if (timeptr)
171 *timeptr = curtime;
172 return curtime;
175 /*********************************************************************
176 * _isatty (CRTDLL.137)
178 BOOL32 CRTDLL__isatty(DWORD x)
180 dprintf_crtdll(stderr,"CRTDLL__isatty(%ld)\n",x);
181 return TRUE;
184 /*********************************************************************
185 * _write (CRTDLL.332)
187 INT32 CRTDLL__write(DWORD x,LPVOID buf,DWORD len)
189 if (x<=2)
190 return write(x,buf,len);
191 /* hmm ... */
192 dprintf_crtdll(stderr,"CRTDLL__write(%ld,%p,%ld)\n",x,buf,len);
193 return len;
197 /*********************************************************************
198 * exit (CRTDLL.359)
200 void CRTDLL_exit(DWORD ret)
202 dprintf_crtdll(stderr,"CRTDLL_exit(%ld)\n",ret);
203 ExitProcess(ret);
207 /*********************************************************************
208 * fflush (CRTDLL.365)
210 void CRTDLL_fflush(DWORD x)
212 dprintf_crtdll(stderr,"CRTDLL_fflush(%ld)\n",x);
216 /*********************************************************************
217 * gets (CRTDLL.391)
219 LPSTR CRTDLL_gets(LPSTR buf)
221 /* BAD, for the whole WINE process blocks... just done this way to test
222 * windows95's ftp.exe.
224 return gets(buf);
228 /*********************************************************************
229 * abs (CRTDLL.339)
231 INT32 CRTDLL_abs(INT32 x)
233 return abs(x);
237 /*********************************************************************
238 * acos (CRTDLL.340)
240 float CRTDLL_acos(float x)
242 return acos(x);
246 /*********************************************************************
247 * asin (CRTDLL.342)
249 float CRTDLL_asin(float x)
251 return asin(x);
255 /*********************************************************************
256 * atan (CRTDLL.343)
258 float CRTDLL_atan(float x)
260 return atan(x);
264 /*********************************************************************
265 * atan2 (CRTDLL.344)
267 float CRTDLL_atan2(float x, float y)
269 return atan2(x,y);
273 /*********************************************************************
274 * atof (CRTDLL.346)
276 float CRTDLL_atof(LPCSTR x)
278 return atof(x);
282 /*********************************************************************
283 * atoi (CRTDLL.347)
285 INT32 CRTDLL_atoi(LPCSTR x)
287 return atoi(x);
291 /*********************************************************************
292 * atol (CRTDLL.348)
294 LONG CRTDLL_atol(LPCSTR x)
296 return atol(x);
300 /*********************************************************************
301 * cos (CRTDLL.354)
303 float CRTDLL_cos(float x)
305 return cos(x);
309 /*********************************************************************
310 * cosh (CRTDLL.355)
312 float CRTDLL_cosh(float x)
314 return cosh(x);
318 /*********************************************************************
319 * exp (CRTDLL.360)
321 float CRTDLL_exp(float x)
323 return exp(x);
327 /*********************************************************************
328 * fabs (CRTDLL.361)
330 float CRTDLL_fabs(float x)
332 return fabs(x);
336 /*********************************************************************
337 * isalnum (CRTDLL.394)
339 CHAR CRTDLL_isalnum(CHAR x)
341 return isalnum(x);
345 /*********************************************************************
346 * isalpha (CRTDLL.395)
348 CHAR CRTDLL_isalpha(CHAR x)
350 return isalpha(x);
354 /*********************************************************************
355 * iscntrl (CRTDLL.396)
357 CHAR CRTDLL_iscntrl(CHAR x)
359 return iscntrl(x);
363 /*********************************************************************
364 * isdigit (CRTDLL.397)
366 CHAR CRTDLL_isdigit(CHAR x)
368 return isdigit(x);
372 /*********************************************************************
373 * isgraph (CRTDLL.398)
375 CHAR CRTDLL_isgraph(CHAR x)
377 return isgraph(x);
381 /*********************************************************************
382 * islower (CRTDLL.400)
384 CHAR CRTDLL_islower(CHAR x)
386 return islower(x);
390 /*********************************************************************
391 * isprint (CRTDLL.401)
393 CHAR CRTDLL_isprint(CHAR x)
395 return isprint(x);
399 /*********************************************************************
400 * ispunct (CRTDLL.402)
402 CHAR CRTDLL_ispunct(CHAR x)
404 return ispunct(x);
408 /*********************************************************************
409 * isspace (CRTDLL.403)
411 CHAR CRTDLL_isspace(CHAR x)
413 return isspace(x);
417 /*********************************************************************
418 * isupper (CRTDLL.404)
420 CHAR CRTDLL_isupper(CHAR x)
422 return isupper(x);
426 /*********************************************************************
427 * isxdigit (CRTDLL.418)
429 CHAR CRTDLL_isxdigit(CHAR x)
431 return isxdigit(x);
435 /*********************************************************************
436 * labs (CRTDLL.419)
438 LONG CRTDLL_labs(LONG x)
440 return labs(x);
444 /*********************************************************************
445 * log (CRTDLL.424)
447 float CRTDLL_log(float x)
449 return log(x);
453 /*********************************************************************
454 * log10 (CRTDLL.425)
456 float CRTDLL_log10(float x)
458 return log10(x);
462 /*********************************************************************
463 * pow (CRTDLL.439)
465 float CRTDLL_pow(float x, float y)
467 return pow(x,y);
471 /*********************************************************************
472 * rand (CRTDLL.446)
474 INT32 CRTDLL_rand()
476 return rand();
480 /*********************************************************************
481 * sin (CRTDLL.456)
483 float CRTDLL_sin(float x)
485 return sin(x);
489 /*********************************************************************
490 * sinh (CRTDLL.457)
492 float CRTDLL_sinh(float x)
494 return sinh(x);
498 /*********************************************************************
499 * sqrt (CRTDLL.459)
501 float CRTDLL_sqrt(float x)
503 return sqrt(x);
507 /*********************************************************************
508 * tan (CRTDLL.486)
510 float CRTDLL_tan(float x)
512 return tan(x);
516 /*********************************************************************
517 * tanh (CRTDLL.487)
519 float CRTDLL_tanh(float x)
521 return tanh(x);
525 /*********************************************************************
526 * tolower (CRTDLL.491)
528 CHAR CRTDLL_tolower(CHAR x)
530 return tolower(x);
534 /*********************************************************************
535 * toupper (CRTDLL.492)
537 CHAR CRTDLL_toupper(CHAR x)
539 return toupper(x);
543 /*********************************************************************
544 * putchar (CRTDLL.442)
546 void CRTDLL_putchar(INT32 x)
548 putchar(x);
552 /*********************************************************************
553 * _mbsicmp (CRTDLL.204)
555 int CRTDLL__mbsicmp(unsigned char *x,unsigned char *y)
557 do {
558 if (!*x)
559 return !!*y;
560 if (!*y)
561 return !!*x;
562 /* FIXME: MBCS handling... */
563 if (*x!=*y)
564 return 1;
565 x++;
566 y++;
567 } while (1);
571 /*********************************************************************
572 * _mbsinc (CRTDLL.205)
574 unsigned char* CRTDLL__mbsinc(unsigned char *x)
576 /* FIXME: mbcs */
577 return x++;
581 /*********************************************************************
582 * vsprintf (CRTDLL.500)
584 int CRTDLL_vsprintf(DWORD *args)
586 /* CMF - This makes a BIG assumption about va_list */
587 return vsprintf((char *) args[0], (char *) args[1], (va_list) &args[2]);
590 /*********************************************************************
591 * vsprintf (CRTDLL.500) (NTDLL.913)
593 int CRTDLL_sscanf(DWORD *args)
595 /* CMF - This makes a BIG assumption about va_list */
596 return vsscanf((char *) args[0], (char *) args[1], (va_list) &args[2]);
600 /*********************************************************************
601 * _mbscpy (CRTDLL.200)
603 unsigned char* CRTDLL__mbscpy(unsigned char *x,unsigned char *y)
605 return strcpy(x,y);
609 /*********************************************************************
610 * _mbscat (CRTDLL.197)
612 unsigned char* CRTDLL__mbscat(unsigned char *x,unsigned char *y)
614 return strcat(x,y);
617 /*********************************************************************
618 * _strupr (CRTDLL.300)
620 LPSTR CRTDLL__strupr(LPSTR x)
622 LPSTR y=x;
624 while (*y) {
625 *y=toupper(*y);
626 y++;
628 return x;
631 /*********************************************************************
632 * _wcsupr (CRTDLL.328)
634 LPWSTR CRTDLL__wcsupr(LPWSTR x)
636 LPWSTR y=x;
638 while (*y) {
639 *y=toupper(*y);
640 y++;
642 return x;
645 /*********************************************************************
646 * _wcslwr (CRTDLL.323)
648 LPWSTR CRTDLL__wcslwr(LPWSTR x)
650 LPWSTR y=x;
652 while (*y) {
653 *y=tolower(*y);
654 y++;
656 return x;
660 /*********************************************************************
661 * malloc (CRTDLL.427)
663 VOID* CRTDLL_malloc(DWORD size)
665 return HeapAlloc(GetProcessHeap(),0,size);
668 /*********************************************************************
669 * new (CRTDLL.001)
671 VOID* CRTDLL_new(DWORD size)
673 VOID* result;
674 if(!(result = HeapAlloc(GetProcessHeap(),0,size)) && new_handler)
675 (*new_handler)();
676 return result;
679 /*********************************************************************
680 * set_new_handler(CRTDLL.003)
682 new_handler_type CRTDLL_set_new_handler(new_handler_type func)
684 new_handler_type old_handler = new_handler;
685 new_handler = func;
686 return old_handler;
689 /*********************************************************************
690 * calloc (CRTDLL.350)
692 VOID* CRTDLL_calloc(DWORD size, DWORD count)
694 return HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, size * count );
697 /*********************************************************************
698 * realloc (CRTDLL.447)
700 VOID* CRTDLL_realloc( VOID *ptr, DWORD size )
702 return HeapReAlloc( GetProcessHeap(), 0, ptr, size );
705 /*********************************************************************
706 * free (CRTDLL.427)
708 VOID CRTDLL_free(LPVOID ptr)
710 HeapFree(GetProcessHeap(),0,ptr);
713 /*********************************************************************
714 * delete (CRTDLL.002)
716 VOID CRTDLL_delete(VOID* ptr)
718 HeapFree(GetProcessHeap(),0,ptr);
721 /*********************************************************************
722 * _strdup (CRTDLL.285)
724 LPSTR CRTDLL__strdup(LPSTR ptr)
726 return HEAP_strdupA(GetProcessHeap(),0,ptr);
729 /*********************************************************************
730 * fclose (CRTDLL.362)
732 DWORD CRTDLL_fclose(LPVOID x)
734 dprintf_crtdll(stdnimp,"fclose(%p)\n",x);
735 return 0;
738 /*********************************************************************
739 * setlocale (CRTDLL.453)
741 LPSTR CRTDLL_setlocale(INT32 category,LPCSTR locale)
743 LPSTR categorystr;
745 switch (category) {
746 case CRTDLL_LC_ALL: categorystr="LC_ALL";break;
747 case CRTDLL_LC_COLLATE: categorystr="LC_COLLATE";break;
748 case CRTDLL_LC_CTYPE: categorystr="LC_CTYPE";break;
749 case CRTDLL_LC_MONETARY: categorystr="LC_MONETARY";break;
750 case CRTDLL_LC_NUMERIC: categorystr="LC_NUMERIC";break;
751 case CRTDLL_LC_TIME: categorystr="LC_TIME";break;
752 default: categorystr = "UNKNOWN?";break;
754 fprintf(stderr,"CRTDLL.setlocale(%s,%s),stub!\n",categorystr,locale);
755 return "C";
758 /*********************************************************************
759 * wcsspn (CRTDLL.516)
761 INT32 CRTDLL_wcsspn(LPWSTR str,LPWSTR accept)
763 LPWSTR s,t;
765 s=str;
766 do {
767 t=accept;
768 while (*t) { if (*t==*s) break;t++;}
769 if (!*t) break;
770 s++;
771 } while (*s);
772 return s-str; /* nr of wchars */
775 /*********************************************************************
776 * wcscspn (CRTDLL.508)
778 INT32 CRTDLL_wcscspn(LPWSTR str,LPWSTR reject)
780 LPWSTR s,t;
782 s=str;
783 do {
784 t=reject;
785 while (*t) { if (*t==*s) break;t++;}
786 if (*t) break;
787 s++;
788 } while (*s);
789 return s-str; /* nr of wchars */
792 /*********************************************************************
793 * wcschr (CRTDLL.504)
795 LPWSTR CRTDLL_wcschr(LPWSTR str,WCHAR xchar)
797 LPWSTR s;
799 s=str;
800 do {
801 if (*s==xchar)
802 return s;
803 } while (*s++);
804 return NULL;
807 /*********************************************************************
808 * towupper (CRTDLL.494)
810 WCHAR CRTDLL_towupper(WCHAR x)
812 return (WCHAR)toupper((CHAR)x);
815 /*********************************************************************
816 * swprintf (CRTDLL.483)
818 DWORD CRTDLL_swprintf(DWORD *args)
820 return WIN32_wsprintf32W(args);
823 /*********************************************************************
824 * _wcsicoll (CRTDLL.322)
826 DWORD CRTDLL__wcsicoll(LPWSTR a1,LPWSTR a2)
828 /* FIXME: handle collates */
829 return lstrcmpi32W(a1,a2);
832 /*********************************************************************
833 * wcscoll (CRTDLL.506)
835 DWORD CRTDLL_wcscoll(LPWSTR a1,LPWSTR a2)
837 /* FIXME: handle collates */
838 return lstrcmp32W(a1,a2);
841 /*********************************************************************
842 * _wcsrev (CRTDLL.326)
844 VOID CRTDLL__wcsrev(LPWSTR s) {
845 LPWSTR e;
847 e=s;
848 while (*e)
849 e++;
850 while (s<e) {
851 WCHAR a;
853 a=*s;*s=*e;*e=a;
854 s++;e--;
858 /*********************************************************************
859 * wcsstr (CRTDLL.517)
861 LPWSTR CRTDLL_wcsstr(LPWSTR s,LPWSTR b)
863 LPWSTR x,y,c;
865 x=s;
866 while (*x) {
867 if (*x==*b) {
868 y=x;c=b;
869 while (*y && *c && *y==*c) { c++;y++; }
870 if (!*c)
871 return x;
873 x++;
875 return NULL;
878 /*********************************************************************
879 * wcsrchr (CRTDLL.515)
881 LPWSTR CRTDLL_wcsrchr(LPWSTR str,WCHAR xchar)
883 LPWSTR s;
885 s=str+lstrlen32W(str);
886 do {
887 if (*s==xchar)
888 return s;
889 s--;
890 } while (s>=str);
891 return NULL;
894 /*********************************************************************
895 * _setmode (CRTDLL.265)
896 * FIXME: dunno what this is.
898 DWORD
899 CRTDLL__setmode(LPVOID x,INT32 y) {
900 /* FIXME */
901 fprintf(stdnimp,"CRTDLL._setmode(%p,%d), STUB.\n",x,y);
902 return 0;
905 /*********************************************************************
906 * atexit (CRTDLL.345)
908 INT32
909 CRTDLL_atexit(LPVOID x) {
910 /* FIXME */
911 fprintf(stdnimp,"CRTDLL.atexit(%p), STUB.\n",x);
912 return 0; /* successful */
915 /*********************************************************************
916 * mbstowcs (CRTDLL.429)
917 * FIXME: check multibyte support
919 INT32
920 CRTDLL_mbstowcs(LPWSTR a,LPSTR b,INT32 nr) {
921 int i;
922 for (i=0;(i<nr) && b[i];i++) {
923 a[i] = (WCHAR)b[i];
925 return i;
928 /*********************************************************************
929 * mbtowc (CRTDLL.430)
930 * FIXME: check multibyte support
932 WCHAR
933 CRTDLL_mbtowc(CHAR a) {
934 return a;
937 /*********************************************************************
938 * _isctype (CRTDLL.138)
940 BOOL32
941 CRTDLL__isctype(CHAR x,CHAR type) {
942 if ((type & CRTDLL_SPACE) && isspace(x))
943 return TRUE;
944 if ((type & CRTDLL_PUNCT) && ispunct(x))
945 return TRUE;
946 if ((type & CRTDLL_LOWER) && islower(x))
947 return TRUE;
948 if ((type & CRTDLL_UPPER) && isupper(x))
949 return TRUE;
950 if ((type & CRTDLL_ALPHA) && isalpha(x))
951 return TRUE;
952 if ((type & CRTDLL_DIGIT) && isdigit(x))
953 return TRUE;
954 if ((type & CRTDLL_CONTROL) && iscntrl(x))
955 return TRUE;
956 /* check CRTDLL_LEADBYTE */
957 return FALSE;
960 /*********************************************************************
961 * _chdrive (CRTDLL.52)
963 BOOL32
964 CRTDLL__chdrive(INT32 newdrive) {
965 /* FIXME: generates errnos */
966 return DRIVE_SetCurrentDrive(newdrive);
969 /*********************************************************************
970 * _chdir (CRTDLL.51)
972 INT32
973 CRTDLL__chdir(LPCSTR newdir) {
974 if (!SetCurrentDirectory32A(newdir))
975 return -1;
976 return 0;
979 /*********************************************************************
980 * _mkdir (CRTDLL.234)
982 INT32
983 CRTDLL__mkdir(LPCSTR newdir) {
984 if (!CreateDirectory32A(newdir,NULL))
985 return -1;
986 return 0;
989 /*********************************************************************
990 * _errno (CRTDLL.52)
991 * Yes, this is a function.
993 LPINT32
994 CRTDLL__errno() {
995 static int crtdllerrno;
996 extern int LastErrorToErrno(DWORD);
998 /* FIXME: we should set the error at the failing function call time */
999 crtdllerrno = LastErrorToErrno(GetLastError());
1000 return &crtdllerrno;