[go: up one dir, main page]

File: file.c

package info (click to toggle)
criterion 2.4.1-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, sid, trixie
  • size: 3,864 kB
  • sloc: ansic: 17,945; cpp: 774; python: 74; makefile: 25; sh: 19
file content (101 lines) | stat: -rw-r--r-- 1,963 bytes parent folder | download | duplicates (2)
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#include <criterion/redirect.h>

int cr_file_match_str(FILE *f, const char *str)
{
    size_t len = strlen(str);

    char buf[512];
    size_t read;
    int matches = 1;

    while ((read = fread(buf, 1, sizeof (buf), f)) > 0) {
        matches = !strncmp(buf, str, read);
        if (!matches || read > len) {
            matches = 0;
            break;
        }

        len -= read;
        str += read;
    }

    /* consume the rest of what's available */
    if (read)
        while (fread(buf, 1, sizeof (buf), f) > 0) ;

    /* there are more bytes in str than in f */
    if (len) {
        return 0;
    }

    return matches;
}

int cr_file_match_file(FILE *f, FILE *ref)
{
    if (f == ref)
        return true;

    char buf1[512];
    char buf2[512];

    fpos_t orig_pos;
    fgetpos(ref, &orig_pos);
    rewind(ref);

    size_t read1 = 1, read2 = 1;
    int matches = 1;
    while (matches
            && (read1 = fread(buf1, 1, sizeof (buf1), f)) > 0
            && (read2 = fread(buf2, 1, sizeof (buf2), ref)) > 0) {
        if (read1 != read2) {
            matches = 0;
            break;
        }

        matches = !memcmp(buf1, buf2, read1);
    }

    /* consume the rest of what's available */
    while (fread(buf1, 1, sizeof (buf1), f) > 0) ;

    fsetpos(ref, &orig_pos);

    return matches;
}

int cr_stdout_match_file(FILE *ref)
{
    FILE *f = cr_get_redirected_stdout();
    int res = cr_file_match_file(f, ref);

    fclose(f);
    return res;
}

int cr_stdout_match_str(const char *ref)
{
    FILE *f = cr_get_redirected_stdout();
    int res = cr_file_match_str(f, ref);

    fclose(f);
    return res;
}

int cr_stderr_match_file(FILE *ref)
{
    FILE *f = cr_get_redirected_stderr();
    int res = cr_file_match_file(f, ref);

    fclose(f);
    return res;
}

int cr_stderr_match_str(const char *ref)
{
    FILE *f = cr_get_redirected_stderr();
    int res = cr_file_match_str(f, ref);

    fclose(f);
    return res;
}