Debian Codegraph @

share/libc/stdio.c

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 #include "__fc_builtin.h"
24 #include "stdbool.h"
25 #include "stdio.h"
26 #include "stdlib.h"
27 #include "stdint.h" // for SIZE_MAX
28 #include "sys/types.h" // for ssize_t
29 #include "errno.h"
30 __PUSH_FC_STDLIB
31
32 FILE __fc_initial_stdout = {.__fc_FILE_id=1};
33 FILE * __fc_stdout = &__fc_initial_stdout;
34
35 FILE __fc_initial_stderr = {.__fc_FILE_id=2};
36 FILE * __fc_stderr = &__fc_initial_stderr;
37
38 FILE __fc_initial_stdin = {.__fc_FILE_id=0};
39 FILE * __fc_stdin = &__fc_initial_stdin;
40
41 // Returns 1 iff mode contains a valid mode string for fopen() and
42 // related functions; that is, one of the following:
43 // "r","w","a","rb","wb","ab","r+","w+","a+",
44 // "rb+","r+b","wb+","w+b","ab+","a+b".
45 /*@
46 requires valid_mode: valid_read_string(mode);
47 */
48 static bool is_valid_mode(char const *mode) {
49 if (!(mode[0] != 'r' || mode[0] != 'w' || mode[0] != 'a')) return false;
50 // single-char mode string; ok
51 if (!mode[1]) return true;
52 // two- or three-char mode string
53 if (!(mode[1] != 'b' || mode[1] != '+')) return false;
54 // two-char mode string; ok
55 if (!mode[2]) return true;
56 if (mode[2] == mode[1] || !(mode[2] != 'b' || mode[2] != '+')) return false;
57 // a three-char mode string is ok; everything else is not
58 return !mode[3];
59 }
60
61 // inefficient but POSIX-conforming implementation of getline
62 ssize_t getline(char **lineptr, size_t *n, FILE *stream) {
63 if (!lineptr || !n || !stream) {
64 errno = EINVAL;
65 //TODO: set error indicator for stream
66 return -1;
67 }
68 if (ferror(stream) || feof(stream)) {
69 //TODO: set error indicator for stream
70 return -1;
71 }
72 if (!*lineptr || *n == 0) {
73 *lineptr = malloc(2);
74 if (!lineptr) {
75 errno = ENOMEM;
76 //TODO: set error indicator for stream
77 return -1;
78 }
79 *n = 2;
80 }
81 size_t cur = 0;
82 while (!ferror(stream) && !feof(stream)) {
83 while (cur < *n-1) {
84 char c = fgetc(stream);
85 if (c == EOF && cur == 0) {
86 // no characters were read
87 //TODO: set error indicator for stream
88 return -1;
89 }
90 if (c != EOF) (*lineptr)[cur++] = c;
91 if (c == '\n' || c == EOF) {
92 // finished reading a line or the file
93 (*lineptr)[cur] = '\0';
94 return cur;
95 }
96 }
97 // try to realloc larger buffer
98 if (*n == SSIZE_MAX) {
99 errno = EOVERFLOW;
100 //TODO: set error indicator for stream
101 return -1;
102 }
103 size_t new_size = *n+1;
104 *lineptr = realloc(*lineptr, new_size);
105 if (!*lineptr) {
106 // failed to realloc larger line
107 errno = ENOMEM;
108 //TODO: set error indicator for stream
109 return -1;
110 }
111 *n = new_size;
112 }
113 //TODO: set error indicator for stream
114 return -1;
115 }
116
117 // Non-POSIX; arbitrarily allocates between 1 and 256 bytes.
118 // This stub is unsound in the general case, but enough for
119 // many test cases.
120 int asprintf(char **strp, const char *fmt, ...) {
121 va_list args;
122 va_start(args, fmt);
123 size_t len = Frama_C_interval(1, 256);
124 *strp = malloc(len);
125 if (!*strp) {
126 va_end(args);
127 return -1;
128 }
129 // Emulate writing to the string
130 Frama_C_make_unknown(*strp, len - 1U);
131 (*strp)[len - 1U] = 0;
132 return len;
133 }
134
135 // TODO: this stub does not ensure that, when fclose is called on the
136 // stream, the memory allocated here will be freed.
137 // (there is currently no metadata field in FILE for this information).
138 FILE *fmemopen(void *restrict buf, size_t size,
139 const char *restrict mode) {
140 if (!is_valid_mode(mode)) {
141 errno = EINVAL;
142 return NULL;
143 }
144 if (!buf) {
145 if (size == 0) {
146 // Some implementations may not support this; non-deterministically
147 // return an error
148 if (Frama_C_interval(0, 1)) {
149 errno = EINVAL;
150 return NULL;
151 }
152 }
153 if (mode[1] != '+' && (mode[1] && mode[2] != '+')) {
154 // null buffer requires an update ('+') mode
155 errno = EINVAL;
156 return NULL;
157 }
158 buf = malloc(size);
159 if (!buf) {
160 errno = ENOMEM;
161 return NULL;
162 }
163 }
164 // Code to emulate a possible exhaustion of open streams; there is currently
165 // no metadata in the FILE structure to indicate when a stream is available.
166 if (Frama_C_interval(0, 1)) {
167 // emulate 'too many open streams'
168 errno = EMFILE;
169 return NULL;
170 }
171 return &__fc_fopen[Frama_C_interval(0, __FC_FOPEN_MAX-1)];
172 }
173
174 __POP_FC_STDLIB