1 /* fseeko.c -- an implementation of fseek() with an off_t argument. 2 Copyright (C) 2003, Free Software Foundation, Inc. 3 4 This program is free software; you can redistribute it and/or modify 5 it under the terms of the GNU General Public License as published by 6 the Free Software Foundation; either version 2, or (at your option) 7 any later version. 8 9 This program is distributed in the hope that it will be useful, 10 but WITHOUT ANY WARRANTY; without even the implied warranty of 11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 GNU General Public License for more details. */ 13 14 #ifdef HAVE_CONFIG_H 15 #include "config.h" 16 #endif 17 18 #include <stdio.h> 19 #include <sys/types.h> 20 21 #ifdef HAVE_LIMITS_H 22 #include <limits.h> 23 #endif 24 #ifndef LONG_MAX 25 #define LONG_MAX ((long) ((unsigned long) ~0 >> 1)) 26 #endif 27 #ifndef LONG_MIN 28 #define LONG_MIN (-1 - LONG_MAX) 29 #endif 30 31 /* 32 * A replacement/substitute for fseeko, for hosts that don't have it. 33 */ 34 35 int 36 fseeko (FILE *stream, off_t offset, int whence) 37 { 38 while (offset != (long) offset) 39 { 40 long pos = (offset < 0) ? LONG_MIN : LONG_MAX; 41 42 if (fseek (stream, pos, whence) != 0) 43 return -1; 44 offset -= pos; 45 whence = SEEK_CUR; 46 } 47 return fseek (stream, (long) offset, whence); 48 } 49