| 1 | /* CIVL model of stdlib.c */
|
|---|
| 2 |
|
|---|
| 3 | #ifndef __STDLIB_CIVL__
|
|---|
| 4 | #define __STDLIB_CIVL__
|
|---|
| 5 | #include<stdlib.h>
|
|---|
| 6 | #include<stdio.h>
|
|---|
| 7 | #include <pointer.cvh>
|
|---|
| 8 | #include <bundle.cvh>
|
|---|
| 9 | #include<civlc.cvh>
|
|---|
| 10 |
|
|---|
| 11 | void swap(void * a, void * b, size_t size) {
|
|---|
| 12 | $bundle bun_a = $bundle_pack(a, size);
|
|---|
| 13 | $bundle bun_b = $bundle_pack(b, size);
|
|---|
| 14 |
|
|---|
| 15 | $bundle_unpack(bun_a, b);
|
|---|
| 16 | $bundle_unpack(bun_b, a);
|
|---|
| 17 | }
|
|---|
| 18 |
|
|---|
| 19 | void qsort(void *base, size_t n, size_t es,
|
|---|
| 20 | int (*cmp)(const void*, const void*)) {
|
|---|
| 21 | for (int i = 1; i < n; i++) {
|
|---|
| 22 | for (int j = i; j > 0; j--) {
|
|---|
| 23 | void * p_j_1 = $pointer_add(base, j-1, es);
|
|---|
| 24 | void * p_j = $pointer_add(base, j, es);
|
|---|
| 25 | int comp = cmp(p_j_1, p_j);
|
|---|
| 26 |
|
|---|
| 27 | if (comp <= 0) continue;
|
|---|
| 28 | else swap(p_j_1, p_j, es);
|
|---|
| 29 | }
|
|---|
| 30 | }
|
|---|
| 31 | }
|
|---|
| 32 |
|
|---|
| 33 | void free(void*ptr){
|
|---|
| 34 | $free(ptr);
|
|---|
| 35 | }
|
|---|
| 36 |
|
|---|
| 37 | int rand(){
|
|---|
| 38 | int tmp;
|
|---|
| 39 |
|
|---|
| 40 | $havoc(&tmp);
|
|---|
| 41 | return tmp;
|
|---|
| 42 | }
|
|---|
| 43 |
|
|---|
| 44 | void srand(unsigned int seed){
|
|---|
| 45 | }
|
|---|
| 46 |
|
|---|
| 47 | void srandom(unsigned int seed){
|
|---|
| 48 | }
|
|---|
| 49 |
|
|---|
| 50 | long int random(){
|
|---|
| 51 | long int tmp;
|
|---|
| 52 |
|
|---|
| 53 | $havoc(&tmp);
|
|---|
| 54 | return tmp;
|
|---|
| 55 | }
|
|---|
| 56 |
|
|---|
| 57 | void exit(int status){
|
|---|
| 58 | $assert(status == 0, "erroneous exit with code %d", status);
|
|---|
| 59 | $exit();
|
|---|
| 60 | }
|
|---|
| 61 |
|
|---|
| 62 | _Noreturn void abort(void){
|
|---|
| 63 | $exit();
|
|---|
| 64 | }
|
|---|
| 65 |
|
|---|
| 66 | int abs(int x){
|
|---|
| 67 | if (x >= 0)
|
|---|
| 68 | return x;
|
|---|
| 69 | return (-x);
|
|---|
| 70 | }
|
|---|
| 71 |
|
|---|
| 72 | int atoi(const char *nptr){
|
|---|
| 73 | $abstract int _atoi(const char * ptr);
|
|---|
| 74 |
|
|---|
| 75 | return _atoi(nptr);
|
|---|
| 76 | }
|
|---|
| 77 |
|
|---|
| 78 | #ifdef _LINUX
|
|---|
| 79 | int rand_r(unsigned int *seedp){
|
|---|
| 80 | int tmp;
|
|---|
| 81 |
|
|---|
| 82 | $havoc(&tmp);
|
|---|
| 83 | return tmp;
|
|---|
| 84 | }
|
|---|
| 85 | #endif
|
|---|
| 86 | #endif
|
|---|