File: vsnprintf_r.c

package info (click to toggle)
socat 2.0.0~beta9-1
  • links: PTS
  • area: main
  • in suites: experimental
  • size: 3,740 kB
  • sloc: ansic: 30,875; sh: 11,630; makefile: 149
file content (569 lines) | stat: -rw-r--r-- 16,200 bytes parent folder | download | duplicates (4)
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
/* vsnprintf_r.c */
/* Copyright Gerhard Rieger */

/* a reduced but async-signal-safe and thread-safe version of vsnprintf */

#include "config.h"

#include <stddef.h>	/* ptrdiff_t */
#include <ctype.h>	/* isdigit() */
#include <stdarg.h>
#include <stdlib.h>
#include <errno.h>
#if HAVE_SYSLOG_H
#include <syslog.h>
#endif
#include <sys/utsname.h>
#include <time.h>	/* time_t, strftime() */
#include <sys/time.h>	/* gettimeofday() */
#include <stdio.h>
#include <string.h>
#if HAVE_UNISTD_H
#include <unistd.h>
#endif

#include "vsnprintf_r.h"

/* helper functions for vsnprintf_r():
   e.g. convert an unsigned long to decimal string.
   in: field (must be long enough for all digits and \0
       n: length of field in bytes
       ulo: the value
   returns: the pointer to the result string (need not be ==field)
*/

/* this function converts an unsigned long number to decimal ASCII
   it is async signal safe and thread safe
   it returns NULL if n==0
   it returns NULL if field is too short to hold the result
   it returns a pointer to the result string (somewhere within field)
   it terminates result with \0
 */
static char *_diag_ulong_to_dec(char *field, size_t n, unsigned long ulo) {
   char *np = field+n;	/* point to the end */

   if (n == 0)  return NULL;
   *--np = '\0';	/* \0 in last char of string */
   /* this is not optimal - uses much CPU, but simple to implement */
   /*  calculate the result from right to left */
   do { if (np==field) return NULL; *--np = '0'+(ulo%10); } while (ulo/=10);
   return np;
}

/* this function converts an unsigned long number to decimal ASCII
   and pads it with space or '0' when size and leading0 are set appropriately
   it is async signal safe and thread safe
   it returns NULL if n==0
   it returns NULL if field is too short to hold the result
   it returns a pointer to the result string (somewhere within field)
   it reduces size to n-1 if it is greater or equal
   it terminates result with \0
 */
static char *diag_ulong_to_dec(char *field, size_t n, unsigned long ulo, int leading0, int size) {
   char *np;
   char c;
   int i;

   if (n == 0)  return NULL;
   np = _diag_ulong_to_dec(field, n, ulo);
   if (np == NULL) return np;
   if (size) {
      if (size >= n)  size = n-1;
      if (leading0) {
	 c = '0';
      } else {
	 c = ' ';
      }
      i = size - strlen(np);
      while (--i >= 0) {
	 *--np = c;
      }
   }	     
   return np;
}

/* this function converts a signed long number to decimal ASCII  
   and pads it with space or '0' when size and leading0 are set appropriately
   it is async signal safe and thread safe
   it returns NULL if n==0
   it returns NULL if field is too short to hold the result
   it returns a pointer to the result string (somewhere within field)
   it reduces size to n-1 if it is greater or equal
   it terminates result with \0
 */
/* like diag_ulong_to_dec but signed; fields need also space for '-' */
static char *diag_long_to_dec(char *field, size_t n, long lo, int leading0, int size) {
   char *np;
   int minus;
   unsigned long ulo;
   int i;

   if ((minus = (lo < 0))) {
      ulo = (~lo)+1;
   } else {
      ulo = lo;
   }
   np = _diag_ulong_to_dec(field, n, ulo);
   if (np == NULL)  return np;

   if (size) {
      if (size >= n)  size = n-1;
      i = size - strlen(np);
      if (leading0) {
	 if (minus) --i; 
	 while (--i >= 0) {
	    *--np = '0';
	 }
	 if (minus)  *--np = '-';
      } else {
	 if (minus)  { *--np = '-'; --i; }
	 while (--i >= 0) {
	    *--np = ' ';
	 }
      }
   } else {
      if (minus)  *--np = '-';
   }
   return np;
}

/* this function converts an unsigned long number to hexadecimal ASCII
   it is async signal safe and thread safe
   it returns NULL if n==0
   it returns NULL if field is too short to hold the result
   it returns a pointer to the result string (somewhere within field)
   it terminates result with \0
 */
static char *diag_ulong_to_hex(char *field, size_t n, unsigned long ulo, int leading0, size_t size) {
   char *np = field+n;	/* point to the end */
   int i;
   char c;

   if (n == 0)  return NULL;
   *--np = '\0';	/* \0 in last char of string */
   /*  calculate the result from right to left */
   do { if (np==field) return NULL; i = (ulo&0x0f);
      *--np = (i<10?'0':('a'-10))+i; }
   while (ulo>>=4);
   if (size) {
      if (size >= n)  size = n-1;
      if (leading0) {
	 c = '0';
      } else {
	 c = ' ';
      }
      i = size - strlen(np);
      while (--i >= 0) {
	 *--np = c;
      }
   }	     
   return np;
}

/* this function converts an unsigned long number to octal ASCII
   it is async signal safe and thread safe
   it returns NULL if n==0
   it returns NULL if field is too short to hold the result
   it returns a pointer to the result string (somewhere within field)
   it terminates result with \0
 */
static char *diag_ulong_to_oct(char *field, size_t n, unsigned long ulo, int leading0, size_t size) {
   char *np = field+n;	/* point to the end */
   int i;
   char c;

   if (n == 0)  return NULL;
   *--np = '\0';	/* \0 in last char of string */
   /* calculate the result from right to left */
   do { if (np==field) return NULL;  i = (ulo&0x07); *--np = '0'+i; }
   while (ulo>>=3);
   if (size) {
      if (size >= n)  size = n-1;
      if (leading0) {
	 c = '0';
      } else {
	 c = ' ';
      }
      i = size - strlen(np);
      while (--i >= 0) {
	 *--np = c;
      }
   }	     
   return np;
}


#if HAVE_TYPE_LONGLONG

/* this function converts an unsigned long long number to decimal ASCII
   it is async signal safe and thread safe
   it returns NULL if n==0
   it returns NULL if field is too short to hold the result
   it returns a pointer to the result string (somewhere within field)
   it terminates result with \0
 */
static char *_diag_ulonglong_to_dec(char *field, size_t n, unsigned long long ull) {
   char *np = field+n;	/* point to the end */

   if (n == 0)  return NULL;
   *--np = '\0';	/* \0 in last char of string */
   /* this is not optimal - uses much CPU, but simple to implement */
   /* calculate the result from right to left */
   do { if (np==field) return NULL; *--np = '0'+(ull%10); } while (ull/=10);
   return np;
}

/* this function converts an unsigned long long number to decimal ASCII
   and pads it with space or '0' when size and leading0 are set appropriately
   it is async signal safe and thread safe
   it returns NULL if n==0
   it returns NULL if field is too short to hold the result
   it returns a pointer to the result string (somewhere within field)
   it reduces size to n-1 if it is greater or equal
   it terminates result with \0
 */
static char *diag_ulonglong_to_dec(char *field, size_t n, unsigned long long ull, int leading0, int size) {
   char *np;
   char c;
   int i;

   if (n == 0)  return NULL;
   np = _diag_ulonglong_to_dec(field, n, ull);
   if (size) {
      if (size >= n)  size = n-1;
      if (leading0) {
	 c = '0';
      } else {
	 c = ' ';
      }
      i = size - strlen(np);
      while (i-- > 0) {
	 *--np = c;
      }
   }	     
   return np;
}

/* this function converts a signed long long number to decimal ASCII  
   and pads it with space or '0' when size and leading0 are set appropriately
   it is async signal safe and thread safe
   it returns NULL if n==0
   it returns NULL if field is too short to hold the result
   it returns a pointer to the result string (somewhere within field)
   it reduces size to n-1 if it is greater or equal
   it terminates result with \0
 */
/* like diag_ulonglong_to_dec but signed; fields need also space for '-' */
static char *diag_longlong_to_dec(char *field, size_t n, long long ll, int leading0, int size) {
   char *np;
   int minus;
   unsigned long ull;
   int i;

   if ((minus = (ll < 0))) {
      ull = (~ll)+1;
   } else {
      ull = ll;
   }
   np = _diag_ulonglong_to_dec(field, n, ull);
   if (np == NULL)  return np;

   if (size) {
      if (size >= n)  size = n-1;
      i = size - strlen(np);
      if (leading0) {
	 if (minus) --i; 
	 while (--i >= 0) {
	    *--np = '0';
	 }
	 if (minus)  *--np = '-';
      } else {
	 if (minus)  { *--np = '-'; --i; }
	 while (--i >= 0) {
	    *--np = ' ';
	 }
      }
   }	     
   return np;
}

/* this function converts an unsigned long long number to hexadecimal ASCII
   it is async signal safe and thread safe
   it returns NULL if n==0
   it returns NULL if field is too short to hold the result
   it returns a pointer to the result string (somewhere within field)
   it terminates result with \0
 */
static char *diag_ulonglong_to_hex(char *field, size_t n, unsigned long long ull, int leading0, size_t size) {
   char *np = field+n;	/* point to the end */
   unsigned int i;
   char c;

   if (n == 0)  return NULL;
   *--np = '\0';	/* \0 in last char of string */
   /* calculate the result from right to left */
   do { if (np==field) return NULL; i = (ull&0x0f);
      *--np = (i<10?'0':('a'-10))+i; }
   while (ull>>=4);
   if (size) {
      if (size >= n)  size = n-1;
      if (leading0) {
	 c = '0';
      } else {
	 c = ' ';
      }
      i = size - strlen(np);
      while (--i >= 0) {
	 *--np = c;
      }
   }	     
   return np;
}

/* this function converts an unsigned long long number to octal ASCII
   it is async signal safe and thread safe
   it returns NULL if n==0
   it returns NULL if field is too short to hold the result
   it returns a pointer to the result string (somewhere within field)
   it terminates result with \0
 */
static char *diag_ulonglong_to_oct(char *field, size_t n, unsigned long long ull, int leading0, size_t size) {
   char *np = field+n;	/* point to the end */
   int i;
   char c;

   if (n == 0)  return NULL;
   *--np = '\0';	/* \0 in last char of string */
   /* calculate the result from right to left */
   do { if (np==field) return NULL;  i = (ull&0x07); *--np = '0'+i; }
   while (ull>>=3);
   if (size) {
      if (size >= n)  size = n-1;
      if (leading0) {
	 c = '0';
      } else {
	 c = ' ';
      }
      i = size - strlen(np);
      while (--i >= 0) {
	 *--np = c;
      }
   }	     
   return np;
}

#endif /* HAVE_TYPE_LONGLONG */


/* this function is designed as a variant of vsnprintf(3) but async signal safe
   and thread safe
   it currently only implements a subset of the format directives
   returns <0 if an error occurred (no scenario know yet)
   returns >=size if output is truncated (conforming to C99 standard)
*/
int vsnprintf_r(char *str, size_t size, const char *format, va_list ap) {
   size_t i = 0;
   char c;
   int full = 0;		/* indicate if output buffer full */

   --size;	/* without trailing \0 */
   while (c = *format++) {
      if (c == '\\') {
	 
      } else if (c == '%') {
#if HAVE_TYPE_LONGLONG
#	 define num_buff_len ((sizeof(unsigned long long)*8+2)/3+1)	/* hold up to u long long in octal w/ \0 */
#else
#	 define num_buff_len ((sizeof(unsigned long)*8+2)/3+1)];	/* hold up to u long in octal w/ \0 */
#endif
	 char lengthmod = '\0';	/* 'h' 'l' 'L' 'z' */
	 int leading0 = 0;	/* or 1 */
	 size_t fsize = 0;	/* size of field */
	 const char *st;		/* string */
	 long  lo; unsigned long  ulo;
#if HAVE_TYPE_LONGLONG
	 long long ll; unsigned long long ull;
#endif
	 char field[num_buff_len];	/* result of number conversion */
	 char *np;			/* num pointer */

	 c = *format++;
	 if (c == '\0')  { break; }
	 
	 /* flag characters */
	 switch (c) {
	 case '0': leading0 = 1;  c = *format++;  break;
	    /* not handled: '#' '-' ' ' '+' '\'' */
	 }
	 if (c == '\0')  { break; }

	 /* field width */
	 switch (c) {
	 case '1': case '2': case '3': case '4':
	 case '5': case '6': case '7': case '8': case '9':
	    do {
	       fsize = 10*fsize+(c-'0');
	       c = *format++;
	    } while (c && isdigit(c));
	    break;
	 }
	 if (c == '\0')  { break; }

	 /* precision - not handles */

	 /* length modifier */
	 switch (c) {
	    /* not handled: 'q' 'j' 't' */
	    /* handled: 'h' 'hh'->'H' 'z' 'Z'->'z' 'l' 'll'->'L' 'L' */
	 case 'Z': c = 'z'; /* fall through */
#if HAVE_TYPE_LONGLONG
	 case 'L':
#endif
	 case 'z': lengthmod = c; c = *format++; break;
	 case 'h':
	    lengthmod = c;
	    if ((c = *format++) == 'h') {
	       lengthmod = 'H'; c = *format++; 
	    }
	    break;
	 case 'l': 
	    lengthmod = c;
	    if ((c = *format++) == 'l') {
	       lengthmod = 'L'; c = *format++; 
	    }
	    break;
	 }
	 if (c == '\0')  { break; }

	 /* conversion specifier */
	 switch (c) {
	 case 'c': c = va_arg(ap, int); /* fall through */
	 case '%': *str++ = c; if (++i == size) { full = 1; } break;

	 case 's': st = va_arg(ap, const char *);
	    /* no modifier handled! */
	    while (c = *st++) {
	       *str++ = c;
	       if (++i == size) { full = 1; break; }
	    }
	    break;
	 case 'd':
#if HAVE_TYPE_LONGLONG
	    if (lengthmod == 'L') {
	       ll = va_arg(ap, long long);
	       np = diag_longlong_to_dec(field, num_buff_len, ll, leading0, fsize);
	       while (c = *np++)  {
		  *str++ = c;
		  if (++i == size) { full = 1; break; }
	       }
	    } else
#endif
	    {
	       switch (lengthmod) {
	       case 'l': lo = va_arg(ap, long); break;
	       case 'z': lo = va_arg(ap, ptrdiff_t); break;
	       default: lo = va_arg(ap, int); break;
	       }
	       np = diag_long_to_dec(field, num_buff_len, lo, leading0, fsize);
	       while (c = *np++)  { *str++ = c;
		  if (++i == size) { full = 1; break; }
	       }
	    }
	    break;
	 case 'u':
#if HAVE_TYPE_LONGLONG
	    if (lengthmod == 'L') {
	       ull = va_arg(ap, unsigned long long);
	       np = diag_ulonglong_to_dec(field, num_buff_len, ull, leading0, fsize);
	       while (c = *np++)  { *str++ = c;
		  if (++i == size) { full = 1; break; }
	       }
	    } else
#endif
	    {
	       switch (lengthmod) {
	       case 'l': ulo = va_arg(ap, unsigned long); break;
	       case 'z': ulo = va_arg(ap, size_t); break;
	       default: ulo = va_arg(ap, unsigned int); break;
	       }
	       np = diag_ulong_to_dec(field, num_buff_len, ulo, leading0, fsize);
	       while (c = *np++)  { *str++ = c;
		  if (++i == size) { full = 1; break; }
	       }
	    }
	    break;
	 case 'p':
	    ulo = va_arg(ap, size_t);
	    np = diag_ulong_to_hex(field, num_buff_len, ulo, leading0, fsize);
	    *str++ = '0'; if (++i == size) { full = 1; break; }
	    *str++ = 'x'; if (++i == size) { full = 1; break; }
	    while (c = *np++)  { *str++ = c;
	       if (++i == size) { full = 1; break; }
	    }
	    break;
	 case 'x':
#if HAVE_TYPE_LONGLONG
	    if (lengthmod == 'L') {
	       ull = va_arg(ap, unsigned long long);
	       np = diag_ulonglong_to_hex(field, num_buff_len, ull, leading0, fsize);
	       while (c = *np++)  { *str++ = c;
		  if (++i == size) { full = 1; break; }
	       }
	    } else
#endif
	    {
	       switch (lengthmod) {
	       case 'l': ulo = va_arg(ap, unsigned long); break;
	       case 'z': ulo = va_arg(ap, size_t); break;
	       default: ulo = va_arg(ap, unsigned int); break;
	       }
	       np = diag_ulong_to_hex(field, num_buff_len, ulo, leading0, fsize);
	       while (c = *np++)  { *str++ = c;
		  if (++i == size) { full = 1; break; }
	       }
	    }
	    break;
	 case 'o':
#if HAVE_TYPE_LONGLONG
	    if (lengthmod == 'L') {
	       ull = va_arg(ap, unsigned long long);
	       np = diag_ulonglong_to_oct(field, num_buff_len, ull, leading0, fsize);
	       while (c = *np++)  { *str++ = c;
		  if (++i == size) break;
	       }
	    } else
#endif
	    {
	       switch (lengthmod) {
	       case 'l': ulo = va_arg(ap, unsigned long); break;
	       case 'z': ulo = va_arg(ap, size_t); break;
	       default: ulo = va_arg(ap, unsigned int); break;
	       }
	       np = diag_ulong_to_oct(field, num_buff_len, ulo, leading0, fsize);
	       while (c = *np++)  { *str++ = c;
		  if (++i == size) { full = 1; break; }
	       }
	    }
	    break;
	 default:
	    *str++ = c;  if (++i == size) { full = 1; break; }
	 }
	 if (full)  break;
      } else {
	 *str++ = c;
	 if (++i == size)  break;
      }
   }
   *str = '\0';
   return i;
}

int snprintf_r(char *str, size_t size, const char *format, ...) {
   int result;
   va_list ap;
   va_start(ap, format);
   result = vsnprintf_r(str, size, format, ap);
   va_end(ap);
   return result;
}