gen_t_subr_prf revision 1.2
11.1Schristos#!/bin/sh
21.1Schristos
31.1Schristoscat << _EOF > $2
41.1Schristos#include <sys/types.h>
51.1Schristos#include <stdio.h>
61.1Schristos#include <stdarg.h>
71.1Schristos#include <stdint.h>
81.1Schristos#include <string.h>
91.1Schristos
101.1Schristos#include <atf-c.h>
111.1Schristos
121.1Schristos#define KPRINTF_BUFSIZE 1024
131.1Schristos#undef putchar
141.1Schristos#define putchar xputchar
151.1Schristosstatic int putchar(char c, int foo, void *b)
161.1Schristos{
171.1Schristos	return fputc(c, stderr);
181.1Schristos}
191.1Schristos
201.1Schristos#define TOBUFONLY 1
211.1Schristosstatic const char HEXDIGITS[] = "0123456789ABCDEF";
221.1Schristosstatic const char hexdigits[] = "0123456789abcdef";
231.1Schristos
241.1Schristostypedef int device_t;
251.1Schristos
261.1Schristos#define device_xname(a) ""
271.2Sjoergint kprintf(const char *, int, void *, char *, va_list) __printflike(1, 0);
281.2Sjoergvoid device_printf(device_t, const char *, ...) __printflike(2, 3);
291.1Schristos
301.1Schristosstatic void
311.1Schristosempty(void)
321.1Schristos{
331.1Schristos}
341.1Schristos
351.1Schristosstatic void (*v_flush)(void) = empty;
361.1Schristos
371.1SchristosATF_TC(snprintf_print);
381.1SchristosATF_TC_HEAD(snprintf_print, tc)
391.1Schristos{
401.1Schristos        atf_tc_set_md_var(tc, "descr", "checks snprintf print");
411.1Schristos}
421.1Schristos 
431.1SchristosATF_TC_BODY(snprintf_print, tc)
441.1Schristos{
451.1Schristos	char buf[10];
461.1Schristos	int i;
471.1Schristos
481.1Schristos	memset(buf, 'x', sizeof(buf));
491.1Schristos	i = snprintf(buf, sizeof(buf), "number %d", 10);
501.1Schristos	ATF_CHECK_EQ(i, 9);
511.1Schristos	ATF_CHECK_STREQ(buf, "number 10");
521.1Schristos}
531.1Schristos
541.1SchristosATF_TC(snprintf_print_overflow);
551.1SchristosATF_TC_HEAD(snprintf_print_overflow, tc)
561.1Schristos{
571.1Schristos        atf_tc_set_md_var(tc, "descr", "checks snprintf print with overflow");
581.1Schristos}
591.1Schristos 
601.1SchristosATF_TC_BODY(snprintf_print_overflow, tc)
611.1Schristos{
621.1Schristos	char buf[10];
631.1Schristos	int i;
641.1Schristos
651.1Schristos	memset(buf, 'x', sizeof(buf));
661.1Schristos	i = snprintf(buf, sizeof(buf), "fjsdfsdjfsdf %d\n", 10);
671.1Schristos	ATF_CHECK_EQ(i, 16);
681.1Schristos	ATF_CHECK_STREQ(buf, "fjsdfsdjf");
691.1Schristos}
701.1Schristos
711.1SchristosATF_TC(snprintf_count);
721.1SchristosATF_TC_HEAD(snprintf_count, tc)
731.1Schristos{
741.1Schristos        atf_tc_set_md_var(tc, "descr", "checks snprintf count");
751.1Schristos}
761.1Schristos 
771.1SchristosATF_TC_BODY(snprintf_count, tc)
781.1Schristos{
791.1Schristos	int i;
801.1Schristos	
811.1Schristos	i = snprintf(NULL, 20, "number %d", 10);
821.1Schristos	ATF_CHECK_EQ(i, 9);
831.1Schristos}
841.1Schristos
851.1SchristosATF_TC(snprintf_count_overflow);
861.1SchristosATF_TC_HEAD(snprintf_count_overflow, tc)
871.1Schristos{
881.1Schristos        atf_tc_set_md_var(tc, "descr", "checks snprintf count with overflow");
891.1Schristos}
901.1Schristos 
911.1SchristosATF_TC_BODY(snprintf_count_overflow, tc)
921.1Schristos{
931.1Schristos	int i;
941.1Schristos
951.1Schristos	i = snprintf(NULL, 10, "fjsdfsdjfsdf %d\n", 10);
961.1Schristos	ATF_CHECK_EQ(i, 16);
971.1Schristos}
981.1Schristos
991.1SchristosATF_TP_ADD_TCS(tp)
1001.1Schristos{
1011.1Schristos        ATF_TP_ADD_TC(tp, snprintf_print);
1021.1Schristos        ATF_TP_ADD_TC(tp, snprintf_print_overflow);
1031.1Schristos        ATF_TP_ADD_TC(tp, snprintf_count);
1041.1Schristos        ATF_TP_ADD_TC(tp, snprintf_count_overflow);
1051.1Schristos
1061.1Schristos        return atf_no_error();
1071.1Schristos}
1081.1Schristos_EOF
1091.1Schristos
1101.1Schristosawk '
1111.1Schristos/^snprintf\(/ {
1121.1Schristos	print prevline
1131.1Schristos	out = 1
1141.1Schristos}
1151.1Schristos{
1161.1Schristos	if (out) print
1171.1Schristos	else prevline = $0
1181.1Schristos}' $1 >>$2
119