1 /*
2  * Copyright (c) 1990 The Regents of the University of California.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms are permitted
6  * provided that the above copyright notice and this paragraph are
7  * duplicated in all such forms and that any documentation,
8  * and/or other materials related to such
9  * distribution and use acknowledge that the software was developed
10  * by the University of California, Berkeley.  The name of the
11  * University may not be used to endorse or promote products derived
12  * from this software without specific prior written permission.
13  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
16  */
17 
18 /*
19 FUNCTION
20 <<fopen64>>---open a large file
21 
22 INDEX
23 	fopen64
24 INDEX
25 	_fopen64_r
26 
27 SYNOPSIS
28 	#include <stdio.h>
29 	FILE *fopen64(const char *<[file]>, const char *<[mode]>);
30 	FILE *_fopen64_r(void *<[reent]>,
31                        const char *<[file]>, const char *<[mode]>);
32 
33 DESCRIPTION
34 <<fopen64>> is identical to <<fopen>> except it opens a large file that
35 is potentially >2GB in size.  See <<fopen>> for further details.
36 
37 RETURNS
38 <<fopen64>> return a file pointer which you can use for other file
39 operations, unless the file you requested could not be opened; in that
40 situation, the result is <<NULL>>.  If the reason for failure was an
41 invalid string at <[mode]>, <<errno>> is set to <<EINVAL>>.
42 
43 PORTABILITY
44 <<fopen64>> is a glibc extension.
45 
46 Supporting OS subroutines required: <<close>>, <<fstat64>>, <<isatty>>,
47 <<lseek64>>, <<open64>>, <<read>>, <<sbrk>>, <<write>>.
48 */
49 
50 /* Copied from fopen.c */
51 
52 #define _GNU_SOURCE
53 #include <stdio.h>
54 #include <errno.h>
55 #include <fcntl.h>
56 #include <sys/lock.h>
57 #include "../stdio/local.h"
58 
59 #ifdef __LARGE64_FILES
60 
61 FILE *
fopen64(const char * file,const char * mode)62 fopen64 (
63 	const char *file,
64 	const char *mode)
65 {
66   register FILE *fp;
67   register int f;
68   int flags, oflags;
69 
70   if ((flags = __sflags (mode, &oflags)) == 0)
71     return NULL;
72   if ((fp = __sfp ()) == NULL)
73     return NULL;
74 
75   if ((f = open64 (file, oflags, 0666)) < 0)
76     {
77       _newlib_sfp_lock_start ();
78       fp->_flags = 0;		/* release */
79 #ifndef __SINGLE_THREAD__
80       __lock_close_recursive (fp->_lock);
81 #endif
82       _newlib_sfp_lock_end ();
83       return NULL;
84     }
85 
86   _newlib_flockfile_start (fp);
87 
88   fp->_file = f;
89   fp->_flags = flags;
90   fp->_cookie = (void *) fp;
91   fp->_read = __sread;
92   fp->_write = __swrite64;
93   fp->_seek = __sseek;
94   fp->_seek64 = __sseek64;
95   fp->_close = __sclose;
96 
97   if (fp->_flags & __SAPP)
98     fseeko64 (fp, 0, SEEK_END);
99 
100 #ifdef __SCLE
101   if (__stextmode (fp->_file))
102     fp->_flags |= __SCLE;
103 #endif
104 
105   fp->_flags |= __SL64;
106 
107   _newlib_flockfile_end (fp);
108   return fp;
109 }
110 
111 #endif /* __LARGE64_FILES */
112