/*
 * This file was generated automatically by xsubpp version 1.938 from the 
 * contents of IO.xs. Do not edit this file, edit IO.xs instead.
 *
 *	ANY CHANGES MADE HERE WILL BE LOST! 
 *
 */

#include "EXTERN.h"
#define PERLIO_NOT_STDIO 1
#include "perl.h"
#include "XSUB.h"

#ifdef I_UNISTD
#  include <unistd.h>
#endif
#ifdef I_FCNTL
#  include <fcntl.h>
#endif

#ifdef PerlIO
typedef int SysRet;
typedef PerlIO * InputStream;
typedef PerlIO * OutputStream;
#else
#define PERLIO_IS_STDIO 1
typedef int SysRet;
typedef FILE * InputStream;
typedef FILE * OutputStream;
#endif

static int
not_here(s)
char *s;
{
    croak("%s not implemented on this architecture", s);
    return -1;
}

static bool
constant(name, pval)
char *name;
IV *pval;
{
    switch (*name) {
    case '_':
	if (strEQ(name, "_IOFBF"))
#ifdef _IOFBF
	    { *pval = _IOFBF; return TRUE; }
#else
	    return FALSE;
#endif
	if (strEQ(name, "_IOLBF"))
#ifdef _IOLBF
	    { *pval = _IOLBF; return TRUE; }
#else
	    return FALSE;
#endif
	if (strEQ(name, "_IONBF"))
#ifdef _IONBF
	    { *pval = _IONBF; return TRUE; }
#else
	    return FALSE;
#endif
	break;
    case 'S':
	if (strEQ(name, "SEEK_SET"))
#ifdef SEEK_SET
	    { *pval = SEEK_SET; return TRUE; }
#else
	    return FALSE;
#endif
	if (strEQ(name, "SEEK_CUR"))
#ifdef SEEK_CUR
	    { *pval = SEEK_CUR; return TRUE; }
#else
	    return FALSE;
#endif
	if (strEQ(name, "SEEK_END"))
#ifdef SEEK_END
	    { *pval = SEEK_END; return TRUE; }
#else
	    return FALSE;
#endif
	break;
    }

    return FALSE;
}


XS(XS_IO__Seekable_fgetpos)
{
    dXSARGS;
    if (items != 1)
	croak("Usage: IO::Seekable::getpos(handle)");
    {
	InputStream	handle = IoIFP(sv_2io(ST(0)));
	SV *	RETVAL;
	if (handle) {
	    Fpos_t pos;
#ifdef PerlIO
	    PerlIO_getpos(handle, &pos);
#else
	    fgetpos(handle, &pos);
#endif
	    ST(0) = sv_2mortal(newSVpv((char*)&pos, sizeof(Fpos_t)));
	}
	else {
	    ST(0) = &sv_undef;
	    errno = EINVAL;
	}
    }
    XSRETURN(1);
}

XS(XS_IO__Seekable_fsetpos)
{
    dXSARGS;
    if (items != 2)
	croak("Usage: IO::Seekable::setpos(handle, pos)");
    {
	InputStream	handle = IoIFP(sv_2io(ST(0)));
	SV *	pos = ST(1);
	SysRet	RETVAL;
	if (handle)
#ifdef PerlIO
	    RETVAL = PerlIO_setpos(handle, (Fpos_t*)SvPVX(pos));
#else
	    RETVAL = fsetpos(handle, (Fpos_t*)SvPVX(pos));
#endif
	else {
	    RETVAL = -1;
	    errno = EINVAL;
	}
	ST(0) = sv_newmortal();
	if (RETVAL != -1) {
	    if (RETVAL == 0)
		sv_setpvn(ST(0), "0 but true", 10);
	    else
		sv_setiv(ST(0), (IV)RETVAL);
	}
    }
    XSRETURN(1);
}

XS(XS_IO__File_new_tmpfile)
{
    dXSARGS;
    if (items < 0 || items > 1)
	croak("Usage: IO::File::new_tmpfile(packname = \"IO::File\")");
    {
	char *	packname;
	OutputStream	RETVAL;

	if (items < 1)
	    packname = "IO::File";
	else {
	    packname = (char *)SvPV(ST(0),na);
	}
#ifdef PerlIO
	RETVAL = PerlIO_tmpfile();
#else
	RETVAL = tmpfile();
#endif
	ST(0) = sv_newmortal();
	{
	    GV *gv = newGVgen("IO::File");
	    if ( do_open(gv, "+>&", 3, FALSE, 0, 0, RETVAL) )
		sv_setsv(ST(0), sv_bless(newRV((SV*)gv), gv_stashpv("IO::File",1)));
	    else
		ST(0) = &sv_undef;
	}
    }
    XSRETURN(1);
}

XS(XS_IO__Handle_constant)
{
    dXSARGS;
    if (items != 1)
	croak("Usage: IO::Handle::constant(name)");
    {
	char *	name = (char *)SvPV(ST(0),na);
	SV *	RETVAL;
	IV i;
	if (constant(name, &i))
	    ST(0) = sv_2mortal(newSViv(i));
	else
	    ST(0) = &sv_undef;
    }
    XSRETURN(1);
}

XS(XS_IO__Handle_ungetc)
{
    dXSARGS;
    if (items != 2)
	croak("Usage: IO::Handle::ungetc(handle, c)");
    {
	InputStream	handle = IoIFP(sv_2io(ST(0)));
	int	c = (int)SvIV(ST(1));
	int	RETVAL;
	if (handle)
#ifdef PerlIO
	    RETVAL = PerlIO_ungetc(handle, c);
#else
	    RETVAL = ungetc(c, handle);
#endif
	else {
	    RETVAL = -1;
	    errno = EINVAL;
	}
	ST(0) = sv_newmortal();
	sv_setiv(ST(0), (IV)RETVAL);
    }
    XSRETURN(1);
}

XS(XS_IO__Handle_ferror)
{
    dXSARGS;
    if (items != 1)
	croak("Usage: IO::Handle::error(handle)");
    {
	InputStream	handle = IoIFP(sv_2io(ST(0)));
	int	RETVAL;
	if (handle)
#ifdef PerlIO
	    RETVAL = PerlIO_error(handle);
#else
	    RETVAL = ferror(handle);
#endif
	else {
	    RETVAL = -1;
	    errno = EINVAL;
	}
	ST(0) = sv_newmortal();
	sv_setiv(ST(0), (IV)RETVAL);
    }
    XSRETURN(1);
}

XS(XS_IO__Handle_clearerr)
{
    dXSARGS;
    if (items != 1)
	croak("Usage: IO::Handle::clearerr(handle)");
    {
	InputStream	handle = IoIFP(sv_2io(ST(0)));
	int	RETVAL;
	if (handle) {
#ifdef PerlIO
	    PerlIO_clearerr(handle);
#else
	    clearerr(handle);
#endif
	    RETVAL = 0;
	}
	else {
	    RETVAL = -1;
	    errno = EINVAL;
	}
	ST(0) = sv_newmortal();
	sv_setiv(ST(0), (IV)RETVAL);
    }
    XSRETURN(1);
}

XS(XS_IO__Handle_untaint)
{
    dXSARGS;
    if (items != 1)
	croak("Usage: IO::Handle::untaint(handle)");
    {
	SV *	handle = ST(0);
	int	RETVAL;
	IO * io;
	io = sv_2io(handle);
	if (io) {
	    IoFLAGS(io) |= IOf_UNTAINT;
	    RETVAL = 0;
	}
        else {
	    RETVAL = -1;
	    errno = EINVAL;
	}
	ST(0) = sv_newmortal();
	sv_setiv(ST(0), (IV)RETVAL);
    }
    XSRETURN(1);
}

XS(XS_IO__Handle_fflush)
{
    dXSARGS;
    if (items != 1)
	croak("Usage: IO::Handle::flush(handle)");
    {
	OutputStream	handle = IoOFP(sv_2io(ST(0)));
	SysRet	RETVAL;
	if (handle)
#ifdef PerlIO
	    RETVAL = PerlIO_flush(handle);
#else
	    RETVAL = Fflush(handle);
#endif
	else {
	    RETVAL = -1;
	    errno = EINVAL;
	}
	ST(0) = sv_newmortal();
	if (RETVAL != -1) {
	    if (RETVAL == 0)
		sv_setpvn(ST(0), "0 but true", 10);
	    else
		sv_setiv(ST(0), (IV)RETVAL);
	}
    }
    XSRETURN(1);
}

XS(XS_IO__Handle_setbuf)
{
    dXSARGS;
    if (items != 2)
	croak("Usage: IO::Handle::setbuf(handle, buf)");
    {
	OutputStream	handle = IoOFP(sv_2io(ST(0)));
	char * buf = SvPOK(ST(1)) ? sv_grow(ST(1), BUFSIZ) : 0;
	if (handle)
#ifdef PERLIO_IS_STDIO
	    setbuf(handle, buf);
#else
	    not_here("IO::Handle::setbuf");
#endif
    }
    XSRETURN(1);
}

XS(XS_IO__Handle_setvbuf)
{
    dXSARGS;
    if (items != 4)
	croak("Usage: IO::Handle::setvbuf(handle, buf, type, size)");
    {
	OutputStream	handle = IoOFP(sv_2io(ST(0)));
	char * buf = SvPOK(ST(1)) ? sv_grow(ST(1), SvIV(ST(3))) : 0;
	int	type = (int)SvIV(ST(2));
	int	size = (int)SvIV(ST(3));
	SysRet	RETVAL;
#ifdef PERLIO_IS_STDIO
#ifdef _IOFBF   /* Should be HAS_SETVBUF once Configure tests for that */
	if (handle)
	    RETVAL = setvbuf(handle, buf, type, size);
	else {
	    RETVAL = -1;
	    errno = EINVAL;
	}
#else
	    RETVAL = (SysRet) not_here("IO::Handle::setvbuf");
#endif /* _IOFBF */
#else
	    not_here("IO::Handle::setvbuf");
#endif
	ST(0) = sv_newmortal();
	if (RETVAL != -1) {
	    if (RETVAL == 0)
		sv_setpvn(ST(0), "0 but true", 10);
	    else
		sv_setiv(ST(0), (IV)RETVAL);
	}
    }
    XSRETURN(1);
}

#ifdef __cplusplus
extern "C"
#endif
XS(boot_IO)
{
    dXSARGS;
    char* file = __FILE__;

    XS_VERSION_BOOTCHECK ;

        newXS("IO::Seekable::getpos", XS_IO__Seekable_fgetpos, file);
        newXS("IO::Seekable::setpos", XS_IO__Seekable_fsetpos, file);
        newXS("IO::File::new_tmpfile", XS_IO__File_new_tmpfile, file);
        newXS("IO::Handle::constant", XS_IO__Handle_constant, file);
        newXS("IO::Handle::ungetc", XS_IO__Handle_ungetc, file);
        newXS("IO::Handle::error", XS_IO__Handle_ferror, file);
        newXS("IO::Handle::clearerr", XS_IO__Handle_clearerr, file);
        newXS("IO::Handle::untaint", XS_IO__Handle_untaint, file);
        newXS("IO::Handle::flush", XS_IO__Handle_fflush, file);
        newXS("IO::Handle::setbuf", XS_IO__Handle_setbuf, file);
        newXS("IO::Handle::setvbuf", XS_IO__Handle_setvbuf, file);
    ST(0) = &sv_yes;
    XSRETURN(1);
}
