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* _Nonnull stdin __INTRODUCED_IN(23); 63 extern FILE* _Nonnull stdout __INTRODUCED_IN(23); 64 extern FILE* _Nonnull 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, "Use stdin/stdout/stderr"); 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* _Nonnull __fp); 107 int fclose(FILE* _Nonnull __fp); 108 __wur int feof(FILE* _Nonnull __fp); 109 __wur int ferror(FILE* _Nonnull __fp); 110 int fflush(FILE* _Nullable __fp); 111 __wur int fgetc(FILE* _Nonnull __fp); 112 char* _Nullable fgets(char* _Nonnull __buf, int __size, FILE* _Nonnull __fp); 113 int fprintf(FILE* _Nonnull __fp , const char* _Nonnull __fmt, ...) __printflike(2, 3); 114 int fputc(int __ch, FILE* _Nonnull __fp); 115 int fputs(const char* _Nonnull __s, FILE* _Nonnull __fp); 116 size_t fread(void* _Nonnull __buf, size_t __size, size_t __count, FILE* _Nonnull __fp); 117 int fscanf(FILE* _Nonnull __fp, const char* _Nonnull __fmt, ...) __scanflike(2, 3); 118 size_t fwrite(const void* _Nonnull __buf, size_t __size, size_t __count, FILE* _Nonnull __fp); 119 __wur int getc(FILE* _Nonnull __fp); 120 __wur int getchar(void); 121 ssize_t getdelim(char* _Nullable * _Nonnull __line_ptr, size_t* _Nonnull __line_length_ptr, int __delimiter, FILE* _Nonnull __fp); 122 ssize_t getline(char* _Nullable * _Nonnull __line_ptr, size_t* _Nonnull __line_length_ptr, FILE* _Nonnull __fp); 123 124 void perror(const char* _Nullable __msg); 125 int printf(const char* _Nonnull __fmt, ...) __printflike(1, 2); 126 int putc(int __ch, FILE* _Nonnull __fp); 127 int putchar(int __ch); 128 int puts(const char* _Nonnull __s); 129 int remove(const char* _Nonnull __path); 130 void rewind(FILE* _Nonnull __fp); 131 int scanf(const char* _Nonnull __fmt, ...) __scanflike(1, 2); 132 void setbuf(FILE* _Nonnull __fp, char* _Nullable __buf); 133 int setvbuf(FILE* _Nonnull __fp, char* _Nullable __buf, int __mode, size_t __size); 134 int sscanf(const char* _Nonnull __s, const char* _Nonnull __fmt, ...) __scanflike(2, 3); 135 int ungetc(int __ch, FILE* _Nonnull __fp); 136 int vfprintf(FILE* _Nonnull __fp, const char* _Nonnull __fmt, va_list __args) __printflike(2, 0); 137 int vprintf(const char* _Nonnull __fp, va_list __args) __printflike(1, 0); 138 139 int dprintf(int __fd, const char* _Nonnull __fmt, ...) __printflike(2, 3); 140 int vdprintf(int __fd, const char* _Nonnull __fmt, va_list __args) __printflike(2, 0); 141 142 #if (defined(__STDC_VERSION__) && __STDC_VERSION__ < 201112L) || \ 143 (defined(__cplusplus) && __cplusplus <= 201103L) 144 char* _Nullable gets(char* _Nonnull __buf) __attribute__((__deprecated__("gets is unsafe, use fgets instead"))); 145 #endif 146 int sprintf(char* __BIONIC_COMPLICATED_NULLNESS __s, const char* _Nonnull __fmt, ...) 147 __printflike(2, 3) __warnattr_strict("sprintf is often misused; please use snprintf"); 148 int vsprintf(char* __BIONIC_COMPLICATED_NULLNESS __s, const char* _Nonnull __fmt, va_list __args) 149 __printflike(2, 0) __warnattr_strict("vsprintf is often misused; please use vsnprintf"); 150 char* _Nullable tmpnam(char* _Nullable __s) 151 __warnattr("tmpnam is unsafe, use mkstemp or tmpfile instead"); 152 #define P_tmpdir "/tmp/" /* deprecated */ 153 char* _Nullable tempnam(const char* _Nullable __dir, const char* _Nullable __prefix) 154 __warnattr("tempnam is unsafe, use mkstemp or tmpfile instead"); 155 156 /** 157 * [rename(2)](http://man7.org/linux/man-pages/man2/rename.2.html) changes 158 * the name or location of a file. 159 * 160 * Returns 0 on success, and returns -1 and sets `errno` on failure. 161 */ 162 int rename(const char* _Nonnull __old_path, const char* _Nonnull __new_path); 163 164 /** 165 * [renameat(2)](http://man7.org/linux/man-pages/man2/renameat.2.html) changes 166 * the name or location of a file, interpreting relative paths using an fd. 167 * 168 * Returns 0 on success, and returns -1 and sets `errno` on failure. 169 */ 170 int renameat(int __old_dir_fd, const char* _Nonnull __old_path, int __new_dir_fd, const char* _Nonnull __new_path); 171 172 #if defined(__USE_GNU) 173 174 /** 175 * Flag for [renameat2(2)](http://man7.org/linux/man-pages/man2/renameat2.2.html) 176 * to fail if the new path already exists. 177 */ 178 #define RENAME_NOREPLACE (1<<0) 179 180 /** 181 * Flag for [renameat2(2)](http://man7.org/linux/man-pages/man2/renameat2.2.html) 182 * to atomically exchange the two paths. 183 */ 184 #define RENAME_EXCHANGE (1<<1) 185 186 /** 187 * Flag for [renameat2(2)](http://man7.org/linux/man-pages/man2/renameat2.2.html) 188 * to create a union/overlay filesystem object. 189 */ 190 #define RENAME_WHITEOUT (1<<2) 191 192 /** 193 * [renameat2(2)](http://man7.org/linux/man-pages/man2/renameat2.2.html) changes 194 * the name or location of a file, interpreting relative paths using an fd, 195 * with optional `RENAME_` flags. 196 * 197 * Returns 0 on success, and returns -1 and sets `errno` on failure. 198 */ 199 int renameat2(int __old_dir_fd, const char* _Nonnull __old_path, int __new_dir_fd, const char* _Nonnull __new_path, unsigned __flags) __INTRODUCED_IN(30); 200 201 #endif 202 203 int fseek(FILE* _Nonnull __fp, long __offset, int __whence); 204 __wur long ftell(FILE* _Nonnull __fp); 205 206 /* See https://android.googlesource.com/platform/bionic/+/main/docs/32-bit-abi.md */ 207 #if defined(__USE_FILE_OFFSET64) 208 int fgetpos(FILE* _Nonnull __fp, fpos_t* _Nonnull __pos) __RENAME(fgetpos64) __INTRODUCED_IN(24); 209 int fsetpos(FILE* _Nonnull __fp, const fpos_t* _Nonnull __pos) __RENAME(fsetpos64) __INTRODUCED_IN(24); 210 int fseeko(FILE* _Nonnull __fp, off_t __offset, int __whence) __RENAME(fseeko64) __INTRODUCED_IN(24); 211 __wur off_t ftello(FILE* _Nonnull __fp) __RENAME(ftello64) __INTRODUCED_IN(24); 212 # if defined(__USE_BSD) 213 /* If __read_fn and __write_fn are both nullptr, it will cause EINVAL */ 214 __wur FILE* _Nullable funopen(const void* _Nullable __cookie, 215 int (* __BIONIC_COMPLICATED_NULLNESS __read_fn)(void* _Nonnull, char* _Nonnull, int), 216 int (* __BIONIC_COMPLICATED_NULLNESS __write_fn)(void* _Nonnull, const char* _Nonnull, int), 217 fpos_t (* _Nullable __seek_fn)(void* _Nonnull, fpos_t, int), 218 int (* _Nullable __close_fn)(void* _Nonnull)) __RENAME(funopen64) __INTRODUCED_IN(24); 219 # endif 220 #else 221 int fgetpos(FILE* _Nonnull __fp, fpos_t* _Nonnull __pos); 222 int fsetpos(FILE* _Nonnull __fp, const fpos_t* _Nonnull __pos); 223 int fseeko(FILE* _Nonnull __fp, off_t __offset, int __whence); 224 __wur off_t ftello(FILE* _Nonnull __fp); 225 # if defined(__USE_BSD) 226 /* If __read_fn and __write_fn are both nullptr, it will cause EINVAL */ 227 __wur FILE* _Nullable funopen(const void* _Nullable __cookie, 228 int (* __BIONIC_COMPLICATED_NULLNESS __read_fn)(void* _Nonnull, char* _Nonnull, int), 229 int (* __BIONIC_COMPLICATED_NULLNESS __write_fn)(void* _Nonnull, const char* _Nonnull, int), 230 fpos_t (* _Nullable __seek_fn)(void* _Nonnull, fpos_t, int), 231 int (* _Nullable __close_fn)(void* _Nonnull)); 232 # endif 233 #endif 234 int fgetpos64(FILE* _Nonnull __fp, fpos64_t* _Nonnull __pos) __INTRODUCED_IN(24); 235 int fsetpos64(FILE* _Nonnull __fp, const fpos64_t* _Nonnull __pos) __INTRODUCED_IN(24); 236 int fseeko64(FILE* _Nonnull __fp, off64_t __offset, int __whence) __INTRODUCED_IN(24); 237 __wur off64_t ftello64(FILE* _Nonnull __fp) __INTRODUCED_IN(24); 238 #if defined(__USE_BSD) 239 /* If __read_fn and __write_fn are both nullptr, it will cause EINVAL */ 240 __wur FILE* _Nullable funopen64(const void* _Nullable __cookie, 241 int (* __BIONIC_COMPLICATED_NULLNESS __read_fn)(void* _Nonnull, char* _Nonnull, int), 242 int (* __BIONIC_COMPLICATED_NULLNESS __write_fn)(void* _Nonnull, const char* _Nonnull, int), 243 fpos64_t (* _Nullable __seek_fn)(void* _Nonnull, fpos64_t, int), 244 int (* _Nullable __close_fn)(void* _Nonnull)) __INTRODUCED_IN(24); 245 #endif 246 247 __wur FILE* _Nullable fopen(const char* _Nonnull __path, const char* _Nonnull __mode); 248 __wur FILE* _Nullable fopen64(const char* _Nonnull __path, const char* _Nonnull __mode) __INTRODUCED_IN(24); 249 FILE* _Nullable freopen(const char* _Nullable __path, const char* _Nonnull __mode, FILE* _Nonnull __fp); 250 FILE* _Nullable freopen64(const char* _Nullable __path, const char* _Nonnull __mode, FILE* _Nonnull __fp) __INTRODUCED_IN(24); 251 __wur FILE* _Nullable tmpfile(void); 252 __wur FILE* _Nullable tmpfile64(void) __INTRODUCED_IN(24); 253 254 int snprintf(char* __BIONIC_COMPLICATED_NULLNESS __buf, size_t __size, const char* _Nonnull __fmt, ...) __printflike(3, 4); 255 int vfscanf(FILE* _Nonnull __fp, const char* _Nonnull __fmt, va_list __args) __scanflike(2, 0); 256 int vscanf(const char* _Nonnull __fmt , va_list __args) __scanflike(1, 0); 257 int vsnprintf(char* __BIONIC_COMPLICATED_NULLNESS __buf, size_t __size, const char* _Nonnull __fmt, va_list __args) __printflike(3, 0); 258 int vsscanf(const char* _Nonnull __s, const char* _Nonnull __fmt, va_list __args) __scanflike(2, 0); 259 260 #define L_ctermid 1024 /* size for ctermid() */ 261 char* _Nonnull ctermid(char* _Nullable __buf) __INTRODUCED_IN(26); 262 263 __wur FILE* _Nullable fdopen(int __fd, const char* _Nonnull __mode); 264 __wur int fileno(FILE* _Nonnull __fp); 265 int pclose(FILE* _Nonnull __fp); 266 __wur FILE* _Nullable popen(const char* _Nonnull __command, const char* _Nonnull __mode); 267 void flockfile(FILE* _Nonnull __fp); 268 int ftrylockfile(FILE* _Nonnull __fp); 269 void funlockfile(FILE* _Nonnull __fp); 270 __wur int getc_unlocked(FILE* _Nonnull __fp); 271 __wur int getchar_unlocked(void); 272 int putc_unlocked(int __ch, FILE* _Nonnull __fp); 273 int putchar_unlocked(int __ch); 274 275 __wur FILE* _Nullable fmemopen(void* _Nullable __buf, size_t __size, const char* _Nonnull __mode) __INTRODUCED_IN(23); 276 __wur FILE* _Nullable open_memstream(char* _Nonnull * _Nonnull __ptr, size_t* _Nonnull __size_ptr) __INTRODUCED_IN(23); 277 278 #if defined(__USE_BSD) || defined(__BIONIC__) /* Historically bionic exposed these. */ 279 int asprintf(char* _Nullable * _Nonnull __s_ptr, const char* _Nonnull __fmt, ...) __printflike(2, 3); 280 char* _Nullable fgetln(FILE* _Nonnull __fp, size_t* _Nonnull __length_ptr); 281 int fpurge(FILE* _Nonnull __fp); 282 void setbuffer(FILE* _Nonnull __fp, char* _Nullable __buf, int __size); 283 int setlinebuf(FILE* _Nonnull __fp); 284 int vasprintf(char* _Nullable * _Nonnull __s_ptr, const char* _Nonnull __fmt, va_list __args) __printflike(2, 0); 285 void clearerr_unlocked(FILE* _Nonnull __fp) __INTRODUCED_IN(23); 286 __wur int feof_unlocked(FILE* _Nonnull __fp) __INTRODUCED_IN(23); 287 __wur int ferror_unlocked(FILE* _Nonnull __fp) __INTRODUCED_IN(23); 288 __wur int fileno_unlocked(FILE* _Nonnull __fp) __INTRODUCED_IN(24); 289 #define fropen(cookie, fn) funopen(cookie, fn, 0, 0, 0) 290 #define fwopen(cookie, fn) funopen(cookie, 0, fn, 0, 0) 291 #endif 292 293 #if defined(__USE_BSD) 294 int fflush_unlocked(FILE* _Nullable __fp) __INTRODUCED_IN(28); 295 __wur int fgetc_unlocked(FILE* _Nonnull __fp) __INTRODUCED_IN(28); 296 int fputc_unlocked(int __ch, FILE* _Nonnull __fp) __INTRODUCED_IN(28); 297 size_t fread_unlocked(void* _Nonnull __buf, size_t __size, size_t __count, FILE* _Nonnull __fp) __INTRODUCED_IN(28); 298 size_t fwrite_unlocked(const void* _Nonnull __buf, size_t __size, size_t __count, FILE* _Nonnull __fp) __INTRODUCED_IN(28); 299 #endif 300 301 #if defined(__USE_GNU) 302 int fputs_unlocked(const char* _Nonnull __s, FILE* _Nonnull __fp) __INTRODUCED_IN(28); 303 char* _Nullable fgets_unlocked(char* _Nonnull __buf, int __size, FILE* _Nonnull __fp) __INTRODUCED_IN(28); 304 #endif 305 306 #if defined(__BIONIC_INCLUDE_FORTIFY_HEADERS) 307 #include <bits/fortify/stdio.h> 308 #endif 309 310 __END_DECLS 311 312 #endif 313