-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhash_file.c
62 lines (47 loc) · 1.29 KB
/
hash_file.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include "hash_file.h"
#include <stddef.h>
#include <stdio.h>
#include <string.h>
#include "test_common.h"
#define BUFFER_LEN 1024
static void extract_hashes(char *string, char *hashes[], size_t num_hashes)
{
char *pch = strtok(string, ",");
for (size_t i = 0; i < num_hashes; i++) {
if (pch == NULL) {
hashes[i] = NULL;
continue;
}
hashes[i] = pch;
pch = strtok(NULL, ",");
}
}
void test_for_each_line(const char *file_name, void (*test)(char **))
{
char path_name[BUFFER_LEN];
snprintf(path_name, sizeof(path_name), "%s/%s", KERL_TEST_FOLDER,
file_name);
FILE *file;
if ((file = fopen(path_name, "r")) == NULL) {
fail_msg("Could not open file %s", path_name);
}
char line[BUFFER_LEN];
unsigned int line_num = 0;
while (fgets(line, sizeof(line), file) != NULL) {
if (++line_num == 1) {
continue;
}
// remove trailing new line
char *pos;
if ((pos = strchr(line, '\n')) != NULL) {
*pos = '\0';
}
else {
fail_msg("Line longer than buffer.");
}
char *hashes[MAX_NUM_HASHES];
extract_hashes(line, hashes, MAX_NUM_HASHES);
test(hashes);
}
fclose(file);
}