summaryrefslogtreecommitdiff
path: root/other/b-scan/tmp/support/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/b-scan/tmp/support/snprintf.c
parent073fe4bf9fca6bf40cef2886d75df832ef4b6fca (diff)
initial
Diffstat (limited to 'other/b-scan/tmp/support/snprintf.c')
-rw-r--r--other/b-scan/tmp/support/snprintf.c331
1 files changed, 331 insertions, 0 deletions
diff --git a/other/b-scan/tmp/support/snprintf.c b/other/b-scan/tmp/support/snprintf.c
new file mode 100644
index 0000000..f6a7a40
--- /dev/null
+++ b/other/b-scan/tmp/support/snprintf.c
@@ -0,0 +1,331 @@
1#ifdef NEED_SNPRINTF
2
3#include <stdio.h>
4#include <stdlib.h>
5#include <string.h>
6
7#ifndef __P
8#define __P(p) p
9#endif
10
11#include <stdarg.h>
12#define VA_LOCAL_DECL va_list ap;
13#define VA_START(f) va_start(ap, f)
14#define VA_END va_end(ap)
15
16#ifdef SOLARIS2
17#ifdef _FILE_OFFSET_BITS
18#define SOLARIS26
19#endif
20#endif
21
22#ifdef SOLARIS26
23#define HAS_SNPRINTF
24#define HAS_VSNPRINTF
25#endif
26#ifdef _SCO_DS_
27#define HAS_SNPRINTF
28#endif
29#ifdef luna2
30#define HAS_VSNPRINTF
31#endif
32
33/**************************************************************
34 * Original:
35 * Patrick Powell Tue Apr 11 09:48:21 PDT 1995
36 * A bombproof version of doprnt (dopr) included.
37 * Sigh. This sort of thing is always nasty do deal with. Note that
38 * the version here does not include floating point...
39 *
40 * snprintf() is used instead of sprintf() as it does limit checks
41 * for string length. This covers a nasty loophole.
42 *
43 * The other functions are there to prevent NULL pointers from
44 * causing nast effects.
45 **************************************************************/
46
47static void dopr(char *, const char *, va_list);
48static char *end;
49
50#ifndef HAS_VSNPRINTF
51int vsnprintf(char *str, size_t count, const char *fmt, va_list args)
52{
53 str[0] = 0;
54 end = str + count - 1;
55 dopr(str, fmt, args);
56 if (count > 0)
57 end[0] = 0;
58 return strlen(str);
59}
60
61#ifndef HAS_SNPRINTF
62/* VARARGS3 */
63int snprintf(char *str, size_t count, const char *fmt,...)
64{
65 int len;
66 VA_LOCAL_DECL
67
68 VA_START(fmt);
69 len = vsnprintf(str, count, fmt, ap);
70 VA_END;
71 return len;
72}
73#endif
74
75/*
76 * dopr(): poor man's version of doprintf
77 */
78
79static void fmtstr __P((char *value, int ljust, int len, int zpad, int maxwidth));
80static void fmtnum __P((long value, int base, int dosign, int ljust, int len, int zpad));
81static void dostr __P((char *, int));
82static char *output;
83static void dopr_outch __P((int c));
84
85static void dopr(char *buffer, const char *format, va_list args)
86{
87 int ch;
88 long value;
89 int longflag = 0;
90 int pointflag = 0;
91 int maxwidth = 0;
92 char *strvalue;
93 int ljust;
94 int len;
95 int zpad;
96
97 output = buffer;
98 while ((ch = *format++)) {
99 switch (ch) {
100 case '%':
101 ljust = len = zpad = maxwidth = 0;
102 longflag = pointflag = 0;
103 nextch:
104 ch = *format++;
105 switch (ch) {
106 case 0:
107 dostr("**end of format**", 0);
108 return;
109 case '-':
110 ljust = 1;
111 goto nextch;
112 case '0': /* set zero padding if len not set */
113 if (len == 0 && !pointflag)
114 zpad = '0';
115 case '1':
116 case '2':
117 case '3':
118 case '4':
119 case '5':
120 case '6':
121 case '7':
122 case '8':
123 case '9':
124 if (pointflag)
125 maxwidth = maxwidth * 10 + ch - '0';
126 else
127 len = len * 10 + ch - '0';
128 goto nextch;
129 case '*':
130 if (pointflag)
131 maxwidth = va_arg(args, int);
132 else
133 len = va_arg(args, int);
134 goto nextch;
135 case '.':
136 pointflag = 1;
137 goto nextch;
138 case 'l':
139 longflag = 1;
140 goto nextch;
141 case 'u':
142 case 'U':
143 /*fmtnum(value,base,dosign,ljust,len,zpad) */
144 if (longflag) {
145 value = va_arg(args, long);
146 }
147 else {
148 value = va_arg(args, int);
149 }
150 fmtnum(value, 10, 0, ljust, len, zpad);
151 break;
152 case 'o':
153 case 'O':
154 /*fmtnum(value,base,dosign,ljust,len,zpad) */
155 if (longflag) {
156 value = va_arg(args, long);
157 }
158 else {
159 value = va_arg(args, int);
160 }
161 fmtnum(value, 8, 0, ljust, len, zpad);
162 break;
163 case 'd':
164 case 'D':
165 if (longflag) {
166 value = va_arg(args, long);
167 }
168 else {
169 value = va_arg(args, int);
170 }
171 fmtnum(value, 10, 1, ljust, len, zpad);
172 break;
173 case 'x':
174 if (longflag) {
175 value = va_arg(args, long);
176 }
177 else {
178 value = va_arg(args, int);
179 }
180 fmtnum(value, 16, 0, ljust, len, zpad);
181 break;
182 case 'X':
183 if (longflag) {
184 value = va_arg(args, long);
185 }
186 else {
187 value = va_arg(args, int);
188 }
189 fmtnum(value, -16, 0, ljust, len, zpad);
190 break;
191 case 's':
192 strvalue = va_arg(args, char *);
193 if (maxwidth > 0 || !pointflag) {
194 if (pointflag && len > maxwidth)
195 len = maxwidth; /* Adjust padding */
196 fmtstr(strvalue, ljust, len, zpad, maxwidth);
197 }
198 break;
199 case 'c':
200 ch = va_arg(args, int);
201 dopr_outch(ch);
202 break;
203 case '%':
204 dopr_outch(ch);
205 continue;
206 default:
207 dostr("???????", 0);
208 }
209 break;
210 default:
211 dopr_outch(ch);
212 break;
213 }
214 }
215 *output = 0;
216}
217
218static void fmtstr(char *value, int ljust, int len, int zpad, int maxwidth)
219{
220 int padlen, strlen; /* amount to pad */
221
222 if (value == 0) {
223 value = "<NULL>";
224 }
225 for (strlen = 0; value[strlen]; ++strlen); /* strlen */
226 if (strlen > maxwidth && maxwidth)
227 strlen = maxwidth;
228 padlen = len - strlen;
229 if (padlen < 0)
230 padlen = 0;
231 if (ljust)
232 padlen = -padlen;
233 while (padlen > 0) {
234 dopr_outch(' ');
235 --padlen;
236 }
237 dostr(value, maxwidth);
238 while (padlen < 0) {
239 dopr_outch(' ');
240 ++padlen;
241 }
242}
243
244static void fmtnum(long value, int base, int dosign, int ljust, int len, int zpad)
245{
246 int signvalue = 0;
247 unsigned long uvalue;
248 char convert[20];
249 int place = 0;
250 int padlen = 0; /* amount to pad */
251 int caps = 0;
252
253 /* DEBUGP(("value 0x%x, base %d, dosign %d, ljust %d, len %d, zpad %d\n",
254 value, base, dosign, ljust, len, zpad )); */
255 uvalue = value;
256 if (dosign) {
257 if (value < 0) {
258 signvalue = '-';
259 uvalue = -value;
260 }
261 }
262 if (base < 0) {
263 caps = 1;
264 base = -base;
265 }
266 do {
267 convert[place++] =
268 (caps ? "0123456789ABCDEF" : "0123456789abcdef")
269 [uvalue % (unsigned) base];
270 uvalue = (uvalue / (unsigned) base);
271 } while (uvalue);
272 convert[place] = 0;
273 padlen = len - place;
274 if (padlen < 0)
275 padlen = 0;
276 if (ljust)
277 padlen = -padlen;
278 /* DEBUGP(( "str '%s', place %d, sign %c, padlen %d\n",
279 convert,place,signvalue,padlen)); */
280 if (zpad && padlen > 0) {
281 if (signvalue) {
282 dopr_outch(signvalue);
283 --padlen;
284 signvalue = 0;
285 }
286 while (padlen > 0) {
287 dopr_outch(zpad);
288 --padlen;
289 }
290 }
291 while (padlen > 0) {
292 dopr_outch(' ');
293 --padlen;
294 }
295 if (signvalue)
296 dopr_outch(signvalue);
297 while (place > 0)
298 dopr_outch(convert[--place]);
299 while (padlen < 0) {
300 dopr_outch(' ');
301 ++padlen;
302 }
303}
304
305static void dostr(char *str, int cut)
306{
307 if (cut) {
308 while (*str && cut-- > 0)
309 dopr_outch(*str++);
310 }
311 else {
312 while (*str)
313 dopr_outch(*str++);
314 }
315}
316
317static void dopr_outch(int c)
318{
319#if 0
320 if (iscntrl(c) && c != '\n' && c != '\t') {
321 c = '@' + (c & 0x1F);
322 if (end == 0 || output < end)
323 *output++ = '^';
324 }
325#endif
326 if (end == 0 || output < end)
327 *output++ = c;
328}
329
330#endif /* HAVE_VSNPRINTF */
331#endif /*n NEED_SNPRINTF */