summaryrefslogtreecommitdiffstats
path: root/private/crt32/stdio/sprintf.c
blob: 7f157758610ae0a49b8d3fd0b74aa0c3bfd15f27 (plain) (blame)
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
/***
*sprintf.c - print formatted to string
*
*	Copyright (c) 1985-1991, Microsoft Corporation. All rights reserved.
*
*Purpose:
*	defines sprintf() and _snprintf() - print formatted data to string
*
*Revision History:
*	09-02-83  RN	initial version
*	04-13-87  JCR	added const to declaration
*	06-24-87  JCR	(1) Made declaration conform to ANSI prototype and use
*			the va_ macros; (2) removed SS_NE_DS conditionals.
*	11-07-87  JCR	Multi-thread support
*	12-11-87  JCR	Added "_LOAD_DS" to declaration
*	05-27-88  PHG	Merged DLL and normal versions
*	06-13-88  JCR	Fake _iob entry is now static so that other routines
*			can assume _iob entries are in DGROUP.
*	08-25-88  GJF	Define MAXSTR to be INT_MAX (from LIMITS.H).
*	06-06-89  JCR	386 mthread support
*	08-18-89  GJF	Clean up, now specific to OS/2 2.0 (i.e., 386 flat
*			model). Also fixed copyright and indents.
*	02-16-90  GJF	Fixed copyright
*	03-19-90  GJF	Made calling type _CALLTYPE2, added #include
*			<cruntime.h> and removed #include <register.h>.
*	07-23-90  SBM	Replaced <assertm.h> by <assert.h>
*	10-03-90  GJF	New-style function declarator.
*	09-24-91  JCR	Added _snprintf()
*
*******************************************************************************/

#include <cruntime.h>
#include <stdio.h>
#include <assert.h>
#include <stdarg.h>
#include <internal.h>
#include <limits.h>
#include <os2dll.h>

#define MAXSTR INT_MAX


/***
#ifndef _COUNT_
*int sprintf(string, format, ...) - print formatted data to string
#else
*int _snprintf(string, cnt, format, ...) - print formatted data to string
#endif
*
*Purpose:
*	Prints formatted data to the using the format string to
*	format data and getting as many arguments as called for
*	Sets up a FILE so file i/o operations can be used, make
*	string look like a huge buffer to it, but _flsbuf will
*	refuse to flush it if it fills up.  Appends '\0' to make
*	it a true string. _output does the real work here
*
*	Allocate the 'fake' _iob[] entry statically instead of on
*	the stack so that other routines can assume that _iob[]
*	entries are in are in DGROUP and, thus, are near.
*
#ifdef _COUNT_
*	The _snprintf() flavor takes a count argument that is
*	the max number of bytes that should be written to the
*	user's buffer.
#endif
*
*	Multi-thread: (1) Since there is no stream, this routine must
*	never try to get the stream lock (i.e., there is no stream
*	lock either). (2) Also, since there is only one statically
*	allocated 'fake' iob, we must lock/unlock to prevent collisions.
*
*Entry:
*	char *string - pointer to place to put output
#ifdef _COUNT_
*	size_t count - max number of bytes to put in buffer
#endif
*	char *format - format string to control data format/number
*	of arguments followed by list of arguments, number and type
*	controlled by format string
*
*Exit:
*	returns number of characters printed
*
*Exceptions:
*
*******************************************************************************/

#ifndef _COUNT_

int _CALLTYPE2 sprintf (
	char *string,
	const char *format,
	...
	)
#else

int _CALLTYPE2 _snprintf (
	char *string,
	size_t count,
	const char *format,
	...
	)
#endif

{
	FILE str;
	REG1 FILE *outfile = &str;
	va_list arglist;
	REG2 int retval;

	va_start(arglist, format);

	assert(string != NULL);
	assert(format != NULL);

	outfile->_flag = _IOWRT|_IOSTRG;
	outfile->_ptr = outfile->_base = string;
#ifndef _COUNT_
	outfile->_cnt = MAXSTR;
#else
	outfile->_cnt = count;
#endif

	retval = _output(outfile,format,arglist);

	_putc_lk('\0',outfile); /* no-lock version */

	return(retval);
}