gen_t_subr_prf revision 1.6
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.6Skre#define kmem_alloc(n, f) malloc(n) 271.6Skre 281.1Schristosstatic int putchar(char c, int foo, void *b) 291.1Schristos{ 301.1Schristos return fputc(c, stderr); 311.1Schristos} 321.1Schristos 331.1Schristos#define TOBUFONLY 1 341.1Schristosstatic const char HEXDIGITS[] = "0123456789ABCDEF"; 351.1Schristosstatic const char hexdigits[] = "0123456789abcdef"; 361.1Schristos 371.1Schristostypedef int device_t; 381.1Schristos 391.4Stls#if 0 401.4Stlsstatic SHA512_CTX kprnd_sha; 411.4Stls#endif 421.4Stls 431.4Stls#define timespec timeval 441.4Stls#define nanotime(ts) gettimeofday(ts, NULL) 451.4Stls 461.1Schristos#define device_xname(a) "" 471.2Sjoergint kprintf(const char *, int, void *, char *, va_list) __printflike(1, 0); 481.2Sjoergvoid device_printf(device_t, const char *, ...) __printflike(2, 3); 491.1Schristos 501.1Schristosstatic void 511.1Schristosempty(void) 521.1Schristos{ 531.1Schristos} 541.1Schristos 551.1Schristosstatic void (*v_flush)(void) = empty; 561.1Schristos 571.1SchristosATF_TC(snprintf_print); 581.1SchristosATF_TC_HEAD(snprintf_print, tc) 591.1Schristos{ 601.1Schristos atf_tc_set_md_var(tc, "descr", "checks snprintf print"); 611.1Schristos} 621.1Schristos 631.1SchristosATF_TC_BODY(snprintf_print, tc) 641.1Schristos{ 651.5Smrg char buf[13]; 661.1Schristos int i; 671.1Schristos 681.1Schristos memset(buf, 'x', sizeof(buf)); 691.1Schristos i = snprintf(buf, sizeof(buf), "number %d", 10); 701.1Schristos ATF_CHECK_EQ(i, 9); 711.1Schristos ATF_CHECK_STREQ(buf, "number 10"); 721.1Schristos} 731.1Schristos 741.1SchristosATF_TC(snprintf_print_overflow); 751.1SchristosATF_TC_HEAD(snprintf_print_overflow, tc) 761.1Schristos{ 771.1Schristos atf_tc_set_md_var(tc, "descr", "checks snprintf print with overflow"); 781.1Schristos} 791.1Schristos 801.1SchristosATF_TC_BODY(snprintf_print_overflow, tc) 811.1Schristos{ 821.1Schristos char buf[10]; 831.1Schristos int i; 841.1Schristos 851.1Schristos memset(buf, 'x', sizeof(buf)); 861.1Schristos i = snprintf(buf, sizeof(buf), "fjsdfsdjfsdf %d\n", 10); 871.1Schristos ATF_CHECK_EQ(i, 16); 881.1Schristos ATF_CHECK_STREQ(buf, "fjsdfsdjf"); 891.1Schristos} 901.1Schristos 911.1SchristosATF_TC(snprintf_count); 921.1SchristosATF_TC_HEAD(snprintf_count, tc) 931.1Schristos{ 941.1Schristos atf_tc_set_md_var(tc, "descr", "checks snprintf count"); 951.1Schristos} 961.1Schristos 971.1SchristosATF_TC_BODY(snprintf_count, tc) 981.1Schristos{ 991.1Schristos int i; 1001.1Schristos 1011.1Schristos i = snprintf(NULL, 20, "number %d", 10); 1021.1Schristos ATF_CHECK_EQ(i, 9); 1031.1Schristos} 1041.1Schristos 1051.1SchristosATF_TC(snprintf_count_overflow); 1061.1SchristosATF_TC_HEAD(snprintf_count_overflow, tc) 1071.1Schristos{ 1081.1Schristos atf_tc_set_md_var(tc, "descr", "checks snprintf count with overflow"); 1091.1Schristos} 1101.1Schristos 1111.1SchristosATF_TC_BODY(snprintf_count_overflow, tc) 1121.1Schristos{ 1131.1Schristos int i; 1141.1Schristos 1151.1Schristos i = snprintf(NULL, 10, "fjsdfsdjfsdf %d\n", 10); 1161.1Schristos ATF_CHECK_EQ(i, 16); 1171.1Schristos} 1181.1Schristos 1191.6SkreATF_TC(vasprintf_print); 1201.6SkreATF_TC_HEAD(vasprintf_print, tc) 1211.6Skre{ 1221.6Skre atf_tc_set_md_var(tc, "descr", "checks vasprintf works"); 1231.6Skre} 1241.6Skre 1251.6Skreint vasp_helper(char **, const char *, ...); 1261.6Skre 1271.6Skreint 1281.6Skrevasp_helper(char **buf, const char *fmt, ...) 1291.6Skre{ 1301.6Skre va_list ap; 1311.6Skre int ret; 1321.6Skre 1331.6Skre va_start(ap, fmt); 1341.6Skre ret = vasprintf(buf, fmt, ap); 1351.6Skre va_end(ap); 1361.6Skre return ret; 1371.6Skre} 1381.6Skre 1391.6SkreATF_TC_BODY(vasprintf_print, tc) 1401.6Skre{ 1411.6Skre int i; 1421.6Skre char *buf; 1431.6Skre 1441.6Skre buf = NULL; 1451.6Skre i = vasp_helper(&buf, "N=%d C=%c S=%s", 7, 'x', "abc"); 1461.6Skre ATF_CHECK_EQ(i, 13); 1471.6Skre ATF_CHECK(buf != NULL); 1481.6Skre ATF_CHECK_STREQ(buf, "N=7 C=x S=abc"); 1491.6Skre free(buf); 1501.6Skre} 1511.6Skre 1521.1SchristosATF_TP_ADD_TCS(tp) 1531.1Schristos{ 1541.1Schristos ATF_TP_ADD_TC(tp, snprintf_print); 1551.1Schristos ATF_TP_ADD_TC(tp, snprintf_print_overflow); 1561.1Schristos ATF_TP_ADD_TC(tp, snprintf_count); 1571.1Schristos ATF_TP_ADD_TC(tp, snprintf_count_overflow); 1581.6Skre ATF_TP_ADD_TC(tp, vasprintf_print); 1591.1Schristos 1601.1Schristos return atf_no_error(); 1611.1Schristos} 1621.1Schristos_EOF 1631.1Schristos 1641.1Schristosawk ' 1651.1Schristos/^snprintf\(/ { 1661.1Schristos print prevline 1671.1Schristos out = 1 1681.1Schristos} 1691.1Schristos{ 1701.1Schristos if (out) print 1711.1Schristos else prevline = $0 1721.1Schristos}' $1 >>$2 173