/*
 * fprintf.c - vfprintf(), vprintf(), fprintf(), printf(), vsprintf(), sprintf()

    This file is part of libRILc, a standard C library for GCC on Amiga OS.
    Copyright © 1997, 1998  Rask Ingemann Lambertsen

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License as published by the Free Software Foundation; either
    version 2 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Library General Public License for more details.

    You should have received a copy of the GNU Library General Public
    License along with this library; if not, write to the Free
    Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

    As a special exception, if you link this library with files compiled
    with a GNU compiler to produce an executable, this does not cause the
    resulting executable to be covered by the GNU General Public License.
    This exception does not however invalidate any other reasons why the
    executable file might be covered by the GNU General Public License.
*/

#include <exec/types.h>
#include <dos/dos.h>
#include <stdio.h>
#include <stdarg.h>

#ifdef __PPC__
#include <ppcproto/dos.h>
#else
#include <proto/dos.h>
#endif

#include "internal/signalcheck.h"

#ifdef __PRINTF_STYLE
#if __PRINTF_STYLE == FLOAT
#define __vrawdofmt __float_vrawdofmt
#endif
#endif

/* Data structure for PutChProc/stuffchar. */
struct PutChData
{
    APTR    stream;
    ULONG   numwritten;
};

/* PutChProc funtion for __vrawdofmt() and (v)(f)printf. */

STATIC int PutChProc (int ch, struct PutChData *pcd)
{
    if (EOF != putc (ch, (FILE *) pcd->stream))
    {
        pcd->numwritten ++;
        return (0);
    }
    else
        return (EOF);
}

struct stuffCharData { char *buf; };

#ifdef __PPC__
STATIC int stuffChar (int ch, struct stuffCharData *scd)
#else
STATIC int stuffChar (int ch, struct stuffCharData *scd)
#endif
{
    *(scd->buf++) = ch;
    return (0);
}

int vfprintf (FILE *stream, const char *fmt, va_list args)
{
    struct PutChData pcd;

    __check_signals ();

    /* Format it. */
    pcd.stream = stream;
    pcd.numwritten = 0;

    __vrawdofmt (&pcd, (int (*)(int, void *)) PutChProc, fmt, args);

    return (pcd.numwritten);
}

int printf (const char *fmt, ...)
{
    int written;
    va_list vargs;

    va_start (vargs, fmt);

    written = vfprintf (stdout, fmt, vargs);

    va_end (vargs);

    return (written);
}

int fprintf (FILE *output, const char *fmt, ...)
{
    int written;
    va_list vargs;

    va_start (vargs, fmt);

    written = vfprintf (output, fmt, vargs);

    va_end (vargs);

    return (written);
}

int vprintf (const char *fmt, va_list vargs)
{
    return (vfprintf (stdout, fmt, vargs));
}

int vsprintf (char *buf, const char *fmt, va_list args)
{
    struct stuffCharData scd;
    size_t outcount;

    __check_signals ();

    /* Format it. */
    scd.buf = buf;

    outcount = __vrawdofmt (&scd, (int (*)(int, void *)) stuffChar, fmt, args);
    /* *scd.buf = '\0'; */
    buf[outcount] = '\0';

/*    return (scd.buf - buf); */ /* Doesn't work?? */
    return (outcount);
}

int sprintf (char *buf, const char *fmt, ...)
{
    int written;
    va_list vargs;

    va_start (vargs, fmt);

    written = vsprintf (buf, fmt, vargs);

    va_end (vargs);

    return (written);
}
