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:  This module supports formatted output to a character buffer.
28
*               Both character and wide character formatting are included.
29
*       For added library granularity, this module could be split
30
*       into two, with "bprintf" functions in one file and the
31
*       "snprintf" functions in another.
32
*
33
*       C library functions:
34
*       _bprintf, _vbprintf
35
*       _bwprintf, _vbwprintf
36
*       _snprintf, _snwprintf
37
*       _vsnprintf, _vsnwprintf
38
*
39
****************************************************************************/
40
 
41
 
42
#include "variety.h"
43
#include "widechar.h"
44
#include 
45
#include 
46
#include "printf.h"
47
 
48
struct  buf_limit {
49
    CHAR_TYPE   *bufptr;
50
    size_t      bufsize;
51
    int         bufover;
52
};
53
 
54
/*
55
 * buf_putc -- append a character to a string in memory
56
 */
57
static slib_callback_t buf_putc; // setup calling convention
58
static void __SLIB_CALLBACK buf_putc( SPECS __SLIB *specs, int op_char )
59
{
60
    struct buf_limit    *bufinfo;
61
 
62
    bufinfo = (struct buf_limit *)specs->_dest;
63
    if( specs->_output_count < bufinfo->bufsize ) {
64
        *( bufinfo->bufptr++ ) = op_char;
65
        specs->_output_count++;
66
    } else {
67
        bufinfo->bufover = -1;
68
    }
69
}
70
 
71
_WCRTLINK int __F_NAME(_vbprintf,_vbwprintf) ( CHAR_TYPE *s, size_t bufsize,
72
        const CHAR_TYPE *format, va_list arg)
73
{
74
    register int            len;
75
    auto struct buf_limit   bufinfo;
76
    slib_callback_t         *tmp;
77
 
78
    bufinfo.bufptr  = s;
79
    bufinfo.bufsize = bufsize - 1;
80
    bufinfo.bufover = 0;
81
#if defined( __386__ ) && defined( __QNX__ )
82
    /* avoid some segment relocations for 32-bit QNX */
83
    tmp = (void (*)())buf_putc;
84
#else
85
    tmp = buf_putc;
86
#endif
87
    len = __F_NAME(__prtf,__wprtf)( &bufinfo, format, arg, tmp );
88
    s[len] = '\0';
89
    return( len );
90
}
91
 
92
_WCRTLINK int __F_NAME(_bprintf,_bwprintf) ( CHAR_TYPE *dest, size_t bufsize,
93
            const CHAR_TYPE *format, ... )
94
{
95
    auto va_list    args;
96
 
97
    va_start( args, format );
98
    return( __F_NAME(_vbprintf,_vbwprintf)( dest, bufsize, format, args ) );
99
}
100
 
101
// Notes:
102
//  _vsnprintf, _vsnwprintf must return an error (-1) when buffer too small
103
//  If a NULL character can fit, append it. If not, no error.
104
 
105
_WCRTLINK int __F_NAME(_vsnprintf,_vsnwprintf) ( CHAR_TYPE *s, size_t bufsize,
106
        const CHAR_TYPE *format, va_list arg)
107
{
108
    register int            len;
109
    auto struct buf_limit   bufinfo;
110
    slib_callback_t         *tmp;
111
 
112
    bufinfo.bufptr  = s;
113
    bufinfo.bufsize = bufsize;
114
    bufinfo.bufover = 0;
115
#if defined( __386__ ) && defined( __QNX__ )
116
    /* avoid some segment relocations for 32-bit QNX */
117
    tmp = (void (*)())buf_putc;
118
#else
119
    tmp = buf_putc;
120
#endif
121
    len = __F_NAME(__prtf,__wprtf)( &bufinfo, format, arg, tmp );
122
    if( len < bufsize ) {
123
        s[len] = '\0';
124
    }
125
    if( bufinfo.bufover == 0 ) {
126
        return( len );
127
    } else {
128
        return( -1 );
129
    }
130
}
131
 
132
_WCRTLINK int __F_NAME(_snprintf,_snwprintf) ( CHAR_TYPE *dest, size_t bufsize,
133
            const CHAR_TYPE *format, ... )
134
{
135
    auto va_list    args;
136
 
137
    va_start( args, format );
138
    return( __F_NAME(_vsnprintf,_vsnwprintf)( dest, bufsize, format, args ) );
139
}