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