/*
 *   stdio.h -- ANSI 
 *
 *   Standard input/output facilities.
 *
 *           Copyright (c) 1990, MetaWare Incorporated
 */

#ifndef _STDIO_H
#define _STDIO_H

#define BUFSIZ	  (512)
#define BUFSIZE   (BUFSIZ)
#define EOF	  (-1)
#ifndef NULL
#define NULL	  ((void *)0)
#endif
#ifndef _SIZE_T_DEFINED
#define _SIZE_T_DEFINED
typedef unsigned int size_t;
#endif

#ifndef _VA_LIST_DEFINED
#    define _VA_LIST_DEFINED
     typedef char *va_list;
#endif

typedef long fpos_t;
#define FOPEN_MAX  (25)	  /* Maximum number of open files */
#define SYS_OPEN (25)
#define FILENAME_MAX  (128)     /* Recommended size for file name buffer. */

/* Elements of the File_flags set:	*/
#define _BUFFERED	(0x01)
        /* Later ANSI additions: */
        #define _IOFBF _BUFFERED
        #define _IOLBF _BUFFERED        /* Line-buffered not supported yet. */
        #define _IONBF 0                /* No bits. */
#define _READING	(0x02)
#define _WRITING	(0x04)
#define _APPENDING	(0x08)
#define _END_OF_FILE	(0x10)
#define _ERROR		(0x20)
#define _FORMATTED_TEXT (0x40)
#define _UNINITIALIZED	(0x80)
#define _SETBUF_BUFFER	(0x100)
#define _TEMP_FILE	(0x200)
#define _CTRL_Z 	(0x400)
#define _WROTE_LAST	(0x800)
typedef short int File_flags;
/* type  File_flags = set of
   (Buffered, Reading, Writing, Appending, End_of_file,
    Error, Formatted_text, Uninitialized, Setbuf_buffer, Temp_file);
*/
typedef char Buffer[BUFSIZ];

typedef struct {
      char * _ptr;		 /* Pointer to current byte in buffer */
      int _cnt; 		 /* Bytes remaining in buffer */
      Buffer *_base;		 /* Address of buffer */
      File_flags _flag;
      char _fd;		 	 /* Associated file handle */
      char Padding;		 /* Round out to even # bytes. */
      int _nextline;
      } _iobuf;

typedef _iobuf FILE;

extern _iobuf _iob[/*FOPEN_MAX*/];
/* For upwards compatibility we put the bufsiz in a parallel array.
 * This prevents the need for all previous customers to recompile.
 * Versions of UNIX have played this same game.
 */
extern unsigned _bufsiz[/*FOPEN_MAX*/];

#define stdin	(&_iob[0])
#define stdout	(&_iob[1])
#define stderr	(&_iob[2])

#ifdef __HIGHC__
#define P_tmpdir "\\"
#endif
#define L_tmpnam (18)	 /* length of file name string generated by tmpnam */
			/* only 13 chars are used by loc, but goc needs 18 */
#define TMP_MAX  (1000)  /* min number of unique names generated by tmpnam */
#define SEEK_SET (0)	 /*From_beginning*/
#define SEEK_CUR (1)	 /*From_current*/
#define SEEK_END (2)	 /*From_end*/

/*  #define _IOFBF 1  */
/*  #define _IOLBF 2  */
/*  #define _IONBF 3  */

/* the MS definitions of the following are not implemented .........
_IOREAD _IOWRT _IOFBF _IOLBF _IONBF _IOMYBUF _IOEOF _IOERR _IOSTRG _IORW
note that we have some of these names defined for our own purposes, but
they may not behave in the same way............caution is in order here!!!
*/

#ifdef __HIGHC__
extern int unlink(const char *__pathname);
#endif
extern int remove(const char *__pathname);
/* extern int rmtmp(void); */
extern int rename(const char *__old, const char *__new);
extern FILE *tmpfile(void);
extern char *tmpnam(char *__s);
/* extern char * tempnam(char *, char *); */
extern int fclose(FILE *__stream);
/* extern int fcloseall(void); */
extern int fflush(FILE *__stream);
/* extern int flushall(void); */
extern FILE *fopen(const char *__pathname, const char *__type);
extern FILE *freopen(const char *__pathname, const char *__type, FILE *__stream);
extern int setvbuf(FILE *__stream, char *__buf, int __mode, size_t __size);
extern void setbuf(FILE *__stream, char *__buf);
extern size_t fread(void *__ptr, size_t __size, size_t __nelem, FILE *__stream);
extern size_t fwrite(const void *__ptr, size_t __size, size_t __nelem, FILE *__stream);
extern int fgetc(FILE *__stream);

#undef getc
extern int getc(FILE *__stream);

#define getc(F) ( ((F)->_cnt > 0) &&		 \
		 ( *(F)->_ptr != '\r' || !((F)->_flag & _FORMATTED_TEXT) ) \
		 ?    (F)->_cnt--, *(F)->_ptr++  \
		 :    fgetc(F)			 \
		)
#undef getchar
extern int getchar(void);
#define getchar() (getc(stdin))
extern char *gets(char *__s);
/* extern int getw(FILE *); */
extern char *fgets(char *__s, int __n, FILE *__stream);
extern int fputs(const char *__s, FILE *__stream);
extern int fputc(int __c, FILE *__stream);
#ifdef __HIGHC__
extern int fputchar(int __c);
extern int fgetchar(void);
#endif

#undef putc
extern int putc(int __c, FILE *__stream);

/* WARNING!  putc refers to its parameter c twice, since newlines 	*/
/* have to be special-cased due to required DOS translation.		*/

#define putc(c,F) ( ((F)->_cnt > 0) && ((c) != '\n') \
		   ?	(F)->_cnt--, *(F)->_ptr++ = (c)  \
		   :	fputc(c,F) \
		  )

#undef putchar
extern int putchar(int __c);
#define putchar(c) (putc(c, stdout))
extern int puts(const char *__s);
/* extern int putw(int, FILE *); */
extern int ungetc(int __c, FILE *__stream);
extern int fseek(FILE *__stream, long __offset, int __ptrname);
extern int fsetpos(FILE *__stream, const fpos_t *__pos);
extern int fgetpos(FILE *__stream, fpos_t *__pos);
extern long ftell(FILE *__stream);
extern void rewind(FILE *__stream);

#undef clearerr
extern void clearerr(FILE *__stream);
/* clear _ERROR and _END_OF_FILE */
#define clearerr(stream) ((stream)->_flag &= ~(_ERROR | _END_OF_FILE))

#undef feof
extern int feof(FILE *__stream);
#define feof(stream) ((stream)->_flag & _END_OF_FILE)

#undef ferror
extern int ferror(FILE *__stream);
#define ferror(stream) ((stream)->_flag & _ERROR)

extern FILE *_fdopen(int __fd, const char *__type);
extern FILE *_fsopen(const char *__pathname, const char *__type, int __share_f);
extern int _unlink(const char *__pathname);
extern int _fputchar(int __c);
extern int _fgetchar(void);

#ifdef __HIGHC__ 
extern FILE *fdopen(int __fd, const char *__type);
#endif

#define _fileno(__stream) ((__stream)->_fd)

#define _O_TEXT   0x4000	/* low level text files */
#define _O_BINARY 0x8000	/* low level binary files */

#ifdef __HIGHC__
#define fileno(stream) ((stream)->_fd)
#endif

extern void perror(const char *__s);  /* MSVERSION & ANSI
OLD HIGHC VERSION extern char *perror(const char *s); */

#if __1167 && __HIGHC__	/* Tell compiler that x87 libraries do not kill 1167 registers. */
		/* This produces more efficient code. */
pragma calling_convention(_DEFAULT_CALLING_CONVENTION | _NO_1167);
#endif
extern int vprintf(const char *__format, va_list __arg);
extern int vfprintf(FILE *__stream, const char *__format, va_list __arg);
extern int vsprintf(char *__s, const char *__format, va_list __arg);
extern int printf(const char *__format, ...);
extern int fprintf(FILE *__stream, const char *__format, ...);
extern int sprintf(char *__s, const char *__format, ...);
extern int fscanf(FILE *__stream, const char *__format, ...);
extern int scanf(const char *__format, ...);
extern int sscanf(char *__s, const char *__format, ...);
#if __1167 && __HIGHC__
pragma calling_convention();
#endif

extern void _setmode(FILE *__stream, int __mode);
/* values for _setmode's mode argument */
#define _TEXT	  (0)	  /* _setmode(F,_TEXT) makes F a text file */
#define _BINARY   (1)	  /* _setmode(F,_BINARY) makes F a binary file */
/* used to set _fmode */
#define _USER_FILES_BINARY	 (1)
#define _STDIN_AND_STDOUT_BINARY (2)
#define _STDERR_BINARY		 (4)
#define _ALL_FILES_BINARY	 \
	     (_USER_FILES_BINARY | _STDIN_AND_STDOUT_BINARY | _STDERR_BINARY)
#define _ALL_FILES_TEXT 	 (~_ALL_FILES_BINARY)
#define _USER_FILES_TEXT	 (_fmode & ~_USER_FILES_BINARY)
#define _STDIN_AND_STDOUT_TEXT	 (_fmode & ~_STDIN_AND_STDOUT_BINARY)
#define _STDERR_TEXT		 (_fmode & ~_STDERR_BINARY)

#endif /* _STDIO_H */
