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