stdio.h revision 156d5a8ae93c6515919dd6706481991c7c0dc600
1/*	$OpenBSD: stdio.h,v 1.35 2006/01/13 18:10:09 miod Exp $	*/
2/*	$NetBSD: stdio.h,v 1.18 1996/04/25 18:29:21 jtc Exp $	*/
3
4/*-
5 * Copyright (c) 1990 The Regents of the University of California.
6 * All rights reserved.
7 *
8 * This code is derived from software contributed to Berkeley by
9 * Chris Torek.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 *    notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 *    notice, this list of conditions and the following disclaimer in the
18 *    documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the University nor the names of its contributors
20 *    may be used to endorse or promote products derived from this software
21 *    without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 *
35 *	@(#)stdio.h	5.17 (Berkeley) 6/3/91
36 */
37
38#ifndef	_STDIO_H_
39#define	_STDIO_H_
40
41#include <sys/cdefs.h>
42#include <sys/types.h>
43
44#include <stdarg.h>
45#include <stddef.h>
46
47#define __need_NULL
48#include <stddef.h>
49
50#include <bits/seek_constants.h>
51
52#if __ANDROID_API__ < __ANDROID_API_N__
53#include <bits/struct_file.h>
54#endif
55
56__BEGIN_DECLS
57
58#if defined(__clang__)
59#pragma clang diagnostic push
60#pragma clang diagnostic ignored "-Wnullability-completeness"
61#endif
62
63typedef off_t fpos_t;
64typedef off64_t fpos64_t;
65
66struct __sFILE;
67typedef struct __sFILE FILE;
68
69#if __ANDROID_API__ >= __ANDROID_API_M__
70extern FILE* stdin __INTRODUCED_IN(23);
71extern FILE* stdout __INTRODUCED_IN(23);
72extern FILE* stderr __INTRODUCED_IN(23);
73
74/* C99 and earlier plus current C++ standards say these must be macros. */
75#define stdin stdin
76#define stdout stdout
77#define stderr stderr
78#else
79/* Before M the actual symbols for stdin and friends had different names. */
80extern FILE __sF[] __REMOVED_IN(23);
81
82#define stdin (&__sF[0])
83#define stdout (&__sF[1])
84#define stderr (&__sF[2])
85#endif
86
87/*
88 * The following three definitions are for ANSI C, which took them
89 * from System V, which brilliantly took internal interface macros and
90 * made them official arguments to setvbuf(), without renaming them.
91 * Hence, these ugly _IOxxx names are *supposed* to appear in user code.
92 *
93 * Although numbered as their counterparts above, the implementation
94 * does not rely on this.
95 */
96#define	_IOFBF	0		/* setvbuf should set fully buffered */
97#define	_IOLBF	1		/* setvbuf should set line buffered */
98#define	_IONBF	2		/* setvbuf should set unbuffered */
99
100#define	BUFSIZ	1024		/* size of buffer used by setbuf */
101#define	EOF	(-1)
102
103/*
104 * FOPEN_MAX is a minimum maximum, and is the number of streams that
105 * stdio can provide without attempting to allocate further resources
106 * (which could fail).  Do not use this for anything.
107 */
108
109#define	FOPEN_MAX	20	/* must be <= OPEN_MAX <sys/syslimits.h> */
110#define	FILENAME_MAX	1024	/* must be <= PATH_MAX <sys/syslimits.h> */
111
112#define	L_tmpnam	1024	/* XXX must be == PATH_MAX */
113#define	TMP_MAX		308915776
114
115/*
116 * Functions defined in ANSI C standard.
117 */
118void	 clearerr(FILE *);
119int	 fclose(FILE *);
120int	 feof(FILE *);
121int	 ferror(FILE *);
122int	 fflush(FILE *);
123int	 fgetc(FILE *);
124char	*fgets(char * __restrict, int, FILE * __restrict) __overloadable
125  __RENAME_CLANG(fgets);
126int	 fprintf(FILE * __restrict , const char * __restrict _Nonnull, ...) __printflike(2, 3);
127int	 fputc(int, FILE *);
128int	 fputs(const char * __restrict, FILE * __restrict);
129size_t	 fread(void * __restrict, size_t, size_t, FILE * __restrict)
130      __overloadable __RENAME_CLANG(fread);
131int	 fscanf(FILE * __restrict, const char * __restrict _Nonnull, ...) __scanflike(2, 3);
132size_t	 fwrite(const void * __restrict, size_t, size_t, FILE * __restrict)
133    __overloadable __RENAME_CLANG(fwrite);
134int	 getc(FILE *);
135int	 getchar(void);
136ssize_t getdelim(char** __restrict, size_t* __restrict, int, FILE* __restrict) __INTRODUCED_IN(18);
137ssize_t getline(char** __restrict, size_t* __restrict, FILE* __restrict) __INTRODUCED_IN(18);
138
139void	 perror(const char *);
140int	 printf(const char * __restrict _Nonnull, ...) __printflike(1, 2);
141int	 putc(int, FILE *);
142int	 putchar(int);
143int	 puts(const char *);
144int	 remove(const char *);
145void	 rewind(FILE *);
146int	 scanf(const char * __restrict _Nonnull, ...) __scanflike(1, 2);
147void	 setbuf(FILE * __restrict, char * __restrict);
148int	 setvbuf(FILE * __restrict, char * __restrict, int, size_t);
149int	 sscanf(const char * __restrict, const char * __restrict _Nonnull, ...) __scanflike(2, 3);
150int	 ungetc(int, FILE *);
151int	 vfprintf(FILE * __restrict, const char * __restrict _Nonnull, __va_list) __printflike(2, 0);
152int	 vprintf(const char * __restrict _Nonnull, __va_list) __printflike(1, 0);
153
154int dprintf(int, const char* __restrict _Nonnull, ...) __printflike(2, 3) __INTRODUCED_IN(21);
155int vdprintf(int, const char* __restrict _Nonnull, __va_list) __printflike(2, 0) __INTRODUCED_IN(21);
156
157#if (defined(__STDC_VERSION__) && __STDC_VERSION__ < 201112L) || \
158    (defined(__cplusplus) && __cplusplus <= 201103L)
159char* gets(char*) __attribute__((deprecated("gets is unsafe, use fgets instead")));
160#endif
161int sprintf(char* __restrict, const char* __restrict _Nonnull, ...)
162    __printflike(2, 3) __warnattr_strict("sprintf is often misused; please use snprintf")
163    __overloadable __RENAME_CLANG(sprintf);
164int vsprintf(char* __restrict, const char* __restrict _Nonnull, __va_list)
165    __overloadable __printflike(2, 0) __RENAME_CLANG(vsprintf)
166    __warnattr_strict("vsprintf is often misused; please use vsnprintf");
167char* tmpnam(char*)
168    __warnattr("tempnam is unsafe, use mkstemp or tmpfile instead");
169#define P_tmpdir "/tmp/" /* deprecated */
170char* tempnam(const char*, const char*)
171    __warnattr("tempnam is unsafe, use mkstemp or tmpfile instead");
172
173int rename(const char*, const char*);
174int renameat(int, const char*, int, const char*);
175
176int fseek(FILE*, long, int);
177long ftell(FILE*);
178
179#if defined(__USE_FILE_OFFSET64)
180int fgetpos(FILE*, fpos_t*) __RENAME(fgetpos64);
181int fsetpos(FILE*, const fpos_t*) __RENAME(fsetpos64);
182int fseeko(FILE*, off_t, int) __RENAME(fseeko64);
183off_t ftello(FILE*) __RENAME(ftello64);
184#  if defined(__USE_BSD)
185FILE* funopen(const void*,
186              int (*)(void*, char*, int),
187              int (*)(void*, const char*, int),
188              fpos_t (*)(void*, fpos_t, int),
189              int (*)(void*)) __RENAME(funopen64);
190#  endif
191#else
192int fgetpos(FILE*, fpos_t*);
193int fsetpos(FILE*, const fpos_t*);
194int fseeko(FILE*, off_t, int);
195off_t ftello(FILE*);
196#  if defined(__USE_BSD)
197FILE* funopen(const void*,
198              int (*)(void*, char*, int),
199              int (*)(void*, const char*, int),
200              fpos_t (*)(void*, fpos_t, int),
201              int (*)(void*));
202#  endif
203#endif
204int fgetpos64(FILE*, fpos64_t*) __INTRODUCED_IN(24);
205int fsetpos64(FILE*, const fpos64_t*) __INTRODUCED_IN(24);
206int fseeko64(FILE*, off64_t, int) __INTRODUCED_IN(24);
207off64_t ftello64(FILE*) __INTRODUCED_IN(24);
208#if defined(__USE_BSD)
209FILE* funopen64(const void*, int (*)(void*, char*, int), int (*)(void*, const char*, int),
210                fpos64_t (*)(void*, fpos64_t, int), int (*)(void*)) __INTRODUCED_IN(24);
211#endif
212
213FILE* fopen(const char* __restrict, const char* __restrict);
214FILE* fopen64(const char* __restrict, const char* __restrict) __INTRODUCED_IN(24);
215FILE* freopen(const char* __restrict, const char* __restrict, FILE* __restrict);
216FILE* freopen64(const char* __restrict, const char* __restrict, FILE* __restrict)
217  __INTRODUCED_IN(24);
218FILE* tmpfile(void);
219FILE* tmpfile64(void) __INTRODUCED_IN(24);
220
221int snprintf(char* __restrict, size_t, const char* __restrict _Nonnull, ...)
222    __printflike(3, 4) __overloadable __RENAME_CLANG(snprintf);
223int vfscanf(FILE* __restrict, const char* __restrict _Nonnull, __va_list) __scanflike(2, 0);
224int vscanf(const char* _Nonnull , __va_list) __scanflike(1, 0);
225int vsnprintf(char* __restrict, size_t, const char* __restrict _Nonnull, __va_list)
226    __printflike(3, 0) __overloadable __RENAME_CLANG(vsnprintf);
227int vsscanf(const char* __restrict _Nonnull, const char* __restrict _Nonnull, __va_list) __scanflike(2, 0);
228
229#define L_ctermid 1024 /* size for ctermid() */
230char* ctermid(char*) __INTRODUCED_IN_FUTURE;
231
232FILE* fdopen(int, const char*);
233int fileno(FILE*);
234int pclose(FILE*);
235FILE* popen(const char*, const char*);
236void flockfile(FILE*);
237int ftrylockfile(FILE*);
238void funlockfile(FILE*);
239int getc_unlocked(FILE*);
240int getchar_unlocked(void);
241int putc_unlocked(int, FILE*);
242int putchar_unlocked(int);
243
244FILE* fmemopen(void*, size_t, const char*) __INTRODUCED_IN(23);
245FILE* open_memstream(char**, size_t*) __INTRODUCED_IN(23);
246
247#if defined(__USE_BSD) || defined(__BIONIC__) /* Historically bionic exposed these. */
248int  asprintf(char** __restrict, const char* __restrict _Nonnull, ...) __printflike(2, 3);
249char* fgetln(FILE* __restrict, size_t* __restrict);
250int fpurge(FILE*);
251void setbuffer(FILE*, char*, int);
252int setlinebuf(FILE*);
253int vasprintf(char** __restrict, const char* __restrict _Nonnull, __va_list) __printflike(2, 0);
254void clearerr_unlocked(FILE*) __INTRODUCED_IN(23);
255int feof_unlocked(FILE*) __INTRODUCED_IN(23);
256int ferror_unlocked(FILE*) __INTRODUCED_IN(23);
257int fileno_unlocked(FILE*) __INTRODUCED_IN(24);
258#define fropen(cookie, fn) funopen(cookie, fn, 0, 0, 0)
259#define fwopen(cookie, fn) funopen(cookie, 0, fn, 0, 0)
260#endif /* __USE_BSD */
261
262char* __fgets_chk(char*, int, FILE*, size_t) __INTRODUCED_IN(17);
263size_t __fread_chk(void* __restrict, size_t, size_t, FILE* __restrict, size_t)
264    __INTRODUCED_IN(24);
265size_t __fwrite_chk(const void* __restrict, size_t, size_t, FILE* __restrict, size_t)
266    __INTRODUCED_IN(24);
267
268#if defined(__BIONIC_FORTIFY) && !defined(__BIONIC_NO_STDIO_FORTIFY)
269
270#if __ANDROID_API__ >= __ANDROID_API_J_MR1__
271__BIONIC_FORTIFY_INLINE __printflike(3, 0)
272int vsnprintf(char *const __pass_object_size dest, size_t size,
273              const char *_Nonnull format, __va_list ap) __overloadable {
274    return __builtin___vsnprintf_chk(dest, size, 0, __bos(dest), format, ap);
275}
276
277__BIONIC_FORTIFY_INLINE __printflike(2, 0)
278int vsprintf(char *const __pass_object_size dest, const char *_Nonnull format,
279             __va_list ap) __overloadable {
280    return __builtin___vsprintf_chk(dest, 0, __bos(dest), format, ap);
281}
282#endif /* __ANDROID_API__ >= __ANDROID_API_J_MR1__ */
283
284#if defined(__clang__)
285#if __ANDROID_API__ >= __ANDROID_API_J_MR1__
286/*
287 * Simple case: `format` can't have format specifiers, so we can just compare
288 * its length to the length of `dest`
289 */
290__BIONIC_ERROR_FUNCTION_VISIBILITY
291int snprintf(char *__restrict dest, size_t size, const char *__restrict format)
292    __overloadable
293    __enable_if(__bos(dest) != __BIONIC_FORTIFY_UNKNOWN_SIZE &&
294                __bos(dest) < __builtin_strlen(format),
295                "format string will always overflow destination buffer")
296    __errorattr("format string will always overflow destination buffer");
297
298__BIONIC_FORTIFY_INLINE
299__printflike(3, 4)
300int snprintf(char *__restrict const __pass_object_size dest,
301             size_t size, const char *__restrict format, ...) __overloadable {
302    va_list va;
303    va_start(va, format);
304    int result = __builtin___vsnprintf_chk(dest, size, 0, __bos(dest), format, va);
305    va_end(va);
306    return result;
307}
308
309__BIONIC_ERROR_FUNCTION_VISIBILITY
310int sprintf(char *__restrict dest, const char *__restrict format) __overloadable
311    __enable_if(__bos(dest) != __BIONIC_FORTIFY_UNKNOWN_SIZE &&
312                __bos(dest) < __builtin_strlen(format),
313                "format string will always overflow destination buffer")
314    __errorattr("format string will always overflow destination buffer");
315
316__BIONIC_FORTIFY_INLINE
317__printflike(2, 3)
318int sprintf(char *__restrict const __pass_object_size dest,
319        const char *__restrict format, ...) __overloadable {
320    va_list va;
321    va_start(va, format);
322    int result = __builtin___vsprintf_chk(dest, 0, __bos(dest), format, va);
323    va_end(va);
324    return result;
325}
326#endif /* __ANDROID_API__ >= __ANDROID_API_J_MR1__ */
327
328#if __ANDROID_API__ >= __ANDROID_API_N__
329__BIONIC_FORTIFY_INLINE
330size_t fread(void *__restrict buf, size_t size, size_t count,
331             FILE *__restrict stream) __overloadable
332        __enable_if(__unsafe_check_mul_overflow(size, count), "size * count overflows")
333        __errorattr("size * count overflows");
334
335__BIONIC_FORTIFY_INLINE
336size_t fread(void *__restrict buf, size_t size, size_t count,
337             FILE *__restrict stream) __overloadable
338    __enable_if(!__unsafe_check_mul_overflow(size, count), "no overflow")
339    __enable_if(__bos(buf) != __BIONIC_FORTIFY_UNKNOWN_SIZE &&
340                size * count > __bos(buf), "size * count is too large")
341    __errorattr("size * count is too large");
342
343__BIONIC_FORTIFY_INLINE
344size_t fread(void *__restrict const __pass_object_size0 buf, size_t size,
345             size_t count, FILE *__restrict stream) __overloadable {
346    size_t bos = __bos0(buf);
347
348    if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
349        return __call_bypassing_fortify(fread)(buf, size, count, stream);
350    }
351
352    return __fread_chk(buf, size, count, stream, bos);
353}
354
355size_t fwrite(const void * __restrict buf, size_t size,
356              size_t count, FILE * __restrict stream) __overloadable
357    __enable_if(__unsafe_check_mul_overflow(size, count),
358                "size * count overflows")
359    __errorattr("size * count overflows");
360
361size_t fwrite(const void * __restrict buf, size_t size,
362              size_t count, FILE * __restrict stream) __overloadable
363    __enable_if(!__unsafe_check_mul_overflow(size, count), "no overflow")
364    __enable_if(__bos(buf) != __BIONIC_FORTIFY_UNKNOWN_SIZE &&
365                size * count > __bos(buf), "size * count is too large")
366    __errorattr("size * count is too large");
367
368__BIONIC_FORTIFY_INLINE
369size_t fwrite(const void * __restrict const __pass_object_size0 buf,
370              size_t size, size_t count, FILE * __restrict stream)
371        __overloadable {
372    size_t bos = __bos0(buf);
373
374    if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
375        return __call_bypassing_fortify(fwrite)(buf, size, count, stream);
376    }
377
378    return __fwrite_chk(buf, size, count, stream, bos);
379}
380#endif /* __ANDROID_API__ >= __ANDROID_API_N__ */
381
382__BIONIC_ERROR_FUNCTION_VISIBILITY
383char *fgets(char* __restrict dest, int size, FILE* stream) __overloadable
384    __enable_if(size < 0, "size is negative")
385    __errorattr("size is negative");
386
387__BIONIC_ERROR_FUNCTION_VISIBILITY
388char *fgets(char* dest, int size, FILE* stream) __overloadable
389    __enable_if(size >= 0 && size > __bos(dest),
390                "size is larger than the destination buffer")
391    __errorattr("size is larger than the destination buffer");
392
393__BIONIC_FORTIFY_INLINE
394char *fgets(char* __restrict const __pass_object_size dest,
395        int size, FILE* stream) __overloadable {
396    size_t bos = __bos(dest);
397
398    if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
399        return __call_bypassing_fortify(fgets)(dest, size, stream);
400    }
401
402    return __fgets_chk(dest, size, stream, bos);
403}
404
405#else /* defined(__clang__) */
406
407size_t __fread_real(void * __restrict, size_t, size_t, FILE * __restrict) __RENAME(fread);
408__errordecl(__fread_too_big_error, "fread called with size * count bigger than buffer");
409__errordecl(__fread_overflow, "fread called with overflowing size * count");
410
411char* __fgets_real(char*, int, FILE*) __RENAME(fgets);
412__errordecl(__fgets_too_big_error, "fgets called with size bigger than buffer");
413__errordecl(__fgets_too_small_error, "fgets called with size less than zero");
414
415size_t __fwrite_real(const void * __restrict, size_t, size_t, FILE * __restrict) __RENAME(fwrite);
416__errordecl(__fwrite_too_big_error, "fwrite called with size * count bigger than buffer");
417__errordecl(__fwrite_overflow, "fwrite called with overflowing size * count");
418
419
420#if __ANDROID_API__ >= __ANDROID_API_J_MR1__
421__BIONIC_FORTIFY_INLINE __printflike(3, 4)
422int snprintf(char *__restrict dest, size_t size, const char* _Nonnull format, ...)
423{
424    return __builtin___snprintf_chk(dest, size, 0, __bos(dest), format,
425                                    __builtin_va_arg_pack());
426}
427
428__BIONIC_FORTIFY_INLINE __printflike(2, 3)
429int sprintf(char *__restrict dest, const char* _Nonnull format, ...) {
430    return __builtin___sprintf_chk(dest, 0, __bos(dest), format,
431                                   __builtin_va_arg_pack());
432}
433#endif /* __ANDROID_API__ >= __ANDROID_API_J_MR1__ */
434
435#if __ANDROID_API__ >= __ANDROID_API_N__
436__BIONIC_FORTIFY_INLINE
437size_t fread(void *__restrict buf, size_t size, size_t count, FILE * __restrict stream) {
438    size_t bos = __bos0(buf);
439
440    if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
441        return __fread_real(buf, size, count, stream);
442    }
443
444    if (__builtin_constant_p(size) && __builtin_constant_p(count)) {
445        size_t total;
446        if (__size_mul_overflow(size, count, &total)) {
447            __fread_overflow();
448        }
449
450        if (total > bos) {
451            __fread_too_big_error();
452        }
453
454        return __fread_real(buf, size, count, stream);
455    }
456
457    return __fread_chk(buf, size, count, stream, bos);
458}
459
460__BIONIC_FORTIFY_INLINE
461size_t fwrite(const void * __restrict buf, size_t size, size_t count, FILE * __restrict stream) {
462    size_t bos = __bos0(buf);
463
464    if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
465        return __fwrite_real(buf, size, count, stream);
466    }
467
468    if (__builtin_constant_p(size) && __builtin_constant_p(count)) {
469        size_t total;
470        if (__size_mul_overflow(size, count, &total)) {
471            __fwrite_overflow();
472        }
473
474        if (total > bos) {
475            __fwrite_too_big_error();
476        }
477
478        return __fwrite_real(buf, size, count, stream);
479    }
480
481    return __fwrite_chk(buf, size, count, stream, bos);
482}
483#endif /* __ANDROID_API__ >= __ANDROID_API_N__ */
484
485__BIONIC_FORTIFY_INLINE
486char *fgets(char* dest, int size, FILE* stream) {
487    size_t bos = __bos(dest);
488
489    // Compiler can prove, at compile time, that the passed in size
490    // is always negative. Force a compiler error.
491    if (__builtin_constant_p(size) && (size < 0)) {
492        __fgets_too_small_error();
493    }
494
495    // Compiler doesn't know destination size. Don't call __fgets_chk
496    if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
497        return __fgets_real(dest, size, stream);
498    }
499
500    // Compiler can prove, at compile time, that the passed in size
501    // is always <= the actual object size. Don't call __fgets_chk
502    if (__builtin_constant_p(size) && (size <= (int) bos)) {
503        return __fgets_real(dest, size, stream);
504    }
505
506    // Compiler can prove, at compile time, that the passed in size
507    // is always > the actual object size. Force a compiler error.
508    if (__builtin_constant_p(size) && (size > (int) bos)) {
509        __fgets_too_big_error();
510    }
511
512    return __fgets_chk(dest, size, stream, bos);
513}
514
515#endif /* defined(__clang__) */
516#endif /* defined(__BIONIC_FORTIFY) */
517
518#if defined(__clang__)
519#pragma clang diagnostic pop
520#endif
521
522__END_DECLS
523
524#endif /* _STDIO_H_ */
525