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