Rocco Rutte:
[apps/madmutt.git] / snprintf.c
1 /**************************************************************
2  * Original:
3  * Patrick Powell Tue Apr 11 09:48:21 PDT 1995
4  * A bombproof version of doprnt (dopr) included.
5  * Sigh.  This sort of thing is always nasty do deal with.  Note that
6  * the version here does not include floating point...
7  *
8  * snprintf() is used instead of sprintf() as it does limit checks
9  * for string length.  This covers a nasty loophole.
10  *
11  * The other functions are there to prevent NULL pointers from
12  * causing nast effects.
13  *
14  * More Recently:
15  *  Brandon Long <blong@fiction.net> 9/15/96 for mutt 0.43
16  *  This was ugly.  It is still ugly.  I opted out of floating point
17  *  numbers, but the formatter understands just about everything
18  *  from the normal C string format, at least as far as I can tell from
19  *  the Solaris 2.5 printf(3S) man page.
20  *
21  *  Brandon Long <blong@fiction.net> 10/22/97 for mutt 0.87.1
22  *    Ok, added some minimal floating point support, which means this
23  *    probably requires libm on most operating systems.  Don't yet
24  *    support the exponent (e,E) and sigfig (g,G).  Also, fmtint()
25  *    was pretty badly broken, it just wasn't being exercised in ways
26  *    which showed it, so that's been fixed.  Also, formated the code
27  *    to mutt conventions, and removed dead code left over from the
28  *    original.  Also, there is now a builtin-test, just compile with:
29  *           gcc -DTEST_SNPRINTF -o snprintf snprintf.c -lm
30  *    and run snprintf for results.
31  * 
32  *  Thomas Roessler <roessler@does-not-exist.org> 01/27/98 for mutt 0.89i
33  *    The PGP code was using unsigned hexadecimal formats. 
34  *    Unfortunately, unsigned formats simply didn't work.
35  *
36  *  Michael Elkins <me@mutt.org> 03/05/98 for mutt 0.90.8
37  *    The original code assumed that both snprintf() and vsnprintf() were
38  *    missing.  Some systems only have snprintf() but not vsnprintf(), so
39  *    the code is now broken down under HAVE_SNPRINTF and HAVE_VSNPRINTF.
40  *
41  **************************************************************/
42
43 #if HAVE_CONFIG_H
44 # include "config.h"
45 #endif
46
47 #if !defined(HAVE_SNPRINTF) || !defined(HAVE_VSNPRINTF)
48
49 #include <string.h>
50 # include <ctype.h>
51 #include <sys/types.h>
52
53 /* Define this as a fall through, HAVE_STDARG_H is probably already set */
54
55 #define HAVE_VARARGS_H
56
57 /* varargs declarations: */
58
59 #if defined(HAVE_STDARG_H)
60 # include <stdarg.h>
61 # define HAVE_STDARGS           /* let's hope that works everywhere (mj) */
62 # define VA_LOCAL_DECL   va_list ap
63 # define VA_START(f)     va_start(ap, f)
64 # define VA_SHIFT(v,t)  ;       /* no-op for ANSI */
65 # define VA_END          va_end(ap)
66 #else
67 # if defined(HAVE_VARARGS_H)
68 #  include <varargs.h>
69 #  undef HAVE_STDARGS
70 #  define VA_LOCAL_DECL   va_list ap
71 #  define VA_START(f)     va_start(ap)  /* f is ignored! */
72 #  define VA_SHIFT(v,t) v = va_arg(ap,t)
73 #  define VA_END        va_end(ap)
74 # else
75 /*XX ** NO VARARGS ** XX*/
76 # endif
77 #endif
78
79 /*int snprintf (char *str, size_t count, const char *fmt, ...);*/
80 /*int vsnprintf (char *str, size_t count, const char *fmt, va_list arg);*/
81
82 static void dopr (char *buffer, size_t maxlen, const char *format,
83                   va_list args);
84 static void fmtstr (char *buffer, size_t * currlen, size_t maxlen,
85                     char *value, int flags, int min, int max);
86 static void fmtint (char *buffer, size_t * currlen, size_t maxlen,
87                     long value, int base, int min, int max, int flags);
88 static void fmtfp (char *buffer, size_t * currlen, size_t maxlen,
89                    long double fvalue, int min, int max, int flags);
90 static void dopr_outch (char *buffer, size_t * currlen, size_t maxlen,
91                         char c);
92
93 /*
94  * dopr(): poor man's version of doprintf
95  */
96
97 /* format read states */
98 #define DP_S_DEFAULT 0
99 #define DP_S_FLAGS   1
100 #define DP_S_MIN     2
101 #define DP_S_DOT     3
102 #define DP_S_MAX     4
103 #define DP_S_MOD     5
104 #define DP_S_CONV    6
105 #define DP_S_DONE    7
106
107 /* format flags - Bits */
108 #define DP_F_MINUS      (1 << 0)
109 #define DP_F_PLUS       (1 << 1)
110 #define DP_F_SPACE      (1 << 2)
111 #define DP_F_NUM        (1 << 3)
112 #define DP_F_ZERO       (1 << 4)
113 #define DP_F_UP         (1 << 5)
114 #define DP_F_UNSIGNED   (1 << 6)
115
116 /* Conversion Flags */
117 #define DP_C_SHORT   1
118 #define DP_C_LONG    2
119 #define DP_C_LDOUBLE 3
120
121 #define char_to_int(p) (p - '0')
122 #undef MAX
123 #define MAX(p,q) ((p >= q) ? p : q)
124
125 static void dopr (char *buffer, size_t maxlen, const char *format,
126                   va_list args)
127 {
128   char ch;
129   long value;
130   long double fvalue;
131   char *strvalue;
132   int min;
133   int max;
134   int state;
135   int flags;
136   int cflags;
137   size_t currlen;
138
139   state = DP_S_DEFAULT;
140   currlen = flags = cflags = min = 0;
141   max = -1;
142   ch = *format++;
143
144   while (state != DP_S_DONE) {
145     if ((ch == '\0') || (currlen >= maxlen))
146       state = DP_S_DONE;
147
148     switch (state) {
149     case DP_S_DEFAULT:
150       if (ch == '%')
151         state = DP_S_FLAGS;
152       else
153         dopr_outch (buffer, &currlen, maxlen, ch);
154       ch = *format++;
155       break;
156     case DP_S_FLAGS:
157       switch (ch) {
158       case '-':
159         flags |= DP_F_MINUS;
160         ch = *format++;
161         break;
162       case '+':
163         flags |= DP_F_PLUS;
164         ch = *format++;
165         break;
166       case ' ':
167         flags |= DP_F_SPACE;
168         ch = *format++;
169         break;
170       case '#':
171         flags |= DP_F_NUM;
172         ch = *format++;
173         break;
174       case '0':
175         flags |= DP_F_ZERO;
176         ch = *format++;
177         break;
178       default:
179         state = DP_S_MIN;
180         break;
181       }
182       break;
183     case DP_S_MIN:
184       if (isdigit ((unsigned char) ch)) {
185         min = 10 * min + char_to_int (ch);
186         ch = *format++;
187       }
188       else if (ch == '*') {
189         min = va_arg (args, int);
190
191         ch = *format++;
192         state = DP_S_DOT;
193       }
194       else
195         state = DP_S_DOT;
196       break;
197     case DP_S_DOT:
198       if (ch == '.') {
199         state = DP_S_MAX;
200         ch = *format++;
201       }
202       else
203         state = DP_S_MOD;
204       break;
205     case DP_S_MAX:
206       if (isdigit ((unsigned char) ch)) {
207         if (max < 0)
208           max = 0;
209         max = 10 * max + char_to_int (ch);
210         ch = *format++;
211       }
212       else if (ch == '*') {
213         max = va_arg (args, int);
214
215         ch = *format++;
216         state = DP_S_MOD;
217       }
218       else
219         state = DP_S_MOD;
220       break;
221     case DP_S_MOD:
222       /* Currently, we don't support Long Long, bummer */
223       switch (ch) {
224       case 'h':
225         cflags = DP_C_SHORT;
226         ch = *format++;
227         break;
228       case 'l':
229         cflags = DP_C_LONG;
230         ch = *format++;
231         break;
232       case 'L':
233         cflags = DP_C_LDOUBLE;
234         ch = *format++;
235         break;
236       default:
237         break;
238       }
239       state = DP_S_CONV;
240       break;
241     case DP_S_CONV:
242       switch (ch) {
243       case 'd':
244       case 'i':
245         if (cflags == DP_C_SHORT)
246           value = va_arg (args, short int);
247
248         else if (cflags == DP_C_LONG)
249           value = va_arg (args, long int);
250
251         else
252           value = va_arg (args, int);
253
254         fmtint (buffer, &currlen, maxlen, value, 10, min, max, flags);
255         break;
256       case 'o':
257         flags |= DP_F_UNSIGNED;
258         if (cflags == DP_C_SHORT)
259           value = va_arg (args, unsigned short int);
260
261         else if (cflags == DP_C_LONG)
262           value = va_arg (args, unsigned long int);
263
264         else
265           value = va_arg (args, unsigned int);
266
267         fmtint (buffer, &currlen, maxlen, value, 8, min, max, flags);
268         break;
269       case 'u':
270         flags |= DP_F_UNSIGNED;
271         if (cflags == DP_C_SHORT)
272           value = va_arg (args, unsigned short int);
273
274         else if (cflags == DP_C_LONG)
275           value = va_arg (args, unsigned long int);
276
277         else
278           value = va_arg (args, unsigned int);
279
280         fmtint (buffer, &currlen, maxlen, value, 10, min, max, flags);
281         break;
282       case 'X':
283         flags |= DP_F_UP;
284       case 'x':
285         flags |= DP_F_UNSIGNED;
286         if (cflags == DP_C_SHORT)
287           value = va_arg (args, unsigned short int);
288
289         else if (cflags == DP_C_LONG)
290           value = va_arg (args, unsigned long int);
291
292         else
293           value = va_arg (args, unsigned int);
294
295         fmtint (buffer, &currlen, maxlen, value, 16, min, max, flags);
296         break;
297       case 'f':
298         if (cflags == DP_C_LDOUBLE)
299           fvalue = va_arg (args, long double);
300
301         else
302           fvalue = va_arg (args, double);
303
304         /* um, floating point? */
305         fmtfp (buffer, &currlen, maxlen, fvalue, min, max, flags);
306         break;
307       case 'E':
308         flags |= DP_F_UP;
309       case 'e':
310         if (cflags == DP_C_LDOUBLE)
311           fvalue = va_arg (args, long double);
312
313         else
314           fvalue = va_arg (args, double);
315
316         break;
317       case 'G':
318         flags |= DP_F_UP;
319       case 'g':
320         if (cflags == DP_C_LDOUBLE)
321           fvalue = va_arg (args, long double);
322
323         else
324           fvalue = va_arg (args, double);
325
326         break;
327       case 'c':
328         dopr_outch (buffer, &currlen, maxlen, va_arg (args, int));
329
330         break;
331       case 's':
332         strvalue = va_arg (args, char *);
333
334         if (max < 0)
335           max = maxlen;         /* ie, no max */
336         fmtstr (buffer, &currlen, maxlen, strvalue, flags, min, max);
337         break;
338       case 'p':
339         strvalue = va_arg (args, void *);
340
341         fmtint (buffer, &currlen, maxlen, (long) strvalue, 16, min, max,
342                 flags);
343         break;
344       case 'n':
345         if (cflags == DP_C_SHORT) {
346           short int *num;
347           num = va_arg (args, short int *);
348
349           *num = currlen;
350         }
351         else if (cflags == DP_C_LONG) {
352           long int *num;
353           num = va_arg (args, long int *);
354
355           *num = currlen;
356         }
357         else {
358           int *num;
359           num = va_arg (args, int *);
360
361           *num = currlen;
362         }
363         break;
364       case '%':
365         dopr_outch (buffer, &currlen, maxlen, ch);
366         break;
367       case 'w':
368         /* not supported yet, treat as next char */
369         ch = *format++;
370         break;
371       default:
372         /* Unknown, skip */
373         break;
374       }
375       ch = *format++;
376       state = DP_S_DEFAULT;
377       flags = cflags = min = 0;
378       max = -1;
379       break;
380     case DP_S_DONE:
381       break;
382     default:
383       /* hmm? */
384       break;                    /* some picky compilers need this */
385     }
386   }
387   if (currlen < maxlen - 1)
388     buffer[currlen] = '\0';
389   else
390     buffer[maxlen - 1] = '\0';
391 }
392
393 static void fmtstr (char *buffer, size_t * currlen, size_t maxlen,
394                     char *value, int flags, int min, int max)
395 {
396   int padlen, strln;            /* amount to pad */
397   int cnt = 0;
398
399   if (value == 0) {
400     value = "<NULL>";
401   }
402
403   for (strln = 0; value[strln]; ++strln);       /* mutt_strlen */
404   padlen = min - strln;
405   if (padlen < 0)
406     padlen = 0;
407   if (flags & DP_F_MINUS)
408     padlen = -padlen;           /* Left Justify */
409
410   while ((padlen > 0) && (cnt < max)) {
411     dopr_outch (buffer, currlen, maxlen, ' ');
412     --padlen;
413     ++cnt;
414   }
415   while (*value && (cnt < max)) {
416     dopr_outch (buffer, currlen, maxlen, *value++);
417     ++cnt;
418   }
419   while ((padlen < 0) && (cnt < max)) {
420     dopr_outch (buffer, currlen, maxlen, ' ');
421     ++padlen;
422     ++cnt;
423   }
424 }
425
426 /* Have to handle DP_F_NUM (ie 0x and 0 alternates) */
427
428 static void fmtint (char *buffer, size_t * currlen, size_t maxlen,
429                     long value, int base, int min, int max, int flags)
430 {
431   int signvalue = 0;
432   unsigned long uvalue;
433   char convert[20];
434   int place = 0;
435   int spadlen = 0;              /* amount to space pad */
436   int zpadlen = 0;              /* amount to zero pad */
437   int caps = 0;
438
439   if (max < 0)
440     max = 0;
441
442   uvalue = value;
443
444   if (!(flags & DP_F_UNSIGNED)) {
445     if (value < 0) {
446       signvalue = '-';
447       uvalue = -value;
448     }
449     else if (flags & DP_F_PLUS) /* Do a sign (+/i) */
450       signvalue = '+';
451     else if (flags & DP_F_SPACE)
452       signvalue = ' ';
453   }
454
455   if (flags & DP_F_UP)
456     caps = 1;                   /* Should characters be upper case? */
457
458   do {
459     convert[place++] = (caps ? "0123456789ABCDEF" : "0123456789abcdef")
460       [uvalue % (unsigned) base];
461     uvalue = (uvalue / (unsigned) base);
462   } while (uvalue && (place < 20));
463   if (place == 20)
464     place--;
465   convert[place] = 0;
466
467   zpadlen = max - place;
468   spadlen = min - MAX (max, place) - (signvalue ? 1 : 0);
469   if (zpadlen < 0)
470     zpadlen = 0;
471   if (spadlen < 0)
472     spadlen = 0;
473   if (flags & DP_F_ZERO) {
474     zpadlen = MAX (zpadlen, spadlen);
475     spadlen = 0;
476   }
477   if (flags & DP_F_MINUS)
478     spadlen = -spadlen;         /* Left Justifty */
479
480 #ifdef DEBUG_SNPRINTF
481   dprint (1, (debugfile, "zpad: %d, spad: %d, min: %d, max: %d, place: %d\n",
482               zpadlen, spadlen, min, max, place));
483 #endif
484
485   /* Spaces */
486   while (spadlen > 0) {
487     dopr_outch (buffer, currlen, maxlen, ' ');
488     --spadlen;
489   }
490
491   /* Sign */
492   if (signvalue)
493     dopr_outch (buffer, currlen, maxlen, signvalue);
494
495   /* Zeros */
496   if (zpadlen > 0) {
497     while (zpadlen > 0) {
498       dopr_outch (buffer, currlen, maxlen, '0');
499       --zpadlen;
500     }
501   }
502
503   /* Digits */
504   while (place > 0)
505     dopr_outch (buffer, currlen, maxlen, convert[--place]);
506
507   /* Left Justified spaces */
508   while (spadlen < 0) {
509     dopr_outch (buffer, currlen, maxlen, ' ');
510     ++spadlen;
511   }
512 }
513
514 static long double abs_val (long double value)
515 {
516   long double result = value;
517
518   if (value < 0)
519     result = -value;
520
521   return result;
522 }
523
524 static long double pow10 (int exp)
525 {
526   long double result = 1;
527
528   while (exp) {
529     result *= 10;
530     exp--;
531   }
532
533   return result;
534 }
535
536 static long round (long double value)
537 {
538   long intpart;
539
540   intpart = value;
541   value = value - intpart;
542   if (value >= 0.5)
543     intpart++;
544
545   return intpart;
546 }
547
548 static void fmtfp (char *buffer, size_t * currlen, size_t maxlen,
549                    long double fvalue, int min, int max, int flags)
550 {
551   int signvalue = 0;
552   long double ufvalue;
553   char iconvert[20];
554   char fconvert[20];
555   int iplace = 0;
556   int fplace = 0;
557   int padlen = 0;               /* amount to pad */
558   int zpadlen = 0;
559   int caps = 0;
560   long intpart;
561   long fracpart;
562
563   /* 
564    * AIX manpage says the default is 0, but Solaris says the default
565    * is 6, and sprintf on AIX defaults to 6
566    */
567   if (max < 0)
568     max = 6;
569
570   ufvalue = abs_val (fvalue);
571
572   if (fvalue < 0)
573     signvalue = '-';
574   else if (flags & DP_F_PLUS)   /* Do a sign (+/i) */
575     signvalue = '+';
576   else if (flags & DP_F_SPACE)
577     signvalue = ' ';
578
579 #if 0
580   if (flags & DP_F_UP)
581     caps = 1;                   /* Should characters be upper case? */
582 #endif
583
584   intpart = ufvalue;
585
586   /* 
587    * Sorry, we only support 9 digits past the decimal because of our 
588    * conversion method
589    */
590   if (max > 9)
591     max = 9;
592
593   /* We "cheat" by converting the fractional part to integer by
594    * multiplying by a factor of 10
595    */
596   fracpart = round ((pow10 (max)) * (ufvalue - intpart));
597
598   if (fracpart >= pow10 (max)) {
599     intpart++;
600     fracpart -= pow10 (max);
601   }
602
603 #ifdef DEBUG_SNPRINTF
604   dprint (1, (debugfile, "fmtfp: %f =? %d.%d\n", fvalue, intpart, fracpart));
605 #endif
606
607   /* Convert integer part */
608   do {
609     iconvert[iplace++] =
610       (caps ? "0123456789ABCDEF" : "0123456789abcdef")[intpart % 10];
611     intpart = (intpart / 10);
612   } while (intpart && (iplace < 20));
613   if (iplace == 20)
614     iplace--;
615   iconvert[iplace] = 0;
616
617   /* Convert fractional part */
618   do {
619     fconvert[fplace++] =
620       (caps ? "0123456789ABCDEF" : "0123456789abcdef")[fracpart % 10];
621     fracpart = (fracpart / 10);
622   } while (fracpart && (fplace < 20));
623   if (fplace == 20)
624     fplace--;
625   fconvert[fplace] = 0;
626
627   /* -1 for decimal point, another -1 if we are printing a sign */
628   padlen = min - iplace - max - 1 - ((signvalue) ? 1 : 0);
629   zpadlen = max - fplace;
630   if (zpadlen < 0)
631     zpadlen = 0;
632   if (padlen < 0)
633     padlen = 0;
634   if (flags & DP_F_MINUS)
635     padlen = -padlen;           /* Left Justifty */
636
637   if ((flags & DP_F_ZERO) && (padlen > 0)) {
638     if (signvalue) {
639       dopr_outch (buffer, currlen, maxlen, signvalue);
640       --padlen;
641       signvalue = 0;
642     }
643     while (padlen > 0) {
644       dopr_outch (buffer, currlen, maxlen, '0');
645       --padlen;
646     }
647   }
648   while (padlen > 0) {
649     dopr_outch (buffer, currlen, maxlen, ' ');
650     --padlen;
651   }
652   if (signvalue)
653     dopr_outch (buffer, currlen, maxlen, signvalue);
654
655   while (iplace > 0)
656     dopr_outch (buffer, currlen, maxlen, iconvert[--iplace]);
657
658   /*
659    * Decimal point.  This should probably use locale to find the correct
660    * char to print out.
661    */
662   dopr_outch (buffer, currlen, maxlen, '.');
663
664   while (fplace > 0)
665     dopr_outch (buffer, currlen, maxlen, fconvert[--fplace]);
666
667   while (zpadlen > 0) {
668     dopr_outch (buffer, currlen, maxlen, '0');
669     --zpadlen;
670   }
671
672   while (padlen < 0) {
673     dopr_outch (buffer, currlen, maxlen, ' ');
674     ++padlen;
675   }
676 }
677
678 static void dopr_outch (char *buffer, size_t * currlen, size_t maxlen, char c)
679 {
680   if (*currlen < maxlen)
681     buffer[(*currlen)++] = c;
682 }
683 #endif /* !defined(HAVE_SNPRINTF) || !defined(HAVE_VSNPRINTF) */
684
685 #ifndef HAVE_VSNPRINTF
686 int vsnprintf (char *str, size_t count, const char *fmt, va_list args)
687 {
688   str[0] = 0;
689   dopr (str, count, fmt, args);
690   return (mutt_strlen (str));
691 }
692 #endif /* !HAVE_VSNPRINTF */
693
694 #ifndef HAVE_SNPRINTF
695 /* VARARGS3 */
696 #ifdef HAVE_STDARGS
697 int snprintf (char *str, size_t count, const char *fmt, ...)
698 #else
699 int snprintf (va_alist)
700      va_dcl
701 #endif
702 {
703 #ifndef HAVE_STDARGS
704   char *str;
705   size_t count;
706   char *fmt;
707 #endif
708   VA_LOCAL_DECL;
709
710   VA_START (fmt);
711   VA_SHIFT (str, char *);
712
713   VA_SHIFT (count, size_t);
714   VA_SHIFT (fmt, char *);
715
716   (void) vsnprintf (str, count, fmt, ap);
717   VA_END;
718   return (mutt_strlen (str));
719 }
720
721 #ifdef TEST_SNPRINTF
722 #ifndef LONG_STRING
723 #define LONG_STRING 1024
724 #endif
725 int main (void)
726 {
727   char buf1[LONG_STRING];
728   char buf2[LONG_STRING];
729   char *fp_fmt[] = {
730     "%-1.5f",
731     "%1.5f",
732     "%123.9f",
733     "%10.5f",
734     "% 10.5f",
735     "%+22.9f",
736     "%+4.9f",
737     "%01.3f",
738     "%4f",
739     "%3.1f",
740     "%3.2f",
741     NULL
742   };
743   double fp_nums[] = { -1.5, 134.21, 91340.2, 341.1234, 0203.9, 0.96, 0.996,
744     0.9996, 1.996, 4.136, 0
745   };
746   char *int_fmt[] = {
747     "%-1.5d",
748     "%1.5d",
749     "%123.9d",
750     "%5.5d",
751     "%10.5d",
752     "% 10.5d",
753     "%+22.33d",
754     "%01.3d",
755     "%4d",
756     NULL
757   };
758   long int_nums[] = { -1, 134, 91340, 341, 0203, 0 };
759   int x, y;
760   int fail = 0;
761   int num = 0;
762
763   printf ("Testing snprintf format codes against system sprintf...\n");
764
765   for (x = 0; fp_fmt[x] != NULL; x++)
766     for (y = 0; fp_nums[y] != 0; y++) {
767       snprintf (buf1, sizeof (buf1), fp_fmt[x], fp_nums[y]);
768       sprintf (buf2, fp_fmt[x], fp_nums[y]);
769       if (strcmp (buf1, buf2)) {
770         printf ("snprintf doesn't match Format: %s\n\tsnprintf = %s\n\tsprintf  = %s\n",        /* __SPRINTF_CHECKED__ */
771                 fp_fmt[x], buf1, buf2);
772         fail++;
773       }
774       num++;
775     }
776
777   for (x = 0; int_fmt[x] != NULL; x++)
778     for (y = 0; int_nums[y] != 0; y++) {
779       snprintf (buf1, sizeof (buf1), int_fmt[x], int_nums[y]);
780       sprintf (buf2, int_fmt[x], int_nums[y]);
781       if (strcmp (buf1, buf2)) {
782         printf ("snprintf doesn't match Format: %s\n\tsnprintf = %s\n\tsprintf  = %s\n",        /* __SPRINTF_CHECKED__ */
783                 int_fmt[x], buf1, buf2);
784         fail++;
785       }
786       num++;
787     }
788   printf ("%d tests failed out of %d.\n", fail, num);
789 }
790 #endif /* SNPRINTF_TEST */
791
792 #endif /* !HAVE_SNPRINTF */