gen_t_subr_prf revision 1.9
11.1Schristos#!/bin/sh
21.1Schristos
31.1Schristoscat << _EOF > $2
41.1Schristos#include <sys/types.h>
51.4Stls#include <sys/time.h>
61.1Schristos#include <stdio.h>
71.1Schristos#include <stdarg.h>
81.1Schristos#include <stdint.h>
91.6Skre#include <stdlib.h>
101.1Schristos#include <string.h>
111.4Stls#include <sha2.h>
121.1Schristos
131.1Schristos#include <atf-c.h>
141.1Schristos
151.3Schristos/* Avoid SSP re-definitions */
161.3Schristos#undef snprintf
171.3Schristos#undef vsnprintf
181.3Schristos#undef sprintf
191.3Schristos#undef vsprintf
201.6Skre#undef vasprintf
211.3Schristos
221.1Schristos#define KPRINTF_BUFSIZE 1024
231.1Schristos#undef putchar
241.1Schristos#define putchar xputchar
251.4Stls
261.8Schristos#define kmem_alloc(n, f)	malloc(n)
271.6Skre
281.9Smrg#define kprintf_lock()		__nothing
291.9Smrg#define kprintf_unlock()	__nothing
301.9Smrg
311.9Smrg/* Arbitrary */
321.9Smrg#define TOCONS	1
331.9Smrg#define TOLOG	2
341.9Smrg
351.9Smrg#define kprintf_internal(f, i1, i2, i3, ...) \
361.9Smrg	printf(f, __VA_ARGS__)
371.9Smrg
381.1Schristosstatic int putchar(char c, int foo, void *b)
391.1Schristos{
401.1Schristos	return fputc(c, stderr);
411.1Schristos}
421.1Schristos
431.1Schristos#define TOBUFONLY 1
441.1Schristosstatic const char HEXDIGITS[] = "0123456789ABCDEF";
451.1Schristosstatic const char hexdigits[] = "0123456789abcdef";
461.1Schristos
471.1Schristostypedef int device_t;
481.1Schristos
491.4Stls#if 0
501.4Stlsstatic SHA512_CTX kprnd_sha;
511.4Stls#endif
521.4Stls
531.4Stls#define timespec timeval
541.4Stls#define nanotime(ts) gettimeofday(ts, NULL)
551.4Stls
561.1Schristos#define device_xname(a) ""
571.2Sjoergint kprintf(const char *, int, void *, char *, va_list) __printflike(1, 0);
581.2Sjoergvoid device_printf(device_t, const char *, ...) __printflike(2, 3);
591.1Schristos
601.1Schristosstatic void
611.1Schristosempty(void)
621.1Schristos{
631.1Schristos}
641.1Schristos
651.1Schristosstatic void (*v_flush)(void) = empty;
661.1Schristos
671.1SchristosATF_TC(snprintf_print);
681.1SchristosATF_TC_HEAD(snprintf_print, tc)
691.1Schristos{
701.8Schristos	atf_tc_set_md_var(tc, "descr", "checks snprintf print");
711.1Schristos}
721.1Schristos 
731.1SchristosATF_TC_BODY(snprintf_print, tc)
741.1Schristos{
751.5Smrg	char buf[13];
761.1Schristos	int i;
771.1Schristos
781.1Schristos	memset(buf, 'x', sizeof(buf));
791.1Schristos	i = snprintf(buf, sizeof(buf), "number %d", 10);
801.1Schristos	ATF_CHECK_EQ(i, 9);
811.1Schristos	ATF_CHECK_STREQ(buf, "number 10");
821.1Schristos}
831.1Schristos
841.1SchristosATF_TC(snprintf_print_overflow);
851.1SchristosATF_TC_HEAD(snprintf_print_overflow, tc)
861.1Schristos{
871.8Schristos	atf_tc_set_md_var(tc, "descr", "checks snprintf print with overflow");
881.1Schristos}
891.1Schristos 
901.1SchristosATF_TC_BODY(snprintf_print_overflow, tc)
911.1Schristos{
921.1Schristos	char buf[10];
931.1Schristos	int i;
941.1Schristos
951.1Schristos	memset(buf, 'x', sizeof(buf));
961.1Schristos	i = snprintf(buf, sizeof(buf), "fjsdfsdjfsdf %d\n", 10);
971.1Schristos	ATF_CHECK_EQ(i, 16);
981.1Schristos	ATF_CHECK_STREQ(buf, "fjsdfsdjf");
991.1Schristos}
1001.1Schristos
1011.1SchristosATF_TC(snprintf_count);
1021.1SchristosATF_TC_HEAD(snprintf_count, tc)
1031.1Schristos{
1041.8Schristos	atf_tc_set_md_var(tc, "descr", "checks snprintf count");
1051.1Schristos}
1061.1Schristos 
1071.1SchristosATF_TC_BODY(snprintf_count, tc)
1081.1Schristos{
1091.1Schristos	int i;
1101.1Schristos	
1111.1Schristos	i = snprintf(NULL, 20, "number %d", 10);
1121.1Schristos	ATF_CHECK_EQ(i, 9);
1131.1Schristos}
1141.1Schristos
1151.1SchristosATF_TC(snprintf_count_overflow);
1161.1SchristosATF_TC_HEAD(snprintf_count_overflow, tc)
1171.1Schristos{
1181.8Schristos	atf_tc_set_md_var(tc, "descr", "checks snprintf count with overflow");
1191.1Schristos}
1201.1Schristos 
1211.1SchristosATF_TC_BODY(snprintf_count_overflow, tc)
1221.1Schristos{
1231.1Schristos	int i;
1241.1Schristos
1251.1Schristos	i = snprintf(NULL, 10, "fjsdfsdjfsdf %d\n", 10);
1261.1Schristos	ATF_CHECK_EQ(i, 16);
1271.1Schristos}
1281.1Schristos
1291.6SkreATF_TC(vasprintf_print);
1301.6SkreATF_TC_HEAD(vasprintf_print, tc)
1311.6Skre{
1321.8Schristos	atf_tc_set_md_var(tc, "descr", "checks vasprintf works");
1331.6Skre}
1341.6Skre
1351.7Schristosstatic int __printflike(2, 3)
1361.6Skrevasp_helper(char **buf, const char *fmt, ...)
1371.6Skre{
1381.6Skre	va_list ap;
1391.6Skre	int ret;
1401.6Skre
1411.6Skre	va_start(ap, fmt);
1421.6Skre	ret = vasprintf(buf, fmt, ap);
1431.6Skre	va_end(ap);
1441.6Skre	return ret;
1451.6Skre}
1461.6Skre
1471.6SkreATF_TC_BODY(vasprintf_print, tc)
1481.6Skre{
1491.6Skre	int i;
1501.6Skre	char *buf;
1511.6Skre
1521.6Skre	buf = NULL;
1531.6Skre	i =  vasp_helper(&buf, "N=%d C=%c S=%s", 7, 'x', "abc");
1541.6Skre	ATF_CHECK_EQ(i, 13);
1551.6Skre	ATF_CHECK(buf != NULL);
1561.6Skre	ATF_CHECK_STREQ(buf, "N=7 C=x S=abc");
1571.6Skre	free(buf);
1581.6Skre}
1591.6Skre
1601.1SchristosATF_TP_ADD_TCS(tp)
1611.1Schristos{
1621.8Schristos	ATF_TP_ADD_TC(tp, snprintf_print);
1631.8Schristos	ATF_TP_ADD_TC(tp, snprintf_print_overflow);
1641.8Schristos	ATF_TP_ADD_TC(tp, snprintf_count);
1651.8Schristos	ATF_TP_ADD_TC(tp, snprintf_count_overflow);
1661.6Skre	ATF_TP_ADD_TC(tp, vasprintf_print);
1671.1Schristos
1681.8Schristos	return atf_no_error();
1691.1Schristos}
1701.1Schristos_EOF
1711.1Schristos
1721.1Schristosawk '
1731.1Schristos/^snprintf\(/ {
1741.1Schristos	print prevline
1751.1Schristos	out = 1
1761.1Schristos}
1771.1Schristos{
1781.1Schristos	if (out) print
1791.1Schristos	else prevline = $0
1801.1Schristos}' $1 >>$2
181