gen_t_subr_prf revision 1.9 1 1.1 christos #!/bin/sh
2 1.1 christos
3 1.1 christos cat << _EOF > $2
4 1.1 christos #include <sys/types.h>
5 1.4 tls #include <sys/time.h>
6 1.1 christos #include <stdio.h>
7 1.1 christos #include <stdarg.h>
8 1.1 christos #include <stdint.h>
9 1.6 kre #include <stdlib.h>
10 1.1 christos #include <string.h>
11 1.4 tls #include <sha2.h>
12 1.1 christos
13 1.1 christos #include <atf-c.h>
14 1.1 christos
15 1.3 christos /* Avoid SSP re-definitions */
16 1.3 christos #undef snprintf
17 1.3 christos #undef vsnprintf
18 1.3 christos #undef sprintf
19 1.3 christos #undef vsprintf
20 1.6 kre #undef vasprintf
21 1.3 christos
22 1.1 christos #define KPRINTF_BUFSIZE 1024
23 1.1 christos #undef putchar
24 1.1 christos #define putchar xputchar
25 1.4 tls
26 1.8 christos #define kmem_alloc(n, f) malloc(n)
27 1.6 kre
28 1.9 mrg #define kprintf_lock() __nothing
29 1.9 mrg #define kprintf_unlock() __nothing
30 1.9 mrg
31 1.9 mrg /* Arbitrary */
32 1.9 mrg #define TOCONS 1
33 1.9 mrg #define TOLOG 2
34 1.9 mrg
35 1.9 mrg #define kprintf_internal(f, i1, i2, i3, ...) \
36 1.9 mrg printf(f, __VA_ARGS__)
37 1.9 mrg
38 1.1 christos static int putchar(char c, int foo, void *b)
39 1.1 christos {
40 1.1 christos return fputc(c, stderr);
41 1.1 christos }
42 1.1 christos
43 1.1 christos #define TOBUFONLY 1
44 1.1 christos static const char HEXDIGITS[] = "0123456789ABCDEF";
45 1.1 christos static const char hexdigits[] = "0123456789abcdef";
46 1.1 christos
47 1.1 christos typedef int device_t;
48 1.1 christos
49 1.4 tls #if 0
50 1.4 tls static SHA512_CTX kprnd_sha;
51 1.4 tls #endif
52 1.4 tls
53 1.4 tls #define timespec timeval
54 1.4 tls #define nanotime(ts) gettimeofday(ts, NULL)
55 1.4 tls
56 1.1 christos #define device_xname(a) ""
57 1.2 joerg int kprintf(const char *, int, void *, char *, va_list) __printflike(1, 0);
58 1.2 joerg void device_printf(device_t, const char *, ...) __printflike(2, 3);
59 1.1 christos
60 1.1 christos static void
61 1.1 christos empty(void)
62 1.1 christos {
63 1.1 christos }
64 1.1 christos
65 1.1 christos static void (*v_flush)(void) = empty;
66 1.1 christos
67 1.1 christos ATF_TC(snprintf_print);
68 1.1 christos ATF_TC_HEAD(snprintf_print, tc)
69 1.1 christos {
70 1.8 christos atf_tc_set_md_var(tc, "descr", "checks snprintf print");
71 1.1 christos }
72 1.1 christos
73 1.1 christos ATF_TC_BODY(snprintf_print, tc)
74 1.1 christos {
75 1.5 mrg char buf[13];
76 1.1 christos int i;
77 1.1 christos
78 1.1 christos memset(buf, 'x', sizeof(buf));
79 1.1 christos i = snprintf(buf, sizeof(buf), "number %d", 10);
80 1.1 christos ATF_CHECK_EQ(i, 9);
81 1.1 christos ATF_CHECK_STREQ(buf, "number 10");
82 1.1 christos }
83 1.1 christos
84 1.1 christos ATF_TC(snprintf_print_overflow);
85 1.1 christos ATF_TC_HEAD(snprintf_print_overflow, tc)
86 1.1 christos {
87 1.8 christos atf_tc_set_md_var(tc, "descr", "checks snprintf print with overflow");
88 1.1 christos }
89 1.1 christos
90 1.1 christos ATF_TC_BODY(snprintf_print_overflow, tc)
91 1.1 christos {
92 1.1 christos char buf[10];
93 1.1 christos int i;
94 1.1 christos
95 1.1 christos memset(buf, 'x', sizeof(buf));
96 1.1 christos i = snprintf(buf, sizeof(buf), "fjsdfsdjfsdf %d\n", 10);
97 1.1 christos ATF_CHECK_EQ(i, 16);
98 1.1 christos ATF_CHECK_STREQ(buf, "fjsdfsdjf");
99 1.1 christos }
100 1.1 christos
101 1.1 christos ATF_TC(snprintf_count);
102 1.1 christos ATF_TC_HEAD(snprintf_count, tc)
103 1.1 christos {
104 1.8 christos atf_tc_set_md_var(tc, "descr", "checks snprintf count");
105 1.1 christos }
106 1.1 christos
107 1.1 christos ATF_TC_BODY(snprintf_count, tc)
108 1.1 christos {
109 1.1 christos int i;
110 1.1 christos
111 1.1 christos i = snprintf(NULL, 20, "number %d", 10);
112 1.1 christos ATF_CHECK_EQ(i, 9);
113 1.1 christos }
114 1.1 christos
115 1.1 christos ATF_TC(snprintf_count_overflow);
116 1.1 christos ATF_TC_HEAD(snprintf_count_overflow, tc)
117 1.1 christos {
118 1.8 christos atf_tc_set_md_var(tc, "descr", "checks snprintf count with overflow");
119 1.1 christos }
120 1.1 christos
121 1.1 christos ATF_TC_BODY(snprintf_count_overflow, tc)
122 1.1 christos {
123 1.1 christos int i;
124 1.1 christos
125 1.1 christos i = snprintf(NULL, 10, "fjsdfsdjfsdf %d\n", 10);
126 1.1 christos ATF_CHECK_EQ(i, 16);
127 1.1 christos }
128 1.1 christos
129 1.6 kre ATF_TC(vasprintf_print);
130 1.6 kre ATF_TC_HEAD(vasprintf_print, tc)
131 1.6 kre {
132 1.8 christos atf_tc_set_md_var(tc, "descr", "checks vasprintf works");
133 1.6 kre }
134 1.6 kre
135 1.7 christos static int __printflike(2, 3)
136 1.6 kre vasp_helper(char **buf, const char *fmt, ...)
137 1.6 kre {
138 1.6 kre va_list ap;
139 1.6 kre int ret;
140 1.6 kre
141 1.6 kre va_start(ap, fmt);
142 1.6 kre ret = vasprintf(buf, fmt, ap);
143 1.6 kre va_end(ap);
144 1.6 kre return ret;
145 1.6 kre }
146 1.6 kre
147 1.6 kre ATF_TC_BODY(vasprintf_print, tc)
148 1.6 kre {
149 1.6 kre int i;
150 1.6 kre char *buf;
151 1.6 kre
152 1.6 kre buf = NULL;
153 1.6 kre i = vasp_helper(&buf, "N=%d C=%c S=%s", 7, 'x', "abc");
154 1.6 kre ATF_CHECK_EQ(i, 13);
155 1.6 kre ATF_CHECK(buf != NULL);
156 1.6 kre ATF_CHECK_STREQ(buf, "N=7 C=x S=abc");
157 1.6 kre free(buf);
158 1.6 kre }
159 1.6 kre
160 1.1 christos ATF_TP_ADD_TCS(tp)
161 1.1 christos {
162 1.8 christos ATF_TP_ADD_TC(tp, snprintf_print);
163 1.8 christos ATF_TP_ADD_TC(tp, snprintf_print_overflow);
164 1.8 christos ATF_TP_ADD_TC(tp, snprintf_count);
165 1.8 christos ATF_TP_ADD_TC(tp, snprintf_count_overflow);
166 1.6 kre ATF_TP_ADD_TC(tp, vasprintf_print);
167 1.1 christos
168 1.8 christos return atf_no_error();
169 1.1 christos }
170 1.1 christos _EOF
171 1.1 christos
172 1.1 christos awk '
173 1.1 christos /^snprintf\(/ {
174 1.1 christos print prevline
175 1.1 christos out = 1
176 1.1 christos }
177 1.1 christos {
178 1.1 christos if (out) print
179 1.1 christos else prevline = $0
180 1.1 christos }' $1 >>$2
181