1 | #include <sys/types.h> |
2 | #include <sys/stat.h> |
3 | #include <fcntl.h> |
4 | #include <errno.h> |
5 | |
6 | #include <stdio.h> |
7 | |
8 | |
9 | #define THE_COOKIE ((void *) 0xdeadbeeful) |
10 | |
11 | static int errors; |
12 | |
13 | |
14 | static int cookieread_called; |
15 | static ssize_t |
16 | cookieread (void *cookie, char *buf, size_t count) |
17 | { |
18 | printf (format: "`%s' called with cookie %#lx\n" , __FUNCTION__, |
19 | (unsigned long int) cookie); |
20 | if (cookie != THE_COOKIE) |
21 | ++errors; |
22 | cookieread_called = 1; |
23 | return 42; |
24 | } |
25 | |
26 | |
27 | static int cookiewrite_called; |
28 | static ssize_t |
29 | cookiewrite (void *cookie, const char *buf, size_t count) |
30 | { |
31 | printf (format: "`%s' called with cookie %#lx\n" , __FUNCTION__, |
32 | (unsigned long int) cookie); |
33 | if (cookie != THE_COOKIE) |
34 | ++errors; |
35 | cookiewrite_called = 1; |
36 | return 43; |
37 | } |
38 | |
39 | |
40 | static int cookieseek_called; |
41 | static int |
42 | cookieseek (void *cookie, off64_t *offset, int whence) |
43 | { |
44 | printf (format: "`%s' called with cookie %#lx\n" , __FUNCTION__, |
45 | (unsigned long int) cookie); |
46 | if (cookie != THE_COOKIE) |
47 | ++errors; |
48 | cookieseek_called = 1; |
49 | return 44; |
50 | } |
51 | |
52 | |
53 | static int cookieclose_called; |
54 | static int |
55 | cookieclose (void *cookie) |
56 | { |
57 | printf (format: "`%s' called with cookie %#lx\n" , __FUNCTION__, |
58 | (unsigned long int) cookie); |
59 | if (cookie != THE_COOKIE) |
60 | ++errors; |
61 | cookieclose_called = 1; |
62 | return 45; |
63 | } |
64 | |
65 | |
66 | static int |
67 | do_test (void) |
68 | { |
69 | cookie_io_functions_t fcts; |
70 | char buf[1]; |
71 | FILE *f; |
72 | |
73 | fcts.read = cookieread; |
74 | fcts.seek = cookieseek; |
75 | fcts.close = cookieclose; |
76 | fcts.write = cookiewrite; |
77 | |
78 | f = fopencookie (THE_COOKIE, modes: "r+" , io_funcs: fcts); |
79 | |
80 | fread (ptr: buf, size: 1, n: 1, stream: f); |
81 | fwrite (buf, 1, 1, f); |
82 | fseek (f, 0, SEEK_CUR); |
83 | fclose (f); |
84 | |
85 | if (cookieread_called == 0 |
86 | || cookiewrite_called == 0 |
87 | || cookieseek_called == 0 |
88 | || cookieclose_called == 0) |
89 | ++errors; |
90 | |
91 | return errors != 0; |
92 | } |
93 | |
94 | #define TEST_FUNCTION do_test () |
95 | #include "../test-skeleton.c" |
96 | |