blob: 6455e3976843d5897c6ae7c5fd6a62e912af3f43 [file] [log] [blame]
Jon Griffiths1db20bf2001-01-10 23:59:25 +00001/*
2 * msvcrt.dll misc functions
3 *
4 * Copyright 2000 Jon Griffiths
Alexandre Julliard0799c1a2002-03-09 23:29:33 +00005 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Jon Griffiths1db20bf2001-01-10 23:59:25 +000019 */
20
Alexandre Julliard2c8a52e2003-03-15 22:26:09 +000021#include "config.h"
22#include "wine/port.h"
23
Francois Gougete7f75c52001-04-10 23:25:25 +000024#include <stdlib.h>
Francois Gougetb29120d2001-04-23 18:20:55 +000025
26#include "msvcrt.h"
Alexandre Julliardbd1689e2002-01-22 00:57:16 +000027#include "wine/debug.h"
28
29WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
Jon Griffiths1db20bf2001-01-10 23:59:25 +000030
Jon Griffiths1db20bf2001-01-10 23:59:25 +000031
32/*********************************************************************
33 * _beep (MSVCRT.@)
34 */
Francois Gouget203a8f82001-04-10 21:16:07 +000035void _beep( unsigned int freq, unsigned int duration)
Jon Griffiths1db20bf2001-01-10 23:59:25 +000036{
37 TRACE(":Freq %d, Duration %d\n",freq,duration);
38 Beep(freq, duration);
39}
40
Jon Griffiths1db20bf2001-01-10 23:59:25 +000041/*********************************************************************
Alexandre Julliardad8cb612006-01-14 17:27:28 +010042 * srand (MSVCRT.@)
43 */
44void MSVCRT_srand( unsigned int seed )
45{
46 thread_data_t *data = msvcrt_get_thread_data();
47 data->random_seed = seed;
48}
49
50/*********************************************************************
Jon Griffiths1db20bf2001-01-10 23:59:25 +000051 * rand (MSVCRT.@)
52 */
Eric Pouechb379e422005-09-06 09:27:37 +000053int MSVCRT_rand(void)
Jon Griffiths1db20bf2001-01-10 23:59:25 +000054{
Alexandre Julliardad8cb612006-01-14 17:27:28 +010055 thread_data_t *data = msvcrt_get_thread_data();
56
57 /* this is the algorithm used by MSVC, according to
58 * http://en.wikipedia.org/wiki/List_of_pseudorandom_number_generators */
59 data->random_seed = data->random_seed * 214013 + 2531011;
60 return (data->random_seed >> 16) & MSVCRT_RAND_MAX;
Jon Griffiths1db20bf2001-01-10 23:59:25 +000061}
62
63/*********************************************************************
64 * _sleep (MSVCRT.@)
65 */
Francois Gouget203a8f82001-04-10 21:16:07 +000066void _sleep(unsigned long timeout)
Jon Griffiths1db20bf2001-01-10 23:59:25 +000067{
Francois Gouget203a8f82001-04-10 21:16:07 +000068 TRACE("_sleep for %ld milliseconds\n",timeout);
Jon Griffiths1db20bf2001-01-10 23:59:25 +000069 Sleep((timeout)?timeout:1);
70}
71
72/*********************************************************************
73 * _lfind (MSVCRT.@)
74 */
Francois Gouget203a8f82001-04-10 21:16:07 +000075void* _lfind(const void* match, const void* start,
76 unsigned int* array_size, unsigned int elem_size,
Alexandre Julliard3f5ff2f2002-12-18 20:17:20 +000077 int (*cf)(const void*,const void*) )
Jon Griffiths1db20bf2001-01-10 23:59:25 +000078{
79 unsigned int size = *array_size;
80 if (size)
81 do
82 {
83 if (cf(match, start) == 0)
84 return (void *)start; /* found */
Gregg Mattinson7c4cb512002-07-03 21:10:43 +000085 start = (char*)start + elem_size;
Jon Griffiths1db20bf2001-01-10 23:59:25 +000086 } while (--size);
87 return NULL;
88}
89
90/*********************************************************************
91 * _lsearch (MSVCRT.@)
92 */
Francois Gouget203a8f82001-04-10 21:16:07 +000093void* _lsearch(const void* match, void* start,
94 unsigned int* array_size, unsigned int elem_size,
Alexandre Julliard3f5ff2f2002-12-18 20:17:20 +000095 int (*cf)(const void*,const void*) )
Jon Griffiths1db20bf2001-01-10 23:59:25 +000096{
97 unsigned int size = *array_size;
98 if (size)
99 do
100 {
101 if (cf(match, start) == 0)
102 return start; /* found */
Gregg Mattinson7c4cb512002-07-03 21:10:43 +0000103 start = (char*)start + elem_size;
Jon Griffiths1db20bf2001-01-10 23:59:25 +0000104 } while (--size);
105
106 /* not found, add to end */
107 memcpy(start, match, elem_size);
108 array_size[0]++;
109 return start;
110}
111
112/*********************************************************************
113 * _chkesp (MSVCRT.@)
Alexandre Julliard2c8a52e2003-03-15 22:26:09 +0000114 *
115 * Trap to a debugger if the value of the stack pointer has changed.
116 *
117 * PARAMS
118 * None.
119 *
120 * RETURNS
121 * Does not return.
122 *
123 * NOTES
124 * This function is available for iX86 only.
125 *
126 * When VC++ generates debug code, it stores the value of the stack pointer
127 * before calling any external function, and checks the value following
128 * the call. It then calls this function, which will trap if the values are
129 * not the same. Usually this means that the prototype used to call
130 * the function is incorrect. It can also mean that the .spec entry has
131 * the wrong calling convention or parameters.
Jon Griffiths1db20bf2001-01-10 23:59:25 +0000132 */
Alexandre Julliard2c8a52e2003-03-15 22:26:09 +0000133#ifdef __i386__
Jon Griffiths1db20bf2001-01-10 23:59:25 +0000134
Alexandre Julliard2c8a52e2003-03-15 22:26:09 +0000135# ifdef __GNUC__
Patrik Stridvall070db4e2004-05-17 21:08:31 +0000136
Alexandre Julliard2c8a52e2003-03-15 22:26:09 +0000137__ASM_GLOBAL_FUNC(_chkesp,
138 "jnz 1f\n\t"
139 "ret\n"
140 "1:\tpushl %ebp\n\t"
141 "movl %esp,%ebp\n\t"
Alexandre Julliardcc01e8f2005-11-23 19:55:06 +0100142 "subl $12,%esp\n\t"
Alexandre Julliard2c8a52e2003-03-15 22:26:09 +0000143 "pushl %eax\n\t"
144 "pushl %ecx\n\t"
145 "pushl %edx\n\t"
146 "call " __ASM_NAME("MSVCRT_chkesp_fail") "\n\t"
147 "popl %edx\n\t"
148 "popl %ecx\n\t"
149 "popl %eax\n\t"
Alexandre Julliardcc01e8f2005-11-23 19:55:06 +0100150 "leave\n\t"
Alexandre Julliard2c8a52e2003-03-15 22:26:09 +0000151 "ret");
152
153void MSVCRT_chkesp_fail(void)
154{
155 ERR("Stack pointer incorrect after last function call - Bad prototype/spec entry?\n");
156 DebugBreak();
Jon Griffiths1db20bf2001-01-10 23:59:25 +0000157}
Alexandre Julliard2c8a52e2003-03-15 22:26:09 +0000158
159# else /* __GNUC__ */
Patrik Stridvall070db4e2004-05-17 21:08:31 +0000160
161/**********************************************************************/
162
163void _chkesp(void)
164{
165}
166
Alexandre Julliard2c8a52e2003-03-15 22:26:09 +0000167# endif /* __GNUC__ */
168
169#endif /* __i386__ */