Release 970215
[wine/multimedia.git] / misc / crtdll.c
blobea185ac09a42f2b5dc914a5fc0566464bb06fca7
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 <string.h>
13 #include <unistd.h>
14 #include <time.h>
15 #include <ctype.h>
16 #include <math.h>
17 #include "win.h"
18 #include "windows.h"
19 #include "stddebug.h"
20 #include "debug.h"
21 #include "module.h"
22 #include "xmalloc.h"
23 #include "heap.h"
24 #include "crtdll.h"
25 #include "drive.h"
27 UINT32 CRTDLL_argc_dll; /* CRTDLL.23 */
28 LPSTR *CRTDLL_argv_dll; /* CRTDLL.24 */
29 LPSTR CRTDLL_acmdln_dll; /* CRTDLL.38 */
30 UINT32 CRTDLL_basemajor_dll; /* CRTDLL.42 */
31 UINT32 CRTDLL_baseminor_dll; /* CRTDLL.43 */
32 UINT32 CRTDLL_baseversion_dll; /* CRTDLL.44 */
33 LPSTR CRTDLL_environ_dll; /* CRTDLL.75 */
34 UINT32 CRTDLL_osmajor_dll; /* CRTDLL.241 */
35 UINT32 CRTDLL_osminor_dll; /* CRTDLL.242 */
36 UINT32 CRTDLL_osver_dll; /* CRTDLL.244 */
37 UINT32 CRTDLL_osversion_dll; /* CRTDLL.245 */
38 UINT32 CRTDLL_winmajor_dll; /* CRTDLL.329 */
39 UINT32 CRTDLL_winminor_dll; /* CRTDLL.330 */
40 UINT32 CRTDLL_winver_dll; /* CRTDLL.331 */
42 typedef VOID (*new_handler_type)(VOID);
44 static new_handler_type new_handler;
46 /*********************************************************************
47 * _GetMainArgs (CRTDLL.022)
49 DWORD
50 CRTDLL__GetMainArgs(LPDWORD argc,LPSTR **argv,LPSTR *environ,DWORD flag)
52 char *cmdline;
53 char **xargv;
54 int xargc,i,afterlastspace;
55 DWORD version;
57 dprintf_crtdll(stderr,"__GetMainArgs(%p,%p,%p,%ld).\n",
58 argc,argv,environ,flag
60 CRTDLL_acmdln_dll = cmdline = xstrdup( GetCommandLine32A() );
62 version = GetVersion32();
63 CRTDLL_osver_dll = version >> 16;
64 CRTDLL_winminor_dll = version & 0xFF;
65 CRTDLL_winmajor_dll = (version>>8) & 0xFF;
66 CRTDLL_baseversion_dll = version >> 16;
67 CRTDLL_winver_dll = ((version >> 8) & 0xFF) + ((version & 0xFF) << 8);
68 CRTDLL_baseminor_dll = (version >> 16) & 0xFF;
69 CRTDLL_basemajor_dll = (version >> 24) & 0xFF;
70 CRTDLL_osversion_dll = version & 0xFFFF;
71 CRTDLL_osminor_dll = version & 0xFF;
72 CRTDLL_osmajor_dll = (version>>8) & 0xFF;
74 /* missing threading init */
76 i=0;xargv=NULL;xargc=0;afterlastspace=0;
77 while (cmdline[i]) {
78 if (cmdline[i]==' ') {
79 xargv=(char**)xrealloc(xargv,sizeof(char*)*(++xargc));
80 cmdline[i]='\0';
81 xargv[xargc-1] = xstrdup(cmdline+afterlastspace);
82 i++;
83 while (cmdline[i]==' ')
84 i++;
85 if (cmdline[i])
86 afterlastspace=i;
87 } else
88 i++;
90 xargv=(char**)xrealloc(xargv,sizeof(char*)*(++xargc));
91 cmdline[i]='\0';
92 xargv[xargc-1] = xstrdup(cmdline+afterlastspace);
93 CRTDLL_argc_dll = xargc;
94 *argc = xargc;
95 CRTDLL_argv_dll = xargv;
96 *argv = xargv;
98 /* FIXME ... use real environment */
99 *environ = xmalloc(sizeof(LPSTR));
100 CRTDLL_environ_dll = *environ;
101 (*environ)[0] = NULL;
102 return 0;
105 typedef void (*_INITTERMFUN)();
107 /*********************************************************************
108 * _initterm (CRTDLL.135)
110 DWORD CRTDLL__initterm(_INITTERMFUN *start,_INITTERMFUN *end)
112 _INITTERMFUN *current;
114 dprintf_crtdll(stddeb,"_initterm(%p,%p)\n",start,end);
115 current=start;
116 while (current<end) {
117 if (*current) (*current)();
118 current++;
120 return 0;
123 /*********************************************************************
124 * srand (CRTDLL.460)
126 void CRTDLL_srand(DWORD seed)
128 /* FIXME: should of course be thread? process? local */
129 srand(seed);
132 /*********************************************************************
133 * fprintf (CRTDLL.373)
135 int CRTDLL_fprintf(DWORD *args)
137 /* FIXME: use args[0] */
138 return vfprintf(stderr,(LPSTR)(args[1]),args+2);
141 /*********************************************************************
142 * printf (CRTDLL.440)
144 int CRTDLL_printf(DWORD *args)
146 return vfprintf(stdout,(LPSTR)(args[0]),args+1);
149 /*********************************************************************
150 * sprintf (CRTDLL.458)
152 int CRTDLL_sprintf(DWORD *args)
154 return vsprintf((LPSTR)(args[0]),(LPSTR)(args[1]),args+2);
157 /*********************************************************************
158 * time (CRTDLL.488)
160 time_t CRTDLL_time(time_t *timeptr)
162 time_t curtime = time(NULL);
164 if (timeptr)
165 *timeptr = curtime;
166 return curtime;
169 /*********************************************************************
170 * _isatty (CRTDLL.137)
172 BOOL32 CRTDLL__isatty(DWORD x)
174 dprintf_crtdll(stderr,"CRTDLL__isatty(%ld)\n",x);
175 return TRUE;
178 /*********************************************************************
179 * _write (CRTDLL.332)
181 INT32 CRTDLL__write(DWORD x,LPVOID buf,DWORD len)
183 if (x<=2)
184 return write(x,buf,len);
185 /* hmm ... */
186 dprintf_crtdll(stderr,"CRTDLL__write(%ld,%p,%ld)\n",x,buf,len);
187 return len;
191 /*********************************************************************
192 * exit (CRTDLL.359)
194 void CRTDLL_exit(DWORD ret)
196 dprintf_crtdll(stderr,"CRTDLL_exit(%ld)\n",ret);
197 ExitProcess(ret);
201 /*********************************************************************
202 * fflush (CRTDLL.365)
204 void CRTDLL_fflush(DWORD x)
206 dprintf_crtdll(stderr,"CRTDLL_fflush(%ld)\n",x);
210 /*********************************************************************
211 * gets (CRTDLL.391)
213 LPSTR CRTDLL_gets(LPSTR buf)
215 /* BAD, for the whole WINE process blocks... just done this way to test
216 * windows95's ftp.exe.
218 return gets(buf);
222 /*********************************************************************
223 * abs (CRTDLL.339)
225 INT32 CRTDLL_abs(INT32 x)
227 return abs(x);
231 /*********************************************************************
232 * acos (CRTDLL.340)
234 float CRTDLL_acos(float x)
236 return acos(x);
240 /*********************************************************************
241 * asin (CRTDLL.342)
243 float CRTDLL_asin(float x)
245 return asin(x);
249 /*********************************************************************
250 * atan (CRTDLL.343)
252 float CRTDLL_atan(float x)
254 return atan(x);
258 /*********************************************************************
259 * atan2 (CRTDLL.344)
261 float CRTDLL_atan2(float x, float y)
263 return atan2(x,y);
267 /*********************************************************************
268 * atof (CRTDLL.346)
270 float CRTDLL_atof(LPCSTR x)
272 return atof(x);
276 /*********************************************************************
277 * atoi (CRTDLL.347)
279 INT32 CRTDLL_atoi(LPCSTR x)
281 return atoi(x);
285 /*********************************************************************
286 * atol (CRTDLL.348)
288 LONG CRTDLL_atol(LPCSTR x)
290 return atol(x);
294 /*********************************************************************
295 * cos (CRTDLL.354)
297 float CRTDLL_cos(float x)
299 return cos(x);
303 /*********************************************************************
304 * cosh (CRTDLL.355)
306 float CRTDLL_cosh(float x)
308 return cosh(x);
312 /*********************************************************************
313 * exp (CRTDLL.360)
315 float CRTDLL_exp(float x)
317 return exp(x);
321 /*********************************************************************
322 * fabs (CRTDLL.361)
324 float CRTDLL_fabs(float x)
326 return fabs(x);
330 /*********************************************************************
331 * isalnum (CRTDLL.394)
333 CHAR CRTDLL_isalnum(CHAR x)
335 return isalnum(x);
339 /*********************************************************************
340 * isalpha (CRTDLL.395)
342 CHAR CRTDLL_isalpha(CHAR x)
344 return isalpha(x);
348 /*********************************************************************
349 * iscntrl (CRTDLL.396)
351 CHAR CRTDLL_iscntrl(CHAR x)
353 return iscntrl(x);
357 /*********************************************************************
358 * isdigit (CRTDLL.397)
360 CHAR CRTDLL_isdigit(CHAR x)
362 return isdigit(x);
366 /*********************************************************************
367 * isgraph (CRTDLL.398)
369 CHAR CRTDLL_isgraph(CHAR x)
371 return isgraph(x);
375 /*********************************************************************
376 * islower (CRTDLL.400)
378 CHAR CRTDLL_islower(CHAR x)
380 return islower(x);
384 /*********************************************************************
385 * isprint (CRTDLL.401)
387 CHAR CRTDLL_isprint(CHAR x)
389 return isprint(x);
393 /*********************************************************************
394 * ispunct (CRTDLL.402)
396 CHAR CRTDLL_ispunct(CHAR x)
398 return ispunct(x);
402 /*********************************************************************
403 * isspace (CRTDLL.403)
405 CHAR CRTDLL_isspace(CHAR x)
407 return isspace(x);
411 /*********************************************************************
412 * isupper (CRTDLL.404)
414 CHAR CRTDLL_isupper(CHAR x)
416 return isupper(x);
420 /*********************************************************************
421 * isxdigit (CRTDLL.418)
423 CHAR CRTDLL_isxdigit(CHAR x)
425 return isxdigit(x);
429 /*********************************************************************
430 * labs (CRTDLL.419)
432 LONG CRTDLL_labs(LONG x)
434 return labs(x);
438 /*********************************************************************
439 * log (CRTDLL.424)
441 float CRTDLL_log(float x)
443 return log(x);
447 /*********************************************************************
448 * log10 (CRTDLL.425)
450 float CRTDLL_log10(float x)
452 return log10(x);
456 /*********************************************************************
457 * pow (CRTDLL.439)
459 float CRTDLL_pow(float x, float y)
461 return pow(x,y);
465 /*********************************************************************
466 * rand (CRTDLL.446)
468 INT32 CRTDLL_rand()
470 return rand();
474 /*********************************************************************
475 * sin (CRTDLL.456)
477 float CRTDLL_sin(float x)
479 return sin(x);
483 /*********************************************************************
484 * sinh (CRTDLL.457)
486 float CRTDLL_sinh(float x)
488 return sinh(x);
492 /*********************************************************************
493 * sqrt (CRTDLL.459)
495 float CRTDLL_sqrt(float x)
497 return sqrt(x);
501 /*********************************************************************
502 * tan (CRTDLL.486)
504 float CRTDLL_tan(float x)
506 return tan(x);
510 /*********************************************************************
511 * tanh (CRTDLL.487)
513 float CRTDLL_tanh(float x)
515 return tanh(x);
519 /*********************************************************************
520 * tolower (CRTDLL.491)
522 CHAR CRTDLL_tolower(CHAR x)
524 return tolower(x);
528 /*********************************************************************
529 * toupper (CRTDLL.492)
531 CHAR CRTDLL_toupper(CHAR x)
533 return toupper(x);
537 /*********************************************************************
538 * putchar (CRTDLL.442)
540 void CRTDLL_putchar(INT32 x)
542 putchar(x);
546 /*********************************************************************
547 * _mbsicmp (CRTDLL.204)
549 int CRTDLL__mbsicmp(unsigned char *x,unsigned char *y)
551 do {
552 if (!*x)
553 return !!*y;
554 if (!*y)
555 return !!*x;
556 /* FIXME: MBCS handling... */
557 if (*x!=*y)
558 return 1;
559 x++;
560 y++;
561 } while (1);
565 /*********************************************************************
566 * _mbsinc (CRTDLL.205)
568 unsigned char* CRTDLL__mbsinc(unsigned char *x)
570 /* FIXME: mbcs */
571 return x++;
575 /*********************************************************************
576 * vsprintf (CRTDLL.500)
578 int CRTDLL_vsprintf(DWORD *args)
580 return vsprintf((char *)args[0],(char *)args[1],args+2);
583 /*********************************************************************
584 * vsprintf (CRTDLL.500) (NTDLL.913)
586 int CRTDLL_sscanf(DWORD *args)
588 return vsscanf((char *)args[0],(char *)args[1],args+2);
592 /*********************************************************************
593 * _mbscpy (CRTDLL.200)
595 unsigned char* CRTDLL__mbscpy(unsigned char *x,unsigned char *y)
597 return strcpy(x,y);
601 /*********************************************************************
602 * _mbscat (CRTDLL.197)
604 unsigned char* CRTDLL__mbscat(unsigned char *x,unsigned char *y)
606 return strcat(x,y);
609 /*********************************************************************
610 * _strupr (CRTDLL.300)
612 LPSTR CRTDLL__strupr(LPSTR x)
614 LPSTR y=x;
616 while (*y) {
617 *y=toupper(*y);
618 y++;
620 return x;
623 /*********************************************************************
624 * _wcsupr (CRTDLL.328)
626 LPWSTR CRTDLL__wcsupr(LPWSTR x)
628 LPWSTR y=x;
630 while (*y) {
631 *y=toupper(*y);
632 y++;
634 return x;
637 /*********************************************************************
638 * _wcslwr (CRTDLL.323)
640 LPWSTR CRTDLL__wcslwr(LPWSTR x)
642 LPWSTR y=x;
644 while (*y) {
645 *y=tolower(*y);
646 y++;
648 return x;
652 /*********************************************************************
653 * malloc (CRTDLL.427)
655 VOID* CRTDLL_malloc(DWORD size)
657 return HeapAlloc(GetProcessHeap(),0,size);
660 /*********************************************************************
661 * new (CRTDLL.001)
663 VOID* CRTDLL_new(DWORD size)
665 VOID* result;
666 if(!(result = HeapAlloc(GetProcessHeap(),0,size)) && new_handler)
667 (*new_handler)();
668 return result;
671 /*********************************************************************
672 * set_new_handler(CRTDLL.003)
674 new_handler_type CRTDLL_set_new_handler(new_handler_type func)
676 new_handler_type old_handler = new_handler;
677 new_handler = func;
678 return old_handler;
681 /*********************************************************************
682 * calloc (CRTDLL.350)
684 VOID* CRTDLL_calloc(DWORD size, DWORD count)
686 return HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, size * count );
689 /*********************************************************************
690 * realloc (CRTDLL.447)
692 VOID* CRTDLL_realloc( VOID *ptr, DWORD size )
694 return HeapReAlloc( GetProcessHeap(), 0, ptr, size );
697 /*********************************************************************
698 * free (CRTDLL.427)
700 VOID CRTDLL_free(LPVOID ptr)
702 HeapFree(GetProcessHeap(),0,ptr);
705 /*********************************************************************
706 * delete (CRTDLL.002)
708 VOID CRTDLL_delete(VOID* ptr)
710 HeapFree(GetProcessHeap(),0,ptr);
713 /*********************************************************************
714 * _strdup (CRTDLL.285)
716 LPSTR CRTDLL__strdup(LPSTR ptr)
718 return HEAP_strdupA(GetProcessHeap(),0,ptr);
721 /*********************************************************************
722 * fclose (CRTDLL.362)
724 DWORD CRTDLL_fclose(LPVOID x)
726 dprintf_crtdll(stdnimp,"fclose(%p)\n",x);
727 return 0;
730 /*********************************************************************
731 * setlocale (CRTDLL.453)
733 LPSTR CRTDLL_setlocale(INT32 category,LPCSTR locale)
735 LPSTR categorystr;
737 switch (category) {
738 case CRTDLL_LC_ALL: categorystr="LC_ALL";break;
739 case CRTDLL_LC_COLLATE: categorystr="LC_COLLATE";break;
740 case CRTDLL_LC_CTYPE: categorystr="LC_CTYPE";break;
741 case CRTDLL_LC_MONETARY: categorystr="LC_MONETARY";break;
742 case CRTDLL_LC_NUMERIC: categorystr="LC_NUMERIC";break;
743 case CRTDLL_LC_TIME: categorystr="LC_TIME";break;
744 default: categorystr = "UNKNOWN?";break;
746 fprintf(stderr,"CRTDLL.setlocale(%s,%s),stub!\n",categorystr,locale);
747 return "C";
750 /*********************************************************************
751 * wcsspn (CRTDLL.516)
753 INT32 CRTDLL_wcsspn(LPWSTR str,LPWSTR accept)
755 LPWSTR s,t;
757 s=str;
758 do {
759 t=accept;
760 while (*t) { if (*t==*s) break;t++;}
761 if (!*t) break;
762 s++;
763 } while (*s);
764 return s-str; /* nr of wchars */
767 /*********************************************************************
768 * wcscspn (CRTDLL.508)
770 INT32 CRTDLL_wcscspn(LPWSTR str,LPWSTR reject)
772 LPWSTR s,t;
774 s=str;
775 do {
776 t=reject;
777 while (*t) { if (*t==*s) break;t++;}
778 if (*t) break;
779 s++;
780 } while (*s);
781 return s-str; /* nr of wchars */
784 /*********************************************************************
785 * wcschr (CRTDLL.504)
787 LPWSTR CRTDLL_wcschr(LPWSTR str,WCHAR xchar)
789 LPWSTR s;
791 s=str;
792 do {
793 if (*s==xchar)
794 return s;
795 } while (*s++);
796 return NULL;
799 /*********************************************************************
800 * towupper (CRTDLL.494)
802 WCHAR CRTDLL_towupper(WCHAR x)
804 return (WCHAR)toupper((CHAR)x);
807 /*********************************************************************
808 * swprintf (CRTDLL.483)
810 DWORD CRTDLL_swprintf(DWORD *args)
812 return WIN32_wsprintf32W(args);
815 /*********************************************************************
816 * _wcsicoll (CRTDLL.322)
818 DWORD CRTDLL__wcsicoll(LPWSTR a1,LPWSTR a2)
820 /* FIXME: handle collates */
821 return lstrcmpi32W(a1,a2);
824 /*********************************************************************
825 * wcscoll (CRTDLL.506)
827 DWORD CRTDLL_wcscoll(LPWSTR a1,LPWSTR a2)
829 /* FIXME: handle collates */
830 return lstrcmp32W(a1,a2);
833 /*********************************************************************
834 * _wcsrev (CRTDLL.326)
836 VOID CRTDLL__wcsrev(LPWSTR s) {
837 LPWSTR e;
839 e=s;
840 while (*e)
841 e++;
842 while (s<e) {
843 WCHAR a;
845 a=*s;*s=*e;*e=a;
846 s++;e--;
850 /*********************************************************************
851 * wcsstr (CRTDLL.517)
853 LPWSTR CRTDLL_wcsstr(LPWSTR s,LPWSTR b)
855 LPWSTR x,y,c;
857 x=s;
858 while (*x) {
859 if (*x==*b) {
860 y=x;c=b;
861 while (*y && *c && *y==*c) { c++;y++; }
862 if (!*c)
863 return x;
865 x++;
867 return NULL;
870 /*********************************************************************
871 * wcsrchr (CRTDLL.515)
873 LPWSTR CRTDLL_wcsrchr(LPWSTR str,WCHAR xchar)
875 LPWSTR s;
877 s=str+lstrlen32W(str);
878 do {
879 if (*s==xchar)
880 return s;
881 s--;
882 } while (s>=str);
883 return NULL;
886 /*********************************************************************
887 * _setmode (CRTDLL.265)
888 * FIXME: dunno what this is.
890 DWORD
891 CRTDLL__setmode(LPVOID x,INT32 y) {
892 /* FIXME */
893 fprintf(stdnimp,"CRTDLL._setmode(%p,%d), STUB.\n",x,y);
894 return 0;
897 /*********************************************************************
898 * atexit (CRTDLL.345)
900 INT32
901 CRTDLL_atexit(LPVOID x) {
902 /* FIXME */
903 fprintf(stdnimp,"CRTDLL.atexit(%p), STUB.\n",x);
904 return 0; /* successful */
907 /*********************************************************************
908 * mbtowc (CRTDLL.430)
909 * FIXME: check multibyte support
911 WCHAR
912 CRTDLL_mbtowc(CHAR a) {
913 return a;
916 /*********************************************************************
917 * _isctype (CRTDLL.138)
919 BOOL32
920 CRTDLL__isctype(CHAR x,CHAR type) {
921 if ((type & CRTDLL_SPACE) && isspace(x))
922 return TRUE;
923 if ((type & CRTDLL_PUNCT) && ispunct(x))
924 return TRUE;
925 if ((type & CRTDLL_LOWER) && islower(x))
926 return TRUE;
927 if ((type & CRTDLL_UPPER) && isupper(x))
928 return TRUE;
929 if ((type & CRTDLL_ALPHA) && isalpha(x))
930 return TRUE;
931 if ((type & CRTDLL_DIGIT) && isdigit(x))
932 return TRUE;
933 if ((type & CRTDLL_CONTROL) && iscntrl(x))
934 return TRUE;
935 /* check CRTDLL_LEADBYTE */
936 return FALSE;
939 /*********************************************************************
940 * _chdrive (CRTDLL.52)
942 BOOL32
943 CRTDLL__chdrive(INT32 newdrive) {
944 /* FIXME: generates errnos */
945 return DRIVE_SetCurrentDrive(newdrive);
948 /*********************************************************************
949 * _errno (CRTDLL.52)
950 * Yes, this is a function.
952 LPINT32
953 CRTDLL__errno() {
954 static int crtdllerrno;
955 extern int LastErrorToErrno(DWORD);
957 /* FIXME: we should set the error at the failing function call time */
958 crtdllerrno = LastErrorToErrno(GetLastError());
959 return &crtdllerrno;