1 /*
2 * SPDX-License-Identifier: BSD-3-Clause
3 *
4 * Copyright © 2024 Keith Packard
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 *
13 * 2. Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
17 *
18 * 3. Neither the name of the copyright holder nor the names of its
19 * contributors may be used to endorse or promote products derived
20 * from this software without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
27 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
28 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
29 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
31 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
33 * OF THE POSSIBILITY OF SUCH DAMAGE.
34 */
35
36 #define _DEFAULT_SOURCE
37 #include <stdio.h>
38 #include <string.h>
39 #include <stdlib.h>
40
41 #if defined(__PICOLIBC__) && !defined(TINY_STDIO)
42 # define IO_T int
43 # define BUF_T char
44 # ifdef __LARGE64_FILES
45 # define SEEK_T _fpos64_t
46 # else
47 # define SEEK_T fpos_t
48 # endif
49 #else
50 # define IO_T ssize_t
51 # define BUF_T void
52 # define SEEK_T __off_t
53 #endif
54
55 static char test_buf[1024];
56 static SEEK_T test_pos;
57 static SEEK_T test_end;
58
59 #define min(a, b) ((SEEK_T) (a) < (SEEK_T) (b) ? (SEEK_T) (a) : (SEEK_T) (b))
60 #define max(a, b) ((SEEK_T) (a) > (SEEK_T) (b) ? (SEEK_T) (a) : (SEEK_T) (b))
61
62 static IO_T
test_read(void * cookie,BUF_T * buf,size_t n)63 test_read(void *cookie, BUF_T *buf, size_t n)
64 {
65 (void) cookie;
66 n = min(n, test_end - test_pos);
67 memcpy(buf, test_buf + test_pos, n);
68 test_pos += n;
69 return n;
70 }
71
72 static IO_T
test_write(void * cookie,const BUF_T * buf,size_t n)73 test_write(void *cookie, const BUF_T *buf, size_t n)
74 {
75 (void) cookie;
76 n = min(n, sizeof(test_buf) - test_pos);
77 memcpy(test_buf + test_pos, buf, n);
78 test_pos += n;
79 test_end = max(test_end, test_pos);
80 return n;
81 }
82
83 static SEEK_T
test_seek(void * cookie,SEEK_T off,int whence)84 test_seek(void *cookie, SEEK_T off, int whence)
85 {
86 SEEK_T new_pos = test_pos;
87 (void) cookie;
88 switch (whence) {
89 case SEEK_CUR:
90 new_pos = test_pos + off;
91 break;
92 case SEEK_SET:
93 new_pos = off;
94 break;
95 case SEEK_END:
96 new_pos = test_pos + off;
97 break;
98 }
99 test_pos = min(test_end, new_pos);
100 return -1;
101 }
102
103 static int
test_close(void * cookie)104 test_close(void *cookie)
105 {
106 (void) cookie;
107 return 0;
108 }
109
110 #define check(condition, message) do { \
111 if (!(condition)) { \
112 printf("%s: %s\n", message, #condition); \
113 exit(1); \
114 } \
115 } while(0)
116
117 #define MESSAGE "hello, world"
118
main(void)119 int main(void)
120 {
121 FILE *fp = funopen(NULL, test_read, test_write, test_seek, test_close);
122 char buf[sizeof(MESSAGE)];
123 size_t ret;
124
125 fprintf(fp, "%s", MESSAGE);
126 fflush(fp);
127 check(memcmp(test_buf, MESSAGE, strlen(MESSAGE)) == 0, "printf");
128 fseek(fp, 0L, SEEK_SET);
129 ret = fread(buf, 1, sizeof(buf), fp);
130 check(ret == strlen(MESSAGE), "fread size");
131 check(memcmp(buf, MESSAGE, strlen(MESSAGE)) == 0, "fread contents");
132 fclose(fp);
133 return 0;
134 }
135