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

source code of glibc/stdio-common/tst-cookie.c