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