summaryrefslogtreecommitdiff
path: root/other/burneye/src/stub/snprintf.c
diff options
context:
space:
mode:
authorRoot THC2026-02-24 12:42:47 +0000
committerRoot THC2026-02-24 12:42:47 +0000
commitc9cbeced5b3f2bdd7407e29c0811e65954132540 (patch)
treeaefc355416b561111819de159ccbd86c3004cf88 /other/burneye/src/stub/snprintf.c
parent073fe4bf9fca6bf40cef2886d75df832ef4b6fca (diff)
initial
Diffstat (limited to 'other/burneye/src/stub/snprintf.c')
-rw-r--r--other/burneye/src/stub/snprintf.c367
1 files changed, 367 insertions, 0 deletions
diff --git a/other/burneye/src/stub/snprintf.c b/other/burneye/src/stub/snprintf.c
new file mode 100644
index 0000000..1ee1e83
--- /dev/null
+++ b/other/burneye/src/stub/snprintf.c
@@ -0,0 +1,367 @@
1/****************************************************************************
2
3 Copyright (c) 1999,2000 WU-FTPD Development Group.
4 All rights reserved.
5
6 Portions Copyright (c) 1980, 1985, 1988, 1989, 1990, 1991, 1993, 1994
7 The Regents of the University of California.
8 Portions Copyright (c) 1993, 1994 Washington University in Saint Louis.
9 Portions Copyright (c) 1996, 1998 Berkeley Software Design, Inc.
10 Portions Copyright (c) 1989 Massachusetts Institute of Technology.
11 Portions Copyright (c) 1998 Sendmail, Inc.
12 Portions Copyright (c) 1983, 1995, 1996, 1997 Eric P. Allman.
13 Portions Copyright (c) 1997 by Stan Barber.
14 Portions Copyright (c) 1997 by Kent Landfield.
15 Portions Copyright (c) 1991, 1992, 1993, 1994, 1995, 1996, 1997
16 Free Software Foundation, Inc.
17
18 Use and distribution of this software and its source code are governed
19 by the terms and conditions of the WU-FTPD Software License ("LICENSE").
20
21 If you did not receive a copy of the license, it may be obtained online
22 at http://www.wu-ftpd.org/license.html.
23
24 $Id: snprintf.c,v 1.1 2001/07/06 19:23:39 scut Exp $
25
26****************************************************************************/
27
28#ifdef VDEBUG
29
30
31#ifndef __P
32#define __P(p) p
33#endif
34
35#include <stdarg.h>
36#define VA_LOCAL_DECL va_list ap;
37#define VA_START(f) va_start(ap, f)
38#define VA_END va_end(ap)
39
40#ifdef SOLARIS2
41#ifdef _FILE_OFFSET_BITS
42#define SOLARIS26
43#endif
44#endif
45
46#ifdef SOLARIS26
47#define HAS_SNPRINTF
48#define HAS_VSNPRINTF
49#endif
50#ifdef _SCO_DS_
51#define HAS_SNPRINTF
52#endif
53#ifdef luna2
54#define HAS_VSNPRINTF
55#endif
56/*
57 ** SNPRINTF, VSNPRINT -- counted versions of printf
58 **
59 ** These versions have been grabbed off the net. They have been
60 ** cleaned up to compile properly and support for .precision and
61 ** %lx has been added.
62 */
63
64/**************************************************************
65 * Original:
66 * Patrick Powell Tue Apr 11 09:48:21 PDT 1995
67 * A bombproof version of doprnt (dopr) included.
68 * Sigh. This sort of thing is always nasty do deal with. Note that
69 * the version here does not include floating point...
70 *
71 * snprintf() is used instead of sprintf() as it does limit checks
72 * for string length. This covers a nasty loophole.
73 *
74 * The other functions are there to prevent NULL pointers from
75 * causing nast effects.
76 **************************************************************/
77
78/*static char _id[] = "$Id: snprintf.c,v 1.1 2001/07/06 19:23:39 scut Exp $"; */
79static void dopr(char *, const char *, va_list);
80static char *end;
81
82#ifndef HAS_VSNPRINTF
83int vsnprintf(char *str, int count, const char *fmt, va_list args)
84{
85 int n = 0;
86 str[0] = 0;
87 end = str + count - 1;
88 dopr(str, fmt, args);
89 if (count > 0)
90 end[0] = 0;
91 while (*str++)
92 n++;
93 return n;
94}
95
96#ifndef HAS_SNPRINTF
97/* VARARGS3 */
98int snprintf(char *str, int count, const char *fmt,...)
99{
100 int len;
101 VA_LOCAL_DECL
102
103 VA_START(fmt);
104 len = vsnprintf(str, count, fmt, ap);
105 VA_END;
106 return len;
107}
108#endif
109
110/*
111 * dopr(): poor man's version of doprintf
112 */
113
114static void fmtstr __P((char *value, int ljust, int len, int zpad, int maxwidth));
115static void fmtnum __P((long value, int base, int dosign, int ljust, int len, int zpad));
116static void dostr __P((char *, int));
117static char *output;
118static void dopr_outch __P((int c));
119
120static void dopr(char *buffer, const char *format, va_list args)
121{
122 int ch;
123 long value;
124 int longflag = 0;
125 int pointflag = 0;
126 int maxwidth = 0;
127 char *strvalue;
128 int ljust;
129 int len;
130 int zpad;
131
132 output = buffer;
133 while ((ch = *format++)) {
134 switch (ch) {
135 case '%':
136 ljust = len = zpad = maxwidth = 0;
137 longflag = pointflag = 0;
138 nextch:
139 ch = *format++;
140 switch (ch) {
141 case 0:
142 dostr("**end of format**", 0);
143 return;
144 case '-':
145 ljust = 1;
146 goto nextch;
147 case '0': /* set zero padding if len not set */
148 if (len == 0 && !pointflag)
149 zpad = '0';
150 case '1':
151 case '2':
152 case '3':
153 case '4':
154 case '5':
155 case '6':
156 case '7':
157 case '8':
158 case '9':
159 if (pointflag)
160 maxwidth = maxwidth * 10 + ch - '0';
161 else
162 len = len * 10 + ch - '0';
163 goto nextch;
164 case '*':
165 if (pointflag)
166 maxwidth = va_arg(args, int);
167 else
168 len = va_arg(args, int);
169 goto nextch;
170 case '.':
171 pointflag = 1;
172 goto nextch;
173 case 'l':
174 longflag = 1;
175 goto nextch;
176 case 'u':
177 case 'U':
178 /*fmtnum(value,base,dosign,ljust,len,zpad) */
179 if (longflag) {
180 value = va_arg(args, long);
181 }
182 else {
183 value = va_arg(args, int);
184 }
185 fmtnum(value, 10, 0, ljust, len, zpad);
186 break;
187 case 'o':
188 case 'O':
189 /*fmtnum(value,base,dosign,ljust,len,zpad) */
190 if (longflag) {
191 value = va_arg(args, long);
192 }
193 else {
194 value = va_arg(args, int);
195 }
196 fmtnum(value, 8, 0, ljust, len, zpad);
197 break;
198 case 'd':
199 case 'D':
200 if (longflag) {
201 value = va_arg(args, long);
202 }
203 else {
204 value = va_arg(args, int);
205 }
206 fmtnum(value, 10, 1, ljust, len, zpad);
207 break;
208 case 'x':
209 if (longflag) {
210 value = va_arg(args, long);
211 }
212 else {
213 value = va_arg(args, int);
214 }
215 fmtnum(value, 16, 0, ljust, len, zpad);
216 break;
217 case 'X':
218 if (longflag) {
219 value = va_arg(args, long);
220 }
221 else {
222 value = va_arg(args, int);
223 }
224 fmtnum(value, -16, 0, ljust, len, zpad);
225 break;
226 case 's':
227 strvalue = va_arg(args, char *);
228 if (maxwidth > 0 || !pointflag) {
229 if (pointflag && len > maxwidth)
230 len = maxwidth; /* Adjust padding */
231 fmtstr(strvalue, ljust, len, zpad, maxwidth);
232 }
233 break;
234 case 'c':
235 ch = va_arg(args, int);
236 dopr_outch(ch);
237 break;
238 case '%':
239 dopr_outch(ch);
240 continue;
241 default:
242 dostr("???????", 0);
243 }
244 break;
245 default:
246 dopr_outch(ch);
247 break;
248 }
249 }
250 *output = 0;
251}
252
253static void fmtstr(char *value, int ljust, int len, int zpad, int maxwidth)
254{
255 int padlen, strlen; /* amount to pad */
256
257 if (value == 0) {
258 value = "<NULL>";
259 }
260 for (strlen = 0; value[strlen]; ++strlen); /* strlen */
261 if (strlen > maxwidth && maxwidth)
262 strlen = maxwidth;
263 padlen = len - strlen;
264 if (padlen < 0)
265 padlen = 0;
266 if (ljust)
267 padlen = -padlen;
268 while (padlen > 0) {
269 dopr_outch(' ');
270 --padlen;
271 }
272 dostr(value, maxwidth);
273 while (padlen < 0) {
274 dopr_outch(' ');
275 ++padlen;
276 }
277}
278
279static void fmtnum(long value, int base, int dosign, int ljust, int len, int zpad)
280{
281 int signvalue = 0;
282 unsigned long uvalue;
283 char convert[20];
284 int place = 0;
285 int padlen = 0; /* amount to pad */
286 int caps = 0;
287
288 /* DEBUGP(("value 0x%x, base %d, dosign %d, ljust %d, len %d, zpad %d\n",
289 value, base, dosign, ljust, len, zpad )); */
290 uvalue = value;
291 if (dosign) {
292 if (value < 0) {
293 signvalue = '-';
294 uvalue = -value;
295 }
296 }
297 if (base < 0) {
298 caps = 1;
299 base = -base;
300 }
301 do {
302 convert[place++] =
303 (caps ? "0123456789ABCDEF" : "0123456789abcdef")
304 [uvalue % (unsigned) base];
305 uvalue = (uvalue / (unsigned) base);
306 } while (uvalue);
307 convert[place] = 0;
308 padlen = len - place;
309 if (padlen < 0)
310 padlen = 0;
311 if (ljust)
312 padlen = -padlen;
313 /* DEBUGP(( "str '%s', place %d, sign %c, padlen %d\n",
314 convert,place,signvalue,padlen)); */
315 if (zpad && padlen > 0) {
316 if (signvalue) {
317 dopr_outch(signvalue);
318 --padlen;
319 signvalue = 0;
320 }
321 while (padlen > 0) {
322 dopr_outch(zpad);
323 --padlen;
324 }
325 }
326 while (padlen > 0) {
327 dopr_outch(' ');
328 --padlen;
329 }
330 if (signvalue)
331 dopr_outch(signvalue);
332 while (place > 0)
333 dopr_outch(convert[--place]);
334 while (padlen < 0) {
335 dopr_outch(' ');
336 ++padlen;
337 }
338}
339
340static void dostr(char *str, int cut)
341{
342 if (cut) {
343 while (*str && cut-- > 0)
344 dopr_outch(*str++);
345 }
346 else {
347 while (*str)
348 dopr_outch(*str++);
349 }
350}
351
352static void dopr_outch(int c)
353{
354#if 0
355 if (iscntrl(c) && c != '\n' && c != '\t') {
356 c = '@' + (c & 0x1F);
357 if (end == 0 || output < end)
358 *output++ = '^';
359 }
360#endif
361 if (end == 0 || output < end)
362 *output++ = c;
363}
364
365#endif
366
367#endif