Debian Codegraph @

share/libc/stdio.h

raw
1 /**************************************************************************/
2 /* */
3 /* This file is part of Frama-C. */
4 /* */
5 /* Copyright (C) 2007-2022 */
6 /* CEA (Commissariat à l'énergie atomique et aux énergies */
7 /* alternatives) */
8 /* */
9 /* you can redistribute it and/or modify it under the terms of the GNU */
10 /* Lesser General Public License as published by the Free Software */
11 /* Foundation, version 2.1. */
12 /* */
13 /* It is distributed in the hope that it will be useful, */
14 /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
15 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
16 /* GNU Lesser General Public License for more details. */
17 /* */
18 /* See the GNU Lesser General Public License version 2.1 */
19 /* for more details (enclosed in the file licenses/LGPLv2.1). */
20 /* */
21 /**************************************************************************/
22
23 /* ISO C: 7.19 */
24 #ifndef __FC_STDIO
25 #define __FC_STDIO
26 #include "features.h"
27 __PUSH_FC_STDLIB
28 #include "__fc_machdep.h"
29 #include "__fc_string_axiomatic.h"
30 #include "stdarg.h"
31 #include "stddef.h"
32 #include "errno.h"
33 #include "__fc_define_stat.h"
34 #include "__fc_define_fpos_t.h"
35 #include "__fc_define_file.h"
36 #include "__fc_define_null.h"
37 #include "__fc_define_eof.h"
38 #include "__fc_define_ssize_t.h"
39
40 #define _IOFBF 0
41 #define _IOLBF 1
42 #define _IONBF 2
43
44 #define BUFSIZ __FC_BUFSIZ
45 #define FOPEN_MAX __FC_FOPEN_MAX
46 #define FILENAME_MAX __FC_FILENAME_MAX
47 #ifndef __FC_L_tmpnam
48 #error machdep should have defined __FC_L_tmpnam!
49 #endif
50 #define L_tmpnam __FC_L_tmpnam
51
52 #include "__fc_define_seek_macros.h"
53
54 #define TMP_MAX __FC_TMP_MAX
55
56 __BEGIN_DECLS
57
58 extern FILE * __fc_stderr;
59 #define stderr (__fc_stderr)
60
61 extern FILE * __fc_stdin;
62 #define stdin (__fc_stdin)
63
64 extern FILE * __fc_stdout;
65 #define stdout (__fc_stdout)
66
67 /*
68 Note: currently some functions only consider the __fc_FILE_id field of FILE.
69 This models the fact that operations on different files are considered
70 non-interferent between them.
71 */
72
73 /*@ // missing: assigns 'filesystem' \from filename[0..];
74 // missing: assigns errno one of several possible values
75 requires valid_filename: valid_read_string(filename);
76 assigns \result \from indirect:filename[0..strlen(filename)];
77 ensures result_ok_or_error: \result == 0 || \result == -1;
78 */
79 extern int remove(const char *filename);
80
81 /*@ // missing: assigns 'filesystem' \from old_name[0..], new_name[0..];
82 // missing: assigns errno one of 21 different possible values
83 requires valid_old_name: valid_read_string(old_name);
84 requires valid_new_name: valid_read_string(new_name);
85 assigns \result \from indirect:old_name[0..strlen(old_name)],
86 indirect:new_name[0..strlen(new_name)];
87 ensures result_ok_or_error: \result == 0 || \result == -1;
88 */
89 extern int rename(const char *old_name, const char *new_name);
90
91 FILE __fc_fopen[__FC_FOPEN_MAX];
92 FILE* const __fc_p_fopen = __fc_fopen;
93
94 /*@
95 assigns \result \from __fc_p_fopen;
96 ensures result_null_or_valid_fd:
97 \result == \null || (\subset(\result,&__fc_fopen[0 .. __FC_FOPEN_MAX-1]));
98 */
99 extern FILE *tmpfile(void);
100
101 char __fc_tmpnam[L_tmpnam];
102 char * const __fc_p_tmpnam = __fc_tmpnam;
103
104 /*@
105 // Note: the tmpnam example in POSIX uses an array of size L_tmpnam+1
106 // missing: assigns __fc_p_tmpnam[0..L_tmpnam] \from 'PRNG and internal state'
107 // missing: if called more than TMP_MAX, behavior is implementation-defined
108 requires valid_s_or_null: s == \null || \valid(s+(0 .. L_tmpnam));
109 assigns __fc_p_tmpnam[0 .. L_tmpnam] \from __fc_p_tmpnam[0 .. L_tmpnam],
110 indirect:s;
111 assigns s[0 .. L_tmpnam] \from indirect:s, __fc_p_tmpnam[0 .. L_tmpnam];
112 assigns \result \from s, __fc_p_tmpnam;
113 ensures result_string_or_null: \result == \null || \result == s ||
114 \result == __fc_p_tmpnam;
115 */
116 extern char *tmpnam(char *s);
117
118 /*@
119 // missing: assigns errno
120 requires valid_stream: \valid(stream);
121 assigns \result \from indirect:stream, indirect:*stream;
122 ensures result_zero_or_EOF: \result == 0 || \result == EOF;
123 */
124 extern int fclose(FILE *stream);
125
126 /*@
127 // missing: assigns errno
128 requires null_or_valid_stream: stream == \null || \valid_read(stream);
129 ensures result_zero_or_EOF: \result == 0 || \result == EOF;
130 assigns \result
131 \from indirect:*stream, indirect:__fc_fopen[0 .. __FC_FOPEN_MAX-1];
132 assigns *stream, __fc_fopen[0 .. __FC_FOPEN_MAX-1]
133 \from indirect:stream, *stream,
134 __fc_fopen[0 .. __FC_FOPEN_MAX-1]; // may flush ALL open streams
135 behavior flush_all:
136 assumes all_streams: stream == \null;
137 assigns __fc_fopen[0 .. __FC_FOPEN_MAX-1]
138 \from __fc_fopen[0 .. __FC_FOPEN_MAX-1]; // flush ALL open streams
139 assigns \result \from indirect:__fc_fopen[0 .. __FC_FOPEN_MAX-1];
140 behavior flush_stream:
141 assumes single_stream: stream != \null;
142 assigns *stream \from *stream;
143 assigns \result \from indirect:*stream;
144 complete behaviors;
145 disjoint behaviors;
146 */
147 extern int fflush(FILE *stream);
148
149 /*@
150 requires valid_filename: valid_read_string(filename);
151 requires valid_mode: valid_read_string(mode);
152 assigns \result \from indirect:filename[0..strlen(filename)],
153 indirect:mode[0..strlen(mode)], __fc_p_fopen;
154 ensures result_null_or_valid_fd:
155 \result==\null || (\subset(\result,&__fc_fopen[0 .. __FC_FOPEN_MAX-1])) ;
156 */
157 extern FILE *fopen(const char * restrict filename,
158 const char * restrict mode);
159
160 /*@
161 requires valid_mode: valid_read_string(mode);
162 assigns \result, __fc_fopen[fd] \from indirect:fd,
163 indirect:mode[0..strlen(mode)],
164 indirect:__fc_fopen[fd], __fc_p_fopen;
165 ensures result_null_or_valid_fd:
166 \result == \null || (\subset(\result,&__fc_fopen[0 .. __FC_FOPEN_MAX-1])) ;
167 */
168 extern FILE *fdopen(int fd, const char *mode);
169
170 /*@
171 requires valid_filename: valid_read_string(filename);
172 requires valid_mode: valid_read_string(mode);
173 requires valid_stream: \valid(stream);
174 assigns \result \from indirect:filename[..], indirect:mode[..], __fc_p_fopen,
175 indirect:stream;
176 assigns *stream \from indirect:filename[..], indirect:mode[..], __fc_p_fopen,
177 indirect:stream;
178 ensures result_null_or_valid_fd:
179 \result==\null || \result \in &__fc_fopen[0 .. __FC_FOPEN_MAX-1];
180 ensures stream_opened:
181 *stream \in __fc_fopen[0 .. __FC_FOPEN_MAX-1];
182 */
183 extern FILE *freopen(const char * restrict filename,
184 const char * restrict mode,
185 FILE * restrict stream);
186
187 /*@ assigns *stream \from buf; */
188 extern void setbuf(FILE * restrict stream,
189 char * restrict buf);
190
191 /*@ assigns *stream \from buf,mode,size; */
192 extern int setvbuf(FILE * restrict stream,
193 char * restrict buf,
194 int mode, size_t size);
195
196 /*@
197 // Axiomatic used by the Variadic plugin to generate specifications
198 // for some functions, e.g. snprintf().
199 axiomatic format_length {
200 //TODO: this logic function will be extended to handle variadic formats
201 logic integer format_length{L}(char *format);
202 }
203 */
204
205 // Direct specifications for variadic functions are unsupported;
206 // use the Variadic plug-in instead.
207 extern int fprintf(FILE * restrict stream,
208 const char * restrict format, ...);
209 extern int fscanf(FILE * restrict stream,
210 const char * restrict format, ...);
211 extern int printf(const char * restrict format, ...);
212 extern int scanf(const char * restrict format, ...);
213 extern int snprintf(char * restrict s, size_t n,
214 const char * restrict format, ...);
215 extern int sprintf(char * restrict s,
216 const char * restrict format, ...);
217 extern int sscanf(const char * restrict s,
218 const char * restrict format, ...);
219
220 /*@ assigns *stream \from format[..], arg; */
221 extern int vfprintf(FILE * restrict stream,
222 const char * restrict format,
223 va_list arg);
224
225 /*@ assigns *stream \from format[..], *stream;
226 // TODO: assign arg too. */
227 extern int vfscanf(FILE * restrict stream,
228 const char * restrict format,
229 va_list arg);
230
231 /*@ assigns *__fc_stdout \from arg; */
232 extern int vprintf(const char * restrict format,
233 va_list arg);
234
235 /*@ assigns *__fc_stdin \from format[..];
236 // TODO: assign arg too. */
237 extern int vscanf(const char * restrict format,
238 va_list arg);
239
240 /*@ assigns s[0..n-1] \from format[..], arg;
241 */
242 extern int vsnprintf(char * restrict s, size_t n,
243 const char * restrict format,
244 va_list arg);
245
246 /*@ assigns s[0..] \from format[..], arg;
247 */
248 extern int vsprintf(char * restrict s,
249 const char * restrict format,
250 va_list arg);
251
252 /* TODO: assigns arg ; */
253 extern int vsscanf(const char * restrict s,
254 const char * restrict format,
255 va_list arg);
256
257 /*@
258 requires valid_stream: \valid(stream);
259 assigns *stream \from *stream;
260 assigns \result \from indirect:*stream;
261 ensures result_uchar_or_eof: 0 <= \result <= __FC_UCHAR_MAX || \result == EOF;
262 */
263 extern int fgetc(FILE *stream);
264
265 /*@
266 requires valid_stream: \valid(stream);
267 requires room_s: \valid(s+(0..size-1));
268 assigns s[0..size-1] \from indirect:size, indirect:*stream;
269 assigns \result \from s, indirect:size, indirect:*stream;
270 ensures result_null_or_same: \result == \null || \result == s;
271 ensures initialization:at_least_one:\result != \null ==> \initialized(&s[0]);
272 // the return value does not tell how many characters were written,
273 // so we can only ensure the first one was initialized
274 ensures terminated_string_on_success:
275 \result != \null ==> valid_string(s);
276 */
277 extern char *fgets(char * restrict s, int size,
278 FILE * restrict stream);
279
280 /*@
281 requires valid_stream: \valid(stream);
282 assigns *stream \from c, *stream;
283 assigns \result \from indirect:*stream;
284 */
285 extern int fputc(int c, FILE *stream);
286
287 /*@
288 requires valid_string_s: valid_read_string(s);
289 assigns *stream \from s[0..strlen(s)], *stream;
290 assigns \result \from indirect:s[0..strlen(s)], indirect:*stream;
291 */
292 extern int fputs(const char * restrict s,
293 FILE * restrict stream);
294
295 /*@
296 requires valid_stream: \valid(stream);
297 assigns \result, *stream \from *stream;
298 */
299 extern int getc(FILE *stream);
300
301 /*@
302 assigns \result, *__fc_stdin \from *__fc_stdin;
303 */
304 extern int getchar(void);
305
306 // Number of characters that will read by gets()
307 /*@
308 axiomatic GetsLength {
309 logic size_t gets_length{L} reads *__fc_stdin;
310 }
311 */
312
313 /*@
314 requires room_s: \valid(s+(0..gets_length));
315 assigns s[0..gets_length] \from *__fc_stdin ;
316 assigns \result \from s, *__fc_stdin;
317 assigns *__fc_stdin \from *__fc_stdin;
318 ensures result_null_or_same: \result == s || \result == \null;
319 */
320 extern char *gets(char *s);
321
322 /*@
323 requires valid_stream: \valid(stream);
324 assigns *stream \from c, *stream;
325 assigns \result \from indirect:*stream;
326 */
327 extern int putc(int c, FILE *stream);
328
329 /*@
330 assigns *__fc_stdout \from c, *__fc_stdout;
331 assigns \result \from indirect:*__fc_stdout;
332 */
333 extern int putchar(int c);
334
335 /*@
336 requires valid_string_s: valid_read_string(s);
337 assigns *__fc_stdout \from s[0..strlen(s)], *__fc_stdout;
338 assigns \result \from indirect:s[0..strlen(s)], indirect:*__fc_stdout;
339 */
340 extern int puts(const char *s);
341
342 /*@
343 requires valid_stream: \valid(stream);
344 assigns *stream \from indirect:c;
345 assigns \result \from indirect:c, indirect:*stream;
346 ensures result_ok_or_error: \result == c || \result == EOF;
347 */
348 extern int ungetc(int c, FILE *stream);
349
350 /*@
351 requires valid_ptr_block: \valid(((char*)ptr)+(0..(nmemb*size)-1));
352 requires valid_stream: \valid(stream);
353 assigns *(((char*)ptr)+(0..(nmemb*size)-1)), *stream
354 \from indirect:size, indirect:nmemb, indirect:*stream;
355 assigns \result \from size, indirect:*stream;
356 ensures size_read: \result <= nmemb;
357 ensures initialization: \initialized(((char*)ptr)+(0..(\result*size)-1));
358 */
359 extern size_t fread(void * restrict ptr,
360 size_t size, size_t nmemb,
361 FILE * restrict stream);
362
363 /*@
364 requires valid_ptr_block: \valid_read(((char*)ptr)+(0..(nmemb*size)-1));
365 requires valid_stream: \valid(stream);
366 assigns *stream, \result \from indirect:*(((char*)ptr)+(0..(nmemb*size)-1));
367 ensures size_written: \result <= nmemb;
368 */
369 extern size_t fwrite(const void * restrict ptr,
370 size_t size, size_t nmemb,
371 FILE * restrict stream);
372
373 /*@
374 //missing: assigns errno (EBADF, EOVERFLOW, ESPIPE);
375 requires valid_stream: \valid(stream);
376 requires valid_pos: \valid(pos);
377 assigns \result, *pos \from indirect:*stream;
378 ensures initialization:pos: \initialized(pos);
379 */
380 extern int fgetpos(FILE * restrict stream,
381 fpos_t * restrict pos);
382
383 /*@
384 requires valid_stream: \valid(stream);
385 requires whence_enum: whence == SEEK_SET || whence == SEEK_CUR || whence == SEEK_END;
386 assigns *stream \from *stream, indirect:offset, indirect:whence;
387 assigns \result, __fc_errno \from indirect:*stream, indirect:offset,
388 indirect:whence;
389 ensures errno_set: __fc_errno \in {EAGAIN, EBADF, EFBIG, EINTR, EINVAL, EIO,
390 ENOSPC, EOVERFLOW, EPIPE, ESPIPE, ENXIO};
391 */
392 extern int fseek(FILE *stream, long int offset, int whence);
393
394 /*@
395 requires valid_stream: \valid(stream);
396 requires whence_enum: whence == SEEK_SET || whence == SEEK_CUR || whence == SEEK_END;
397 assigns *stream \from *stream, indirect:offset, indirect:whence;
398 assigns \result, __fc_errno \from indirect:*stream, indirect:offset,
399 indirect:whence;
400 ensures errno_set: __fc_errno \in {EAGAIN, EBADF, EFBIG, EINTR, EINVAL, EIO,
401 ENOSPC, EOVERFLOW, EPIPE, ESPIPE, ENXIO};
402 */
403 extern int fseeko(FILE *stream, off_t offset, int whence);
404
405 /*@
406 requires valid_stream: \valid(stream);
407 requires valid_pos: \valid_read(pos);
408 requires initialization:pos: \initialized(pos);
409 assigns *stream \from *pos;
410 assigns \result \from indirect:*stream, indirect:*pos;
411 assigns __fc_errno \from __fc_errno, indirect:*stream, indirect:*pos;
412 ensures errno_set: __fc_errno \in {EAGAIN, EBADF, EFBIG, EINTR, EIO,
413 ENOSPC, EPIPE, ESPIPE, ENXIO};
414 */
415 extern int fsetpos(FILE *stream, const fpos_t *pos);
416
417 /*@ // missing: assigns errno: EBADF, EOVERFLOW, ESPIPE
418 requires valid_stream: \valid(stream);
419 assigns \result, __fc_errno \from indirect:*stream ;
420 ensures success_or_error:
421 \result == -1 || (\result >= 0 && __fc_errno == \old(__fc_errno));
422 ensures errno_set: __fc_errno \in {EBADF, EOVERFLOW, ESPIPE};
423 */
424 extern long int ftell(FILE *stream);
425
426 /*@ // missing: assigns errno: EBADF, EOVERFLOW, ESPIPE
427 requires valid_stream: \valid(stream);
428 assigns \result, __fc_errno \from indirect:*stream ;
429 ensures success_or_error:
430 \result == -1 || (\result >= 0 && __fc_errno == \old(__fc_errno));
431 ensures errno_set: __fc_errno \in {EBADF, EOVERFLOW, ESPIPE};
432 */
433 extern off_t ftello(FILE *stream);
434
435 /*@
436 requires valid_stream: \valid(stream);
437 assigns *stream \from \nothing;
438 */
439 extern void rewind(FILE *stream);
440
441 /*@
442 requires valid_stream: \valid(stream);
443 assigns *stream \from \nothing;
444 */
445 extern void clearerr(FILE *stream);
446
447 /*@
448 requires valid_stream: \valid(stream);
449 assigns \result \from indirect:*stream;
450 */
451 extern int feof(FILE *stream);
452
453 /*@
454 requires valid_stream: \valid(stream);
455 assigns \result \from indirect:*stream;
456 */
457 extern int fileno(FILE *stream);
458
459 /*@
460 requires valid_stream: \valid(stream);
461 assigns *stream \from \nothing;
462 */
463 extern void flockfile(FILE *stream);
464
465 /*@
466 requires valid_stream: \valid(stream);
467 assigns *stream \from \nothing;
468 */
469 extern void funlockfile(FILE *stream);
470
471 /*@
472 requires valid_stream: \valid(stream);
473 assigns \result,*stream \from \nothing;
474 */
475 extern int ftrylockfile(FILE *stream);
476
477 /*@
478 requires valid_stream: \valid(stream);
479 assigns \result \from indirect:*stream;
480 */
481 extern int ferror(FILE *stream);
482
483 /*@
484 requires valid_string_s: valid_read_string(s);
485 assigns __fc_stdout \from __fc_errno, s[0..strlen(s)];
486 */
487 extern void perror(const char *s);
488
489 /*@
490 requires valid_stream: \valid(stream);
491 assigns \result,*stream \from *stream;
492 */
493 extern int getc_unlocked(FILE *stream);
494
495 /*@
496 assigns \result \from *__fc_stdin;
497 */
498 extern int getchar_unlocked(void);
499
500 /*@
501 requires valid_stream: \valid(stream);
502 assigns *stream \from c;
503 assigns \result \from indirect:*stream;
504 */
505 extern int putc_unlocked(int c, FILE *stream);
506
507 /*@
508 assigns *__fc_stdout \from c;
509 assigns \result \from indirect:*__fc_stdout;
510 */
511 extern int putchar_unlocked(int c);
512
513 /*@
514 requires valid_stream: \valid(stream);
515 assigns *stream \from \nothing;
516 */
517 extern void clearerr_unlocked(FILE *stream);
518
519 /*@
520 requires valid_stream: \valid(stream);
521 assigns \result \from indirect:*stream;
522 */
523 extern int feof_unlocked(FILE *stream);
524
525 /*@
526 requires valid_stream: \valid(stream);
527 assigns \result \from indirect:*stream;
528 */
529 extern int ferror_unlocked(FILE *stream);
530
531 /*@
532 requires valid_stream: \valid(stream);
533 assigns \result \from indirect:*stream;
534 */
535 extern int fileno_unlocked(FILE *stream);
536
537 extern int fflush_unlocked(FILE *stream);
538 extern int fgetc_unlocked(FILE *stream);
539 extern int fputc_unlocked(int c, FILE *stream);
540 extern size_t fread_unlocked(void *ptr, size_t size, size_t n,
541 FILE *stream);
542 extern size_t fwrite_unlocked(const void *ptr, size_t size, size_t n,
543 FILE *stream);
544
545 extern char *fgets_unlocked(char *s, int n, FILE *stream);
546 extern int fputs_unlocked(const char *s, FILE *stream);
547
548 extern int dprintf(int fd, const char *restrict format, ...);
549
550 /*@ axiomatic pipe_streams {
551 predicate is_open_pipe{L}(FILE *stream); // is stream an open pipe?
552 // Logic label L is not used, but must be present because the
553 // predicate depends on the memory state
554 }
555 */
556
557 /*@
558 requires valid_command: valid_read_string(command);
559 requires valid_type: valid_read_string(type);
560 assigns \result \from indirect:*command, indirect:*type,
561 __fc_p_fopen;
562 assigns __fc_fopen[0..] \from indirect:*command, indirect:*type,
563 __fc_fopen[0..];
564 ensures result_error_or_valid_open_pipe:
565 \result == \null ||
566 (\subset(\result,&__fc_fopen[0 .. __FC_FOPEN_MAX-1]) &&
567 is_open_pipe(\result));
568 */
569 extern FILE *popen(const char *command, const char *type);
570
571 /*@
572 requires valid_stream: \valid(stream);
573 requires open_pipe: is_open_pipe(stream);
574 assigns \result \from indirect:*stream;
575 ensures closed_stream: !is_open_pipe(stream);
576 */
577 extern int pclose(FILE *stream);
578
579 // This file may be included by non-POSIX machdeps, which do not define
580 // ssize_t, so we must check it
581 #ifdef __FC_POSIX_VERSION
582 // No specification given; include "stdio.c" to use Frama-C's implementation
583 ssize_t getline(char **lineptr, size_t *n, FILE *stream);
584 #endif
585
586 // POSIX extension
587 /*@
588 requires valid_or_null_buff: buf == \null || \valid((char*)buf + (0 .. size-1));
589 requires valid_mode: valid_read_string(mode);
590 assigns __fc_errno \from indirect: buf, indirect: size,
591 indirect: mode[0..strlen(mode)];
592 assigns \result \from __fc_p_fopen,
593 indirect: buf, indirect: size, indirect: mode[0..strlen(mode)];
594 ensures result_error_or_valid:
595 \result == \null || \result \in &__fc_fopen[0 .. __FC_FOPEN_MAX-1];
596 ensures errno_set:
597 __fc_errno == \old(errno) ||
598 __fc_errno \in {EINVAL, EMFILE, ENOMEM};
599 allocates buf;
600 */
601 extern FILE *fmemopen(void *restrict buf, size_t size,
602 const char *restrict mode);
603
604 // Non-POSIX; allocates memory, so requires 'stdio.c' to be included
605
606 #include "__fc_alloc_axiomatic.h"
607
608 /*@
609 requires valid_strp: \valid(strp);
610 requires valid_fmt: valid_read_string(fmt);
611 assigns __fc_heap_status \from indirect:fmt[0 ..], __fc_heap_status;
612 assigns \result \from indirect:fmt[0 ..], indirect:__fc_heap_status;
613 assigns *strp \from fmt[0 ..], indirect:__fc_heap_status;
614 //missing: variadic arguments
615 ensures result_error_or_written_byes: \result == -1 || \result >= 0;
616 allocates *strp;
617 */
618 int asprintf(char **strp, const char *fmt, ...);
619
620 __END_DECLS
621
622 #define IOV_MAX 1024
623
624 __POP_FC_STDLIB
625 #endif