home *** CD-ROM | disk | FTP | other *** search
/ Aminet 18 / aminetcdnumber181997.iso / Aminet / misc / emu / AROSdev.lha / AROS / compiler / clib / vsnprintf.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-01-09  |  1.7 KB  |  96 lines

  1. /*
  2.     (C) 1995-96 AROS - The Amiga Replacement OS
  3.     $Id: vsnprintf.c,v 1.3 1997/01/08 04:12:53 ldp Exp $
  4.  
  5.     Desc: C function vsnprintf()
  6.     Lang: english
  7. */
  8. /* Original source from libnix */
  9. #define AROS_ALMOST_COMPATIBLE
  10. #include <stdio.h>
  11.  
  12. struct data
  13. {
  14.     char * str;
  15.     size_t n;
  16. };
  17.  
  18. static int _vsnprintf_uc (int c, struct data * data)
  19. {
  20.     if (data->n)
  21.     {
  22.     *(data->str) ++ = c;
  23.     data->n --;
  24.  
  25.     return 1;
  26.     }
  27.  
  28.     return EOF;
  29. }
  30.  
  31. /*****************************************************************************
  32.  
  33.     NAME */
  34.     #include <stdio.h>
  35. #include <stdarg.h>
  36.  
  37.     int vsnprintf (
  38.  
  39. /*  SYNOPSIS */
  40.     char       * str,
  41.     size_t         n,
  42.     const char * format,
  43.     va_list      args)
  44.  
  45. /*  FUNCTION
  46.     Format a list of arguments and put them into the string str.
  47.     The function makes sure that no more than n characters (including
  48.     the terminal 0 byte) are written into str.
  49.  
  50.     INPUTS
  51.     str - The formatted result is stored here
  52.     n - The size of str
  53.     format - A printf() format string.
  54.     args - A list of arguments for the format string.
  55.  
  56.     RESULT
  57.     The number of characters written or -1 if the string was too small.
  58.     In this case, the string is not 0-terminated.
  59.  
  60.     NOTES
  61.     No check is beeing made that str is large enough to contain
  62.     the result.
  63.  
  64.     EXAMPLE
  65.  
  66.     BUGS
  67.  
  68.     SEE ALSO
  69.     printf(), sprintf(), fprintf(), vprintf(), vfprintf(), snprintf(),
  70.     vsnprintf()
  71.  
  72.     INTERNALS
  73.  
  74.     HISTORY
  75.     11.12.1996 digulla created
  76.  
  77. ******************************************************************************/
  78. {
  79.     int rc;
  80.     struct data data;
  81.  
  82.     data.n = n;
  83.     data.str = str;
  84.  
  85.     rc = __vcformat (&data, (void *)_vsnprintf_uc, format, args);
  86.  
  87.     if (data.n)
  88.     {
  89.     *(data.str) = 0;
  90.  
  91.     return (n - data.n);
  92.     }
  93.  
  94.     return -1;
  95. } /* vsnprintf */
  96.