Debian Codegraph @

share/libc/stdlib.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.20 */
24 #ifndef __FC_STDLIB
25 #define __FC_STDLIB
26 #include "features.h"
27 __PUSH_FC_STDLIB
28 #include "__fc_machdep.h"
29 #include "__fc_define_size_t.h"
30 #include "__fc_define_wchar_t.h"
31 #include "__fc_alloc_axiomatic.h"
32 #include "__fc_string_axiomatic.h"
33
34 __BEGIN_DECLS
35
36 #ifndef __div_t_defined
37 typedef struct __fc_div_t {
38 int quot; /* Quotient. */
39 int rem; /* Remainder. */
40 } div_t;
41 #define __div_t_defined
42 #endif
43
44 #ifndef __ldiv_t_defined
45 typedef struct __fc_ldiv_t {
46 long int quot; /* Quotient. */
47 long int rem; /* Remainder. */
48 } ldiv_t;
49 #define __ldiv_t_defined
50 #endif
51
52 #ifndef __lldiv_t_defined
53 typedef struct __fc_lldiv_t {
54 long long int quot; /* Quotient. */
55 long long int rem; /* Remainder. */
56 } lldiv_t;
57 #define __lldiv_t_defined
58 #endif
59
60 #include "__fc_define_null.h"
61
62 /* These could be customizable */
63 #define EXIT_FAILURE 1
64 #define EXIT_SUCCESS 0
65
66 #include "limits.h"
67
68 #define RAND_MAX __FC_RAND_MAX
69 #define MB_CUR_MAX __FC_MB_CUR_MAX
70
71 /*@
72 requires valid_nptr: \valid_read(nptr); // cannot be precise, valid_read_string too strong
73 assigns \result \from indirect:nptr, indirect:nptr[0 ..];
74 */
75 extern double atof(const char *nptr);
76
77 /*@
78 requires valid_nptr: \valid_read(nptr); // cannot be precise, valid_read_string too strong
79 assigns \result \from indirect:nptr, indirect:nptr[0 ..];
80 */
81 extern int atoi(const char *nptr);
82
83 /*@
84 requires valid_nptr: \valid_read(nptr); // cannot be precise, valid_read_string too strong
85 assigns \result \from indirect:nptr, indirect:nptr[0 ..];
86 */
87 extern long int atol(const char *nptr);
88
89 /*@
90 requires valid_nptr: \valid_read(nptr); // cannot be precise, valid_read_string too strong
91 assigns \result \from indirect:nptr, indirect:nptr[0 ..];
92 */
93 extern long long int atoll(const char *nptr);
94
95 /* See ISO C: 7.20.1.3 to complete these specifications */
96
97 /*@
98 requires valid_nptr: \valid_read(nptr); // cannot be precise, valid_read_string too strong
99 requires separation: \separated(nptr, endptr);
100 assigns \result \from indirect:nptr, indirect:nptr[0 ..];
101 assigns *endptr \from nptr, indirect:nptr[0 ..], indirect:endptr;
102 behavior no_storage:
103 assumes null_endptr: endptr == \null;
104 assigns \result \from indirect:nptr, indirect:nptr[0 ..];
105 behavior store_position:
106 assumes nonnull_endptr: endptr != \null;
107 requires valid_endptr: \valid(endptr);
108 assigns \result \from indirect:nptr, indirect:nptr[0 ..];
109 assigns *endptr \from nptr, indirect:nptr[0 ..], indirect:endptr;
110 ensures initialization: \initialized(endptr);
111 ensures valid_endptr: \valid_read(endptr);
112 ensures position_subset: \subset(*endptr, nptr + (0 ..));
113 complete behaviors;
114 disjoint behaviors;
115 */
116 extern double strtod(const char * restrict nptr,
117 char ** restrict endptr);
118
119 /*@
120 requires valid_nptr: \valid_read(nptr); // cannot be precise, valid_read_string too strong
121 requires separation: \separated(nptr, endptr);
122 assigns \result \from indirect:nptr, indirect:nptr[0 ..];
123 assigns *endptr \from nptr, indirect:nptr[0 ..], indirect:endptr;
124 behavior no_storage:
125 assumes null_endptr: endptr == \null;
126 assigns \result \from indirect:nptr, indirect:nptr[0 ..];
127 behavior store_position:
128 assumes nonnull_endptr: endptr != \null;
129 requires valid_endptr: \valid(endptr);
130 assigns \result \from indirect:nptr, indirect:nptr[0 ..];
131 assigns *endptr \from nptr, indirect:nptr[0 ..], indirect:endptr;
132 ensures initialization: \initialized(endptr);
133 ensures valid_endptr: \valid_read(endptr);
134 ensures position_subset: \subset(*endptr, nptr + (0 ..));
135 complete behaviors;
136 disjoint behaviors;
137 */
138 extern float strtof(const char * restrict nptr,
139 char ** restrict endptr);
140
141 /*@
142 requires valid_nptr: \valid_read(nptr); // cannot be precise, valid_read_string too strong
143 requires separation: \separated(nptr, endptr);
144 assigns \result \from indirect:nptr, indirect:nptr[0 ..];
145 assigns *endptr \from nptr, indirect:nptr[0 ..], indirect:endptr;
146 behavior no_storage:
147 assumes null_endptr: endptr == \null;
148 assigns \result \from indirect:nptr, indirect:nptr[0 ..];
149 behavior store_position:
150 assumes nonnull_endptr: endptr != \null;
151 requires valid_endptr: \valid(endptr);
152 assigns \result \from indirect:nptr, indirect:nptr[0 ..];
153 assigns *endptr \from nptr, indirect:nptr[0 ..], indirect:endptr;
154 ensures initialization: \initialized(endptr);
155 ensures valid_endptr: \valid_read(endptr);
156 ensures position_subset: \subset(*endptr, nptr + (0 ..));
157 complete behaviors;
158 disjoint behaviors;
159 */
160 extern long double strtold(const char * restrict nptr,
161 char ** restrict endptr);
162
163 /* TODO: See ISO C 7.20.1.4 to complete these specifications */
164 /*@
165 requires valid_nptr: \valid_read(nptr); // cannot be precise, valid_read_string too strong
166 requires separation: \separated(nptr, endptr);
167 requires base_range: base == 0 || 2 <= base <= 36;
168 assigns \result \from indirect:nptr, indirect:nptr[0 ..], indirect:base;
169 assigns *endptr \from nptr, indirect:nptr[0 ..], indirect:endptr, indirect:base;
170 behavior no_storage:
171 assumes null_endptr: endptr == \null;
172 assigns \result \from indirect:nptr, indirect:nptr[0 ..], indirect:base;
173 behavior store_position:
174 assumes nonnull_endptr: endptr != \null;
175 requires valid_endptr: \valid(endptr);
176 assigns \result \from indirect:nptr, indirect:nptr[0 ..], indirect:base;
177 assigns *endptr \from nptr, indirect:nptr[0 ..], indirect:endptr, indirect:base;
178 ensures initialization: \initialized(endptr);
179 ensures valid_endptr: \valid_read(endptr);
180 ensures position_subset: \subset(*endptr, nptr + (0 ..));
181 complete behaviors;
182 disjoint behaviors;
183 */
184 extern long int strtol(
185 const char * restrict nptr,
186 char ** restrict endptr,
187 int base);
188
189 /*@
190 requires valid_nptr: \valid_read(nptr); // cannot be precise, valid_read_string too strong
191 requires separation: \separated(nptr, endptr);
192 requires base_range: base == 0 || 2 <= base <= 36;
193 assigns \result \from indirect:nptr, indirect:nptr[0 ..], indirect:base;
194 assigns *endptr \from nptr, indirect:nptr[0 ..], indirect:endptr, indirect:base;
195 behavior no_storage:
196 assumes null_endptr: endptr == \null;
197 assigns \result \from indirect:nptr, indirect:nptr[0 ..], indirect:base;
198 behavior store_position:
199 assumes nonnull_endptr: endptr != \null;
200 requires valid_endptr: \valid(endptr);
201 assigns \result \from indirect:nptr, indirect:nptr[0 ..], indirect:base;
202 assigns *endptr \from nptr, indirect:nptr[0 ..], indirect:endptr, indirect:base;
203 ensures initialization: \initialized(endptr);
204 ensures valid_endptr: \valid_read(endptr);
205 ensures position_subset: \subset(*endptr, nptr + (0 ..));
206 complete behaviors;
207 disjoint behaviors;
208 */
209 extern long long int strtoll(
210 const char * restrict nptr,
211 char ** restrict endptr,
212 int base);
213
214 /*@
215 requires valid_nptr: \valid_read(nptr); // cannot be precise, valid_read_string too strong
216 requires separation: \separated(nptr, endptr);
217 requires base_range: base == 0 || 2 <= base <= 36;
218 assigns \result \from indirect:nptr, indirect:nptr[0 ..], indirect:base;
219 assigns *endptr \from nptr, indirect:nptr[0 ..], indirect:endptr, indirect:base;
220 behavior no_storage:
221 assumes null_endptr: endptr == \null;
222 assigns \result \from indirect:nptr, indirect:nptr[0 ..], indirect:base;
223 behavior store_position:
224 assumes nonnull_endptr: endptr != \null;
225 requires valid_endptr: \valid(endptr);
226 assigns \result \from indirect:nptr, indirect:nptr[0 ..], indirect:base;
227 assigns *endptr \from nptr, indirect:nptr[0 ..], indirect:endptr, indirect:base;
228 ensures initialization: \initialized(endptr);
229 ensures valid_endptr: \valid_read(endptr);
230 ensures position_subset: \subset(*endptr, nptr + (0 ..));
231 complete behaviors;
232 disjoint behaviors;
233 */
234 extern unsigned long int strtoul(
235 const char * restrict nptr,
236 char ** restrict endptr,
237 int base);
238
239 /*@
240 requires valid_nptr: \valid_read(nptr); // cannot be precise, valid_read_string too strong
241 requires separation: \separated(nptr, endptr);
242 requires base_range: base == 0 || 2 <= base <= 36;
243 assigns \result \from indirect:nptr, indirect:nptr[0 ..], indirect:base;
244 assigns *endptr \from nptr, indirect:nptr[0 ..], indirect:endptr, indirect:base;
245 behavior no_storage:
246 assumes null_endptr: endptr == \null;
247 assigns \result \from indirect:nptr, indirect:nptr[0 ..], indirect:base;
248 behavior store_position:
249 assumes nonnull_endptr: endptr != \null;
250 requires valid_endptr: \valid(endptr);
251 assigns \result \from indirect:nptr, indirect:nptr[0 ..], indirect:base;
252 assigns *endptr \from nptr, indirect:nptr[0 ..], indirect:endptr, indirect:base;
253 ensures initialization: \initialized(endptr);
254 ensures valid_endptr: \valid_read(endptr);
255 ensures position_subset: \subset(*endptr, nptr + (0 ..));
256 complete behaviors;
257 disjoint behaviors;
258 */
259 extern unsigned long long int strtoull(
260 const char * restrict nptr,
261 char ** restrict endptr,
262 int base);
263
264 //@ ghost extern int __fc_random_counter __attribute__((unused));
265 const unsigned long __fc_rand_max = __FC_RAND_MAX;
266
267 /* ISO C: 7.20.2 */
268 /*@ assigns \result \from __fc_random_counter ;
269 @ assigns __fc_random_counter \from __fc_random_counter ;
270 @ ensures result_range: 0 <= \result <= __fc_rand_max ;
271 */
272 extern int rand(void);
273
274 /*@ assigns __fc_random_counter \from seed ; */
275 extern void srand(unsigned int seed);
276
277 /*@
278 assigns \result \from __fc_random_counter;
279 ensures result_range: 0 <= \result <= __fc_rand_max;
280 */
281 extern long int random(void);
282
283 /*@ assigns __fc_random_counter \from seed; */
284 extern void srandom(unsigned int seed);
285
286 // used to check if some *48() functions have called the seed initializer
287 int __fc_random48_init;
288
289 __FC_EXTERN unsigned short __fc_random48_counter[3];
290 unsigned short *__fc_p_random48_counter = __fc_random48_counter;
291
292 /*@
293 assigns __fc_random48_counter[0..2] \from seed;
294 assigns __fc_random48_init \from \nothing;
295 ensures random48_initialized: __fc_random48_init == 1;
296 */
297 extern void srand48 (long int seed);
298
299 /*@
300 requires initialization:initialized_seed16v: \initialized(seed16v+(0..2));
301 assigns __fc_random48_counter[0..2] \from indirect:seed16v[0..2];
302 assigns __fc_random48_init \from \nothing;
303 assigns \result \from __fc_p_random48_counter;
304 ensures random48_initialized: __fc_random48_init == 1;
305 ensures result_counter: \result == __fc_p_random48_counter;
306 */
307 extern unsigned short *seed48(unsigned short seed16v[3]);
308
309 /*@
310 assigns __fc_random48_counter[0..2] \from param[0..5];
311 assigns __fc_random48_init \from \nothing;
312 ensures random48_initialized: __fc_random48_init == 1;
313 */
314 extern void lcong48(unsigned short param[7]);
315
316 /*@
317 requires random48_initialized: __fc_random48_init == 1;
318 assigns __fc_random48_counter[0..2] \from __fc_random48_counter[0..2];
319 assigns \result \from __fc_random48_counter[0..2];
320 ensures result_range: \is_finite(\result) && 0.0 <= \result < 1.0;
321 */
322 extern double drand48(void);
323
324 /*@
325 requires initialization:initialized_xsubi: \initialized(xsubi+(0..2));
326 assigns __fc_random48_counter[0..2] \from __fc_random48_counter[0..2];
327 assigns \result \from __fc_random48_counter[0..2];
328 ensures result_range: \is_finite(\result) && 0.0 <= \result < 1.0;
329 */
330 extern double erand48(unsigned short xsubi[3]);
331
332 /*@
333 requires random48_initialized: __fc_random48_init == 1;
334 assigns __fc_random48_counter[0..2] \from __fc_random48_counter[0..2];
335 assigns \result \from __fc_random48_counter[0..2];
336 ensures result_range: 0 <= \result < 2147483648;
337 */
338 extern long int lrand48 (void);
339
340 /*@
341 requires initialization:initialized_xsubi: \initialized(xsubi+(0..2));
342 assigns __fc_random48_counter[0..2] \from __fc_random48_counter[0..2];
343 assigns \result \from __fc_random48_counter[0..2];
344 ensures result_range: 0 <= \result < 2147483648;
345 */
346 extern long int nrand48 (unsigned short xsubi[3]);
347
348 /*@
349 requires random48_initialized: __fc_random48_init == 1;
350 assigns __fc_random48_counter[0..2] \from __fc_random48_counter[0..2];
351 assigns \result \from __fc_random48_counter[0..2];
352 ensures result_range: -2147483648 <= \result < 2147483648;
353 */
354 extern long int mrand48 (void);
355
356 /*@
357 requires initialization:initialized_xsubi: \initialized(xsubi+(0..2));
358 assigns __fc_random48_counter[0..2] \from __fc_random48_counter[0..2];
359 assigns \result \from __fc_random48_counter[0..2];
360 ensures result_range: -2147483648 <= \result < 2147483648;
361 */
362 extern long int jrand48 (unsigned short xsubi[3]);
363
364 /* ISO C: 7.20.3.1 */
365 /*@
366 allocates \result;
367 assigns __fc_heap_status \from indirect:nmemb, indirect:size, __fc_heap_status;
368 assigns \result \from indirect:nmemb, indirect:size,
369 indirect:__fc_heap_status;
370
371 behavior allocation:
372 assumes can_allocate: is_allocable(nmemb * size);
373 ensures allocation: \fresh(\result, nmemb * size);
374 ensures initialization: \initialized(((char *)\result)+(0..nmemb*size-1));
375 ensures zero_initialization: \subset(((char *)\result)[0..nmemb*size-1], {0});
376
377 behavior no_allocation:
378 assumes cannot_allocate: !is_allocable(nmemb * size);
379 assigns \result \from \nothing;
380 allocates \nothing;
381 ensures null_result: \result == \null;
382
383 complete behaviors;
384 disjoint behaviors; */
385 extern void *calloc(size_t nmemb, size_t size);
386
387 /*@ allocates \result;
388 @ assigns __fc_heap_status \from size, __fc_heap_status;
389 @ assigns \result \from indirect:size, indirect:__fc_heap_status;
390 @ behavior allocation:
391 @ assumes can_allocate: is_allocable(size);
392 @ assigns __fc_heap_status \from size, __fc_heap_status;
393 @ assigns \result \from indirect:size, indirect:__fc_heap_status;
394 @ ensures allocation: \fresh(\result,size);
395 @ behavior no_allocation:
396 @ assumes cannot_allocate: !is_allocable(size);
397 @ assigns \result \from \nothing;
398 @ allocates \nothing;
399 @ ensures null_result: \result==\null;
400 @ complete behaviors;
401 @ disjoint behaviors;
402 @*/
403 extern void *malloc(size_t size);
404
405 /*@ requires freeable: p==\null || \freeable(p);
406 @ frees p;
407 @ assigns __fc_heap_status \from __fc_heap_status;
408 @ behavior deallocation:
409 @ assumes nonnull_p: p!=\null;
410 @ assigns __fc_heap_status \from __fc_heap_status;
411 @ ensures freed: \allocable(p);
412 @ behavior no_deallocation:
413 @ assumes null_p: p==\null;
414 @ assigns \nothing;
415 @ frees \nothing;
416 @ complete behaviors;
417 @ disjoint behaviors;
418 @*/
419 extern void free(void *p);
420
421 /*@
422 requires freeable: ptr == \null || \freeable(ptr);
423 allocates \result;
424 frees ptr;
425 assigns __fc_heap_status \from __fc_heap_status;
426 assigns \result \from size, ptr, __fc_heap_status;
427
428 behavior allocation:
429 assumes can_allocate: is_allocable(size);
430 allocates \result;
431 assigns \result \from size, __fc_heap_status;
432 ensures allocation: \fresh(\result,size);
433
434 behavior deallocation:
435 assumes nonnull_ptr: ptr != \null;
436 assumes can_allocate: is_allocable(size);
437 frees ptr;
438 ensures freed: \allocable(ptr);
439 ensures freeable: \result == \null || \freeable(\result);
440
441 behavior fail:
442 assumes cannot_allocate: !is_allocable(size);
443 allocates \nothing;
444 frees \nothing;
445 assigns \result \from size, __fc_heap_status;
446 ensures null_result: \result == \null;
447
448 complete behaviors;
449 disjoint behaviors allocation, fail;
450 disjoint behaviors deallocation, fail;
451 */
452 extern void *realloc(void *ptr, size_t size);
453
454 // reallocarray is non-POSIX (glibc)
455 /*@
456 requires freeable: ptr == \null || \freeable(ptr);
457 allocates \result;
458 frees ptr;
459 assigns __fc_heap_status \from indirect:nmemb, indirect:size,
460 __fc_heap_status;
461 assigns \result \from ptr, indirect:nmemb, indirect:size,
462 indirect:__fc_heap_status;
463
464 behavior allocation:
465 assumes can_allocate: is_allocable(nmemb * size);
466 allocates \result;
467 ensures allocation: \fresh(\result, nmemb * size);
468
469 behavior deallocation:
470 assumes nonnull_ptr: ptr != \null;
471 assumes can_allocate: is_allocable(nmemb * size);
472 frees ptr;
473 ensures freed: \allocable(ptr);
474 ensures freeable: \result == \null || \freeable(\result);
475
476 behavior fail:
477 assumes cannot_allocate: !is_allocable(nmemb * size);
478 allocates \nothing;
479 frees \nothing;
480 assigns \result \from indirect:nmemb, indirect:size,
481 indirect:__fc_heap_status;
482 ensures null_result: \result == \null;
483
484 complete behaviors;
485 disjoint behaviors allocation, fail;
486 disjoint behaviors deallocation, fail;
487 */
488 extern void *reallocarray(void *ptr, size_t nmemb, size_t size);
489
490 /* ISO C: 7.20.4 */
491
492 /*@
493 assigns \exit_status \from \nothing;
494 exits status: \exit_status != EXIT_SUCCESS;
495 ensures never_terminates: \false;
496 */
497 extern void abort(void) __attribute__ ((__noreturn__));
498
499 /*@ assigns \result \from \nothing ;*/
500 extern int atexit(void (*func)(void));
501
502 /*@ assigns \result \from \nothing ;*/
503 extern int at_quick_exit(void (*func)(void));
504
505 /*@
506 assigns \exit_status \from status;
507 exits status: \exit_status == status;
508 ensures never_terminates: \false;
509 */
510 extern void exit(int status) __attribute__ ((__noreturn__));
511
512 /*@
513 assigns \nothing;
514 ensures never_terminates: \false;
515 */
516 extern void _Exit(int status) __attribute__ ((__noreturn__));
517
518 extern char *__fc_env[ARG_MAX];
519
520 /*@
521 requires valid_name: valid_read_string(name);
522 assigns \result \from __fc_env[0..], indirect:name, name[0 ..];
523 ensures null_or_valid_result: \result == \null || \valid(\result);
524 */
525 extern char *getenv(const char *name);
526
527 /*@
528 requires valid_string: valid_read_string(string);
529 assigns __fc_env[0..] \from __fc_env[0..], string;
530 assigns \result \from indirect:__fc_env[0..], indirect:string;
531 */
532 extern int putenv(char *string);
533
534 /*@
535 requires valid_name: valid_read_string(name);
536 requires valid_value: valid_read_string(value);
537 assigns \result, __fc_env[0..]
538 \from __fc_env[0..], indirect:name, indirect:name[0 ..],
539 indirect:value, indirect:value[0 ..], indirect:overwrite;
540 ensures result_ok_or_error: \result == 0 || \result == -1;
541 */
542 extern int setenv(const char *name, const char *value, int overwrite);
543
544 /*@
545 requires valid_name: valid_read_string(name);
546 assigns \result, __fc_env[0..]
547 \from __fc_env[0..], indirect:name, indirect:name[0 ..];
548 ensures result_ok_or_error: \result == 0 || \result == -1;
549 */
550 extern int unsetenv(const char *name);
551
552 /*@
553 assigns \nothing;
554 ensures never_terminates: \false;
555 */
556 extern void quick_exit(int status) __attribute__ ((__noreturn__));
557
558 /*@
559 requires null_or_valid_string_command:
560 command == \null || valid_read_string(command);
561 assigns \result \from indirect:command, indirect:command[0 ..];
562 */
563 extern int system(const char *command);
564
565 /* ISO C: 7.20.5 */
566
567 /* TODO: use one of the well known specification with high order compare :-) */
568 // NOTE: the assigns of function [compar] are not currently taken into account
569 // by ACSL. If [compar] is not purely functional, the result may be unsound.
570 // To ensure soundness, you should manually give a specification to the
571 // comparison function that is equivalent to:
572 // assigns \result \from *(<type>*)a, *(<type>*)b;
573 // where <type> is the type of the compared arguments.
574 /*@
575 requires valid_function_compar: \valid_function(compar);
576 assigns \result \from indirect:key, ((char*)key)[0 .. size-1], base,
577 ((char*)base)[0 .. size * (nmemb-1)], indirect:nmemb,
578 indirect:size, indirect:*compar;
579 ensures null_or_correct_result: \result == \null ||
580 \subset(\result, (void*)(((char*)base) + (0 .. size * (nmemb-1))));
581 */
582 extern void *bsearch(const void *key, const void *base, size_t nmemb,
583 size_t size, int (*compar)(const void *, const void *));
584
585 // NOTE: the assigns of function [compar] are not currently taken into account
586 // by ACSL. If [compar] is not purely functional, the result may be unsound.
587 /*@
588 requires valid_function_compar: \valid_function(compar);
589 assigns ((char*)base)[0 ..] \from indirect:base, ((char*)base)[0 ..],
590 indirect:nmemb, indirect:size,
591 indirect:compar, indirect:*compar;
592 */
593 extern void qsort(void *base, size_t nmemb, size_t size,
594 int (*compar)(const void *, const void *));
595
596 /* ISO C: 7.20.6 */
597
598 /*@
599 requires abs_representable: j > INT_MIN;
600 assigns \result \from j;
601 behavior negative:
602 assumes negative: j < 0;
603 ensures opposite_result: \result == -j;
604 behavior nonnegative:
605 assumes nonnegative: j >= 0;
606 ensures same_result: \result == j;
607 complete behaviors;
608 disjoint behaviors;
609 */
610 extern int abs(int j);
611
612 /*@
613 requires abs_representable: j > LONG_MIN ;
614 assigns \result \from j;
615 behavior negative:
616 assumes negative: j < 0;
617 ensures opposite_result: \result == -j;
618 behavior nonnegative:
619 assumes nonnegative: j >= 0;
620 ensures same_result: \result == j;
621 complete behaviors;
622 disjoint behaviors;
623 */
624 extern long int labs(long int j);
625
626 /*@
627 requires abs_representable: j > LLONG_MIN ;
628 assigns \result \from j;
629 behavior negative:
630 assumes negative: j < 0;
631 ensures opposite_result: \result == -j;
632 behavior nonnegative:
633 assumes nonnegative: j >= 0;
634 ensures same_result: \result == j;
635 complete behaviors;
636 disjoint behaviors;
637 */
638 extern long long int llabs(long long int j);
639
640 /*@ assigns \result \from numer,denom ; */
641 extern div_t div(int numer, int denom);
642 /*@ assigns \result \from numer,denom ; */
643 extern ldiv_t ldiv(long int numer, long int denom);
644 /*@ assigns \result \from numer,denom ; */
645 extern lldiv_t lldiv(long long int numer, long long int denom);
646
647 /* ISO C: 7.20.7 */
648
649 //@ ghost extern int __fc_mblen_state;
650
651 /*@ assigns \result, __fc_mblen_state \from
652 indirect:s, indirect:s[0 ..], indirect:n, __fc_mblen_state; */
653 extern int mblen(const char *s, size_t n);
654
655 //@ ghost extern int __fc_mbtowc_state;
656
657 /*@
658 requires separation: \separated(pwc, s);
659 assigns \result \from indirect:s, indirect:s[0 .. n-1], indirect:n,
660 __fc_mbtowc_state;
661 assigns pwc[0 .. \result-1], __fc_mbtowc_state
662 \from indirect:s, s[0 .. n-1], indirect:n, __fc_mbtowc_state;
663 ensures consumed_range: \result <= n;
664 */
665 extern int mbtowc(wchar_t * restrict pwc,
666 const char * restrict s,
667 size_t n);
668
669 //@ ghost extern int __fc_wctomb_state;
670
671 /*@
672 //requires room_string: \valid(s + (0 .. __fc_mb_cur_max - 1));
673 assigns \result \from indirect:wc, __fc_wctomb_state;
674 assigns s[0 ..], __fc_wctomb_state \from wc, __fc_wctomb_state;
675 */
676 extern int wctomb(char *s, wchar_t wc);
677
678 /* ISO C: 7.20.8 */
679
680 /*@
681 requires separation: \separated(pwcs, s);
682 assigns \result \from indirect:s, indirect:s[0 .. n-1], indirect:n;
683 assigns pwcs[0 .. n-1] \from indirect:s, s[0 .. n-1], indirect:n;
684 */
685 extern size_t mbstowcs(wchar_t * restrict pwcs,
686 const char * restrict s,
687 size_t n);
688
689 /*@
690 requires separation: \separated(s, pwcs);
691 assigns \result \from indirect:pwcs, indirect:pwcs[0 .. n-1], indirect:n;
692 assigns s[0 .. n-1] \from indirect:pwcs, pwcs[0 .. n-1], indirect:n;
693 */
694 extern size_t wcstombs(char * restrict s,
695 const wchar_t * restrict pwcs,
696 size_t n);
697
698 // Note: this specification should ideally use a more specific predicate,
699 // such as 'is_allocable_aligned(alignment, size)'.
700 /*@
701 requires valid_memptr: \valid(memptr);
702 requires alignment_is_a_suitable_power_of_two:
703 alignment >= sizeof(void*) &&
704 ((size_t)alignment & ((size_t)alignment - 1)) == 0;
705 allocates *memptr;
706 assigns __fc_heap_status \from indirect:alignment, size, __fc_heap_status;
707 assigns \result \from indirect:alignment, indirect:size,
708 indirect:__fc_heap_status;
709 behavior allocation:
710 assumes can_allocate: is_allocable(size);
711 assigns __fc_heap_status \from indirect:alignment, size, __fc_heap_status;
712 assigns \result \from indirect:alignment, indirect:size,
713 indirect:__fc_heap_status;
714 ensures allocation: \fresh(*memptr,size);
715 ensures result_zero: \result == 0;
716 behavior no_allocation:
717 assumes cannot_allocate: !is_allocable(size);
718 assigns \result \from indirect:alignment;
719 allocates \nothing;
720 ensures result_non_zero: \result < 0 || \result > 0;
721 complete behaviors;
722 disjoint behaviors;
723 */
724 extern int posix_memalign(void **memptr, size_t alignment, size_t size);
725
726 /*@
727 // missing: requires 'last 6 characters of template must be XXXXXX'
728 // missing: assigns \result, templat[0..] \from 'filesystem', 'RNG';
729 requires valid_template: valid_string(templat);
730 requires template_len: strlen(templat) >= 6;
731 assigns templat[0..] \from \nothing;
732 assigns \result \from \nothing;
733 ensures result_error_or_valid_fd: \result == -1 ||
734 0 <= \result < __FC_FOPEN_MAX;
735 */
736 extern int mkstemp(char *templat);
737
738 /*@
739 // missing: requires 'last (6+suffixlen) characters of template must be X's'
740 // missing: assigns \result, templat[0..] \from 'filesystem', 'RNG';
741 requires valid_template: valid_string(templat);
742 requires template_len: strlen(templat) >= 6 + suffixlen;
743 requires non_negative_suffixlen: suffixlen >= 0;
744 assigns templat[0..] \from \nothing;
745 assigns \result \from \nothing;
746 ensures result_error_or_valid_fd: \result == -1 ||
747 0 <= \result < __FC_FOPEN_MAX;
748 */
749 extern int mkstemps(char *templat, int suffixlen);
750
751 // This function may allocate memory for the result, which is not supported by
752 // some plugins such as Eva. In such cases, it is preferable to use the stub
753 // provided in stdlib.c.
754 extern char *realpath(const char *restrict file_name,
755 char *restrict resolved_name);
756
757 // Non-POSIX; GNU extension
758 // This function may allocate memory for the result, which is not supported by
759 // some plugins such as Eva. In such cases, it is preferable to use the stub
760 // provided in stdlib.c.
761 extern char *canonicalize_file_name(const char *path);
762
763 __END_DECLS
764
765 __POP_FC_STDLIB
766 #endif