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
);
32 static unsigned int output_format
;
34 /*********************************************************************
37 void CDECL
MSVCRT__beep( unsigned int freq
, unsigned int duration
)
39 TRACE(":Freq %d, Duration %d\n",freq
,duration
);
43 /*********************************************************************
46 void CDECL
MSVCRT_srand( unsigned int seed
)
48 thread_data_t
*data
= msvcrt_get_thread_data();
49 data
->random_seed
= seed
;
52 /*********************************************************************
55 int CDECL
MSVCRT_rand(void)
57 thread_data_t
*data
= msvcrt_get_thread_data();
59 /* this is the algorithm used by MSVC, according to
60 * http://en.wikipedia.org/wiki/List_of_pseudorandom_number_generators */
61 data
->random_seed
= data
->random_seed
* 214013 + 2531011;
62 return (data
->random_seed
>> 16) & MSVCRT_RAND_MAX
;
65 /*********************************************************************
68 int CDECL
MSVCRT_rand_s(unsigned int *pval
)
70 if (!pval
|| !RtlGenRandom(pval
, sizeof(*pval
)))
72 *MSVCRT__errno() = MSVCRT_EINVAL
;
78 /*********************************************************************
81 void CDECL
MSVCRT__sleep(MSVCRT_ulong timeout
)
83 TRACE("_sleep for %d milliseconds\n",timeout
);
84 Sleep((timeout
)?timeout
:1);
87 /*********************************************************************
90 void* CDECL
_lfind(const void* match
, const void* start
,
91 unsigned int* array_size
, unsigned int elem_size
,
92 int (CDECL
*cf
)(const void*,const void*) )
94 unsigned int size
= *array_size
;
98 if (cf(match
, start
) == 0)
99 return (void *)start
; /* found */
100 start
= (const char *)start
+ elem_size
;
105 /*********************************************************************
106 * _lsearch (MSVCRT.@)
108 void* CDECL
_lsearch(const void* match
, void* start
,
109 unsigned int* array_size
, unsigned int elem_size
,
110 int (CDECL
*cf
)(const void*,const void*) )
112 unsigned int size
= *array_size
;
116 if (cf(match
, start
) == 0)
117 return start
; /* found */
118 start
= (char*)start
+ elem_size
;
121 /* not found, add to end */
122 memcpy(start
, match
, elem_size
);
127 /*********************************************************************
128 * bsearch_s (msvcrt.@)
130 void* CDECL
MSVCRT_bsearch_s(const void *key
, const void *base
,
131 MSVCRT_size_t nmemb
, MSVCRT_size_t size
,
132 int (__cdecl
*compare
)(void *, const void *, const void *), void *ctx
)
135 ssize_t max
= nmemb
- 1;
137 if (!MSVCRT_CHECK_PMT(size
!= 0)) return NULL
;
138 if (!MSVCRT_CHECK_PMT(compare
!= NULL
)) return NULL
;
142 ssize_t cursor
= (min
+ max
) / 2;
143 int ret
= compare(ctx
, key
,(const char *)base
+(cursor
*size
));
145 return (char*)base
+(cursor
*size
);
154 static int CDECL
compare_wrapper(void *ctx
, const void *e1
, const void *e2
)
156 int (__cdecl
*compare
)(const void *, const void *) = ctx
;
157 return compare(e1
, e2
);
160 /*********************************************************************
163 void* CDECL
MSVCRT_bsearch(const void *key
, const void *base
, MSVCRT_size_t nmemb
,
164 MSVCRT_size_t size
, int (__cdecl
*compar
)(const void *, const void *))
166 return MSVCRT_bsearch_s(key
, base
, nmemb
, size
, compare_wrapper
, compar
);
168 /*********************************************************************
171 * Trap to a debugger if the value of the stack pointer has changed.
180 * This function is available for iX86 only.
182 * When VC++ generates debug code, it stores the value of the stack pointer
183 * before calling any external function, and checks the value following
184 * the call. It then calls this function, which will trap if the values are
185 * not the same. Usually this means that the prototype used to call
186 * the function is incorrect. It can also mean that the .spec entry has
187 * the wrong calling convention or parameters.
193 __ASM_GLOBAL_FUNC(_chkesp
,
197 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
198 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
200 __ASM_CFI(".cfi_def_cfa_register %ebp\n\t")
205 "call " __ASM_NAME("MSVCRT_chkesp_fail") "\n\t"
210 __ASM_CFI(".cfi_def_cfa %esp,4\n\t")
211 __ASM_CFI(".cfi_same_value %ebp\n\t")
214 void CDECL
MSVCRT_chkesp_fail(void)
216 ERR("Stack pointer incorrect after last function call - Bad prototype/spec entry?\n");
220 # else /* __GNUC__ */
222 /**********************************************************************/
224 void CDECL
_chkesp(void)
228 # endif /* __GNUC__ */
230 #endif /* __i386__ */
232 /*********************************************************************
233 * Helper function for MSVCRT_qsort_s.
235 * Based on NTDLL_qsort in dlls/ntdll/misc.c
237 static void MSVCRT_mergesort( void *arr
, void *barr
, size_t elemsize
,
238 int (CDECL
*compar
)(void *, const void *, const void *),
239 size_t left
, size_t right
, void *context
)
243 m
=left
+(right
-left
)/2;
244 MSVCRT_mergesort(arr
, barr
, elemsize
, compar
, left
, m
, context
);
245 MSVCRT_mergesort(arr
, barr
, elemsize
, compar
, m
+1, right
, context
);
247 #define X(a,i) ((char*)a+elemsize*(i))
248 for (i
=m
+1; i
>left
; i
--)
249 memcpy (X(barr
,(i
-1)),X(arr
,(i
-1)),elemsize
);
250 for (j
=m
; j
<right
; j
++)
251 memcpy (X(barr
,(right
+m
-j
)),X(arr
,(j
+1)),elemsize
);
253 /* i=left; j=right; */
254 for (k
=left
; i
<=m
&& j
>m
; k
++) {
255 if (i
==j
|| compar(context
, X(barr
,i
),X(barr
,j
))<=0) {
256 memcpy(X(arr
,k
),X(barr
,i
),elemsize
);
259 memcpy(X(arr
,k
),X(barr
,j
),elemsize
);
263 for (; i
<=m
; i
++, k
++)
264 memcpy(X(arr
,k
),X(barr
,i
),elemsize
);
265 for (; j
>m
; j
--, k
++)
266 memcpy(X(arr
,k
),X(barr
,j
),elemsize
);
271 /*********************************************************************
274 * Based on NTDLL_qsort in dlls/ntdll/misc.c
276 void CDECL
MSVCRT_qsort_s(void *base
, MSVCRT_size_t nmemb
, MSVCRT_size_t size
,
277 int (CDECL
*compar
)(void *, const void *, const void *), void *context
)
280 const size_t total_size
= nmemb
*size
;
282 if (!MSVCRT_CHECK_PMT(base
!= NULL
|| (base
== NULL
&& nmemb
== 0))) return;
283 if (!MSVCRT_CHECK_PMT(size
> 0)) return;
284 if (!MSVCRT_CHECK_PMT(compar
!= NULL
)) return;
285 if (total_size
/ size
!= nmemb
) return;
287 if (nmemb
< 2) return;
289 secondarr
= MSVCRT_malloc(total_size
);
292 MSVCRT_mergesort(base
, secondarr
, size
, compar
, 0, nmemb
-1, context
);
293 MSVCRT_free(secondarr
);
296 /*********************************************************************
299 void CDECL
MSVCRT_qsort(void *base
, MSVCRT_size_t nmemb
, MSVCRT_size_t size
,
300 int (CDECL
*compar
)(const void*, const void*))
302 return MSVCRT_qsort_s(base
, nmemb
, size
, compare_wrapper
, compar
);
305 /*********************************************************************
306 * _get_output_format (MSVCRT.@)
308 unsigned int CDECL
_get_output_format(void)
310 return output_format
;
313 /*********************************************************************
314 * _set_output_format (MSVCRT.@)
316 unsigned int CDECL
_set_output_format(unsigned int new_output_format
)
318 unsigned int ret
= output_format
;
320 if(!MSVCRT_CHECK_PMT(new_output_format
==0 || new_output_format
==MSVCRT__TWO_DIGIT_EXPONENT
))
323 output_format
= new_output_format
;
327 /*********************************************************************
328 * _resetstkoflw (MSVCRT.@)
330 int CDECL
MSVCRT__resetstkoflw(void)
334 /* causes stack fault that updates NtCurrentTeb()->Tib.StackLimit */
335 return VirtualProtect( &stack_addr
, 1, PAGE_GUARD
|PAGE_READWRITE
, NULL
);