| 1182 | Return true if successful. */ |
| 1183 | |
| 1184 | static bool |
| 1185 | digest_file (char const *filename, int *binary, unsigned char *bin_result, |
| 1186 | bool *missing, MAYBE_UNUSED intmax_t *length) |
| 1187 | { |
| 1188 | FILE *fp; |
| 1189 | int err; |
| 1190 | bool is_stdin = streq (filename, "-"); |
| 1191 | |
| 1192 | *missing = false; |
| 1193 | |
| 1194 | if (is_stdin) |
| 1195 | { |
| 1196 | have_read_stdin = true; |
| 1197 | fp = stdin; |
| 1198 | if (O_BINARY && *binary) |
| 1199 | { |
| 1200 | if (*binary < 0) |
| 1201 | *binary = ! isatty (STDIN_FILENO); |
| 1202 | if (*binary) |
| 1203 | xset_binary_mode (STDIN_FILENO, O_BINARY); |
| 1204 | } |
| 1205 | } |
| 1206 | else |
| 1207 | { |
| 1208 | fp = fopen (filename, O_BINARY ? (*binary ? "rb" : "rt") : "r"); |
| 1209 | if (fp == NULL) |
| 1210 | { |
| 1211 | if (ignore_missing && errno == ENOENT) |
| 1212 | { |
| 1213 | *missing = true; |
| 1214 | return true; |
| 1215 | } |
| 1216 | error (0, errno, "%s", quotef (filename)); |
| 1217 | return false; |
| 1218 | } |
| 1219 | } |
| 1220 | |
| 1221 | fadvise (fp, FADVISE_SEQUENTIAL); |
| 1222 | |
| 1223 | #if HASH_ALGO_CKSUM |
| 1224 | if (cksum_algorithm == blake2b |
| 1225 | || cksum_algorithm == sha2 || cksum_algorithm == sha3) |
| 1226 | *length = digest_length >> 3; |
| 1227 | err = DIGEST_STREAM (fp, bin_result, length); |
| 1228 | #elif HASH_ALGO_SUM |
| 1229 | err = DIGEST_STREAM (fp, bin_result, length); |
| 1230 | #elif HASH_ALGO_BLAKE2 |
| 1231 | err = DIGEST_STREAM (fp, bin_result, digest_length >> 3); |
| 1232 | #else |
| 1233 | err = DIGEST_STREAM (fp, bin_result); |
| 1234 | #endif |
| 1235 | err = err ? errno : 0; |
| 1236 | if (is_stdin) |
| 1237 | clearerr (fp); |
| 1238 | else if (fclose (fp) != 0 && !err) |
| 1239 | err = errno; |
| 1240 | |
| 1241 | if (err) |
no test coverage detected