Subversion Repositories Kolibri OS

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
553 serge 1
/****************************************************************************
2
*
3
*                            Open Watcom Project
4
*
5
*    Portions Copyright (c) 1983-2002 Sybase, Inc. All Rights Reserved.
6
*
7
*  ========================================================================
8
*
9
*    This file contains Original Code and/or Modifications of Original
10
*    Code as defined in and that are subject to the Sybase Open Watcom
11
*    Public License version 1.0 (the 'License'). You may not use this file
12
*    except in compliance with the License. BY USING THIS FILE YOU AGREE TO
13
*    ALL TERMS AND CONDITIONS OF THE LICENSE. A copy of the License is
14
*    provided with the Original Code and Modifications, and is also
15
*    available at www.sybase.com/developer/opensource.
16
*
17
*    The Original Code and all software distributed under the License are
18
*    distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
19
*    EXPRESS OR IMPLIED, AND SYBASE AND ALL CONTRIBUTORS HEREBY DISCLAIM
20
*    ALL SUCH WARRANTIES, INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF
21
*    MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR
22
*    NON-INFRINGEMENT. Please see the License for the specific language
23
*    governing rights and limitations under the License.
24
*
25
*  ========================================================================
26
*
27
* Description:  Implementation of strerror_s() - bounds checking strerror().
28
*
29
****************************************************************************/
30
 
31
 
32
#include "variety.h"
33
#include "saferlib.h"
34
#include "widechar.h"
35
#include 
36
#include 
37
#include 
38
#include "errstr.h"
39
 
40
 
41
_WCRTLINK errno_t __F_NAME(strerror_s,wcserror_s)( CHAR_TYPE *s,
42
                                                   rsize_t maxsize,
43
                                                   errno_t errnum )
44
/*****************************************************************/
45
{
46
    errno_t     rc = -1;
47
 
48
#ifdef __WIDECHAR__
49
    wchar_t     Wide_Error_String[ 128 ];
50
#endif
51
    char        *msg;
52
    CHAR_TYPE   *p;
53
    CHAR_TYPE   *p2;
54
    size_t      msglen;
55
    int         k;
56
 
57
    /* runtime-constraints */
58
    // s not NULL
59
    // maxsize <= RSIZE_MAX
60
    // maxsize != 0
61
    if( __check_constraint_nullptr( s ) &&
62
        __check_constraint_maxsize( maxsize ) &&
63
        __check_constraint_zero( maxsize ) ) {
64
 
65
        if( errnum < 0 || errnum >= _sys_nerr ) {
66
            msg = UNKNOWN_ERROR;
67
        } else {
68
            msg = _sys_errlist[ errnum ];
69
        }
70
 
71
        msglen = strlen( msg );
72
 
73
#ifdef __WIDECHAR__
74
        _AToUni( Wide_Error_String, msg );
75
        p2 = Wide_Error_String;
76
#else
77
        p2 = msg;
78
#endif
79
        p = s;
80
        k = 0;
81
        // copy string up to end of string or end of receiving field
82
        while ( (k < maxsize - 1) && (*p2 != NULLCHAR) ) {
83
            *p++ = *p2++;
84
            ++k;
85
        };
86
 
87
        if( (maxsize > 3) && (msglen > maxsize - 4) ) {
88
            /* msg does not fit, indicate it's incomplete */
89
            /* and terminate string                       */
90
            __F_NAME(strcpy,wcscpy)( p - 3, STRING( "..." ) );
91
        } else {
92
            *p = NULLCHAR;                                  // terminate string
93
        }
94
        if( msglen < maxsize) {
95
            rc = 0;                                         // msg not truncated
96
        }
97
    }
98
    return( rc );
99
}