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 #include <bits/seek_constants.h> 48 49 #if __ANDROID_API__ < 24 50 #include <bits/struct_file.h> 51 #endif 52 53 __BEGIN_DECLS 54 55 typedef off_t fpos_t; 56 typedef off64_t fpos64_t; 57 58 struct __sFILE; 59 typedef struct __sFILE FILE; 60 61 #if __ANDROID_API__ >= 23 62 extern FILE* stdin __INTRODUCED_IN(23); 63 extern FILE* stdout __INTRODUCED_IN(23); 64 extern FILE* stderr __INTRODUCED_IN(23); 65 66 /* C99 and earlier plus current C++ standards say these must be macros. */ 67 #define stdin stdin 68 #define stdout stdout 69 #define stderr stderr 70 #else 71 /* Before M the actual symbols for stdin and friends had different names. */ 72 extern FILE __sF[] __REMOVED_IN(23); 73 74 #define stdin (&__sF[0]) 75 #define stdout (&__sF[1]) 76 #define stderr (&__sF[2]) 77 #endif 78 79 /* 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 */ 93 #define EOF (-1) 94 95 /* 96 * 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. 99 */ 100 #define FOPEN_MAX 20 101 #define FILENAME_MAX 4096 102 103 #define L_tmpnam 4096 104 #define TMP_MAX 308915776 105 106 void clearerr(FILE* __fp); 107 int fclose(FILE* __fp); 108 int feof(FILE* __fp); 109 int ferror(FILE* __fp); 110 int fflush(FILE* __fp); 111 int fgetc(FILE* __fp); 112 char* fgets(char* __buf, int __size, FILE* __fp); 113 int fprintf(FILE* __fp , const char* __fmt, ...) __printflike(2, 3); 114 int fputc(int __ch, FILE* __fp); 115 int fputs(const char* __s, FILE* __fp); 116 size_t fread(void* __buf, size_t __size, size_t __count, FILE* __fp); 117 int fscanf(FILE* __fp, const char* __fmt, ...) __scanflike(2, 3); 118 size_t fwrite(const void* __buf, size_t __size, size_t __count, FILE* __fp); 119 int getc(FILE* __fp); 120 int getchar(void); 121 ssize_t getdelim(char** __line_ptr, size_t* __line_length_ptr, int __delimiter, FILE* __fp) __INTRODUCED_IN(18); 122 ssize_t getline(char** __line_ptr, size_t* __line_length_ptr, FILE* __fp) __INTRODUCED_IN(18); 123 124 void perror(const char* __msg); 125 int printf(const char* __fmt, ...) __printflike(1, 2); 126 int putc(int __ch, FILE* __fp); 127 int putchar(int __ch); 128 int puts(const char* __s); 129 int remove(const char* __path); 130 void rewind(FILE* __fp); 131 int scanf(const char* __fmt, ...) __scanflike(1, 2); 132 void setbuf(FILE* __fp, char* __buf); 133 int setvbuf(FILE* __fp, char* __buf, int __mode, size_t __size); 134 int sscanf(const char* __s, const char* __fmt, ...) __scanflike(2, 3); 135 int ungetc(int __ch, FILE* __fp); 136 int vfprintf(FILE* __fp, const char* __fmt, va_list __args) __printflike(2, 0); 137 int vprintf(const char* __fp, va_list __args) __printflike(1, 0); 138 139 #if __ANDROID_API__ >= 21 140 int dprintf(int __fd, const char* __fmt, ...) __printflike(2, 3) __INTRODUCED_IN(21); 141 int vdprintf(int __fd, const char* __fmt, va_list __args) __printflike(2, 0) __INTRODUCED_IN(21); 142 #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 */ 150 int dprintf(int __fd, const char* __fmt, ...) __RENAME(fdprintf) __printflike(2, 3); 151 int vdprintf(int __fd, const char* __fmt, va_list __args) __RENAME(vfdprintf) __printflike(2, 0); 152 #endif 153 154 #if (defined(__STDC_VERSION__) && __STDC_VERSION__ < 201112L) || \ 155 (defined(__cplusplus) && __cplusplus <= 201103L) 156 char* gets(char* __buf) __attribute__((deprecated("gets is unsafe, use fgets instead"))); 157 #endif 158 int sprintf(char* __s, const char* __fmt, ...) 159 __printflike(2, 3) __warnattr_strict("sprintf is often misused; please use snprintf"); 160 int vsprintf(char* __s, const char* __fmt, va_list __args) 161 __printflike(2, 0) __warnattr_strict("vsprintf is often misused; please use vsnprintf"); 162 char* tmpnam(char* __s) 163 __warnattr("tmpnam is unsafe, use mkstemp or tmpfile instead"); 164 #define P_tmpdir "/tmp/" /* deprecated */ 165 char* tempnam(const char* __dir, const char* __prefix) 166 __warnattr("tempnam is unsafe, use mkstemp or tmpfile instead"); 167 168 /** 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 */ 174 int rename(const char* __old_path, const char* __new_path); 175 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 */ 182 int renameat(int __old_dir_fd, const char* __old_path, int __new_dir_fd, const char* __new_path); 183 184 #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 */ 211 int 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 215 int fseek(FILE* __fp, long __offset, int __whence); 216 long ftell(FILE* __fp); 217 218 /* See https://android.googlesource.com/platform/bionic/+/master/docs/32-bit-abi.md */ 219 #if defined(__USE_FILE_OFFSET64) 220 int fgetpos(FILE* __fp, fpos_t* __pos) __RENAME(fgetpos64) __INTRODUCED_IN(24); 221 int fsetpos(FILE* __fp, const fpos_t* __pos) __RENAME(fsetpos64) __INTRODUCED_IN(24); 222 int fseeko(FILE* __fp, off_t __offset, int __whence) __RENAME(fseeko64) __INTRODUCED_IN(24); 223 off_t ftello(FILE* __fp) __RENAME(ftello64) __INTRODUCED_IN(24); 224 # if defined(__USE_BSD) 225 FILE* 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); 230 # endif 231 #else 232 int fgetpos(FILE* __fp, fpos_t* __pos); 233 int fsetpos(FILE* __fp, const fpos_t* __pos); 234 int fseeko(FILE* __fp, off_t __offset, int __whence); 235 off_t ftello(FILE* __fp); 236 # if defined(__USE_BSD) 237 FILE* 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*)); 242 # endif 243 #endif 244 int fgetpos64(FILE* __fp, fpos64_t* __pos) __INTRODUCED_IN(24); 245 int fsetpos64(FILE* __fp, const fpos64_t* __pos) __INTRODUCED_IN(24); 246 int fseeko64(FILE* __fp, off64_t __offset, int __whence) __INTRODUCED_IN(24); 247 off64_t ftello64(FILE* __fp) __INTRODUCED_IN(24); 248 #if defined(__USE_BSD) 249 FILE* 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); 254 #endif 255 256 FILE* fopen(const char* __path, const char* __mode); 257 FILE* fopen64(const char* __path, const char* __mode) __INTRODUCED_IN(24); 258 FILE* freopen(const char* __path, const char* __mode, FILE* __fp); 259 FILE* freopen64(const char* __path, const char* __mode, FILE* __fp) __INTRODUCED_IN(24); 260 FILE* tmpfile(void); 261 FILE* tmpfile64(void) __INTRODUCED_IN(24); 262 263 int snprintf(char* __buf, size_t __size, const char* __fmt, ...) __printflike(3, 4); 264 int vfscanf(FILE* __fp, const char* __fmt, va_list __args) __scanflike(2, 0); 265 int vscanf(const char* __fmt , va_list __args) __scanflike(1, 0); 266 int vsnprintf(char* __buf, size_t __size, const char* __fmt, va_list __args) __printflike(3, 0); 267 int vsscanf(const char* __s, const char* __fmt, va_list __args) __scanflike(2, 0); 268 269 #define L_ctermid 1024 /* size for ctermid() */ 270 char* ctermid(char* __buf) __INTRODUCED_IN(26); 271 272 FILE* fdopen(int __fd, const char* __mode); 273 int fileno(FILE* __fp); 274 int pclose(FILE* __fp); 275 FILE* popen(const char* __command, const char* __mode); 276 void flockfile(FILE* __fp); 277 int ftrylockfile(FILE* __fp); 278 void funlockfile(FILE* __fp); 279 int getc_unlocked(FILE* __fp); 280 int getchar_unlocked(void); 281 int putc_unlocked(int __ch, FILE* __fp); 282 int putchar_unlocked(int __ch); 283 284 FILE* fmemopen(void* __buf, size_t __size, const char* __mode) __INTRODUCED_IN(23); 285 FILE* open_memstream(char** __ptr, size_t* __size_ptr) __INTRODUCED_IN(23); 286 287 #if defined(__USE_BSD) || defined(__BIONIC__) /* Historically bionic exposed these. */ 288 int asprintf(char** __s_ptr, const char* __fmt, ...) __printflike(2, 3); 289 char* fgetln(FILE* __fp, size_t* __length_ptr); 290 int fpurge(FILE* __fp); 291 void setbuffer(FILE* __fp, char* __buf, int __size); 292 int setlinebuf(FILE* __fp); 293 int vasprintf(char** __s_ptr, const char* __fmt, va_list __args) __printflike(2, 0); 294 void clearerr_unlocked(FILE* __fp) __INTRODUCED_IN(23); 295 int feof_unlocked(FILE* __fp) __INTRODUCED_IN(23); 296 int ferror_unlocked(FILE* __fp) __INTRODUCED_IN(23); 297 int fileno_unlocked(FILE* __fp) __INTRODUCED_IN(24); 298 #define fropen(cookie, fn) funopen(cookie, fn, 0, 0, 0) 299 #define fwopen(cookie, fn) funopen(cookie, 0, fn, 0, 0) 300 #endif 301 302 #if defined(__USE_BSD) 303 int fflush_unlocked(FILE* __fp) __INTRODUCED_IN(28); 304 int fgetc_unlocked(FILE* __fp) __INTRODUCED_IN(28); 305 int fputc_unlocked(int __ch, FILE* __fp) __INTRODUCED_IN(28); 306 size_t fread_unlocked(void* __buf, size_t __size, size_t __count, FILE* __fp) __INTRODUCED_IN(28); 307 size_t fwrite_unlocked(const void* __buf, size_t __size, size_t __count, FILE* __fp) __INTRODUCED_IN(28); 308 #endif 309 310 #if defined(__USE_GNU) 311 int fputs_unlocked(const char* __s, FILE* __fp) __INTRODUCED_IN(28); 312 char* fgets_unlocked(char* __buf, int __size, FILE* __fp) __INTRODUCED_IN(28); 313 #endif 314 315 #if defined(__BIONIC_INCLUDE_FORTIFY_HEADERS) 316 #include <bits/fortify/stdio.h> 317 #endif 318 319 __END_DECLS 320 321 #endif 322