| Absolute File Name: | /home/opencoverage/opencoverage/guest-scripts/coreutils/src/lib/read-file.c |
| Switch to Source code | Preprocessed file |
| Line | Source | Count | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 1 | - | |||||||||||||
| 2 | - | |||||||||||||
| 3 | - | |||||||||||||
| 4 | - | |||||||||||||
| 5 | - | |||||||||||||
| 6 | - | |||||||||||||
| 7 | - | |||||||||||||
| 8 | - | |||||||||||||
| 9 | - | |||||||||||||
| 10 | - | |||||||||||||
| 11 | - | |||||||||||||
| 12 | - | |||||||||||||
| 13 | - | |||||||||||||
| 14 | - | |||||||||||||
| 15 | - | |||||||||||||
| 16 | - | |||||||||||||
| 17 | - | |||||||||||||
| 18 | - | |||||||||||||
| 19 | char * | - | ||||||||||||
| 20 | fread_file (FILE *stream, size_t *length) | - | ||||||||||||
| 21 | { | - | ||||||||||||
| 22 | char *buf = | - | ||||||||||||
| 23 | ((void *)0) | - | ||||||||||||
| 24 | ; | - | ||||||||||||
| 25 | size_t alloc = | - | ||||||||||||
| 26 | 8192 | - | ||||||||||||
| 27 | ; | - | ||||||||||||
| 28 | - | |||||||||||||
| 29 | - | |||||||||||||
| 30 | - | |||||||||||||
| 31 | { | - | ||||||||||||
| 32 | struct stat st; | - | ||||||||||||
| 33 | - | |||||||||||||
| 34 | if (fstat (fileno (stream), &st) >= 0
| 0-41 | ||||||||||||
| 35 | ((((
| 16-25 | ||||||||||||
| 36 | st.st_mode
| 16-25 | ||||||||||||
| 37 | )) & 0170000) == (0100000))
| 16-25 | ||||||||||||
| 38 | ) | - | ||||||||||||
| 39 | { | - | ||||||||||||
| 40 | off_t pos = ftello (stream); | - | ||||||||||||
| 41 | - | |||||||||||||
| 42 | if (pos >= 0
| 0-25 | ||||||||||||
| 43 | { | - | ||||||||||||
| 44 | off_t alloc_off = st.st_size - pos; | - | ||||||||||||
| 45 | - | |||||||||||||
| 46 | - | |||||||||||||
| 47 | if ( | - | ||||||||||||
| 48 | (
| 0-22 | ||||||||||||
| 49 | - 1 < alloc_off
| 0-22 | ||||||||||||
| 50 | { | - | ||||||||||||
| 51 | - | |||||||||||||
| 52 | (*__errno_location ()) | - | ||||||||||||
| 53 | = | - | ||||||||||||
| 54 | 12 | - | ||||||||||||
| 55 | ; | - | ||||||||||||
| 56 | return never executed: return ((void *)0) ;never executed: return ((void *)0) ; | 0 | ||||||||||||
| 57 | ((void *)0) never executed: return ((void *)0) ; | 0 | ||||||||||||
| 58 | ; never executed: return ((void *)0) ; | 0 | ||||||||||||
| 59 | } | - | ||||||||||||
| 60 | - | |||||||||||||
| 61 | alloc = alloc_off + 1; | - | ||||||||||||
| 62 | } executed 22 times by 2 tests: end of blockExecuted by:
| 22 | ||||||||||||
| 63 | } executed 25 times by 2 tests: end of blockExecuted by:
| 25 | ||||||||||||
| 64 | } | - | ||||||||||||
| 65 | - | |||||||||||||
| 66 | if (!(buf = malloc (alloc))
| 0-41 | ||||||||||||
| 67 | return never executed: return ((void *)0) ;never executed: return ((void *)0) ; | 0 | ||||||||||||
| 68 | ((void *)0) never executed: return ((void *)0) ; | 0 | ||||||||||||
| 69 | ; never executed: return ((void *)0) ; | 0 | ||||||||||||
| 70 | - | |||||||||||||
| 71 | { | - | ||||||||||||
| 72 | size_t size = 0; | - | ||||||||||||
| 73 | int save_errno; | - | ||||||||||||
| 74 | - | |||||||||||||
| 75 | for (;;) | - | ||||||||||||
| 76 | { | - | ||||||||||||
| 77 | - | |||||||||||||
| 78 | - | |||||||||||||
| 79 | size_t requested = alloc - size; | - | ||||||||||||
| 80 | size_t count = fread (buf + size, 1, requested, stream); | - | ||||||||||||
| 81 | size += count; | - | ||||||||||||
| 82 | - | |||||||||||||
| 83 | if (count != requested
| 1-41 | ||||||||||||
| 84 | { | - | ||||||||||||
| 85 | save_errno = | - | ||||||||||||
| 86 | (*__errno_location ()) | - | ||||||||||||
| 87 | ; | - | ||||||||||||
| 88 | if (ferror (stream)
| 0-41 | ||||||||||||
| 89 | break; never executed: break; | 0 | ||||||||||||
| 90 | - | |||||||||||||
| 91 | - | |||||||||||||
| 92 | if (size < alloc - 1
| 19-22 | ||||||||||||
| 93 | { | - | ||||||||||||
| 94 | char *smaller_buf = realloc (buf, size + 1); | - | ||||||||||||
| 95 | if (smaller_buf !=
| 0-19 | ||||||||||||
| 96 | ((void *)0)
| 0-19 | ||||||||||||
| 97 | ) | - | ||||||||||||
| 98 | buf = smaller_buf; executed 19 times by 2 tests: buf = smaller_buf;Executed by:
| 19 | ||||||||||||
| 99 | } executed 19 times by 2 tests: end of blockExecuted by:
| 19 | ||||||||||||
| 100 | - | |||||||||||||
| 101 | buf[size] = '\0'; | - | ||||||||||||
| 102 | *length = size; | - | ||||||||||||
| 103 | return executed 41 times by 2 tests: buf;return buf;Executed by:
executed 41 times by 2 tests: return buf;Executed by:
| 41 | ||||||||||||
| 104 | } | - | ||||||||||||
| 105 | - | |||||||||||||
| 106 | { | - | ||||||||||||
| 107 | char *new_buf; | - | ||||||||||||
| 108 | - | |||||||||||||
| 109 | if (alloc ==
| 0-1 | ||||||||||||
| 110 | (18446744073709551615UL)
| 0-1 | ||||||||||||
| 111 | ) | - | ||||||||||||
| 112 | { | - | ||||||||||||
| 113 | save_errno = | - | ||||||||||||
| 114 | 12 | - | ||||||||||||
| 115 | ; | - | ||||||||||||
| 116 | break; never executed: break; | 0 | ||||||||||||
| 117 | } | - | ||||||||||||
| 118 | - | |||||||||||||
| 119 | if (alloc <
| 0-1 | ||||||||||||
| 120 | (18446744073709551615UL)
| 0-1 | ||||||||||||
| 121 | - alloc / 2
| 0-1 | ||||||||||||
| 122 | alloc = alloc + alloc / 2; executed 1 time by 1 test: alloc = alloc + alloc / 2;Executed by:
| 1 | ||||||||||||
| 123 | else | - | ||||||||||||
| 124 | alloc = never executed: alloc = (18446744073709551615UL) ; | 0 | ||||||||||||
| 125 | (18446744073709551615UL) never executed: alloc = (18446744073709551615UL) ; | 0 | ||||||||||||
| 126 | ; never executed: alloc = (18446744073709551615UL) ; | 0 | ||||||||||||
| 127 | - | |||||||||||||
| 128 | if (!(new_buf = realloc (buf, alloc))
| 0-1 | ||||||||||||
| 129 | { | - | ||||||||||||
| 130 | save_errno = | - | ||||||||||||
| 131 | (*__errno_location ()) | - | ||||||||||||
| 132 | ; | - | ||||||||||||
| 133 | break; never executed: break; | 0 | ||||||||||||
| 134 | } | - | ||||||||||||
| 135 | - | |||||||||||||
| 136 | buf = new_buf; | - | ||||||||||||
| 137 | } | - | ||||||||||||
| 138 | } executed 1 time by 1 test: end of blockExecuted by:
| 1 | ||||||||||||
| 139 | - | |||||||||||||
| 140 | free (buf); | - | ||||||||||||
| 141 | - | |||||||||||||
| 142 | (*__errno_location ()) | - | ||||||||||||
| 143 | = save_errno; | - | ||||||||||||
| 144 | return never executed: return ((void *)0) ;never executed: return ((void *)0) ; | 0 | ||||||||||||
| 145 | ((void *)0) never executed: return ((void *)0) ; | 0 | ||||||||||||
| 146 | ; never executed: return ((void *)0) ; | 0 | ||||||||||||
| 147 | } | - | ||||||||||||
| 148 | } | - | ||||||||||||
| 149 | - | |||||||||||||
| 150 | static char * | - | ||||||||||||
| 151 | internal_read_file (const char *filename, size_t *length, const char *mode) | - | ||||||||||||
| 152 | { | - | ||||||||||||
| 153 | FILE *stream = fopen (filename, mode); | - | ||||||||||||
| 154 | char *out; | - | ||||||||||||
| 155 | int save_errno; | - | ||||||||||||
| 156 | - | |||||||||||||
| 157 | if (!stream
| 0-15 | ||||||||||||
| 158 | return never executed: return ((void *)0) ;never executed: return ((void *)0) ; | 0 | ||||||||||||
| 159 | ((void *)0) never executed: return ((void *)0) ; | 0 | ||||||||||||
| 160 | ; never executed: return ((void *)0) ; | 0 | ||||||||||||
| 161 | - | |||||||||||||
| 162 | out = fread_file (stream, length); | - | ||||||||||||
| 163 | - | |||||||||||||
| 164 | save_errno = | - | ||||||||||||
| 165 | (*__errno_location ()) | - | ||||||||||||
| 166 | ; | - | ||||||||||||
| 167 | - | |||||||||||||
| 168 | if ( | - | ||||||||||||
| 169 | rpl_fclose
| 0-15 | ||||||||||||
| 170 | (stream) != 0
| 0-15 | ||||||||||||
| 171 | { | - | ||||||||||||
| 172 | if (out
| 0 | ||||||||||||
| 173 | { | - | ||||||||||||
| 174 | save_errno = | - | ||||||||||||
| 175 | (*__errno_location ()) | - | ||||||||||||
| 176 | ; | - | ||||||||||||
| 177 | free (out); | - | ||||||||||||
| 178 | } never executed: end of block | 0 | ||||||||||||
| 179 | - | |||||||||||||
| 180 | (*__errno_location ()) | - | ||||||||||||
| 181 | = save_errno; | - | ||||||||||||
| 182 | return never executed: return ((void *)0) ;never executed: return ((void *)0) ; | 0 | ||||||||||||
| 183 | ((void *)0) never executed: return ((void *)0) ; | 0 | ||||||||||||
| 184 | ; never executed: return ((void *)0) ; | 0 | ||||||||||||
| 185 | } | - | ||||||||||||
| 186 | - | |||||||||||||
| 187 | return executed 15 times by 1 test: out;return out;Executed by:
executed 15 times by 1 test: return out;Executed by:
| 15 | ||||||||||||
| 188 | } | - | ||||||||||||
| 189 | - | |||||||||||||
| 190 | - | |||||||||||||
| 191 | - | |||||||||||||
| 192 | - | |||||||||||||
| 193 | - | |||||||||||||
| 194 | - | |||||||||||||
| 195 | - | |||||||||||||
| 196 | char * | - | ||||||||||||
| 197 | read_file (const char *filename, size_t *length) | - | ||||||||||||
| 198 | { | - | ||||||||||||
| 199 | return executed 15 times by 1 test: internal_read_file (filename, length, "r");return internal_read_file (filename, length, "r");Executed by:
executed 15 times by 1 test: return internal_read_file (filename, length, "r");Executed by:
| 15 | ||||||||||||
| 200 | } | - | ||||||||||||
| 201 | char * | - | ||||||||||||
| 202 | read_binary_file (const char *filename, size_t *length) | - | ||||||||||||
| 203 | { | - | ||||||||||||
| 204 | return never executed: internal_read_file (filename, length, "rb");return internal_read_file (filename, length, "rb");never executed: return internal_read_file (filename, length, "rb"); | 0 | ||||||||||||
| 205 | } | - | ||||||||||||
| Switch to Source code | Preprocessed file |