blob: 1bf578c1dfdb5a4fb8390538f9f9e026383d0b92 [file] [log] [blame]
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001/* $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>
Elliott Hughes3975cec2012-11-29 17:25:23 -080042#include <sys/types.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080043
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080044#include <stdarg.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080045#include <stddef.h>
46
Elliott Hughesfd936ae2016-08-12 10:16:34 -070047#include <bits/seek_constants.h>
48
Elliott Hughes95c6cd72019-12-20 13:26:14 -080049#if __ANDROID_API__ < 24
Dan Albert3037ea42016-10-06 15:46:45 -070050#include <bits/struct_file.h>
51#endif
52
Dan Albert658727e2014-10-07 11:10:36 -070053__BEGIN_DECLS
54
Elliott Hughes9677fab2016-01-25 15:50:59 -080055typedef off_t fpos_t;
56typedef off64_t fpos64_t;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080057
Elliott Hughesf0141df2015-10-12 12:44:23 -070058struct __sFILE;
59typedef struct __sFILE FILE;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080060
Elliott Hughes95c6cd72019-12-20 13:26:14 -080061#if __ANDROID_API__ >= 23
Josh Gao14adff12016-04-29 12:00:55 -070062extern FILE* stdin __INTRODUCED_IN(23);
63extern FILE* stdout __INTRODUCED_IN(23);
64extern FILE* stderr __INTRODUCED_IN(23);
Dan Albert32c79c22016-07-15 11:32:23 -070065
Elliott Hughes168667c2014-11-14 14:42:59 -080066/* C99 and earlier plus current C++ standards say these must be macros. */
67#define stdin stdin
68#define stdout stdout
69#define stderr stderr
Dan Albert32c79c22016-07-15 11:32:23 -070070#else
71/* Before M the actual symbols for stdin and friends had different names. */
Dan Albert3037ea42016-10-06 15:46:45 -070072extern FILE __sF[] __REMOVED_IN(23);
Dan Albert32c79c22016-07-15 11:32:23 -070073
Dan Albert3037ea42016-10-06 15:46:45 -070074#define stdin (&__sF[0])
75#define stdout (&__sF[1])
76#define stderr (&__sF[2])
Dan Albert32c79c22016-07-15 11:32:23 -070077#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080078
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080079/*
80 * The following three definitions are for ANSI C, which took them
81 * from System V, which brilliantly took internal interface macros and
82 * made them official arguments to setvbuf(), without renaming them.
83 * Hence, these ugly _IOxxx names are *supposed* to appear in user code.
84 *
85 * Although numbered as their counterparts above, the implementation
86 * does not rely on this.
87 */
88#define _IOFBF 0 /* setvbuf should set fully buffered */
89#define _IOLBF 1 /* setvbuf should set line buffered */
90#define _IONBF 2 /* setvbuf should set unbuffered */
91
92#define BUFSIZ 1024 /* size of buffer used by setbuf */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080093#define EOF (-1)
94
95/*
Elliott Hughesd04c1832013-05-14 16:08:43 -070096 * FOPEN_MAX is a minimum maximum, and is the number of streams that
97 * stdio can provide without attempting to allocate further resources
98 * (which could fail). Do not use this for anything.
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080099 */
Elliott Hughesb15feb72017-07-31 17:20:18 -0700100#define FOPEN_MAX 20
101#define FILENAME_MAX 4096
Elliott Hughesd04c1832013-05-14 16:08:43 -0700102
Elliott Hughesb15feb72017-07-31 17:20:18 -0700103#define L_tmpnam 4096
104#define TMP_MAX 308915776
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800105
Elliott Hughesff26a162017-08-17 22:34:21 +0000106void clearerr(FILE* __fp);
107int fclose(FILE* __fp);
108int feof(FILE* __fp);
109int ferror(FILE* __fp);
110int fflush(FILE* __fp);
111int fgetc(FILE* __fp);
George Burgess IV90242352018-02-06 12:51:31 -0800112char* fgets(char* __buf, int __size, FILE* __fp);
Elliott Hughesff26a162017-08-17 22:34:21 +0000113int fprintf(FILE* __fp , const char* __fmt, ...) __printflike(2, 3);
114int fputc(int __ch, FILE* __fp);
115int fputs(const char* __s, FILE* __fp);
George Burgess IV90242352018-02-06 12:51:31 -0800116size_t fread(void* __buf, size_t __size, size_t __count, FILE* __fp);
Elliott Hughesff26a162017-08-17 22:34:21 +0000117int fscanf(FILE* __fp, const char* __fmt, ...) __scanflike(2, 3);
George Burgess IV90242352018-02-06 12:51:31 -0800118size_t fwrite(const void* __buf, size_t __size, size_t __count, FILE* __fp);
Elliott Hughesff26a162017-08-17 22:34:21 +0000119int getc(FILE* __fp);
120int getchar(void);
121ssize_t getdelim(char** __line_ptr, size_t* __line_length_ptr, int __delimiter, FILE* __fp) __INTRODUCED_IN(18);
122ssize_t getline(char** __line_ptr, size_t* __line_length_ptr, FILE* __fp) __INTRODUCED_IN(18);
Elliott Hughesc13fb752013-12-17 20:43:30 -0800123
Elliott Hughesff26a162017-08-17 22:34:21 +0000124void perror(const char* __msg);
125int printf(const char* __fmt, ...) __printflike(1, 2);
126int putc(int __ch, FILE* __fp);
127int putchar(int __ch);
128int puts(const char* __s);
129int remove(const char* __path);
130void rewind(FILE* __fp);
131int scanf(const char* __fmt, ...) __scanflike(1, 2);
132void setbuf(FILE* __fp, char* __buf);
133int setvbuf(FILE* __fp, char* __buf, int __mode, size_t __size);
134int sscanf(const char* __s, const char* __fmt, ...) __scanflike(2, 3);
135int ungetc(int __ch, FILE* __fp);
136int vfprintf(FILE* __fp, const char* __fmt, va_list __args) __printflike(2, 0);
137int vprintf(const char* __fp, va_list __args) __printflike(1, 0);
Elliott Hughesd04c1832013-05-14 16:08:43 -0700138
Dan Albert5f7135e2017-07-26 14:09:45 -0700139#if __ANDROID_API__ >= 21
Elliott Hughesff26a162017-08-17 22:34:21 +0000140int dprintf(int __fd, const char* __fmt, ...) __printflike(2, 3) __INTRODUCED_IN(21);
141int vdprintf(int __fd, const char* __fmt, va_list __args) __printflike(2, 0) __INTRODUCED_IN(21);
Dan Albert5f7135e2017-07-26 14:09:45 -0700142#else
143/*
144 * Old versions of Android called these fdprintf and vfdprintf out of fears that the glibc names
145 * would collide with user debug printfs.
146 *
147 * Allow users to just use dprintf and vfdprintf on any version by renaming those calls to their
148 * legacy equivalents if needed.
149 */
Elliott Hughesff26a162017-08-17 22:34:21 +0000150int dprintf(int __fd, const char* __fmt, ...) __RENAME(fdprintf) __printflike(2, 3);
151int vdprintf(int __fd, const char* __fmt, va_list __args) __RENAME(vfdprintf) __printflike(2, 0);
Dan Albert5f7135e2017-07-26 14:09:45 -0700152#endif
Elliott Hughesfcac8ff2014-05-22 01:24:30 -0700153
Elliott Hughesf6495c72016-07-25 09:20:57 -0700154#if (defined(__STDC_VERSION__) && __STDC_VERSION__ < 201112L) || \
155 (defined(__cplusplus) && __cplusplus <= 201103L)
Elliott Hughesff26a162017-08-17 22:34:21 +0000156char* gets(char* __buf) __attribute__((deprecated("gets is unsafe, use fgets instead")));
Dan Albert96350462014-06-17 23:31:21 +0000157#endif
Elliott Hughesff26a162017-08-17 22:34:21 +0000158int sprintf(char* __s, const char* __fmt, ...)
George Burgess IV90242352018-02-06 12:51:31 -0800159 __printflike(2, 3) __warnattr_strict("sprintf is often misused; please use snprintf");
Elliott Hughesff26a162017-08-17 22:34:21 +0000160int vsprintf(char* __s, const char* __fmt, va_list __args)
George Burgess IV90242352018-02-06 12:51:31 -0800161 __printflike(2, 0) __warnattr_strict("vsprintf is often misused; please use vsnprintf");
Elliott Hughesff26a162017-08-17 22:34:21 +0000162char* tmpnam(char* __s)
George Burgess IV7cc779f2017-02-09 00:00:31 -0800163 __warnattr("tempnam is unsafe, use mkstemp or tmpfile instead");
Elliott Hughes3ba55f82016-06-08 18:11:23 -0700164#define P_tmpdir "/tmp/" /* deprecated */
Elliott Hughesff26a162017-08-17 22:34:21 +0000165char* tempnam(const char* __dir, const char* __prefix)
George Burgess IV7cc779f2017-02-09 00:00:31 -0800166 __warnattr("tempnam is unsafe, use mkstemp or tmpfile instead");
Elliott Hughesd04c1832013-05-14 16:08:43 -0700167
Elliott Hughes05b675e2019-04-17 13:01:06 -0700168/**
169 * [rename(2)](http://man7.org/linux/man-pages/man2/rename.2.html) changes
170 * the name or location of a file.
171 *
172 * Returns 0 on success, and returns -1 and sets `errno` on failure.
173 */
Elliott Hughesff26a162017-08-17 22:34:21 +0000174int rename(const char* __old_path, const char* __new_path);
Elliott Hughes05b675e2019-04-17 13:01:06 -0700175
176/**
177 * [renameat(2)](http://man7.org/linux/man-pages/man2/renameat.2.html) changes
178 * the name or location of a file, interpreting relative paths using an fd.
179 *
180 * Returns 0 on success, and returns -1 and sets `errno` on failure.
181 */
Elliott Hughesff26a162017-08-17 22:34:21 +0000182int renameat(int __old_dir_fd, const char* __old_path, int __new_dir_fd, const char* __new_path);
Elliott Hughesd04c1832013-05-14 16:08:43 -0700183
Elliott Hughes05b675e2019-04-17 13:01:06 -0700184#if defined(__USE_GNU)
185
186/**
187 * Flag for [renameat2(2)](http://man7.org/linux/man-pages/man2/renameat2.2.html)
188 * to fail if the new path already exists.
189 */
190#define RENAME_NOREPLACE (1<<0)
191
192/**
193 * Flag for [renameat2(2)](http://man7.org/linux/man-pages/man2/renameat2.2.html)
194 * to atomically exchange the two paths.
195 */
196#define RENAME_EXCHANGE (1<<1)
197
198/**
199 * Flag for [renameat2(2)](http://man7.org/linux/man-pages/man2/renameat2.2.html)
200 * to create a union/overlay filesystem object.
201 */
202#define RENAME_WHITEOUT (1<<2)
203
204/**
205 * [renameat2(2)](http://man7.org/linux/man-pages/man2/renameat2.2.html) changes
206 * the name or location of a file, interpreting relative paths using an fd,
207 * with optional `RENAME_` flags.
208 *
209 * Returns 0 on success, and returns -1 and sets `errno` on failure.
210 */
211int renameat2(int __old_dir_fd, const char* __old_path, int __new_dir_fd, const char* __new_path, unsigned __flags) __INTRODUCED_IN(30);
212
213#endif
214
Elliott Hughesff26a162017-08-17 22:34:21 +0000215int fseek(FILE* __fp, long __offset, int __whence);
216long ftell(FILE* __fp);
Elliott Hughes03e65eb2016-01-26 14:13:04 -0800217
Elliott Hughesa3481742017-11-28 14:47:17 -0800218/* See https://android.googlesource.com/platform/bionic/+/master/docs/32-bit-abi.md */
Elliott Hughes00fedf52017-07-05 15:23:50 -0700219#if defined(__USE_FILE_OFFSET64)
Elliott Hughesff26a162017-08-17 22:34:21 +0000220int fgetpos(FILE* __fp, fpos_t* __pos) __RENAME(fgetpos64) __INTRODUCED_IN(24);
221int fsetpos(FILE* __fp, const fpos_t* __pos) __RENAME(fsetpos64) __INTRODUCED_IN(24);
222int fseeko(FILE* __fp, off_t __offset, int __whence) __RENAME(fseeko64) __INTRODUCED_IN(24);
223off_t ftello(FILE* __fp) __RENAME(ftello64) __INTRODUCED_IN(24);
Elliott Hughes03e65eb2016-01-26 14:13:04 -0800224# if defined(__USE_BSD)
Elliott Hughesff26a162017-08-17 22:34:21 +0000225FILE* funopen(const void* __cookie,
226 int (*__read_fn)(void*, char*, int),
227 int (*__write_fn)(void*, const char*, int),
228 fpos_t (*__seek_fn)(void*, fpos_t, int),
229 int (*__close_fn)(void*)) __RENAME(funopen64) __INTRODUCED_IN(24);
Elliott Hughes03e65eb2016-01-26 14:13:04 -0800230# endif
Elliott Hughes68dc20d2015-02-06 22:28:49 -0800231#else
Elliott Hughesff26a162017-08-17 22:34:21 +0000232int fgetpos(FILE* __fp, fpos_t* __pos);
233int fsetpos(FILE* __fp, const fpos_t* __pos);
234int fseeko(FILE* __fp, off_t __offset, int __whence);
235off_t ftello(FILE* __fp);
Elliott Hughes03e65eb2016-01-26 14:13:04 -0800236# if defined(__USE_BSD)
Elliott Hughesff26a162017-08-17 22:34:21 +0000237FILE* funopen(const void* __cookie,
238 int (*__read_fn)(void*, char*, int),
239 int (*__write_fn)(void*, const char*, int),
240 fpos_t (*__seek_fn)(void*, fpos_t, int),
241 int (*__close_fn)(void*));
Elliott Hughes03e65eb2016-01-26 14:13:04 -0800242# endif
Elliott Hughes68dc20d2015-02-06 22:28:49 -0800243#endif
Elliott Hughesff26a162017-08-17 22:34:21 +0000244int fgetpos64(FILE* __fp, fpos64_t* __pos) __INTRODUCED_IN(24);
245int fsetpos64(FILE* __fp, const fpos64_t* __pos) __INTRODUCED_IN(24);
246int fseeko64(FILE* __fp, off64_t __offset, int __whence) __INTRODUCED_IN(24);
247off64_t ftello64(FILE* __fp) __INTRODUCED_IN(24);
Elliott Hughes03e65eb2016-01-26 14:13:04 -0800248#if defined(__USE_BSD)
Elliott Hughesff26a162017-08-17 22:34:21 +0000249FILE* funopen64(const void* __cookie,
250 int (*__read_fn)(void*, char*, int),
251 int (*__write_fn)(void*, const char*, int),
252 fpos64_t (*__seek_fn)(void*, fpos64_t, int),
253 int (*__close_fn)(void*)) __INTRODUCED_IN(24);
Elliott Hughes03e65eb2016-01-26 14:13:04 -0800254#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800255
Elliott Hughesff26a162017-08-17 22:34:21 +0000256FILE* fopen(const char* __path, const char* __mode);
257FILE* fopen64(const char* __path, const char* __mode) __INTRODUCED_IN(24);
258FILE* freopen(const char* __path, const char* __mode, FILE* __fp);
259FILE* freopen64(const char* __path, const char* __mode, FILE* __fp) __INTRODUCED_IN(24);
Elliott Hughesf226ee52016-02-03 11:24:28 -0800260FILE* tmpfile(void);
Josh Gao14adff12016-04-29 12:00:55 -0700261FILE* tmpfile64(void) __INTRODUCED_IN(24);
Elliott Hughesf226ee52016-02-03 11:24:28 -0800262
George Burgess IV90242352018-02-06 12:51:31 -0800263int snprintf(char* __buf, size_t __size, const char* __fmt, ...) __printflike(3, 4);
Elliott Hughesff26a162017-08-17 22:34:21 +0000264int vfscanf(FILE* __fp, const char* __fmt, va_list __args) __scanflike(2, 0);
265int vscanf(const char* __fmt , va_list __args) __scanflike(1, 0);
George Burgess IV90242352018-02-06 12:51:31 -0800266int vsnprintf(char* __buf, size_t __size, const char* __fmt, va_list __args) __printflike(3, 0);
Elliott Hughesff26a162017-08-17 22:34:21 +0000267int vsscanf(const char* __s, const char* __fmt, va_list __args) __scanflike(2, 0);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800268
Elliott Hughes3ba55f82016-06-08 18:11:23 -0700269#define L_ctermid 1024 /* size for ctermid() */
Elliott Hughesff26a162017-08-17 22:34:21 +0000270char* ctermid(char* __buf) __INTRODUCED_IN(26);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800271
Elliott Hughesff26a162017-08-17 22:34:21 +0000272FILE* fdopen(int __fd, const char* __mode);
273int fileno(FILE* __fp);
274int pclose(FILE* __fp);
275FILE* popen(const char* __command, const char* __mode);
276void flockfile(FILE* __fp);
277int ftrylockfile(FILE* __fp);
278void funlockfile(FILE* __fp);
279int getc_unlocked(FILE* __fp);
Elliott Hughes3ba55f82016-06-08 18:11:23 -0700280int getchar_unlocked(void);
Elliott Hughesff26a162017-08-17 22:34:21 +0000281int putc_unlocked(int __ch, FILE* __fp);
282int putchar_unlocked(int __ch);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800283
Elliott Hughesff26a162017-08-17 22:34:21 +0000284FILE* fmemopen(void* __buf, size_t __size, const char* __mode) __INTRODUCED_IN(23);
285FILE* open_memstream(char** __ptr, size_t* __size_ptr) __INTRODUCED_IN(23);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800286
Elliott Hughes9c8d7112016-06-13 13:23:42 -0700287#if defined(__USE_BSD) || defined(__BIONIC__) /* Historically bionic exposed these. */
Elliott Hughesff26a162017-08-17 22:34:21 +0000288int asprintf(char** __s_ptr, const char* __fmt, ...) __printflike(2, 3);
289char* fgetln(FILE* __fp, size_t* __length_ptr);
290int fpurge(FILE* __fp);
291void setbuffer(FILE* __fp, char* __buf, int __size);
292int setlinebuf(FILE* __fp);
293int vasprintf(char** __s_ptr, const char* __fmt, va_list __args) __printflike(2, 0);
294void clearerr_unlocked(FILE* __fp) __INTRODUCED_IN(23);
295int feof_unlocked(FILE* __fp) __INTRODUCED_IN(23);
296int ferror_unlocked(FILE* __fp) __INTRODUCED_IN(23);
297int fileno_unlocked(FILE* __fp) __INTRODUCED_IN(24);
Elliott Hughes03e65eb2016-01-26 14:13:04 -0800298#define fropen(cookie, fn) funopen(cookie, fn, 0, 0, 0)
299#define fwopen(cookie, fn) funopen(cookie, 0, fn, 0, 0)
Elliott Hughes37ad9592017-10-30 17:47:12 -0700300#endif
301
302#if defined(__USE_BSD)
Elliott Hughescc0fe6e2018-01-30 08:54:12 -0800303int fflush_unlocked(FILE* __fp) __INTRODUCED_IN(28);
304int fgetc_unlocked(FILE* __fp) __INTRODUCED_IN(28);
305int fputc_unlocked(int __ch, FILE* __fp) __INTRODUCED_IN(28);
306size_t fread_unlocked(void* __buf, size_t __size, size_t __count, FILE* __fp) __INTRODUCED_IN(28);
307size_t fwrite_unlocked(const void* __buf, size_t __size, size_t __count, FILE* __fp) __INTRODUCED_IN(28);
Elliott Hughes37ad9592017-10-30 17:47:12 -0700308#endif
309
310#if defined(__USE_GNU)
Elliott Hughescc0fe6e2018-01-30 08:54:12 -0800311int fputs_unlocked(const char* __s, FILE* __fp) __INTRODUCED_IN(28);
312char* fgets_unlocked(char* __buf, int __size, FILE* __fp) __INTRODUCED_IN(28);
Elliott Hughes37ad9592017-10-30 17:47:12 -0700313#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800314
George Burgess IVb97049c2017-07-24 15:05:05 -0700315#if defined(__BIONIC_INCLUDE_FORTIFY_HEADERS)
316#include <bits/fortify/stdio.h>
317#endif
Nick Kralevichcffdf662012-06-11 15:50:57 -0700318
Dan Albert658727e2014-10-07 11:10:36 -0700319__END_DECLS
320
Elliott Hughesff26a162017-08-17 22:34:21 +0000321#endif