misc.c 4.65 KB
Newer Older
1 2 3 4
/*
 * msvcrt.dll misc functions
 *
 * Copyright 2000 Jon Griffiths
5 6 7 8 9 10 11 12 13 14 15 16 17
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * 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
19 20
 */

21 22 23
#include "config.h"
#include "wine/port.h"

24
#include <stdlib.h>
25 26

#include "msvcrt.h"
27 28 29
#include "wine/debug.h"

WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
30 31 32 33 34


/*********************************************************************
 *		_beep (MSVCRT.@)
 */
35
void CDECL _beep( unsigned int freq, unsigned int duration)
36 37 38 39 40
{
    TRACE(":Freq %d, Duration %d\n",freq,duration);
    Beep(freq, duration);
}

41 42 43
/*********************************************************************
 *		srand (MSVCRT.@)
 */
44
void CDECL MSVCRT_srand( unsigned int seed )
45 46 47 48 49
{
    thread_data_t *data = msvcrt_get_thread_data();
    data->random_seed = seed;
}

50 51 52
/*********************************************************************
 *		rand (MSVCRT.@)
 */
53
int CDECL MSVCRT_rand(void)
54
{
55 56 57 58 59 60
    thread_data_t *data = msvcrt_get_thread_data();

    /* this is the algorithm used by MSVC, according to
     * http://en.wikipedia.org/wiki/List_of_pseudorandom_number_generators */
    data->random_seed = data->random_seed * 214013 + 2531011;
    return (data->random_seed >> 16) & MSVCRT_RAND_MAX;
61 62 63 64 65
}

/*********************************************************************
 *		_sleep (MSVCRT.@)
 */
66
void CDECL MSVCRT__sleep(unsigned long timeout)
67
{
68
  TRACE("_sleep for %ld milliseconds\n",timeout);
69 70 71 72 73 74
  Sleep((timeout)?timeout:1);
}

/*********************************************************************
 *		_lfind (MSVCRT.@)
 */
75 76 77
void* CDECL _lfind(const void* match, const void* start,
                   unsigned int* array_size, unsigned int elem_size,
                   int (*cf)(const void*,const void*) )
78 79 80 81 82 83 84
{
  unsigned int size = *array_size;
  if (size)
    do
    {
      if (cf(match, start) == 0)
        return (void *)start; /* found */
85
      start = (const char *)start + elem_size;
86 87 88 89 90 91 92
    } while (--size);
  return NULL;
}

/*********************************************************************
 *		_lsearch (MSVCRT.@)
 */
93 94 95
void* CDECL _lsearch(const void* match, void* start,
                     unsigned int* array_size, unsigned int elem_size,
                     int (*cf)(const void*,const void*) )
96 97 98 99 100 101 102
{
  unsigned int size = *array_size;
  if (size)
    do
    {
      if (cf(match, start) == 0)
        return start; /* found */
103
      start = (char*)start + elem_size;
104 105 106 107 108 109 110 111 112 113
    } while (--size);

  /* not found, add to end */
  memcpy(start, match, elem_size);
  array_size[0]++;
  return start;
}

/*********************************************************************
 *		_chkesp (MSVCRT.@)
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
 *
 * Trap to a debugger if the value of the stack pointer has changed.
 *
 * PARAMS
 *  None.
 *
 * RETURNS
 *  Does not return.
 *
 * NOTES
 *  This function is available for iX86 only.
 *
 *  When VC++ generates debug code, it stores the value of the stack pointer
 *  before calling any external function, and checks the value following
 *  the call. It then calls this function, which will trap if the values are
 *  not the same. Usually this means that the prototype used to call
 *  the function is incorrect.  It can also mean that the .spec entry has
 *  the wrong calling convention or parameters.
132
 */
133 134 135
#ifdef __i386__

# ifdef __GNUC__
136

137 138 139 140 141
__ASM_GLOBAL_FUNC(_chkesp,
                  "jnz 1f\n\t"
                  "ret\n"
                  "1:\tpushl %ebp\n\t"
                  "movl %esp,%ebp\n\t"
142
                  "subl $12,%esp\n\t"
143 144 145 146 147 148 149
                  "pushl %eax\n\t"
                  "pushl %ecx\n\t"
                  "pushl %edx\n\t"
                  "call " __ASM_NAME("MSVCRT_chkesp_fail") "\n\t"
                  "popl %edx\n\t"
                  "popl %ecx\n\t"
                  "popl %eax\n\t"
150
                  "leave\n\t"
151
                  "ret")
152

153
void CDECL MSVCRT_chkesp_fail(void)
154 155 156
{
  ERR("Stack pointer incorrect after last function call - Bad prototype/spec entry?\n");
  DebugBreak();
157
}
158 159

# else  /* __GNUC__ */
160 161 162

/**********************************************************************/

163
void CDECL _chkesp(void)
164 165 166
{
}

167 168 169
# endif  /* __GNUC__ */

#endif  /* __i386__ */