misc.c 8.66 KB
Newer Older
1 2
/*
 * Helper functions for ntdll
3 4
 *
 * Copyright 2000 Juergen Schmied
5
 * Copyright 2010 Marcus Meissner
6 7 8 9 10 11 12 13 14 15 16 17 18
 *
 * 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
19
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 21 22 23
 */

#include "config.h"

24 25
#include <time.h>
#include <math.h>
26 27 28
#ifdef HAVE_SYS_UTSNAME_H
#include <sys/utsname.h>
#endif
29

30
#include "wine/library.h"
31
#include "wine/debug.h"
32 33
#include "ntdll_misc.h"

34
WINE_DEFAULT_DEBUG_CHANNEL(ntdll);
35

36 37 38 39 40
#if defined(__GNUC__) && defined(__i386__)
#define DO_FPU(x,y) __asm__ __volatile__( x " %0;fwait" : "=m" (y) : )
#define POP_FPU(x) DO_FPU("fstpl",x)
#endif

41
LPCSTR debugstr_ObjectAttributes(const OBJECT_ATTRIBUTES *oa)
42
{
43 44 45 46
    if (!oa) return "<null>";
    return wine_dbg_sprintf( "{name=%s, attr=0x%08x, hRoot=%p, sd=%p}\n",
                             debugstr_us(oa->ObjectName), oa->Attributes,
                             oa->RootDirectory, oa->SecurityDescriptor );
47 48
}

49
LPCSTR debugstr_us( const UNICODE_STRING *us )
50
{
51
    if (!us) return "<null>";
52
    return debugstr_wn(us->Buffer, us->Length / sizeof(WCHAR));
53 54
}

55
/*********************************************************************
56
 *                  _ftol   (NTDLL.@)
57 58 59
 *
 * VERSION
 *	[GNUC && i386]
60
 */
61
#if defined(__GNUC__) && defined(__i386__)
62
LONGLONG CDECL NTDLL__ftol(void)
63 64 65 66 67
{
	/* don't just do DO_FPU("fistp",retval), because the rounding
	 * mode must also be set to "round towards zero"... */
	double fl;
	POP_FPU(fl);
68
	return (LONGLONG)fl;
69
}
70 71 72
#endif /* defined(__GNUC__) && defined(__i386__) */

/*********************************************************************
73
 *                  _ftol   (NTDLL.@)
74 75 76 77 78 79
 *
 * FIXME
 *	Should be register function
 * VERSION
 *	[!GNUC && i386]
 */
Dmitry Timoshkov's avatar
Dmitry Timoshkov committed
80
#if !defined(__GNUC__) && defined(__i386__)
81
LONGLONG CDECL NTDLL__ftol(double fl)
82 83
{
	FIXME("should be register function\n");
84
	return (LONGLONG)fl;
85
}
86 87
#endif /* !defined(__GNUC__) && defined(__i386__) */

88
/*********************************************************************
89
 *                  _CIpow   (NTDLL.@)
90 91
 * VERSION
 *	[GNUC && i386]
92
 */
93
#if defined(__GNUC__) && defined(__i386__)
94
double CDECL NTDLL__CIpow(void)
95 96 97 98 99 100
{
	double x,y;
	POP_FPU(y);
	POP_FPU(x);
	return pow(x,y);
}
101 102 103 104
#endif /* defined(__GNUC__) && defined(__i386__) */


/*********************************************************************
105
 *                  _CIpow   (NTDLL.@)
106 107 108 109 110 111 112 113
 *
 * FIXME
 *	Should be register function
 *
 * VERSION
 *	[!GNUC && i386]
 */
#if !defined(__GNUC__) && defined(__i386__)
114
double CDECL NTDLL__CIpow(double x,double y)
115 116 117 118
{
	FIXME("should be register function\n");
	return pow(x,y);
}
119 120
#endif /* !defined(__GNUC__) && defined(__i386__) */

121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
/*********************************************************************
 *                  wine_get_version   (NTDLL.@)
 */
const char * CDECL NTDLL_wine_get_version(void)
{
    return wine_get_version();
}

/*********************************************************************
 *                  wine_get_build_id   (NTDLL.@)
 */
const char * CDECL NTDLL_wine_get_build_id(void)
{
    return wine_get_build_id();
}

137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
/*********************************************************************
 *                  wine_get_host_version   (NTDLL.@)
 */
void CDECL NTDLL_wine_get_host_version( const char **sysname, const char **release )
{
#ifdef HAVE_SYS_UTSNAME_H
    static struct utsname buf;
    static int init_done;

    if (!init_done)
    {
        uname( &buf );
        init_done = 1;
    }
    if (sysname) *sysname = buf.sysname;
    if (release) *release = buf.release;
#else
    if (sysname) *sysname = "";
    if (release) *release = "";
#endif
}

159 160 161
/*********************************************************************
 *                  abs   (NTDLL.@)
 */
162
int CDECL NTDLL_abs( int i )
163 164 165 166 167 168 169
{
    return abs( i );
}

/*********************************************************************
 *                  labs   (NTDLL.@)
 */
170
LONG CDECL NTDLL_labs( LONG i )
171 172 173 174 175 176 177
{
    return labs( i );
}

/*********************************************************************
 *                  atan   (NTDLL.@)
 */
178
double CDECL NTDLL_atan( double d )
179 180 181 182 183 184 185
{
    return atan( d );
}

/*********************************************************************
 *                  ceil   (NTDLL.@)
 */
186
double CDECL NTDLL_ceil( double d )
187 188 189 190 191 192 193
{
    return ceil( d );
}

/*********************************************************************
 *                  cos   (NTDLL.@)
 */
194
double CDECL NTDLL_cos( double d )
195 196 197 198 199 200 201
{
    return cos( d );
}

/*********************************************************************
 *                  fabs   (NTDLL.@)
 */
202
double CDECL NTDLL_fabs( double d )
203 204 205 206 207 208 209
{
    return fabs( d );
}

/*********************************************************************
 *                  floor   (NTDLL.@)
 */
210
double CDECL NTDLL_floor( double d )
211 212 213 214 215 216 217
{
    return floor( d );
}

/*********************************************************************
 *                  log   (NTDLL.@)
 */
218
double CDECL NTDLL_log( double d )
219 220 221 222 223 224 225
{
    return log( d );
}

/*********************************************************************
 *                  pow   (NTDLL.@)
 */
226
double CDECL NTDLL_pow( double x, double y )
227 228 229 230 231 232 233
{
    return pow( x, y );
}

/*********************************************************************
 *                  sin   (NTDLL.@)
 */
234
double CDECL NTDLL_sin( double d )
235 236 237 238 239 240 241
{
    return sin( d );
}

/*********************************************************************
 *                  sqrt   (NTDLL.@)
 */
242
double CDECL NTDLL_sqrt( double d )
243 244 245 246 247 248 249
{
    return sqrt( d );
}

/*********************************************************************
 *                  tan   (NTDLL.@)
 */
250
double CDECL NTDLL_tan( double d )
251 252 253
{
    return tan( d );
}
254 255


256
static void
257 258
NTDLL_mergesort( void *arr, void *barr, size_t elemsize, int(__cdecl *compar)(const void *, const void *),
                 size_t left, size_t right )
259 260
{
    if(right>left) {
261
        size_t i, j, k, m;
262
        m=left+(right-left)/2;
263 264
        NTDLL_mergesort( arr, barr, elemsize, compar, left, m);
        NTDLL_mergesort( arr, barr, elemsize, compar, m+1, right);
265 266

#define X(a,i) ((char*)a+elemsize*(i))
267 268 269
        for (k=left, i=left, j=m+1; i<=m && j<=right; k++) {
            if (compar(X(arr, i), X(arr,j)) <= 0) {
                memcpy(X(barr,k), X(arr, i), elemsize);
270 271
                i++;
            } else {
272 273
                memcpy(X(barr,k), X(arr, j), elemsize);
                j++;
274 275
            }
        }
276 277 278 279 280 281
        if (i<=m)
            memcpy(X(barr,k), X(arr,i), (m-i+1)*elemsize);
        else
            memcpy(X(barr,k), X(arr,j), (right-j+1)*elemsize);

        memcpy(X(arr, left), X(barr, left), (right-left+1)*elemsize);
282 283 284 285 286 287 288 289 290 291
    }
#undef X
}

/*********************************************************************
 *                  qsort   (NTDLL.@)
 */
void __cdecl NTDLL_qsort( void *base, size_t nmemb, size_t size,
                          int(__cdecl *compar)(const void *, const void *) )
{
292 293 294
    void *secondarr;
    if (nmemb < 2 || size == 0) return;
    secondarr = RtlAllocateHeap (GetProcessHeap(), 0, nmemb*size);
295
    NTDLL_mergesort( base, secondarr, size, compar, 0, nmemb-1 );
296 297
    RtlFreeHeap (GetProcessHeap(),0, secondarr);
}
298 299 300 301 302 303 304 305

/*********************************************************************
 *                  bsearch   (NTDLL.@)
 */
void * __cdecl
NTDLL_bsearch( const void *key, const void *base, size_t nmemb,
               size_t size, int (__cdecl *compar)(const void *, const void *) )
{
306 307 308 309 310 311 312
    ssize_t min = 0;
    ssize_t max = nmemb - 1;

    while (min <= max)
    {
        ssize_t cursor = (min + max) / 2;
        int ret = compar(key,(const char *)base+(cursor*size));
313 314 315
        if (!ret)
            return (char*)base+(cursor*size);
        if (ret < 0)
316
            max = cursor - 1;
317
        else
318
            min = cursor + 1;
319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336
    }
    return NULL;
}


/*********************************************************************
 *                  _lfind   (NTDLL.@)
 */
void * __cdecl _lfind( const void *key, const void *base, unsigned int *nmemb,
                       size_t size, int(__cdecl *compar)(const void *, const void *) )
{
    size_t i, n = *nmemb;

    for (i=0;i<n;i++)
        if (!compar(key,(char*)base+(size*i)))
            return (char*)base+(size*i);
    return NULL;
}