summaryrefslogtreecommitdiff
path: root/src/pal/src/cruntime/misctls.cpp
blob: e46582ec179fd72e2459290495510c2608b45445 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

/*++



Module Name:

    cruntime/misctls.ccpp

Abstract:

    Implementation of C runtime functions that don't fit anywhere else
    and depend on per-thread data



--*/

#include "pal/thread.hpp"
#include "pal/palinternal.h"

extern "C"
{
#include "pal/dbgmsg.h"
#include "pal/misc.h"
}

#include <errno.h>
/* <stdarg.h> needs to be included after "palinternal.h" to avoid name
   collision for va_start and va_end */
#include <stdarg.h>
#include <time.h>
#if HAVE_CRT_EXTERNS_H
#include <crt_externs.h>
#endif  // HAVE_CRT_EXTERNS_H

using namespace CorUnix;

SET_DEFAULT_DEBUG_CHANNEL(CRT);

/*++
Function:

    localtime

See MSDN for more details.
--*/

struct PAL_tm *
__cdecl
PAL_localtime(const PAL_time_t *clock)
{
    CPalThread *pThread = NULL;
    struct tm tmpResult;
    struct PAL_tm *result = NULL;

    PERF_ENTRY(localtime);
    ENTRY( "localtime( clock=%p )\n",clock );

    /* Get the per-thread buffer from the thread structure. */
    pThread = InternalGetCurrentThread();

    result = &pThread->crtInfo.localtimeBuffer;

    localtime_r(reinterpret_cast<const time_t*>(clock), &tmpResult);

    // Copy the result into the Windows struct.
    result->tm_sec = tmpResult.tm_sec;
    result->tm_min = tmpResult.tm_min;
    result->tm_hour = tmpResult.tm_hour;
    result->tm_mday = tmpResult.tm_mday;
    result->tm_mon  = tmpResult.tm_mon;
    result->tm_year = tmpResult.tm_year;
    result->tm_wday = tmpResult.tm_wday;
    result->tm_yday = tmpResult.tm_yday;
    result->tm_isdst = tmpResult.tm_isdst;

    LOGEXIT( "localtime returned %p\n", result );
    PERF_EXIT(localtime);

    return result;
}

/*++
Function:

    ctime

    There appears to be a difference between the FreeBSD and windows
    implementations.  FreeBSD gives Wed Dec 31 18:59:59 1969 for a
    -1 param, and Windows returns NULL

See MSDN for more details.
--*/
char *
__cdecl
PAL_ctime( const PAL_time_t *clock )
{
    CPalThread *pThread = NULL;
    char * retval = NULL;

    PERF_ENTRY(ctime);
    ENTRY( "ctime( clock=%p )\n",clock );
    if(*clock < 0)
    {
        /*If the input param is less than zero the value
         *returned is less than the Unix epoch
         *1st of January 1970*/
        WARN("The input param is less than zero");
        goto done;
    }

    /* Get the per-thread buffer from the thread structure. */
    pThread = InternalGetCurrentThread();

    retval = pThread->crtInfo.ctimeBuffer;

    ctime_r(reinterpret_cast<const time_t*>(clock),retval);

done:

    LOGEXIT( "ctime() returning %p (%s)\n",retval,retval);
    PERF_EXIT(ctime);

    return retval;
}

/**
Function:

    _ecvt

See MSDN for more information.

NOTES:
    There is a difference between PAL _ecvt and Win32 _ecvt.

    If Window's _ecvt receives a double 0.000000000000000000005, and count 50
    the result is "49999999999999998000000000000000000000000000000000"

    Under BSD the same call will result in :
    49999999999999998021734900744965462766153934333829

    The difference is due to the difference between BSD and Win32 sprintf.

--*/
char * __cdecl
_ecvt( double value, int count, int * dec, int * sign )
{
    CONST CHAR * FORMAT_STRING = "%.348e";
    CHAR TempBuffer[ ECVT_MAX_BUFFER_SIZE ];
    CPalThread *pThread = NULL;
    LPSTR lpReturnBuffer = NULL;
    LPSTR lpStartOfReturnBuffer = NULL;
    LPSTR lpTempBuffer = NULL;
    LPSTR lpEndOfTempBuffer = NULL;
    INT nTempBufferLength = 0;
    CHAR ExponentBuffer[ 6 ];
    INT nExponentValue = 0;
    INT LoopIndex = 0;

    PERF_ENTRY(_ecvt);
    ENTRY( "_ecvt( value=%.30g, count=%d, dec=%p, sign=%p )\n",
           value, count, dec, sign );

    /* Get the per-thread buffer from the thread structure. */
    pThread = InternalGetCurrentThread();

    lpStartOfReturnBuffer = lpReturnBuffer = pThread->crtInfo.ECVTBuffer;

    /* Sanity checks */
    if ( !dec || !sign )
    {
        ERROR( "dec and sign have to be valid pointers.\n" );
        *lpReturnBuffer = '\0';
        goto done;
    }
    else
    {
        *dec = *sign = 0;
    }

    if ( value < 0.0 )
    {
        *sign = 1;
    }

    if ( count > ECVT_MAX_COUNT_SIZE )
    {
        count = ECVT_MAX_COUNT_SIZE;
    }

    /* Get the string to work with. */
    sprintf_s( TempBuffer, sizeof(TempBuffer), FORMAT_STRING, value );

    /* Check to see if value was a valid number. */
    if ( strcmp( "NaN", TempBuffer ) == 0 || strcmp( "-NaN", TempBuffer ) == 0 )
    {
        TRACE( "value was not a number!\n" );
        if (strcpy_s( lpStartOfReturnBuffer, ECVT_MAX_BUFFER_SIZE, "1#QNAN0" ) != SAFECRT_SUCCESS)
        {
            ERROR( "strcpy_s failed!\n" );
            *lpStartOfReturnBuffer = '\0';
            goto done;
        }

        *dec = 1;
        goto done;
    }

    /* Check to see if it is infinite. */
    if ( strcmp( "Inf", TempBuffer ) == 0 || strcmp( "-Inf", TempBuffer ) == 0 )
    {
        TRACE( "value is infinite!\n" );
        if (strcpy_s( lpStartOfReturnBuffer, ECVT_MAX_BUFFER_SIZE, "1#INF00" ) != SAFECRT_SUCCESS)
        {
            ERROR( "strcpy_s failed!\n" );
            *lpStartOfReturnBuffer = '\0';
            goto done;
        }

        *dec = 1;
        if ( *TempBuffer == '-' )
        {
            *sign = 1;
        }
        goto done;
    }

    nTempBufferLength = strlen( TempBuffer );
    lpEndOfTempBuffer = &(TempBuffer[ nTempBufferLength ]);

    /* Extract the exponent, and convert it to integer. */
    while ( *lpEndOfTempBuffer != 'e' && nTempBufferLength > 0 )
    {
        nTempBufferLength--;
        lpEndOfTempBuffer--;
    }
    
    ExponentBuffer[ 0 ] = '\0';
    if (strncat_s( ExponentBuffer, sizeof(ExponentBuffer), lpEndOfTempBuffer + 1, 5 ) != SAFECRT_SUCCESS)
    {
        ERROR( "strncat_s failed!\n" );
        *lpStartOfReturnBuffer = '\0';
        goto done;
    }

    nExponentValue = atoi( ExponentBuffer );

    /* End the string at the 'e' */
    *lpEndOfTempBuffer = '\0';
    nTempBufferLength--;

    /* Determine decimal location. */
    if ( nExponentValue == 0 )
    {
        *dec = 1;
    }
    else
    {
        *dec = nExponentValue + 1;
    }

    if ( value == 0.0 )
    {
        *dec = 0;
    }
    /* Copy the string from the temp buffer upto count characters, 
    removing the sign, and decimal as required. */
    lpTempBuffer = TempBuffer;
    *lpReturnBuffer = '0';
    lpReturnBuffer++;

    while ( LoopIndex < ECVT_MAX_COUNT_SIZE )
    {
        if ( isdigit(*lpTempBuffer) )
        {
            *lpReturnBuffer = *lpTempBuffer;
            LoopIndex++;
            lpReturnBuffer++;
        }
        lpTempBuffer++;

        if ( LoopIndex == count + 1 )
        {
            break;
        }
    }

    *lpReturnBuffer = '\0';

    /* Round if needed. If count is less then 0 
    then windows does not round for some reason.*/
    nTempBufferLength = strlen( lpStartOfReturnBuffer ) - 1;
    
    /* Add one for the preceeding zero. */
    lpReturnBuffer = ( lpStartOfReturnBuffer + 1 );

    if ( nTempBufferLength >= count && count >= 0 )
    {
        /* Determine whether I need to round up. */
        if ( *(lpReturnBuffer + count) >= '5' )
        {
            CHAR cNumberToBeRounded;
            if ( count != 0 )
            {
                cNumberToBeRounded = *(lpReturnBuffer + count - 1);
            }
            else
            {
                cNumberToBeRounded = *lpReturnBuffer;
            }
            
            if ( cNumberToBeRounded < '9' )
            {
                if ( count > 0 )
                {
                    /* Add one to the character. */
                    (*(lpReturnBuffer + count - 1))++;
                }
                else
                {
                    if ( cNumberToBeRounded >= '5' )
                    {
                        (*dec)++;
                    }
                }
            }
            else
            {
                LPSTR lpRounding = NULL;

                if ( count > 0 )
                {
                    lpRounding = lpReturnBuffer + count - 1;
                }
                else
                {
                    lpRounding = lpReturnBuffer + count;
                }

                while ( cNumberToBeRounded == '9' )
                {
                    cNumberToBeRounded = *lpRounding;
                    
                    if ( cNumberToBeRounded == '9' )
                    {
                        *lpRounding = '0';
                        lpRounding--;
                    }
                }
                
                if ( lpRounding == lpStartOfReturnBuffer )
                {
                    /* Overflow. number is a whole number now. */
                    *lpRounding = '1';
                    memset( ++lpRounding, '0', count);

                    /* The decimal has moved. */
                    (*dec)++;
                }
                else
                {
                    *lpRounding = ++cNumberToBeRounded;
                }
            }
        }
        else
        {
            /* Get rid of the preceding 0 */
            lpStartOfReturnBuffer++;
        }
    }

    if ( *lpStartOfReturnBuffer == '0' )
    {
        lpStartOfReturnBuffer++;
    }

    if ( count >= 0 )
    {
        *(lpStartOfReturnBuffer + count) = '\0';
    }
    else
    {
        *lpStartOfReturnBuffer = '\0';
    }

done:

    LOGEXIT( "_ecvt returning %p (%s)\n", lpStartOfReturnBuffer , lpStartOfReturnBuffer );
    PERF_EXIT(_ecvt);

    return lpStartOfReturnBuffer;
}