/*
 * Libraries and headers for PDC release 3.3 (C) 1989 Lionel Hummel.
 * PDC Software Distribution (C) 1989 Lionel Hummel and Paul Petersen.
 * PDC I/O Library (C) 1987 by J.A. Lydiatt.
 *
 * This code is freely redistributable upon the conditions that this
 * notice remains intact and that modified versions of this file not
 * be included as part of the PDC Software Distribution without the
 * express consent of the copyright holders.  No warrantee of any
 * kind is provided with this code.  For further information, contact:
 *
 *  PDC Software Distribution    Internet:                     BIX:
 *  P.O. Box 4006             or hummel@cs.uiuc.edu            lhummel
 *  Urbana, IL  61801-8801       petersen@uicsrd.csrd.uiuc.edu
 */

/* Copied from fprintf.c and modified. -ryb */

/*  vfprintf.c - various ways to output formatted strings
 *
 *  vfprintf    prints a string to a buffered output stream
 *  vprintf     prints a formatted string to stdout
 *  vsprintf    prints formatted a string into a given buffer
 */

#include <stdio.h>
#include <stdarg.h>

int _format (int (*) (int c, void *), const char *, va_list, void *);

/*
 * This routine called by printf and fprintf to output one character.
 */

static int
output (int c, void *stream)
{
  return fputc (c, (FILE *) stream);
}

/*
 * This routing called by sprintf to output on character to the buffer.
 */

static int
putone (int c, void *bufptr)
{
  *(*(char **)bufptr)++ = c;
  return (unsigned char) c;
}

int
vfprintf (FILE *fp, const char *fmt, va_list args)
{
  int count;

  count = _format (output, fmt, args, (void *) fp);
  return count;
}

int
vprintf (const char *fmt, va_list args)
{
  int count;

  count = _format (output, fmt, args, (void *) stdout);
  return count;
}

int
vsprintf (char *strbuf, const char *fmt, va_list args)
{
  int len;
  char *strptr = strbuf;

  len = _format (putone, fmt, args, (void *) &strptr);

  *strptr = '\0';

  return len;
}
