date and time created 91/03/07 10:23:53 by bostic
[unix-history] / usr / src / lib / libc / stdio / snprintf.c
CommitLineData
c9be6cfe
KB
1/*-
2 * Copyright (c) 1990 The Regents of the University of California.
3 * All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Chris Torek.
7 *
8 * %sccs.include.redist.c%
9 */
10
11#if defined(LIBC_SCCS) && !defined(lint)
12static char sccsid[] = "@(#)snprintf.c 5.1 (Berkeley) %G%";
13#endif /* LIBC_SCCS and not lint */
14
15#include <stdio.h>
16#if __STDC__
17#include <stdarg.h>
18#else
19#include <varargs.h>
20#endif
21
22#if __STDC__
23snprintf(char *str, size_t n, char const *fmt, ...)
24#else
25snprintf(str, n, fmt, va_alist)
26 char *str;
27 size_t n;
28 char *fmt;
29 va_dcl
30#endif
31{
32 int ret;
33 va_list ap;
34 FILE f;
35
36 if ((int)n < 1)
37 return (EOF);
38#if __STDC__
39 va_start(ap, fmt);
40#else
41 va_start(ap);
42#endif
43 f._flags = __SWR | __SSTR;
44 f._bf._base = f._p = (unsigned char *)str;
45 f._bf._size = f._w = n - 1;
46 ret = vfprintf(&f, fmt, ap);
47 *f._p = 0;
48 va_end(ap);
49 return (ret);
50}