summaryrefslogtreecommitdiff
path: root/src/pal/src/cruntime/misctls.cpp
blob: 2df32fe115f8c47204c250f10ea3e6c89a69aad7 (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
// 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;
}

UINT GetExponent(double d)
{
    return (*((UINT*)&d + 1) >> 20) & 0x000007ff;
}

/**
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 )
{
    PERF_ENTRY(_ecvt);
    ENTRY( "_ecvt( value=%.30g, count=%d, dec=%p, sign=%p )\n",
           value, count, dec, sign );
    
    _ASSERTE(dec != nullptr && sign != nullptr);
    CPalThread *pThread = InternalGetCurrentThread();
    LPSTR lpStartOfReturnBuffer = pThread->crtInfo.ECVTBuffer;

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

    // the caller of _ecvt should already checked the Infinity and NAN values
    _ASSERTE(GetExponent(value) != 0x7ff);

    CHAR TempBuffer[ECVT_MAX_BUFFER_SIZE];
   
    *dec = *sign = 0;

    if (value < 0.0)
    {
        *sign = 1;
    }
    
    {
        // we have issue #10290 tracking fixing the sign of 0.0 across the platforms
        if (value == 0.0)
        {
            for (int j = 0; j < count; j++)
            {
                lpStartOfReturnBuffer[j] = '0';
            }
            lpStartOfReturnBuffer[count] = '\0';
            goto done;
        } 
        
        int tempBufferLength = snprintf(TempBuffer, ECVT_MAX_BUFFER_SIZE, "%.40e", value);
        _ASSERTE(tempBufferLength > 0 && ECVT_MAX_BUFFER_SIZE > tempBufferLength);
        
        //
        // Calculate the exponent value
        //

        int exponentIndex = strrchr(TempBuffer, 'e') - TempBuffer; 
        _ASSERTE(exponentIndex > 0 && (exponentIndex < tempBufferLength - 1));

        int i = exponentIndex + 1;
        int exponentSign = 1;
        if (TempBuffer[i] == '-')
        {
            exponentSign = -1;
            i++;
        }
        else if (TempBuffer[i] == '+')
        {
            i++;
        }

        int exponentValue = 0;
        while (i < tempBufferLength)
        {
            _ASSERTE(TempBuffer[i] >= '0' && TempBuffer[i] <= '9');
            exponentValue = exponentValue * 10 + ((BYTE) TempBuffer[i] - (BYTE) '0');
            i++;
        }
        exponentValue *= exponentSign;
        
        //
        // Determine decimal location.
        // 

        if (exponentValue == 0)
        {
            *dec = 1;
        }
        else
        {
            *dec = exponentValue + 1;
        }
        
        //
        // Copy the string from the temp buffer upto precision characters, removing the sign, and decimal as required.
        // 

        i = 0;
        int mantissaIndex = 0;
        while (i < count && mantissaIndex < exponentIndex)
        {
            if (TempBuffer[mantissaIndex] >= '0' && TempBuffer[mantissaIndex] <= '9')
            {
                lpStartOfReturnBuffer[i] = TempBuffer[mantissaIndex];
                i++;
            }
            mantissaIndex++;
        }

        while (i < count)
        {
            lpStartOfReturnBuffer[i] = '0'; // append zeros as needed
            i++;
        }

        lpStartOfReturnBuffer[i] = '\0';
        
        //
        // Round if needed
        //

        if (mantissaIndex >= exponentIndex || TempBuffer[mantissaIndex] < '5')
        {
            goto done;
        }

        i = count - 1;
        while (lpStartOfReturnBuffer[i] == '9' && i > 0)
        {
            lpStartOfReturnBuffer[i] = '0';
            i--;
        }

        if (i == 0 && lpStartOfReturnBuffer[i] == '9')
        {
            lpStartOfReturnBuffer[i] = '1';
            (*dec)++;
        }
        else
        {
            lpStartOfReturnBuffer[i]++;
        }    
    }

done:

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