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 __nodiscard int feof(FILE* _Nonnull __fp); 109 __nodiscard int ferror(FILE* _Nonnull __fp); 110 int fflush(FILE* _Nullable __fp); 111 __nodiscard 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 __nodiscard int getc(FILE* _Nonnull __fp); 120 __nodiscard 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)](https://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)](https://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)](https://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)](https://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)](https://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)](https://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 200 #if __BIONIC_AVAILABILITY_GUARD(30) 201 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); 202 #endif /* __BIONIC_AVAILABILITY_GUARD(30) */ 203 204 205 #endif 206 207 int fseek(FILE* _Nonnull __fp, long __offset, int __whence); 208 __nodiscard long ftell(FILE* _Nonnull __fp); 209 210 /* See https://android.googlesource.com/platform/bionic/+/main/docs/32-bit-abi.md */ 211 #if defined(__USE_FILE_OFFSET64) 212 213 #if __BIONIC_AVAILABILITY_GUARD(24) 214 int fgetpos(FILE* _Nonnull __fp, fpos_t* _Nonnull __pos) __RENAME(fgetpos64) __INTRODUCED_IN(24); 215 int fsetpos(FILE* _Nonnull __fp, const fpos_t* _Nonnull __pos) __RENAME(fsetpos64) __INTRODUCED_IN(24); 216 int fseeko(FILE* _Nonnull __fp, off_t __offset, int __whence) __RENAME(fseeko64) __INTRODUCED_IN(24); 217 __nodiscard off_t ftello(FILE* _Nonnull __fp) __RENAME(ftello64) __INTRODUCED_IN(24); 218 #endif /* __BIONIC_AVAILABILITY_GUARD(24) */ 219 220 # if defined(__USE_BSD) 221 /* If __read_fn and __write_fn are both nullptr, it will cause EINVAL */ 222 223 #if __BIONIC_AVAILABILITY_GUARD(24) 224 __nodiscard FILE* _Nullable funopen(const void* _Nullable __cookie, 225 int (* __BIONIC_COMPLICATED_NULLNESS __read_fn)(void* _Nonnull, char* _Nonnull, int), 226 int (* __BIONIC_COMPLICATED_NULLNESS __write_fn)(void* _Nonnull, const char* _Nonnull, int), 227 fpos_t (* _Nullable __seek_fn)(void* _Nonnull, fpos_t, int), 228 int (* _Nullable __close_fn)(void* _Nonnull)) __RENAME(funopen64) __INTRODUCED_IN(24); 229 #endif /* __BIONIC_AVAILABILITY_GUARD(24) */ 230 231 # endif 232 #else 233 int fgetpos(FILE* _Nonnull __fp, fpos_t* _Nonnull __pos); 234 int fsetpos(FILE* _Nonnull __fp, const fpos_t* _Nonnull __pos); 235 int fseeko(FILE* _Nonnull __fp, off_t __offset, int __whence); 236 __nodiscard off_t ftello(FILE* _Nonnull __fp); 237 # if defined(__USE_BSD) 238 /* If __read_fn and __write_fn are both nullptr, it will cause EINVAL */ 239 __nodiscard FILE* _Nullable funopen(const void* _Nullable __cookie, 240 int (* __BIONIC_COMPLICATED_NULLNESS __read_fn)(void* _Nonnull, char* _Nonnull, int), 241 int (* __BIONIC_COMPLICATED_NULLNESS __write_fn)(void* _Nonnull, const char* _Nonnull, int), 242 fpos_t (* _Nullable __seek_fn)(void* _Nonnull, fpos_t, int), 243 int (* _Nullable __close_fn)(void* _Nonnull)); 244 # endif 245 #endif 246 247 #if __BIONIC_AVAILABILITY_GUARD(24) 248 int fgetpos64(FILE* _Nonnull __fp, fpos64_t* _Nonnull __pos) __INTRODUCED_IN(24); 249 int fsetpos64(FILE* _Nonnull __fp, const fpos64_t* _Nonnull __pos) __INTRODUCED_IN(24); 250 int fseeko64(FILE* _Nonnull __fp, off64_t __offset, int __whence) __INTRODUCED_IN(24); 251 __nodiscard off64_t ftello64(FILE* _Nonnull __fp) __INTRODUCED_IN(24); 252 #endif /* __BIONIC_AVAILABILITY_GUARD(24) */ 253 254 #if defined(__USE_BSD) 255 /* If __read_fn and __write_fn are both nullptr, it will cause EINVAL */ 256 257 #if __BIONIC_AVAILABILITY_GUARD(24) 258 __nodiscard FILE* _Nullable funopen64(const void* _Nullable __cookie, 259 int (* __BIONIC_COMPLICATED_NULLNESS __read_fn)(void* _Nonnull, char* _Nonnull, int), 260 int (* __BIONIC_COMPLICATED_NULLNESS __write_fn)(void* _Nonnull, const char* _Nonnull, int), 261 fpos64_t (* _Nullable __seek_fn)(void* _Nonnull, fpos64_t, int), 262 int (* _Nullable __close_fn)(void* _Nonnull)) __INTRODUCED_IN(24); 263 #endif /* __BIONIC_AVAILABILITY_GUARD(24) */ 264 265 #endif 266 267 __nodiscard FILE* _Nullable fopen(const char* _Nonnull __path, const char* _Nonnull __mode); 268 269 #if __BIONIC_AVAILABILITY_GUARD(24) 270 __nodiscard FILE* _Nullable fopen64(const char* _Nonnull __path, const char* _Nonnull __mode) __INTRODUCED_IN(24); 271 #endif /* __BIONIC_AVAILABILITY_GUARD(24) */ 272 273 FILE* _Nullable freopen(const char* _Nullable __path, const char* _Nonnull __mode, FILE* _Nonnull __fp); 274 275 #if __BIONIC_AVAILABILITY_GUARD(24) 276 FILE* _Nullable freopen64(const char* _Nullable __path, const char* _Nonnull __mode, FILE* _Nonnull __fp) __INTRODUCED_IN(24); 277 #endif /* __BIONIC_AVAILABILITY_GUARD(24) */ 278 279 __nodiscard FILE* _Nullable tmpfile(void); 280 281 #if __BIONIC_AVAILABILITY_GUARD(24) 282 __nodiscard FILE* _Nullable tmpfile64(void) __INTRODUCED_IN(24); 283 #endif /* __BIONIC_AVAILABILITY_GUARD(24) */ 284 285 286 int snprintf(char* __BIONIC_COMPLICATED_NULLNESS __buf, size_t __size, const char* _Nonnull __fmt, ...) __printflike(3, 4); 287 int vfscanf(FILE* _Nonnull __fp, const char* _Nonnull __fmt, va_list __args) __scanflike(2, 0); 288 int vscanf(const char* _Nonnull __fmt , va_list __args) __scanflike(1, 0); 289 int vsnprintf(char* __BIONIC_COMPLICATED_NULLNESS __buf, size_t __size, const char* _Nonnull __fmt, va_list __args) __printflike(3, 0); 290 int vsscanf(const char* _Nonnull __s, const char* _Nonnull __fmt, va_list __args) __scanflike(2, 0); 291 292 #define L_ctermid 1024 /* size for ctermid() */ 293 294 #if __BIONIC_AVAILABILITY_GUARD(26) 295 char* _Nonnull ctermid(char* _Nullable __buf) __INTRODUCED_IN(26); 296 #endif /* __BIONIC_AVAILABILITY_GUARD(26) */ 297 298 299 __nodiscard FILE* _Nullable fdopen(int __fd, const char* _Nonnull __mode); 300 __nodiscard int fileno(FILE* _Nonnull __fp); 301 int pclose(FILE* _Nonnull __fp); 302 __nodiscard FILE* _Nullable popen(const char* _Nonnull __command, const char* _Nonnull __mode); 303 void flockfile(FILE* _Nonnull __fp); 304 int ftrylockfile(FILE* _Nonnull __fp); 305 void funlockfile(FILE* _Nonnull __fp); 306 __nodiscard int getc_unlocked(FILE* _Nonnull __fp); 307 __nodiscard int getchar_unlocked(void); 308 int putc_unlocked(int __ch, FILE* _Nonnull __fp); 309 int putchar_unlocked(int __ch); 310 311 312 #if __BIONIC_AVAILABILITY_GUARD(23) 313 __nodiscard FILE* _Nullable fmemopen(void* _Nullable __buf, size_t __size, const char* _Nonnull __mode) __INTRODUCED_IN(23); 314 __nodiscard FILE* _Nullable open_memstream(char* _Nonnull * _Nonnull __ptr, size_t* _Nonnull __size_ptr) __INTRODUCED_IN(23); 315 #endif /* __BIONIC_AVAILABILITY_GUARD(23) */ 316 317 318 #if defined(__USE_BSD) || defined(__BIONIC__) /* Historically bionic exposed these. */ 319 int asprintf(char* _Nullable * _Nonnull __s_ptr, const char* _Nonnull __fmt, ...) __printflike(2, 3); 320 char* _Nullable fgetln(FILE* _Nonnull __fp, size_t* _Nonnull __length_ptr); 321 int fpurge(FILE* _Nonnull __fp); 322 void setbuffer(FILE* _Nonnull __fp, char* _Nullable __buf, int __size); 323 int setlinebuf(FILE* _Nonnull __fp); 324 int vasprintf(char* _Nullable * _Nonnull __s_ptr, const char* _Nonnull __fmt, va_list __args) __printflike(2, 0); 325 326 #if __BIONIC_AVAILABILITY_GUARD(23) 327 void clearerr_unlocked(FILE* _Nonnull __fp) __INTRODUCED_IN(23); 328 __nodiscard int feof_unlocked(FILE* _Nonnull __fp) __INTRODUCED_IN(23); 329 __nodiscard int ferror_unlocked(FILE* _Nonnull __fp) __INTRODUCED_IN(23); 330 #endif /* __BIONIC_AVAILABILITY_GUARD(23) */ 331 332 333 #if __BIONIC_AVAILABILITY_GUARD(24) 334 __nodiscard int fileno_unlocked(FILE* _Nonnull __fp) __INTRODUCED_IN(24); 335 #endif /* __BIONIC_AVAILABILITY_GUARD(24) */ 336 337 #define fropen(cookie, fn) funopen(cookie, fn, 0, 0, 0) 338 #define fwopen(cookie, fn) funopen(cookie, 0, fn, 0, 0) 339 #endif 340 341 #if defined(__USE_BSD) 342 343 #if __BIONIC_AVAILABILITY_GUARD(28) 344 int fflush_unlocked(FILE* _Nullable __fp) __INTRODUCED_IN(28); 345 __nodiscard int fgetc_unlocked(FILE* _Nonnull __fp) __INTRODUCED_IN(28); 346 int fputc_unlocked(int __ch, FILE* _Nonnull __fp) __INTRODUCED_IN(28); 347 size_t fread_unlocked(void* _Nonnull __buf, size_t __size, size_t __count, FILE* _Nonnull __fp) __INTRODUCED_IN(28); 348 size_t fwrite_unlocked(const void* _Nonnull __buf, size_t __size, size_t __count, FILE* _Nonnull __fp) __INTRODUCED_IN(28); 349 #endif /* __BIONIC_AVAILABILITY_GUARD(28) */ 350 351 #endif 352 353 #if defined(__USE_GNU) 354 355 #if __BIONIC_AVAILABILITY_GUARD(28) 356 int fputs_unlocked(const char* _Nonnull __s, FILE* _Nonnull __fp) __INTRODUCED_IN(28); 357 char* _Nullable fgets_unlocked(char* _Nonnull __buf, int __size, FILE* _Nonnull __fp) __INTRODUCED_IN(28); 358 #endif /* __BIONIC_AVAILABILITY_GUARD(28) */ 359 360 #endif 361 362 #if defined(__BIONIC_INCLUDE_FORTIFY_HEADERS) 363 #include <bits/fortify/stdio.h> 364 #endif 365 366 __END_DECLS 367 368 #endif 369