Home | History | Annotate | Line # | Download | only in kernel
gen_t_subr_prf revision 1.4
      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 <string.h>
     10 #include <sha2.h>
     11 
     12 #include <atf-c.h>
     13 
     14 /* Avoid SSP re-definitions */
     15 #undef snprintf
     16 #undef vsnprintf
     17 #undef sprintf
     18 #undef vsprintf
     19 
     20 #define KPRINTF_BUFSIZE 1024
     21 #undef putchar
     22 #define putchar xputchar
     23 
     24 static int putchar(char c, int foo, void *b)
     25 {
     26 	return fputc(c, stderr);
     27 }
     28 
     29 #define TOBUFONLY 1
     30 static const char HEXDIGITS[] = "0123456789ABCDEF";
     31 static const char hexdigits[] = "0123456789abcdef";
     32 
     33 typedef int device_t;
     34 
     35 #if 0
     36 static SHA512_CTX kprnd_sha;
     37 #endif
     38 
     39 #define timespec timeval
     40 #define nanotime(ts) gettimeofday(ts, NULL)
     41 
     42 #define device_xname(a) ""
     43 int kprintf(const char *, int, void *, char *, va_list) __printflike(1, 0);
     44 void device_printf(device_t, const char *, ...) __printflike(2, 3);
     45 
     46 static void
     47 empty(void)
     48 {
     49 }
     50 
     51 static void (*v_flush)(void) = empty;
     52 
     53 ATF_TC(snprintf_print);
     54 ATF_TC_HEAD(snprintf_print, tc)
     55 {
     56         atf_tc_set_md_var(tc, "descr", "checks snprintf print");
     57 }
     58  
     59 ATF_TC_BODY(snprintf_print, tc)
     60 {
     61 	char buf[10];
     62 	int i;
     63 
     64 	memset(buf, 'x', sizeof(buf));
     65 	i = snprintf(buf, sizeof(buf), "number %d", 10);
     66 	ATF_CHECK_EQ(i, 9);
     67 	ATF_CHECK_STREQ(buf, "number 10");
     68 }
     69 
     70 ATF_TC(snprintf_print_overflow);
     71 ATF_TC_HEAD(snprintf_print_overflow, tc)
     72 {
     73         atf_tc_set_md_var(tc, "descr", "checks snprintf print with overflow");
     74 }
     75  
     76 ATF_TC_BODY(snprintf_print_overflow, tc)
     77 {
     78 	char buf[10];
     79 	int i;
     80 
     81 	memset(buf, 'x', sizeof(buf));
     82 	i = snprintf(buf, sizeof(buf), "fjsdfsdjfsdf %d\n", 10);
     83 	ATF_CHECK_EQ(i, 16);
     84 	ATF_CHECK_STREQ(buf, "fjsdfsdjf");
     85 }
     86 
     87 ATF_TC(snprintf_count);
     88 ATF_TC_HEAD(snprintf_count, tc)
     89 {
     90         atf_tc_set_md_var(tc, "descr", "checks snprintf count");
     91 }
     92  
     93 ATF_TC_BODY(snprintf_count, tc)
     94 {
     95 	int i;
     96 	
     97 	i = snprintf(NULL, 20, "number %d", 10);
     98 	ATF_CHECK_EQ(i, 9);
     99 }
    100 
    101 ATF_TC(snprintf_count_overflow);
    102 ATF_TC_HEAD(snprintf_count_overflow, tc)
    103 {
    104         atf_tc_set_md_var(tc, "descr", "checks snprintf count with overflow");
    105 }
    106  
    107 ATF_TC_BODY(snprintf_count_overflow, tc)
    108 {
    109 	int i;
    110 
    111 	i = snprintf(NULL, 10, "fjsdfsdjfsdf %d\n", 10);
    112 	ATF_CHECK_EQ(i, 16);
    113 }
    114 
    115 ATF_TP_ADD_TCS(tp)
    116 {
    117         ATF_TP_ADD_TC(tp, snprintf_print);
    118         ATF_TP_ADD_TC(tp, snprintf_print_overflow);
    119         ATF_TP_ADD_TC(tp, snprintf_count);
    120         ATF_TP_ADD_TC(tp, snprintf_count_overflow);
    121 
    122         return atf_no_error();
    123 }
    124 _EOF
    125 
    126 awk '
    127 /^snprintf\(/ {
    128 	print prevline
    129 	out = 1
    130 }
    131 {
    132 	if (out) print
    133 	else prevline = $0
    134 }' $1 >>$2
    135