#ifndef _STDIOM_H
#define _STDIOM_H
#include <thread.h>
#include <synch.h>
#include <mtlib.h>
#include <stdarg.h>
#include "file64.h"
#include <wchar.h>
#include "mse.h"
#define _DEF_FLAG_MASK 0377
#ifdef _LP64
#define _BYTE_MODE_FLAG 0400
#define _WC_MODE_FLAG 01000
#define _IONOLOCK 02000
#define _SEEKABLE 04000
#define SET_IONOLOCK(iop) ((iop)->_flag |= _IONOLOCK)
#define CLEAR_IONOLOCK(iop) ((iop)->_flag &= ~_IONOLOCK)
#define GET_IONOLOCK(iop) ((iop)->_flag & _IONOLOCK)
#define SET_BYTE_MODE(iop) ((iop)->_flag |= _BYTE_MODE_FLAG)
#define CLEAR_BYTE_MODE(iop) ((iop)->_flag &= ~_BYTE_MODE_FLAG)
#define GET_BYTE_MODE(iop) ((iop)->_flag & _BYTE_MODE_FLAG)
#define SET_WC_MODE(iop) ((iop)->_flag |= _WC_MODE_FLAG)
#define CLEAR_WC_MODE(iop) ((iop)->_flag &= ~_WC_MODE_FLAG)
#define GET_WC_MODE(iop) ((iop)->_flag & _WC_MODE_FLAG)
#define GET_NO_MODE(iop) (!((iop)->_flag & \
(_BYTE_MODE_FLAG | _WC_MODE_FLAG)))
#define SET_SEEKABLE(iop) ((iop)->_flag |= _SEEKABLE)
#define CLEAR_SEEKABLE(iop) ((iop)->_flag &= ~_SEEKABLE)
#define GET_SEEKABLE(iop) ((iop)->_flag & _SEEKABLE)
#else
#define _BYTE_MODE_FLAG 0001
#define _WC_MODE_FLAG 0002
#define SET_IONOLOCK(iop) ((iop)->__ionolock = 1)
#define CLEAR_IONOLOCK(iop) ((iop)->__ionolock = 0)
#define GET_IONOLOCK(iop) ((iop)->__ionolock)
#define SET_BYTE_MODE(iop) ((iop)->__orientation |= _BYTE_MODE_FLAG)
#define CLEAR_BYTE_MODE(iop) ((iop)->__orientation &= ~_BYTE_MODE_FLAG)
#define GET_BYTE_MODE(iop) ((iop)->__orientation & _BYTE_MODE_FLAG)
#define SET_WC_MODE(iop) ((iop)->__orientation |= _WC_MODE_FLAG)
#define CLEAR_WC_MODE(iop) ((iop)->__orientation &= ~_WC_MODE_FLAG)
#define GET_WC_MODE(iop) ((iop)->__orientation & _WC_MODE_FLAG)
#define GET_NO_MODE(iop) (!((iop)->__orientation & \
(_BYTE_MODE_FLAG | _WC_MODE_FLAG)))
#define SET_SEEKABLE(iop) ((iop)->__seekable = 1)
#define CLEAR_SEEKABLE(iop) ((iop)->__seekable = 0)
#define GET_SEEKABLE(iop) ((iop)->__seekable)
#define STDIOP(iop) ((iop) >= &_iob[0] && (iop) < &_iob[_NFILE])
#define IOPIND(iop) ((iop) - &_iob[0])
#endif
typedef unsigned char Uchar;
#define _flockrel(rl) cancel_safe_mutex_unlock(rl)
#define MAXVAL (MAXINT - (MAXINT % BUFSIZ))
#define PUSHBACK (((3 + sizeof (int) - 1) / sizeof (int)) * sizeof (int))
#define _SMBFSZ (((PUSHBACK + 4) < 8) ? 8 : (PUSHBACK + 4))
#if BUFSIZ == 1024
#define MULTIBFSZ(SZ) ((SZ) & ~0x3ff)
#elif BUFSIZ == 512
#define MULTIBFSZ(SZ) ((SZ) & ~0x1ff)
#else
#define MULTIBFSZ(SZ) ((SZ) - (SZ % BUFSIZ))
#endif
#undef _bufend
#define _bufend(iop) _realbufend(iop)
extern Uchar _smbuf[][_SMBFSZ];
extern void __cleanup(void);
extern void _flushlbf(void);
extern FILE *_findiop(void);
#ifdef _LP64
extern int __filbuf(FILE *);
extern int __flsbuf(int, FILE *);
#define _realbufend(iop) ((iop)->_end)
#else
extern Uchar *_realbufend(FILE *iop);
extern rmutex_t *_reallock(FILE *iop);
#endif
extern void _setbufend(FILE *iop, Uchar *end);
extern rmutex_t *_flockget(FILE *iop);
extern int _xflsbuf(FILE *iop);
extern int _wrtchk(FILE *iop);
extern void _bufsync(FILE *iop, Uchar *bufend);
extern int _fflush_u(FILE *iop);
extern int close_fd(FILE *iop);
extern int _doscan(FILE *, const char *, va_list);
#ifdef _LP64
extern void close_pid(void);
#endif
extern int _get_fd(FILE *);
extern int _file_set(FILE *, int, const char *);
#ifdef _LP64
#define SET_FILE(iop, fd) ((iop)->_file = (fd))
#else
#define SET_FILE(iop, fd) (iop)->_magic = (fd); (iop)->__extendedfd = 0
#endif
#ifdef _LP64
#define _FILE_FD_MAX INT_MAX
#else
#define _FILE_FD_MAX 255
#endif
extern int _fileno(FILE *iop);
extern Uchar *_findbuf(FILE *iop);
extern FILE *_endopen(const char *, const char *, FILE *, int);
extern size_t _fwrite_unlocked(const void *, size_t, size_t, FILE *);
int _getc_internal(FILE *);
int _putc_internal(int, FILE *);
int _ungetc_unlocked(int, FILE *);
#define _needsync(p, bufend) ((bufend - (p)->_ptr) < \
((p)->_cnt < 0 ? 0 : (p)->_cnt))
#define _WRTCHK(iop) ((((iop->_flag & (_IOWRT | _IOEOF)) != _IOWRT) || \
(iop->_base == 0) || \
(iop->_ptr == iop->_base && iop->_cnt == 0 && \
!(iop->_flag & (_IONBF | _IOLBF)))) \
? _wrtchk(iop) : 0)
#ifdef _LP64
#define IOB_LCK(iop) (&((iop)->_lock))
#else
#define IOB_LCK(iop) (STDIOP(iop) ? &_xftab[IOPIND(iop)]._lock \
: _reallock(iop))
extern struct xFILEdata _xftab[];
#endif
extern ssize_t _xread(FILE *iop, void *buf, size_t nbytes);
extern ssize_t _xwrite(FILE *iop, const void *buf, size_t nbytes);
extern off_t _xseek(FILE *iop, off_t off, int whence);
extern off64_t _xseek64(FILE *iop, off64_t off, int whence);
extern int _xclose(FILE *iop);
extern void *_xdata(FILE *iop);
extern int _xassoc(FILE *iop, fread_t readf, fwrite_t writef,
fseek_t seekf, fclose_t closef, void *data);
extern void _xunassoc(FILE *iop);
extern int _stdio_flags(const char *type, int *oflags, int *fflags);
extern boolean_t memstream_seek(size_t base, off_t off, size_t max,
size_t *nposp);
extern int memstream_newsize(size_t pos, size_t alloc, size_t nbytes,
size_t *nallocp);
extern off64_t ftell_common(FILE *iop);
#endif