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 #if defined(LIBC_SCCS) && !defined(lint)
53 static char sccsid[] = "%W% (Berkeley) %G%";
54 #endif /* LIBC_SCCS and not lint */
55 
56 #define _DEFAULT_SOURCE
57 #include <stdio.h>
58 #include <errno.h>
59 #include <fcntl.h>
60 #include <sys/lock.h>
61 #include "../stdio/local.h"
62 
63 #ifdef __LARGE64_FILES
64 
65 FILE *
fopen64(const char * file,const char * mode)66 fopen64 (
67 	const char *file,
68 	const char *mode)
69 {
70   register FILE *fp;
71   register int f;
72   int flags, oflags;
73 
74   if ((flags = __sflags (mode, &oflags)) == 0)
75     return NULL;
76   if ((fp = __sfp ()) == NULL)
77     return NULL;
78 
79   if ((f = open64 (file, oflags, 0666)) < 0)
80     {
81       _newlib_sfp_lock_start ();
82       fp->_flags = 0;		/* release */
83 #ifndef __SINGLE_THREAD__
84       __lock_close_recursive (fp->_lock);
85 #endif
86       _newlib_sfp_lock_end ();
87       return NULL;
88     }
89 
90   _newlib_flockfile_start (fp);
91 
92   fp->_file = f;
93   fp->_flags = flags;
94   fp->_cookie = (void *) fp;
95   fp->_read = __sread;
96   fp->_write = __swrite64;
97   fp->_seek = __sseek;
98   fp->_seek64 = __sseek64;
99   fp->_close = __sclose;
100 
101   if (fp->_flags & __SAPP)
102     fseeko64 (fp, 0, SEEK_END);
103 
104 #ifdef __SCLE
105   if (__stextmode (fp->_file))
106     fp->_flags |= __SCLE;
107 #endif
108 
109   fp->_flags |= __SL64;
110 
111   _newlib_flockfile_end (fp);
112   return fp;
113 }
114 
115 #endif /* __LARGE64_FILES */
116