| 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 "glob.h" |
| 24 |
#include "stdlib.h" |
| 25 |
#include "__fc_builtin.h" |
| 26 |
__PUSH_FC_STDLIB |
| 27 |
|
| 28 |
int glob(const char *pattern, int flags, |
| 29 |
int (*errfunc) (const char *epath, int eerrno), |
| 30 |
glob_t *pglob) { |
| 31 |
// arbitrarily simulate finding a number of results between 0 and 10 |
| 32 |
pglob->gl_pathc = Frama_C_interval(0, 10); |
| 33 |
// reserve_offs is 0 unless GLOB_DOOFFS is set; it is the number of |
| 34 |
// offsets to skip |
| 35 |
size_t reserve_offs = flags & GLOB_DOOFFS ? pglob->gl_offs : 0; |
| 36 |
// prev_len is 0 unless GLOB_APPEND is set; it is the length |
| 37 |
// of the previous call to glob() |
| 38 |
size_t prev_len = 0; |
| 39 |
// count previous list size, used for new allocation |
| 40 |
if (flags & GLOB_APPEND) |
| 41 |
while (pglob->gl_pathv[reserve_offs+prev_len]) prev_len++; |
| 42 |
|
| 43 |
// path points to pglob->gl_pathv if GLOB_APPEND, or NULL otherwise |
| 44 |
char **path = flags & GLOB_APPEND ? pglob->gl_pathv : NULL; |
| 45 |
if (pglob->gl_pathc == 0) { // no results found |
| 46 |
if (flags & GLOB_NOCHECK) { |
| 47 |
// allocate 1 slot per reserved offset, + previous length, |
| 48 |
// +1 for the pattern itself (to be recopied), +1 for the terminator |
| 49 |
pglob->gl_pathv = |
| 50 |
realloc(path, |
| 51 |
(reserve_offs + prev_len + 2) * sizeof(char*)); |
| 52 |
if (!pglob->gl_pathv) return GLOB_NOSPACE; |
| 53 |
// 0-init reserved offsets |
| 54 |
for (size_t i = 0; i < reserve_offs; i++) pglob->gl_pathv[i] = 0; |
| 55 |
pglob->gl_pathv[reserve_offs + prev_len] = (char*)pattern; |
| 56 |
pglob->gl_pathv[reserve_offs + prev_len + 1] = 0; // terminator |
| 57 |
return 0; |
| 58 |
} else { |
| 59 |
return GLOB_NOMATCH; |
| 60 |
} |
| 61 |
} |
| 62 |
// found some results |
| 63 |
pglob->gl_pathv = |
| 64 |
// allocate 1 slot per reserved offset, + previous length, |
| 65 |
// +1 for each result, +1 for the terminator |
| 66 |
realloc(path, |
| 67 |
(reserve_offs + prev_len + pglob->gl_pathc + 1) * sizeof(char*)); |
| 68 |
if (!pglob->gl_pathv) return GLOB_NOSPACE; |
| 69 |
|
| 70 |
// 0-init reserved offsets |
| 71 |
for (size_t i = 0; i < reserve_offs; i++) pglob->gl_pathv[i] = 0; |
| 72 |
|
| 73 |
for (size_t i = 0; i < pglob->gl_pathc; i++) { |
| 74 |
pglob->gl_pathv[reserve_offs + prev_len + i] = (char*)"glob result"; |
| 75 |
} |
| 76 |
pglob->gl_pathv[prev_len + reserve_offs + pglob->gl_pathc] = 0; // terminator |
| 77 |
if (Frama_C_nondet(0, 1)) { // simulate "no error" |
| 78 |
return 0; |
| 79 |
} else { |
| 80 |
// simulate error during operation |
| 81 |
if (errfunc) { |
| 82 |
int res = errfunc("glob.c error path", Frama_C_interval(0, 255)); |
| 83 |
if (res || flags & GLOB_ERR) return GLOB_ABORTED; |
| 84 |
} |
| 85 |
return 0; |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
void globfree(glob_t *pglob) { |
| 90 |
// check for gl_pathc, because: |
| 91 |
// "if gl_pathc is zero, the contents of gl_pathv are undefined" |
| 92 |
if (pglob->gl_pathc > 0) |
| 93 |
free(pglob->gl_pathv); |
| 94 |
} |
| 95 |
|
| 96 |
__POP_FC_STDLIB |