2000-08-11 21:53:56 +00:00
|
|
|
/*
|
2014-06-30 09:10:35 +00:00
|
|
|
** fseek.c
|
|
|
|
**
|
|
|
|
** Christian Groessler, 2000-08-07
|
|
|
|
** Ullrich von Bassewitz, 2004-05-12
|
|
|
|
*/
|
2000-08-11 21:53:56 +00:00
|
|
|
|
|
|
|
|
2003-11-06 18:04:07 +00:00
|
|
|
|
2000-08-11 21:53:56 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <errno.h>
|
2003-06-12 18:17:25 +00:00
|
|
|
#include <unistd.h>
|
2000-08-11 21:53:56 +00:00
|
|
|
#include "_file.h"
|
|
|
|
|
2004-05-13 21:29:18 +00:00
|
|
|
|
2003-11-06 18:04:07 +00:00
|
|
|
|
|
|
|
/*****************************************************************************/
|
2013-05-09 11:56:54 +00:00
|
|
|
/* Code */
|
2003-11-06 18:04:07 +00:00
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
|
|
|
2000-08-11 21:53:56 +00:00
|
|
|
|
2004-05-13 21:29:18 +00:00
|
|
|
int __fastcall__ fseek (register FILE* f, long offset, int whence)
|
2000-08-11 21:53:56 +00:00
|
|
|
{
|
|
|
|
long res;
|
|
|
|
|
|
|
|
/* Is the file open? */
|
|
|
|
if ((f->f_flags & _FOPEN) == 0) {
|
2010-06-03 20:43:30 +00:00
|
|
|
_seterrno (EINVAL); /* File not open */
|
2003-11-06 18:04:07 +00:00
|
|
|
return -1;
|
2000-08-11 21:53:56 +00:00
|
|
|
}
|
|
|
|
|
2004-05-13 21:29:18 +00:00
|
|
|
/* If we have a pushed back character, and whence is relative to the
|
2014-06-30 09:10:35 +00:00
|
|
|
** current position, correct the offset.
|
|
|
|
*/
|
2004-05-13 21:29:18 +00:00
|
|
|
if ((f->f_flags & _FPUSHBACK) && whence == SEEK_CUR) {
|
|
|
|
--offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Do the seek */
|
2000-08-11 21:53:56 +00:00
|
|
|
res = lseek(f->f_fd, offset, whence);
|
2004-05-13 21:29:18 +00:00
|
|
|
|
2010-06-03 20:43:30 +00:00
|
|
|
/* If the seek was successful. Discard any effects of the ungetc function,
|
2014-06-30 09:10:35 +00:00
|
|
|
** and clear the end-of-file indicator. Otherwise set the error indicator
|
|
|
|
** on the stream, and return -1. We will check for >= 0 here, because that
|
|
|
|
** saves some code, and we don't have files with 2 gigabytes in size
|
|
|
|
** anyway:-)
|
|
|
|
*/
|
2010-06-03 20:43:30 +00:00
|
|
|
if (res >= 0) {
|
|
|
|
f->f_flags &= ~(_FEOF | _FPUSHBACK);
|
|
|
|
return 0;
|
|
|
|
} else {
|
2004-05-13 21:29:18 +00:00
|
|
|
f->f_flags |= _FERROR;
|
|
|
|
return -1;
|
|
|
|
}
|
2000-08-11 21:53:56 +00:00
|
|
|
}
|
|
|
|
|