1 /****************************************************************
2 Copyright (C) 1997 Lucent Technologies
3 All Rights Reserved
4 
5 Permission to use, copy, modify, and distribute this software and
6 its documentation for any purpose and without fee is hereby
7 granted, provided that the above copyright notice appear in all
8 copies and that both that the copyright notice and this
9 permission notice and warranty disclaimer appear in supporting
10 documentation, and that the name of Lucent or any of its entities
11 not be used in advertising or publicity pertaining to
12 distribution of the software without specific, written prior
13 permission.
14 
15 LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
16 INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.
17 IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY
18 SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
20 IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
21 ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
22 THIS SOFTWARE.
23 ****************************************************************/
24 
25 /* This is for Sun (and other systems with brain-damaged sprintf that
26  * doesn't return the count of characters transmitted).
27  * It implements only the relevant subset of sprintf.
28  * With ANSI C (and other sensible systems), you can omit Sprintf.o
29  * if you add -DSprintf=sprintf to CFLAGS (in the makefile).
30  */
31 
32 #include "stdio.h"
33 #include "string.h"
34 
35 #ifdef KR_headers
36 #ifndef size_t__
37 #define size_t int
38 #define size_t__
39 #endif
40 
41 #include "varargs.h"
42 
43 #else
44 
45 #include "stddef.h"
46 #include "stdarg.h"
47 #include "stdlib.h"
48 
49 #endif
50 #ifdef __cplusplus
51 extern "C" {
52 #endif
53 
54  static void
bad(fmt)55 bad
56 #ifdef KR_headers
57 (fmt) char *fmt;
58 #else
59 (const char *fmt)
60 #endif
61 {
62 	printf("bad fmt in Sprintf, starting with \"%s\"\n", fmt);
63 	exit(1);
64 	}
65 
66 #define put(x) *outbuf++ = x
67 
68  int
Sprintf(va_alist)69 Sprintf
70 #ifdef KR_headers
71 	(va_alist)
72  va_dcl
73 #else
74 	(char *outbuf, const char *fmt, ...)
75 #endif
76 {
77 	char *ob0, *s;
78 	char buf[32];
79 	va_list ap;
80 	long i, j;
81 
82 #ifdef KR_headers
83 	char *outbuf, *fmt;
84 	va_start(ap);
85 	outbuf = va_arg(ap, char*);
86 	fmt = va_arg(ap, char*);
87 #else
88 	va_start(ap, fmt);
89 #endif
90 	ob0 = outbuf;
91 	for(;;) {
92 		for(;;) {
93 			switch(i = *fmt++) {
94 				case 0:
95 					goto done;
96 				case '%':
97 					break;
98 				default:
99 					put(i);
100 					continue;
101 				}
102 			break;
103 			}
104 		switch(*fmt++) {
105 			case 'c':
106 				i = va_arg(ap, int);
107 				put(i);
108 				continue;
109 			case 'l':
110 				if (*fmt != 'd')
111 					bad(fmt);
112 				fmt++;
113 				i = va_arg(ap, long);
114 				goto have_i;
115 			case 'd':
116 				i = va_arg(ap, int);
117  have_i:
118 				if (i < 0) {
119 					put('-');
120 					i = -i;
121 					}
122 				s = buf;
123 				do {
124 					j = i / 10;
125 					*s++ = i - 10*j + '0';
126 					}
127 					while(i = j);
128 				do {
129 					i = *--s;
130 					put(i);
131 					}
132 					while(s > buf);
133 				continue;
134 			case 's':
135 				s = va_arg(ap, char*);
136 				while(i = *s++)
137 					{ put(i); }
138 				continue;
139 			default:
140 				bad(fmt);
141 			}
142 		}
143  done:
144 	*outbuf = 0;
145 	return outbuf - ob0;
146 	}
147 #ifdef __cplusplus
148 	}
149 #endif
150