2 * msvcrt.dll misc functions
4 * Copyright 2000 Jon Griffiths
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.
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.
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include "wine/port.h"
27 #include "wine/debug.h"
30 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
33 /*********************************************************************
36 void CDECL MSVCRT__beep( unsigned int freq, unsigned int duration)
38 TRACE(":Freq %d, Duration %d\n",freq,duration);
42 /*********************************************************************
45 void CDECL MSVCRT_srand( unsigned int seed )
47 thread_data_t *data = msvcrt_get_thread_data();
48 data->random_seed = seed;
51 /*********************************************************************
54 int CDECL MSVCRT_rand(void)
56 thread_data_t *data = msvcrt_get_thread_data();
58 /* this is the algorithm used by MSVC, according to
59 * http://en.wikipedia.org/wiki/List_of_pseudorandom_number_generators */
60 data->random_seed = data->random_seed * 214013 + 2531011;
61 return (data->random_seed >> 16) & MSVCRT_RAND_MAX;
64 /*********************************************************************
67 int CDECL MSVCRT_rand_s(unsigned int *pval)
69 if (!pval || !RtlGenRandom(pval, sizeof(*pval)))
71 *MSVCRT__errno() = MSVCRT_EINVAL;
77 /*********************************************************************
80 void CDECL MSVCRT__sleep(MSVCRT_ulong timeout)
82 TRACE("_sleep for %d milliseconds\n",timeout);
83 Sleep((timeout)?timeout:1);
86 /*********************************************************************
89 void* CDECL _lfind(const void* match, const void* start,
90 unsigned int* array_size, unsigned int elem_size,
91 int (CDECL *cf)(const void*,const void*) )
93 unsigned int size = *array_size;
97 if (cf(match, start) == 0)
98 return (void *)start; /* found */
99 start = (const char *)start + elem_size;
104 /*********************************************************************
105 * _lsearch (MSVCRT.@)
107 void* CDECL _lsearch(const void* match, void* start,
108 unsigned int* array_size, unsigned int elem_size,
109 int (CDECL *cf)(const void*,const void*) )
111 unsigned int size = *array_size;
115 if (cf(match, start) == 0)
116 return start; /* found */
117 start = (char*)start + elem_size;
120 /* not found, add to end */
121 memcpy(start, match, elem_size);
126 /*********************************************************************
129 * Trap to a debugger if the value of the stack pointer has changed.
138 * This function is available for iX86 only.
140 * When VC++ generates debug code, it stores the value of the stack pointer
141 * before calling any external function, and checks the value following
142 * the call. It then calls this function, which will trap if the values are
143 * not the same. Usually this means that the prototype used to call
144 * the function is incorrect. It can also mean that the .spec entry has
145 * the wrong calling convention or parameters.
151 __ASM_GLOBAL_FUNC(_chkesp,
155 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
156 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
158 __ASM_CFI(".cfi_def_cfa_register %ebp\n\t")
163 "call " __ASM_NAME("MSVCRT_chkesp_fail") "\n\t"
168 __ASM_CFI(".cfi_def_cfa %esp,4\n\t")
169 __ASM_CFI(".cfi_same_value %ebp\n\t")
172 void CDECL MSVCRT_chkesp_fail(void)
174 ERR("Stack pointer incorrect after last function call - Bad prototype/spec entry?\n");
178 # else /* __GNUC__ */
180 /**********************************************************************/
182 void CDECL _chkesp(void)
186 # endif /* __GNUC__ */
188 #endif /* __i386__ */
190 /*********************************************************************
191 * Helper function for MSVCRT_qsort_s.
193 * Based on NTDLL_qsort in dlls/ntdll/misc.c
195 static void MSVCRT_mergesort( void *arr, void *barr, size_t elemsize,
196 int (CDECL *compar)(void *, const void *, const void *),
197 size_t left, size_t right, void *context )
201 m=left+(right-left)/2;
202 MSVCRT_mergesort(arr, barr, elemsize, compar, left, m, context);
203 MSVCRT_mergesort(arr, barr, elemsize, compar, m+1, right, context);
205 #define X(a,i) ((char*)a+elemsize*(i))
206 for (i=m+1; i>left; i--)
207 memcpy (X(barr,(i-1)),X(arr,(i-1)),elemsize);
208 for (j=m; j<right; j++)
209 memcpy (X(barr,(right+m-j)),X(arr,(j+1)),elemsize);
211 /* i=left; j=right; */
212 for (k=left; i<=m && j>m; k++) {
213 if (i==j || compar(context, X(barr,i),X(barr,j))<=0) {
214 memcpy(X(arr,k),X(barr,i),elemsize);
217 memcpy(X(arr,k),X(barr,j),elemsize);
221 for (; i<=m; i++, k++)
222 memcpy(X(arr,k),X(barr,i),elemsize);
223 for (; j>m; j--, k++)
224 memcpy(X(arr,k),X(barr,j),elemsize);
229 /*********************************************************************
232 * Based on NTDLL_qsort in dlls/ntdll/misc.c
234 void CDECL MSVCRT_qsort_s(void *base, MSVCRT_size_t nmemb, MSVCRT_size_t size,
235 int (CDECL *compar)(void *, const void *, const void *), void *context)
238 const size_t total_size = nmemb*size;
240 if (!MSVCRT_CHECK_PMT(base != NULL || (base == NULL && nmemb == 0)) ||
241 !MSVCRT_CHECK_PMT(size > 0) || !MSVCRT_CHECK_PMT(compar != NULL) ||
242 total_size / size != nmemb)
244 *MSVCRT__errno() = MSVCRT_EINVAL;
248 if (nmemb < 2) return;
250 secondarr = MSVCRT_malloc(total_size);
253 MSVCRT_mergesort(base, secondarr, size, compar, 0, nmemb-1, context);
254 MSVCRT_free(secondarr);
257 /*********************************************************************
258 * _get_output_format (MSVCRT.@)
260 unsigned int CDECL _get_output_format(void)
265 /*********************************************************************
266 * _resetstkoflw (MSVCRT.@)
268 int CDECL MSVCRT__resetstkoflw(void)
272 /* causes stack fault that updates NtCurrentTeb()->Tib.StackLimit */
273 return VirtualProtect( &stack_addr, 1, PAGE_GUARD|PAGE_READWRITE, NULL );