/*
 * stdio.h for libRILc.

    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.
*/

#ifndef _STDIO_H
#define _STDIO_H

#ifdef __cplusplus
extern "C" {
#endif

/* Prevent exec/types.h from typedef'ing FLOAT (so GCC builds). */
#define FLOAT __exec__FLOAT

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

/* Fix exec/types.h conflict with libg++ */
#undef GLOBAL
#undef FLOAT

/* The internal structure of file pointers. The layout of the structure is
 * considered private and changes a couple of times between releases. */
typedef struct
{
  struct MinNode node; /* Node for linkage. */
  BPTR fh; /* dos.library file handle associated with file. */
  int  socket; /* socket associated with file. */
  unsigned int eof_flag : 1;   /* feof () */
  unsigned int error_flag : 1; /* ferror () */
  unsigned int closable : 1;   /* 1 -> don't close fh. */
  unsigned int freeable : 1;   /* 1 -> don't __free_file this FILE. */
} FILE;

/* Standard buffer size for files. */
#define BUFSIZ 16384

#define EOF ENDSTREAMCH

#define SEEK_SET OFFSET_BEGINNING
#define SEEK_CUR OFFSET_CURRENT
#define SEEK_END OFFSET_END

/* For mktemp(), tmpnam(), tempnam(). */
#define P_tmpdir "T:"
#define L_tmpnam 16    /* Round numbers are evil. This is a round number. */ 
#define TMP_MAX 1      /* I'm lazy right now. */

/* Select the *printf() formatting style. Default is as per ANSI but without
 * floating point formatting (%f, %G, ...). Set __PRINTF_STYLE to FLOAT to
 * enable floating point formatting. To get RawDoFmt() style formatting, set
 * __PRINTF_STYLE to AMIGA.
 */

#ifdef __PRINTF_STYLE
#if __PRINTF_STYLE==AMIGA
#define vfprintf __amiga_vfprintf
#define  vprintf __amiga_vprintf
#define  fprintf __amiga_fprintf
#define   printf __amiga_printf
#define vsprintf __amiga_vsprintf
#define  sprintf __amiga_sprintf
#elif __PRINTF_STYLE==FLOAT
#define vfprintf __float_vfprintf
#define  vprintf __float_vprintf
#define  fprintf __float_fprintf
#define   printf __float_printf
#define vsprintf __float_vsprintf
#define  sprintf __float_sprintf

#define vfscanf __float_vfscanf
#define  vscanf __float_vscanf
#define  fscanf __float_fscanf
#define   scanf __float_scanf
#define vsscanf __float_vsscanf
#define  sscanf __float_sscanf
#endif
#endif

extern FILE *stdin, *stdout, *stderr;

/* Functions defined by ANSI. */
int fclose (FILE *file);
int fflush (FILE *file);
int fgetc (FILE *file);
int getchar (void);
char *fgets (char *str, int size, FILE *stream);
FILE *fopen (const char *name, const char *modes);
FILE *freopen (const char *name, const char *modes, FILE *oldFile);
int vfprintf (FILE *stream, const char *fmt, va_list args);
int printf (const char *fmt, ...);
int fprintf (FILE *output, const char *fmt, ...);
int vprintf (const char *fmt, va_list vargs);
int vsprintf (char *buf0, const char *fmt, va_list args);
int sprintf (char *buf, const char *fmt, ...);
int fputc (int ch, FILE *file);
int putchar (int ch);
int fputs (const char *string, FILE *file);
int puts (const char *string);
size_t fread (void *ptr, const size_t size, const size_t nmemb, FILE *stream);
int fseek (FILE *file, const long int offset, const int refPoint);
long int ftell (FILE *stream);
void rewind (FILE *stream);
size_t fwrite (const void *ptr, const size_t size, const size_t nmemb, FILE *stream);
void perror (const char *errmsg);
int rename (const char *from, const char *to);
int ungetc (int c, FILE *file);
int vfscanf (FILE *stream, const char *fmt, va_list args);
int scanf (const char *fmt, ...);
int fscanf (FILE *output, const char *fmt, ...);
int vscanf (const char *fmt, va_list vargs);
int vsscanf (char *buf, const char *fmt, va_list args);
int sscanf (char *buf, const char *fmt, ...);
int ferror (FILE *file);
int feof (FILE *file);
void clearerr (FILE *file);

/* Non-ANSI extensions. */
int getw (int wch, FILE *stream);
int putw (int wch, FILE *stream);
FILE *fdopen (int file, const char *modes);
/* FILE *fhopen (BPTR fh); *//* libRILc extension. */
int fileno (FILE *file);

/* libnix formatting routines used by (__float_)*printf() */
int __vrawdofmt (void *stream, int (*putch)(int, void *), const char *format, va_list args);
int __float_vrawdofmt (void *stream, int (*putch)(int, void *), const char *format, va_list args);

/* libnix scanning routines used by (__float_)*scanf() */
int __vrawscanf (void *stream, int (*getchfunc)(void *), int (*ungetchfunc)(int, void *), const char *format, va_list args);
int __float_vrawscanf (void *stream, int (*getchfunc)(void *), int (*ungetchfunc)(int, void *), const char *format, va_list args);

/* Functions for which macros exist. */
#define getc(a) fgetc(a)
#define putc(a,b) fputc(a,b)
#define getchar() getc(stdin)
#define putchar(a) putc(a,stdout)

#ifdef __cplusplus
}
#endif

#endif /* _STDIO_H */
