stdio.h revision 9eb3ae122fd9759251063ee351cb23cc769dbe61
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__BEGIN_DECLS
51
52typedef off_t fpos_t;
53typedef off64_t fpos64_t;
54
55struct __sFILE;
56typedef struct __sFILE FILE;
57
58extern FILE* stdin __INTRODUCED_IN(23);
59extern FILE* stdout __INTRODUCED_IN(23);
60extern FILE* stderr __INTRODUCED_IN(23);
61/* C99 and earlier plus current C++ standards say these must be macros. */
62#define stdin stdin
63#define stdout stdout
64#define stderr stderr
65
66/*
67 * The following three definitions are for ANSI C, which took them
68 * from System V, which brilliantly took internal interface macros and
69 * made them official arguments to setvbuf(), without renaming them.
70 * Hence, these ugly _IOxxx names are *supposed* to appear in user code.
71 *
72 * Although numbered as their counterparts above, the implementation
73 * does not rely on this.
74 */
75#define	_IOFBF	0		/* setvbuf should set fully buffered */
76#define	_IOLBF	1		/* setvbuf should set line buffered */
77#define	_IONBF	2		/* setvbuf should set unbuffered */
78
79#define	BUFSIZ	1024		/* size of buffer used by setbuf */
80#define	EOF	(-1)
81
82/*
83 * FOPEN_MAX is a minimum maximum, and is the number of streams that
84 * stdio can provide without attempting to allocate further resources
85 * (which could fail).  Do not use this for anything.
86 */
87
88#define	FOPEN_MAX	20	/* must be <= OPEN_MAX <sys/syslimits.h> */
89#define	FILENAME_MAX	1024	/* must be <= PATH_MAX <sys/syslimits.h> */
90
91#define	L_tmpnam	1024	/* XXX must be == PATH_MAX */
92#define	TMP_MAX		308915776
93
94#define SEEK_SET 0
95#define SEEK_CUR 1
96#define SEEK_END 2
97
98/*
99 * Functions defined in ANSI C standard.
100 */
101void	 clearerr(FILE *);
102int	 fclose(FILE *);
103int	 feof(FILE *);
104int	 ferror(FILE *);
105int	 fflush(FILE *);
106int	 fgetc(FILE *);
107char	*fgets(char * __restrict, int, FILE * __restrict);
108int	 fprintf(FILE * __restrict , const char * __restrict _Nonnull, ...) __printflike(2, 3);
109int	 fputc(int, FILE *);
110int	 fputs(const char * __restrict, FILE * __restrict);
111size_t	 fread(void * __restrict, size_t, size_t, FILE * __restrict);
112int	 fscanf(FILE * __restrict, const char * __restrict _Nonnull, ...) __scanflike(2, 3);
113size_t	 fwrite(const void * __restrict, size_t, size_t, FILE * __restrict);
114int	 getc(FILE *);
115int	 getchar(void);
116ssize_t getdelim(char** __restrict, size_t* __restrict, int, FILE* __restrict) __INTRODUCED_IN(18);
117ssize_t getline(char** __restrict, size_t* __restrict, FILE* __restrict) __INTRODUCED_IN(18);
118
119void	 perror(const char *);
120int	 printf(const char * __restrict _Nonnull, ...) __printflike(1, 2);
121int	 putc(int, FILE *);
122int	 putchar(int);
123int	 puts(const char *);
124int	 remove(const char *);
125void	 rewind(FILE *);
126int	 scanf(const char * __restrict _Nonnull, ...) __scanflike(1, 2);
127void	 setbuf(FILE * __restrict, char * __restrict);
128int	 setvbuf(FILE * __restrict, char * __restrict, int, size_t);
129int	 sscanf(const char * __restrict, const char * __restrict _Nonnull, ...) __scanflike(2, 3);
130int	 ungetc(int, FILE *);
131int	 vfprintf(FILE * __restrict, const char * __restrict _Nonnull, __va_list) __printflike(2, 0);
132int	 vprintf(const char * __restrict _Nonnull, __va_list) __printflike(1, 0);
133
134int dprintf(int, const char* __restrict _Nonnull, ...) __printflike(2, 3) __INTRODUCED_IN(21);
135int vdprintf(int, const char* __restrict _Nonnull, __va_list) __printflike(2, 0) __INTRODUCED_IN(21);
136
137#if __STDC_VERSION__ < 201112L
138char* gets(char*) __attribute__((deprecated("gets is unsafe, use fgets instead")));
139#endif
140int sprintf(char* __restrict, const char* __restrict _Nonnull, ...) __printflike(2, 3) __warnattr("sprintf is often misused; please use snprintf");
141int vsprintf(char* __restrict, const char* __restrict _Nonnull, __va_list) __printflike(2, 0) __warnattr("vsprintf is often misused; please use vsnprintf");
142char* tmpnam(char*) __attribute__((deprecated("tmpnam is unsafe, use mkstemp or tmpfile instead")));
143#if defined(__USE_BSD) || defined(__USE_GNU)
144#define P_tmpdir "/tmp/" /* deprecated */
145char* tempnam(const char*, const char*)
146    __attribute__((deprecated("tempnam is unsafe, use mkstemp or tmpfile instead")));
147#endif
148
149int rename(const char*, const char*);
150int renameat(int, const char*, int, const char*);
151
152int fseek(FILE*, long, int);
153long ftell(FILE*);
154
155#if defined(__USE_FILE_OFFSET64)
156int fgetpos(FILE*, fpos_t*) __RENAME(fgetpos64);
157int fsetpos(FILE*, const fpos_t*) __RENAME(fsetpos64);
158int fseeko(FILE*, off_t, int) __RENAME(fseeko64);
159off_t ftello(FILE*) __RENAME(ftello64);
160#  if defined(__USE_BSD)
161FILE* funopen(const void*,
162              int (*)(void*, char*, int),
163              int (*)(void*, const char*, int),
164              fpos_t (*)(void*, fpos_t, int),
165              int (*)(void*)) __RENAME(funopen64);
166#  endif
167#else
168int fgetpos(FILE*, fpos_t*);
169int fsetpos(FILE*, const fpos_t*);
170int fseeko(FILE*, off_t, int);
171off_t ftello(FILE*);
172#  if defined(__USE_BSD)
173FILE* funopen(const void*,
174              int (*)(void*, char*, int),
175              int (*)(void*, const char*, int),
176              fpos_t (*)(void*, fpos_t, int),
177              int (*)(void*));
178#  endif
179#endif
180int fgetpos64(FILE*, fpos64_t*) __INTRODUCED_IN(24);
181int fsetpos64(FILE*, const fpos64_t*) __INTRODUCED_IN(24);
182int fseeko64(FILE*, off64_t, int) __INTRODUCED_IN(24);
183off64_t ftello64(FILE*) __INTRODUCED_IN(24);
184#if defined(__USE_BSD)
185FILE* funopen64(const void*, int (*)(void*, char*, int), int (*)(void*, const char*, int),
186                fpos64_t (*)(void*, fpos64_t, int), int (*)(void*)) __INTRODUCED_IN(24);
187#endif
188
189FILE* fopen(const char* __restrict, const char* __restrict);
190FILE* fopen64(const char* __restrict, const char* __restrict) __INTRODUCED_IN(24);
191FILE* freopen(const char* __restrict, const char* __restrict, FILE* __restrict);
192FILE* freopen64(const char* __restrict, const char* __restrict, FILE* __restrict)
193  __INTRODUCED_IN(24);
194FILE* tmpfile(void);
195FILE* tmpfile64(void) __INTRODUCED_IN(24);
196
197int snprintf(char* __restrict, size_t, const char* __restrict _Nonnull, ...) __printflike(3, 4);
198int vfscanf(FILE* __restrict, const char* __restrict _Nonnull, __va_list) __scanflike(2, 0);
199int vscanf(const char* _Nonnull , __va_list) __scanflike(1, 0);
200int vsnprintf(char* __restrict, size_t, const char* __restrict _Nonnull, __va_list) __printflike(3, 0);
201int vsscanf(const char* __restrict _Nonnull, const char* __restrict _Nonnull, __va_list) __scanflike(2, 0);
202
203#define L_ctermid 1024 /* size for ctermid() */
204char* ctermid(char*) __INTRODUCED_IN_FUTURE;
205
206FILE* fdopen(int, const char*);
207int fileno(FILE*);
208int pclose(FILE*);
209FILE* popen(const char*, const char*);
210void flockfile(FILE*);
211int ftrylockfile(FILE*);
212void funlockfile(FILE*);
213int getc_unlocked(FILE*);
214int getchar_unlocked(void);
215int putc_unlocked(int, FILE*);
216int putchar_unlocked(int);
217
218FILE* fmemopen(void*, size_t, const char*) __INTRODUCED_IN(23);
219FILE* open_memstream(char**, size_t*) __INTRODUCED_IN(23);
220
221#if defined(__USE_BSD) || defined(__BIONIC__) /* Historically bionic exposed these. */
222int  asprintf(char** __restrict, const char* __restrict _Nonnull, ...) __printflike(2, 3);
223char* fgetln(FILE* __restrict, size_t* __restrict);
224int fpurge(FILE*);
225void setbuffer(FILE*, char*, int);
226int setlinebuf(FILE*);
227int vasprintf(char** __restrict, const char* __restrict _Nonnull, __va_list) __printflike(2, 0);
228void clearerr_unlocked(FILE*) __INTRODUCED_IN(23);
229int feof_unlocked(FILE*) __INTRODUCED_IN(23);
230int ferror_unlocked(FILE*) __INTRODUCED_IN(23);
231int fileno_unlocked(FILE*) __INTRODUCED_IN(24);
232#define fropen(cookie, fn) funopen(cookie, fn, 0, 0, 0)
233#define fwopen(cookie, fn) funopen(cookie, 0, fn, 0, 0)
234#endif /* __USE_BSD */
235
236extern char* __fgets_chk(char*, int, FILE*, size_t) __INTRODUCED_IN(17);
237extern char* __fgets_real(char*, int, FILE*) __RENAME(fgets);
238__errordecl(__fgets_too_big_error, "fgets called with size bigger than buffer");
239__errordecl(__fgets_too_small_error, "fgets called with size less than zero");
240
241extern size_t __fread_chk(void* __restrict, size_t, size_t, FILE* __restrict, size_t)
242  __INTRODUCED_IN(24);
243extern size_t __fread_real(void * __restrict, size_t, size_t, FILE * __restrict) __RENAME(fread);
244__errordecl(__fread_too_big_error, "fread called with size * count bigger than buffer");
245__errordecl(__fread_overflow, "fread called with overflowing size * count");
246
247extern size_t __fwrite_chk(const void* __restrict, size_t, size_t, FILE* __restrict, size_t)
248  __INTRODUCED_IN(24);
249extern size_t __fwrite_real(const void * __restrict, size_t, size_t, FILE * __restrict) __RENAME(fwrite);
250__errordecl(__fwrite_too_big_error, "fwrite called with size * count bigger than buffer");
251__errordecl(__fwrite_overflow, "fwrite called with overflowing size * count");
252
253#if defined(__BIONIC_FORTIFY)
254
255__BIONIC_FORTIFY_INLINE
256__printflike(3, 0) int vsnprintf(char* dest, size_t size, const char* _Nonnull format, __va_list ap) {
257    return __builtin___vsnprintf_chk(dest, size, 0, __bos(dest), format, ap);
258}
259
260__BIONIC_FORTIFY_INLINE
261__printflike(2, 0) int vsprintf(char* dest, const char* _Nonnull format, __va_list ap) {
262    return __builtin___vsprintf_chk(dest, 0, __bos(dest), format, ap);
263}
264
265#if defined(__clang__)
266  #if !defined(snprintf)
267    #define __wrap_snprintf(dest, size, ...) __builtin___snprintf_chk(dest, size, 0, __bos(dest), __VA_ARGS__)
268    #define snprintf(...) __wrap_snprintf(__VA_ARGS__)
269  #endif
270#else
271__BIONIC_FORTIFY_INLINE
272__printflike(3, 4) int snprintf(char* dest, size_t size, const char* _Nonnull format, ...) {
273    return __builtin___snprintf_chk(dest, size, 0, __bos(dest), format, __builtin_va_arg_pack());
274}
275#endif
276
277#if defined(__clang__)
278  #if !defined(sprintf)
279    #define __wrap_sprintf(dest, ...) __builtin___sprintf_chk(dest, 0, __bos(dest), __VA_ARGS__)
280    #define sprintf(...) __wrap_sprintf(__VA_ARGS__)
281  #endif
282#else
283__BIONIC_FORTIFY_INLINE
284__printflike(2, 3) int sprintf(char* dest, const char* _Nonnull format, ...) {
285    return __builtin___sprintf_chk(dest, 0, __bos(dest), format, __builtin_va_arg_pack());
286}
287#endif
288
289__BIONIC_FORTIFY_INLINE
290size_t fread(void * __restrict buf, size_t size, size_t count, FILE * __restrict stream) {
291    size_t bos = __bos0(buf);
292
293#if !defined(__clang__)
294    if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
295        return __fread_real(buf, size, count, stream);
296    }
297
298    if (__builtin_constant_p(size) && __builtin_constant_p(count)) {
299        size_t total;
300        if (__size_mul_overflow(size, count, &total)) {
301            __fread_overflow();
302        }
303
304        if (total > bos) {
305            __fread_too_big_error();
306        }
307
308        return __fread_real(buf, size, count, stream);
309    }
310#endif
311
312    return __fread_chk(buf, size, count, stream, bos);
313}
314
315__BIONIC_FORTIFY_INLINE
316size_t fwrite(const void * __restrict buf, size_t size, size_t count, FILE * __restrict stream) {
317    size_t bos = __bos0(buf);
318
319#if !defined(__clang__)
320    if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
321        return __fwrite_real(buf, size, count, stream);
322    }
323
324    if (__builtin_constant_p(size) && __builtin_constant_p(count)) {
325        size_t total;
326        if (__size_mul_overflow(size, count, &total)) {
327            __fwrite_overflow();
328        }
329
330        if (total > bos) {
331            __fwrite_too_big_error();
332        }
333
334        return __fwrite_real(buf, size, count, stream);
335    }
336#endif
337
338    return __fwrite_chk(buf, size, count, stream, bos);
339}
340
341#if !defined(__clang__)
342
343__BIONIC_FORTIFY_INLINE
344char *fgets(char* dest, int size, FILE* stream) {
345    size_t bos = __bos(dest);
346
347    // Compiler can prove, at compile time, that the passed in size
348    // is always negative. Force a compiler error.
349    if (__builtin_constant_p(size) && (size < 0)) {
350        __fgets_too_small_error();
351    }
352
353    // Compiler doesn't know destination size. Don't call __fgets_chk
354    if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
355        return __fgets_real(dest, size, stream);
356    }
357
358    // Compiler can prove, at compile time, that the passed in size
359    // is always <= the actual object size. Don't call __fgets_chk
360    if (__builtin_constant_p(size) && (size <= (int) bos)) {
361        return __fgets_real(dest, size, stream);
362    }
363
364    // Compiler can prove, at compile time, that the passed in size
365    // is always > the actual object size. Force a compiler error.
366    if (__builtin_constant_p(size) && (size > (int) bos)) {
367        __fgets_too_big_error();
368    }
369
370    return __fgets_chk(dest, size, stream, bos);
371}
372
373#endif /* !defined(__clang__) */
374
375#endif /* defined(__BIONIC_FORTIFY) */
376
377__END_DECLS
378
379#endif /* _STDIO_H_ */
380