2 * netsniff-ng - the packet sniffing beast
3 * By Daniel Borkmann <daniel@netsniff-ng.org>
4 * Copyright 2009, 2010 Daniel Borkmann.
5 * Subject to the GPL, version 2.
18 #define term_trailing_size 5
19 #define term_starting_size 3
20 #define term_curr_size (get_tty_size() - term_trailing_size)
22 static char buffer
[1024];
23 static volatile size_t buffer_use
= 0;
24 static struct spinlock buffer_lock
;
26 static inline void __tprintf_flush_newline(void)
30 for (i
= 0; i
< term_starting_size
; ++i
)
34 static inline int __tprintf_flush_skip(char *buffer
, int i
, size_t max
)
37 if (val
== ' ' || val
== ',')
42 static void __tprintf_flush(void)
45 static ssize_t line_count
= 0;
46 size_t term_len
= term_curr_size
;
48 for (i
= 0; i
< buffer_use
; ++i
) {
49 if (buffer
[i
] == '\n') {
50 term_len
= term_curr_size
;
53 if (line_count
== term_len
) {
54 __tprintf_flush_newline();
55 line_count
= term_starting_size
;
56 while (i
< buffer_use
&&
57 __tprintf_flush_skip(buffer
, i
, buffer_use
))
61 fputc(buffer
[i
], stdout
);
66 access_once(buffer_use
) = 0;
69 void tprintf_flush(void)
71 spinlock_lock(&buffer_lock
);
73 spinlock_unlock(&buffer_lock
);
76 void tprintf_init(void)
78 spinlock_init(&buffer_lock
);
81 void tprintf_cleanup(void)
84 spinlock_destroy(&buffer_lock
);
87 void tprintf(char *msg
, ...)
93 spinlock_lock(&buffer_lock
);
95 avail
= sizeof(buffer
) - buffer_use
;
99 ret
= vsnprintf(buffer
+ buffer_use
, avail
, msg
, vl
);
103 panic("vsnprintf screwed up in tprintf!\n");
104 if (ret
> sizeof(buffer
))
105 panic("No mem in tprintf left!\n");
109 avail
= sizeof(buffer
) - buffer_use
;
113 ret
= vsnprintf(buffer
+ buffer_use
, avail
, msg
, vl
);
117 panic("vsnprintf screwed up in tprintf!\n");
122 spinlock_unlock(&buffer_lock
);