USB device can now be unbind
[AROS.git] / compiler / stdc / vsnprintf.c
blob84dd2e3b5cc86aaea1bc2b7c5aaf993ee2d99b24
1 /*
2 Copyright © 1995-2001, The AROS Development Team. All rights reserved.
3 $Id$
5 C99 function vsnprintf().
6 */
7 /* Original source from libnix */
9 #include <stdarg.h>
10 #include <stddef.h>
12 struct data
14 char * str;
15 size_t n;
18 static int _vsnprintf_uc (int c, struct data * data)
20 if (data->n)
22 *(data->str) ++ = c;
23 data->n --;
26 return 1;
29 /*****************************************************************************
31 NAME */
32 #include <stdio.h>
34 int vsnprintf (
36 /* SYNOPSIS */
37 char * str,
38 size_t n,
39 const char * format,
40 va_list args)
42 /* FUNCTION
43 Format a list of arguments and put them into the string str.
44 The function makes sure that no more than n characters (including
45 the terminal 0 byte) are written into str.
46 If n is zero, nothing is written, and s may be a null pointer. Otherwise,
47 output characters beyond the n-1st are discarded rather than being written
48 to the array, and a null character is written at the end of the characters
49 actually written into the array. If copying takes place between objects
50 that overlap, the behavior is undefined.
52 INPUTS
53 str - The formatted result is stored here
54 n - The size of str
55 format - A printf() format string.
56 args - A list of arguments for the format string.
58 RESULT
59 Function returns the number of characters that would have been
60 written had n been sufficiently large, not counting the terminating null
61 character, or a negative value if an encoding error occurred. Thus, the
62 null-terminated output has been completely written if and only if the
63 returned value is nonnegative and less than n.
65 NOTES
67 EXAMPLE
69 BUGS
71 SEE ALSO
72 printf(), sprintf(), fprintf(), vprintf(), vfprintf(), snprintf(),
73 vsnprintf()
75 INTERNALS
77 ******************************************************************************/
79 int rc;
80 struct data data;
82 if (str == NULL && n != 0) n = 0;
84 data.n = n;
85 data.str = str;
87 rc = __vcformat (&data, (void *)_vsnprintf_uc, format, args);
89 if (data.n)
91 *(data.str) = 0;
93 else if ( n != 0 )
95 str[n-1] = 0;
98 return rc;
99 } /* vsnprintf */