/*
    (C) 1995-96 AROS - The Amiga Replacement OS
    $Id: fseek.c,v 1.1 1997/03/06 14:21:47 digulla Exp $

    Desc: Change the position in a stream
    Lang: english
*/
#include <errno.h>
#include <dos/dos.h>
#include <proto/dos.h>
#include "__errno.h"
#include "__stdio.h"

/*****************************************************************************

    NAME */
#include <stdio.h>

	int fseek (

/*  SYNOPSIS */
	FILE * stream,
	long   offset,
	int    whence)

/*  FUNCTION
	Change the current position in a stream.

    INPUTS
	stream - Modify this stream
	offset, whence - How to modify the current position. whence
		can be SEEK_SET, then offset is the absolute position
		in the file (0 is the first byte), SEEK_CUR then the
		position will change by offset (ie. -5 means to move
		5 bytes to the beginning of the file) or SEEK_END.
		SEEK_END means that the offset is relative to the
		end of the file (-1 is the last byte and 0 is
		the EOF).

    RESULT
	0 on success and -1 on error. If an error occurred, the global
	variable errno is set.

    NOTES

    EXAMPLE

    BUGS

    SEE ALSO
	fopen(), fwrite()

    INTERNALS

    HISTORY
	15.12.1996 digulla created

******************************************************************************/
{
    int  cnt;
    BPTR fh;

    switch (whence)
    {
    case SEEK_SET: whence = OFFSET_BEGINNING; break;
    case SEEK_CUR: whence = OFFSET_CURRENT; break;
    case SEEK_END: whence = OFFSET_END; break;
    default:
	errno = EINVAL;
	return -1;
    }

    if (((IPTR)stream) < 3)
    {
	errno = EINVAL;
	return -1;
    }

    fh = (BPTR)(stream->fh);

    cnt = Seek (fh, offset, whence);

    if (cnt == -1)
    {
	errno = IoErr2errno (IoErr ());

	cnt = -1;
    }

    return cnt;
} /* fseek */

