gen_t_subr_prf revision 1.6 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 static int putchar(char c, int foo, void *b)
29 {
30 return fputc(c, stderr);
31 }
32
33 #define TOBUFONLY 1
34 static const char HEXDIGITS[] = "0123456789ABCDEF";
35 static const char hexdigits[] = "0123456789abcdef";
36
37 typedef int device_t;
38
39 #if 0
40 static SHA512_CTX kprnd_sha;
41 #endif
42
43 #define timespec timeval
44 #define nanotime(ts) gettimeofday(ts, NULL)
45
46 #define device_xname(a) ""
47 int kprintf(const char *, int, void *, char *, va_list) __printflike(1, 0);
48 void device_printf(device_t, const char *, ...) __printflike(2, 3);
49
50 static void
51 empty(void)
52 {
53 }
54
55 static void (*v_flush)(void) = empty;
56
57 ATF_TC(snprintf_print);
58 ATF_TC_HEAD(snprintf_print, tc)
59 {
60 atf_tc_set_md_var(tc, "descr", "checks snprintf print");
61 }
62
63 ATF_TC_BODY(snprintf_print, tc)
64 {
65 char buf[13];
66 int i;
67
68 memset(buf, 'x', sizeof(buf));
69 i = snprintf(buf, sizeof(buf), "number %d", 10);
70 ATF_CHECK_EQ(i, 9);
71 ATF_CHECK_STREQ(buf, "number 10");
72 }
73
74 ATF_TC(snprintf_print_overflow);
75 ATF_TC_HEAD(snprintf_print_overflow, tc)
76 {
77 atf_tc_set_md_var(tc, "descr", "checks snprintf print with overflow");
78 }
79
80 ATF_TC_BODY(snprintf_print_overflow, tc)
81 {
82 char buf[10];
83 int i;
84
85 memset(buf, 'x', sizeof(buf));
86 i = snprintf(buf, sizeof(buf), "fjsdfsdjfsdf %d\n", 10);
87 ATF_CHECK_EQ(i, 16);
88 ATF_CHECK_STREQ(buf, "fjsdfsdjf");
89 }
90
91 ATF_TC(snprintf_count);
92 ATF_TC_HEAD(snprintf_count, tc)
93 {
94 atf_tc_set_md_var(tc, "descr", "checks snprintf count");
95 }
96
97 ATF_TC_BODY(snprintf_count, tc)
98 {
99 int i;
100
101 i = snprintf(NULL, 20, "number %d", 10);
102 ATF_CHECK_EQ(i, 9);
103 }
104
105 ATF_TC(snprintf_count_overflow);
106 ATF_TC_HEAD(snprintf_count_overflow, tc)
107 {
108 atf_tc_set_md_var(tc, "descr", "checks snprintf count with overflow");
109 }
110
111 ATF_TC_BODY(snprintf_count_overflow, tc)
112 {
113 int i;
114
115 i = snprintf(NULL, 10, "fjsdfsdjfsdf %d\n", 10);
116 ATF_CHECK_EQ(i, 16);
117 }
118
119 ATF_TC(vasprintf_print);
120 ATF_TC_HEAD(vasprintf_print, tc)
121 {
122 atf_tc_set_md_var(tc, "descr", "checks vasprintf works");
123 }
124
125 int vasp_helper(char **, const char *, ...);
126
127 int
128 vasp_helper(char **buf, const char *fmt, ...)
129 {
130 va_list ap;
131 int ret;
132
133 va_start(ap, fmt);
134 ret = vasprintf(buf, fmt, ap);
135 va_end(ap);
136 return ret;
137 }
138
139 ATF_TC_BODY(vasprintf_print, tc)
140 {
141 int i;
142 char *buf;
143
144 buf = NULL;
145 i = vasp_helper(&buf, "N=%d C=%c S=%s", 7, 'x', "abc");
146 ATF_CHECK_EQ(i, 13);
147 ATF_CHECK(buf != NULL);
148 ATF_CHECK_STREQ(buf, "N=7 C=x S=abc");
149 free(buf);
150 }
151
152 ATF_TP_ADD_TCS(tp)
153 {
154 ATF_TP_ADD_TC(tp, snprintf_print);
155 ATF_TP_ADD_TC(tp, snprintf_print_overflow);
156 ATF_TP_ADD_TC(tp, snprintf_count);
157 ATF_TP_ADD_TC(tp, snprintf_count_overflow);
158 ATF_TP_ADD_TC(tp, vasprintf_print);
159
160 return atf_no_error();
161 }
162 _EOF
163
164 awk '
165 /^snprintf\(/ {
166 print prevline
167 out = 1
168 }
169 {
170 if (out) print
171 else prevline = $0
172 }' $1 >>$2
173