/************************************************************

	StdIO.h
	Input / output
	
	Copyright © Apple Computer,Inc.  1985-1991.

    This file is used in these builds: ROM System

	Copyright American Telephone & Telegraph
	Used with permission, Apple Computer Inc. (1985)
	All Rights Reserved.

	Change History (most recent first):

		 <6>	11/19/91	JL		Changed int param type of _flsbuf to unsigned char type for
									consistency.
		 <5>	  8/8/91	JL		Updated copyright
		 <4>	10/24/90	JL		Matching MPW version

	To Do:
************************************************************/


#ifndef __STDIO__
#define __STDIO__

#define NULL 0

#ifndef __size_t__
#define __size_t__
typedef unsigned int size_t;
#endif

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


/*
 *	The basic data structure for a stream is the FILE.
 */

typedef struct {
	int 			_cnt;
	unsigned char	*_ptr;
	unsigned char	*_base;
	unsigned char	*_end;
	unsigned short	_size;
	unsigned short	_flag;
	unsigned short	_file;
} FILE;


/*
 *	fpos_t is a type that can express any position in a file.  A file's
 *	end-of-file marker has type fpos_t.
 */

typedef long fpos_t;


/*
 *	These macros give the meanings of bits in a FILE's _flag.  setvbuf() takes
 *	one of _IOFBF, _IOLBF, or _IONBF as its third argument.
 */

#define _IOFBF		0			/* Pseudo-flag, default buffering style */
#define _IOREAD 	(1<<0)		/* Current mode is for reading */
#define _IOWRT		(1<<1)		/* Current mode is for writing */
#define _IONBF		(1<<2)		/* no buffering */
#define _IOMYBUF	(1<<3)		/* buffer was allocated by stdio */
#define _IOEOF		(1<<4)
#define _IOERR		(1<<5)
#define _IOLBF		(1<<6)		/* fflush(iop) when a \n is written */
#define _IORW		(1<<7)		/* Enable read/write access */
#define _IOSYNC		(1<<8)		/* Input triggers fflush() to output fp's */
#define _IOBINARY	(1<<9)		/* Binary stream */


/*
 *	Default file buffer sizes used by setbuf() and setvbuf().
 */

#define BUFSIZ	1024			/* default file buffer size */
#define _LBFSIZ  100			/* Line buffer size */


/*
 *	The standard end-of-file indicator.
 */

#define EOF		(-1)


/*
 *	L_tmpnam is the size of char array long enough to hold a temporary file name
 *	generated by tmpnam(), including the trailing null byte.  The name is in the
 *	form tmp.AAAXXXXXX, where AAA is a sequence of lower case letters ("aaa", "baa",
 *	... "zzz" on successive calls), and XXXXXX is a lower case letter followed by a sequence
 *	of digits, all determined at runtime.
 *	TMP_MAX is the number of distinct file names that tmpnam() can generate.
 */

#define L_tmpnam	14
#define TMP_MAX		17576


/*
 *	The minimum number of files that a program is guaranteed to be able to have
 *	open simultaneously (including the pre-opened stdin, stdout, and stderr).
 *	The numbers are listed in Inside Macintosh, page IV-178, as:
 *	64K ROM, 128K Macintosh		12 files
 *	64K ROM, 512K Macintosh		40 files
 *	128K ROM					40 files per volume
 */

#define FOPEN_MAX	12


/*
 *	Maximum length of a file name, including a trailing zero byte.
 */

#define FILENAME_MAX	32


/*
 *	For use by fseek():
 */

#ifndef __FCNTL__			/* these defns exactly paralled in FCntl.h for lseek() */

#define SEEK_CUR	1
#define SEEK_END	2
#define SEEK_SET	0
#endif


/*
 *	The standard predefined streams.
 */

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


#ifdef __cplusplus
extern "C" {
#endif

/*
 *	Operations on files
 */

int remove(const char *filename);
int rename(const char *oldname, const char *newname);
FILE *tmpfile(void);
char *tmpnam(char *s);


/*
 *	File access functions
 */

int fclose(FILE *stream);
int fflush(FILE *stream);
FILE *fopen(const char *filename, const char *mode);
FILE *freopen(const char *filename, const char *mode, FILE *stream);
void setbuf(FILE *stream, char *buf);
int setvbuf(FILE *stream, char *buf, int mode, size_t size);


/*
 *	Formatted input/output functions
 */

int fprintf(FILE *stream, const char *format, ...);
int fscanf(FILE *stream, const char *format, ...);
int printf(const char *format, ...);
int scanf(const char *format, ...);
int sprintf(char *s, const char *format, ...);
int sscanf(const char *s, const char *format, ...);
int vfprintf(FILE *stream, const char *format, va_list arg);
int vprintf(const char *format, va_list arg);
int vsprintf(char *s, const char *format, va_list arg);


/*
 *	Character input/output functions and macros
 */

int fgetc(FILE *stream);
char *fgets(char *s, int n, FILE *stream);
int fputc(int c, FILE *stream);
int fputs(const char *s, FILE *stream);
int getc(FILE *stream);
#define getc(p) 	(--(p)->_cnt >= 0 ? (int) *(p)->_ptr++ : _filbuf(p))
int getchar(void);
#define getchar()	getc(stdin)
char *gets(char *s);
int putc(int c, FILE *stream);
#define putc(x, p)	(--(p)->_cnt >= 0 ? \
						((int) (*(p)->_ptr++ = (unsigned char) (x))) : \
						_flsbuf((unsigned char) (x), (p)))
int putchar(int c);
#define putchar(x)	putc((x), stdout)
int puts(const char *s);
int ungetc(int c, FILE *stream);


/*
 *	Direct input/output functions
 */

size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream);
size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream);


/*
 *	File positioning functions
 */

int fgetpos(FILE *stream, fpos_t *pos);
int fseek(FILE *stream, long int offset, int whence);
int fsetpos(FILE *stream, const fpos_t *pos);
long int ftell(FILE *stream);
void rewind(FILE *stream);


/*
 *	Error-handling functions and macros
 */

void clearerr(FILE *stream);
#define clearerr(p) ((void)((p)->_flag &= ~(_IOERR | _IOEOF)))
int feof(FILE *stream);
#define feof(p) 	((p)->_flag & _IOEOF)
int ferror(FILE *stream);
#define ferror(p)	((p)->_flag & _IOERR)
void perror(const char *s);

/*
 * For macros
 */

extern FILE _iob[];
#define _NFILE 20
int _filbuf(FILE *);
int _flsbuf(unsigned char, FILE *);

/*
 *	Non-ANSI extensions
 */

#ifndef __STDC__

#define fileno(p)	(p)->_file
FILE *fdopen(int fildes, const char *mode);
void fsetfileinfo (char *filename, unsigned long newcreator, unsigned long newtype);
int getw(FILE *stream);
int putw(int w, FILE *stream);

#endif

#ifdef __cplusplus
}
#endif


#endif
