• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
52 typedef off_t fpos_t;
53 typedef off64_t fpos64_t;
54 
55 struct __sFILE;
56 typedef struct __sFILE FILE;
57 
58 extern FILE* stdin;
59 extern FILE* stdout;
60 extern FILE* stderr;
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 /* System V/ANSI C; this is the wrong way to do this, do *not* use these. */
92 #if __BSD_VISIBLE || __XPG_VISIBLE
93 #define	P_tmpdir	"/tmp/"
94 #endif
95 #define	L_tmpnam	1024	/* XXX must be == PATH_MAX */
96 #define	TMP_MAX		308915776
97 
98 #define SEEK_SET 0
99 #define SEEK_CUR 1
100 #define SEEK_END 2
101 
102 /*
103  * Functions defined in ANSI C standard.
104  */
105 void	 clearerr(FILE *);
106 int	 fclose(FILE *);
107 int	 feof(FILE *);
108 int	 ferror(FILE *);
109 int	 fflush(FILE *);
110 int	 fgetc(FILE *);
111 char	*fgets(char * __restrict, int, FILE * __restrict);
112 int	 fprintf(FILE * __restrict , const char * __restrict, ...)
113 		__printflike(2, 3);
114 int	 fputc(int, FILE *);
115 int	 fputs(const char * __restrict, FILE * __restrict);
116 size_t	 fread(void * __restrict, size_t, size_t, FILE * __restrict);
117 int	 fscanf(FILE * __restrict, const char * __restrict, ...)
118 		__scanflike(2, 3);
119 size_t	 fwrite(const void * __restrict, size_t, size_t, FILE * __restrict);
120 int	 getc(FILE *);
121 int	 getchar(void);
122 ssize_t	 getdelim(char ** __restrict, size_t * __restrict, int,
123 	    FILE * __restrict);
124 ssize_t	 getline(char ** __restrict, size_t * __restrict, FILE * __restrict);
125 
126 void	 perror(const char *);
127 int	 printf(const char * __restrict, ...)
128 		__printflike(1, 2);
129 int	 putc(int, FILE *);
130 int	 putchar(int);
131 int	 puts(const char *);
132 int	 remove(const char *);
133 void	 rewind(FILE *);
134 int	 scanf(const char * __restrict, ...)
135 		__scanflike(1, 2);
136 void	 setbuf(FILE * __restrict, char * __restrict);
137 int	 setvbuf(FILE * __restrict, char * __restrict, int, size_t);
138 int	 sscanf(const char * __restrict, const char * __restrict, ...)
139 		__scanflike(2, 3);
140 int	 ungetc(int, FILE *);
141 int	 vfprintf(FILE * __restrict, const char * __restrict, __va_list)
142 		__printflike(2, 0);
143 int	 vprintf(const char * __restrict, __va_list)
144 		__printflike(1, 0);
145 
146 int dprintf(int, const char * __restrict, ...) __printflike(2, 3);
147 int vdprintf(int, const char * __restrict, __va_list) __printflike(2, 0);
148 
149 #ifndef __AUDIT__
150 #if !defined(__STDC_VERSION__) || __STDC_VERSION__ < 201112L
151 char* gets(char*) __attribute__((deprecated("gets is unsafe, use fgets instead")));
152 #endif
153 int sprintf(char* __restrict, const char* __restrict, ...)
154     __printflike(2, 3) __warnattr("sprintf is often misused; please use snprintf");
155 int vsprintf(char* __restrict, const char* __restrict, __va_list)
156     __printflike(2, 0) __warnattr("vsprintf is often misused; please use vsnprintf");
157 char* tmpnam(char*) __attribute__((deprecated("tmpnam is unsafe, use mkstemp or tmpfile instead")));
158 #if __XPG_VISIBLE
159 char* tempnam(const char*, const char*)
160     __attribute__((deprecated("tempnam is unsafe, use mkstemp or tmpfile instead")));
161 #endif
162 #endif
163 
164 int rename(const char*, const char*);
165 int renameat(int, const char*, int, const char*);
166 
167 int fseek(FILE*, long, int);
168 long ftell(FILE*);
169 
170 #if defined(__USE_FILE_OFFSET64)
171 int fgetpos(FILE*, fpos_t*) __RENAME(fgetpos64);
172 int fsetpos(FILE*, const fpos_t*) __RENAME(fsetpos64);
173 int fseeko(FILE*, off_t, int) __RENAME(fseeko64);
174 off_t ftello(FILE*) __RENAME(ftello64);
175 #  if defined(__USE_BSD)
176 FILE* funopen(const void*,
177               int (*)(void*, char*, int),
178               int (*)(void*, const char*, int),
179               fpos_t (*)(void*, fpos_t, int),
180               int (*)(void*)) __RENAME(funopen64);
181 #  endif
182 #else
183 int fgetpos(FILE*, fpos_t*);
184 int fsetpos(FILE*, const fpos_t*);
185 int fseeko(FILE*, off_t, int);
186 off_t ftello(FILE*);
187 #  if defined(__USE_BSD)
188 FILE* funopen(const void*,
189               int (*)(void*, char*, int),
190               int (*)(void*, const char*, int),
191               fpos_t (*)(void*, fpos_t, int),
192               int (*)(void*));
193 #  endif
194 #endif
195 int fgetpos64(FILE*, fpos64_t*);
196 int fsetpos64(FILE*, const fpos64_t*);
197 int fseeko64(FILE*, off64_t, int);
198 off64_t ftello64(FILE*);
199 #if defined(__USE_BSD)
200 FILE* funopen64(const void*,
201                 int (*)(void*, char*, int),
202                 int (*)(void*, const char*, int),
203                 fpos64_t (*)(void*, fpos64_t, int),
204                 int (*)(void*));
205 #endif
206 
207 FILE* fopen(const char* __restrict, const char* __restrict);
208 FILE* fopen64(const char* __restrict, const char* __restrict);
209 FILE* freopen(const char* __restrict, const char* __restrict, FILE* __restrict);
210 FILE* freopen64(const char* __restrict, const char* __restrict, FILE* __restrict);
211 FILE* tmpfile(void);
212 FILE* tmpfile64(void);
213 
214 #if __ISO_C_VISIBLE >= 1999 || __BSD_VISIBLE
215 int	 snprintf(char * __restrict, size_t, const char * __restrict, ...)
216 		__printflike(3, 4);
217 int	 vfscanf(FILE * __restrict, const char * __restrict, __va_list)
218 		__scanflike(2, 0);
219 int	 vscanf(const char *, __va_list)
220 		__scanflike(1, 0);
221 int	 vsnprintf(char * __restrict, size_t, const char * __restrict, __va_list)
222 		__printflike(3, 0);
223 int	 vsscanf(const char * __restrict, const char * __restrict, __va_list)
224 		__scanflike(2, 0);
225 #endif /* __ISO_C_VISIBLE >= 1999 || __BSD_VISIBLE */
226 
227 /*
228  * Functions defined in POSIX 1003.1.
229  */
230 #if __BSD_VISIBLE || __POSIX_VISIBLE || __XPG_VISIBLE
231 #define	L_ctermid	1024	/* size for ctermid(); PATH_MAX */
232 
233 FILE	*fdopen(int, const char *);
234 int	 fileno(FILE *);
235 
236 #if (__POSIX_VISIBLE >= 199209)
237 int	 pclose(FILE *);
238 FILE	*popen(const char *, const char *);
239 #endif
240 
241 #if __POSIX_VISIBLE >= 199506
242 void	 flockfile(FILE *);
243 int	 ftrylockfile(FILE *);
244 void	 funlockfile(FILE *);
245 
246 /*
247  * These are normally used through macros as defined below, but POSIX
248  * requires functions as well.
249  */
250 int	 getc_unlocked(FILE *);
251 int	 getchar_unlocked(void);
252 int	 putc_unlocked(int, FILE *);
253 int	 putchar_unlocked(int);
254 #endif /* __POSIX_VISIBLE >= 199506 */
255 
256 #if __POSIX_VISIBLE >= 200809
257 FILE* fmemopen(void*, size_t, const char*);
258 FILE* open_memstream(char**, size_t*);
259 #endif /* __POSIX_VISIBLE >= 200809 */
260 
261 #endif /* __BSD_VISIBLE || __POSIX_VISIBLE || __XPG_VISIBLE */
262 
263 /*
264  * Routines that are purely local.
265  */
266 #if __BSD_VISIBLE
267 int	 asprintf(char ** __restrict, const char * __restrict, ...)
268 		__printflike(2, 3);
269 char	*fgetln(FILE * __restrict, size_t * __restrict);
270 int	 fpurge(FILE *);
271 void	 setbuffer(FILE *, char *, int);
272 int	 setlinebuf(FILE *);
273 int	 vasprintf(char ** __restrict, const char * __restrict,
274     __va_list)
275 		__printflike(2, 0);
276 
277 void clearerr_unlocked(FILE*);
278 int feof_unlocked(FILE*);
279 int ferror_unlocked(FILE*);
280 int fileno_unlocked(FILE*);
281 
282 #define fropen(cookie, fn) funopen(cookie, fn, 0, 0, 0)
283 #define fwopen(cookie, fn) funopen(cookie, 0, fn, 0, 0)
284 #endif /* __BSD_VISIBLE */
285 
286 extern char* __fgets_chk(char*, int, FILE*, size_t);
287 extern char* __fgets_real(char*, int, FILE*) __RENAME(fgets);
288 __errordecl(__fgets_too_big_error, "fgets called with size bigger than buffer");
289 __errordecl(__fgets_too_small_error, "fgets called with size less than zero");
290 
291 extern size_t __fread_chk(void * __restrict, size_t, size_t, FILE * __restrict, size_t);
292 extern size_t __fread_real(void * __restrict, size_t, size_t, FILE * __restrict) __RENAME(fread);
293 __errordecl(__fread_too_big_error, "fread called with size * count bigger than buffer");
294 __errordecl(__fread_overflow, "fread called with overflowing size * count");
295 
296 extern size_t __fwrite_chk(const void * __restrict, size_t, size_t, FILE * __restrict, size_t);
297 extern size_t __fwrite_real(const void * __restrict, size_t, size_t, FILE * __restrict) __RENAME(fwrite);
298 __errordecl(__fwrite_too_big_error, "fwrite called with size * count bigger than buffer");
299 __errordecl(__fwrite_overflow, "fwrite called with overflowing size * count");
300 
301 #if defined(__BIONIC_FORTIFY)
302 
303 __BIONIC_FORTIFY_INLINE
304 __printflike(3, 0)
vsnprintf(char * dest,size_t size,const char * format,__va_list ap)305 int vsnprintf(char *dest, size_t size, const char *format, __va_list ap)
306 {
307     return __builtin___vsnprintf_chk(dest, size, 0, __bos(dest), format, ap);
308 }
309 
310 __BIONIC_FORTIFY_INLINE
311 __printflike(2, 0)
vsprintf(char * dest,const char * format,__va_list ap)312 int vsprintf(char *dest, const char *format, __va_list ap)
313 {
314     return __builtin___vsprintf_chk(dest, 0, __bos(dest), format, ap);
315 }
316 
317 #if defined(__clang__)
318   #if !defined(snprintf)
319     #define __wrap_snprintf(dest, size, ...) __builtin___snprintf_chk(dest, size, 0, __bos(dest), __VA_ARGS__)
320     #define snprintf(...) __wrap_snprintf(__VA_ARGS__)
321   #endif
322 #else
323 __BIONIC_FORTIFY_INLINE
324 __printflike(3, 4)
snprintf(char * dest,size_t size,const char * format,...)325 int snprintf(char *dest, size_t size, const char *format, ...)
326 {
327     return __builtin___snprintf_chk(dest, size, 0,
328         __bos(dest), format, __builtin_va_arg_pack());
329 }
330 #endif
331 
332 #if defined(__clang__)
333   #if !defined(sprintf)
334     #define __wrap_sprintf(dest, ...) __builtin___sprintf_chk(dest, 0, __bos(dest), __VA_ARGS__)
335     #define sprintf(...) __wrap_sprintf(__VA_ARGS__)
336   #endif
337 #else
338 __BIONIC_FORTIFY_INLINE
339 __printflike(2, 3)
sprintf(char * dest,const char * format,...)340 int sprintf(char *dest, const char *format, ...)
341 {
342     return __builtin___sprintf_chk(dest, 0,
343         __bos(dest), format, __builtin_va_arg_pack());
344 }
345 #endif
346 
347 __BIONIC_FORTIFY_INLINE
fread(void * __restrict buf,size_t size,size_t count,FILE * __restrict stream)348 size_t fread(void * __restrict buf, size_t size, size_t count, FILE * __restrict stream) {
349     size_t bos = __bos0(buf);
350 
351 #if !defined(__clang__)
352     if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
353         return __fread_real(buf, size, count, stream);
354     }
355 
356     if (__builtin_constant_p(size) && __builtin_constant_p(count)) {
357         size_t total;
358         if (__size_mul_overflow(size, count, &total)) {
359             __fread_overflow();
360         }
361 
362         if (total > bos) {
363             __fread_too_big_error();
364         }
365 
366         return __fread_real(buf, size, count, stream);
367     }
368 #endif
369 
370     return __fread_chk(buf, size, count, stream, bos);
371 }
372 
373 __BIONIC_FORTIFY_INLINE
fwrite(const void * __restrict buf,size_t size,size_t count,FILE * __restrict stream)374 size_t fwrite(const void * __restrict buf, size_t size, size_t count, FILE * __restrict stream) {
375     size_t bos = __bos0(buf);
376 
377 #if !defined(__clang__)
378     if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
379         return __fwrite_real(buf, size, count, stream);
380     }
381 
382     if (__builtin_constant_p(size) && __builtin_constant_p(count)) {
383         size_t total;
384         if (__size_mul_overflow(size, count, &total)) {
385             __fwrite_overflow();
386         }
387 
388         if (total > bos) {
389             __fwrite_too_big_error();
390         }
391 
392         return __fwrite_real(buf, size, count, stream);
393     }
394 #endif
395 
396     return __fwrite_chk(buf, size, count, stream, bos);
397 }
398 
399 #if !defined(__clang__)
400 
401 __BIONIC_FORTIFY_INLINE
fgets(char * dest,int size,FILE * stream)402 char *fgets(char* dest, int size, FILE* stream) {
403     size_t bos = __bos(dest);
404 
405     // Compiler can prove, at compile time, that the passed in size
406     // is always negative. Force a compiler error.
407     if (__builtin_constant_p(size) && (size < 0)) {
408         __fgets_too_small_error();
409     }
410 
411     // Compiler doesn't know destination size. Don't call __fgets_chk
412     if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
413         return __fgets_real(dest, size, stream);
414     }
415 
416     // Compiler can prove, at compile time, that the passed in size
417     // is always <= the actual object size. Don't call __fgets_chk
418     if (__builtin_constant_p(size) && (size <= (int) bos)) {
419         return __fgets_real(dest, size, stream);
420     }
421 
422     // Compiler can prove, at compile time, that the passed in size
423     // is always > the actual object size. Force a compiler error.
424     if (__builtin_constant_p(size) && (size > (int) bos)) {
425         __fgets_too_big_error();
426     }
427 
428     return __fgets_chk(dest, size, stream, bos);
429 }
430 
431 #endif /* !defined(__clang__) */
432 
433 #endif /* defined(__BIONIC_FORTIFY) */
434 
435 __END_DECLS
436 
437 #endif /* _STDIO_H_ */
438