1 /*
2  * Copyright (c) 1993, 1994, 1995, 1996, 1997
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that: (1) source code distributions
7  * retain the above copyright notice and this paragraph in its entirety, (2)
8  * distributions including binary code include the above copyright notice and
9  * this paragraph in its entirety in the documentation or other materials
10  * provided with the distribution, and (3) all advertising materials mentioning
11  * features or use of this software display the following acknowledgement:
12  * ``This product includes software developed by the University of California,
13  * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
14  * the University nor the names of its contributors may be used to endorse
15  * or promote products derived from this software without specific prior
16  * written permission.
17  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
18  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
19  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
20  *
21  * 64-bit-offset fseek and ftell.
22  */
23 
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27 
28 #include <stdio.h>
29 #include <errno.h>
30 
31 #include "tcpslice.h"
32 
33 #if defined(HAVE_FSEEKO)
34 /*
35  * We have fseeko(), so we have ftello().
36  * If we have large file support (files larger than 2^31-1 bytes),
37  * fseeko() will let us seek to a current file position with more
38  * than 32 bits and ftello() will give us a current file position
39  * with more than 32 bits.
40  */
41 int
fseek64(FILE * p,const int64_t offset,const int whence)42 fseek64(FILE *p, const int64_t offset, const int whence)
43 {
44 	off_t off_t_offset;
45 
46 	/*
47 	 * Make sure the offset fits.
48 	 */
49 	off_t_offset = (off_t)offset;
50 	if (offset != off_t_offset) {
51 		/*
52 		 * It doesn't.  Fail with EINVAL.
53 		 */
54 		errno = EINVAL;
55 		return (-1);
56 	}
57 	return (fseeko(p, off_t_offset, whence));
58 }
59 
60 int64_t
ftell64(FILE * p)61 ftell64(FILE *p)
62 {
63 	return (ftello(p));
64 }
65 #elif defined(_MSC_VER)
66 /*
67  * We have Visual Studio; we support only 2005 and later, so we have
68  * _fseeki64() and _ftelli64().
69  */
70 int
fseek64(FILE * p,const int64_t offset,const int whence)71 fseek64(FILE *p, const int64_t offset, const int whence)
72 {
73 	return (_fseeki64(p, offset, whence));
74 }
75 
76 int64_t
ftell64(FILE * p)77 ftell64(FILE *p)
78 {
79 	return (_ftelli64(p));
80 }
81 #else
82 /*
83  * We don't have ftello() or _ftelli64(), so fall back on ftell().
84  * Either long is 64 bits, in which case ftell() should suffice,
85  * or this is probably an older 32-bit UN*X without large file
86  * support, which means you'll probably get errors trying to
87  * write files > 2^31-1, so it won't matter anyway.
88  *
89  * XXX - what about MinGW?
90  */
91 int
fseek64(FILE * p,const int64_t offset,const int whence)92 fseek64(FILE *p, const int64_t offset, const int whence)
93 {
94 	long long_offset;
95 
96 	/*
97 	 * Make sure the offset fits.
98 	 */
99 	long_offset = (long)offset;
100 	if (offset != long_offset) {
101 		/*
102 		 * It doesn't.  Fail with EINVAL.
103 		 */
104 		errno = EINVAL;
105 		return (-1);
106 	}
107 	return (fseek(p, long_offset, whence));
108 }
109 
110 int64_t
ftell64(FILE * p)111 ftell64(FILE *p)
112 {
113 	return (ftell(p));
114 }
115 #endif
116