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 static char buffer
[1024];
19 static size_t buffer_use
= 0;
20 static struct spinlock buffer_lock
;
21 static size_t lcount
= 0;
23 size_t tprintf_get_free_count(void)
27 spinlock_lock(&buffer_lock
);
28 ret
= get_tty_size() - 5 - lcount
;
29 spinlock_unlock(&buffer_lock
);
35 * We want to print our stuff terminal aligned. Since we're printing packets
36 * we're in slowpath anyways. If stdin/stdout are connected to a terminal
37 * then default size = 1024; else size = 4096.
39 void tprintf_flush(void)
42 size_t flush_len
= get_tty_size() - 5;
44 while (buffer_use
-- > 0) {
45 if (lcount
== flush_len
) {
48 while (buffer_use
> 0 && (*ptr
== ' ' ||
49 *ptr
== ',' || *ptr
== '\n')) {
56 flush_len
= get_tty_size() - 5;
60 /* Collect in stream buffer. */
69 bug_on(buffer_use
> 0);
72 void tprintf_init(void)
74 spinlock_init(&buffer_lock
);
75 memset(buffer
, 0, sizeof(buffer
));
78 void tprintf_cleanup(void)
80 spinlock_lock(&buffer_lock
);
82 spinlock_unlock(&buffer_lock
);
84 spinlock_destroy(&buffer_lock
);
87 void tprintf(char *msg
, ...)
94 spinlock_lock(&buffer_lock
);
95 ret
= vsnprintf(buffer
+ buffer_use
,
96 sizeof(buffer
) - buffer_use
,
99 /* Something screwed up! Unexpected. */
101 if (ret
>= sizeof(buffer
) - buffer_use
) {
103 /* Rewrite the buffer */
104 ret
= vsnprintf(buffer
+ buffer_use
,
105 sizeof(buffer
) - buffer_use
,
108 /* Again, we've failed! This shouldn't happen! So
109 * switch to vfprintf temporarily :-( */
110 if (ret
>= sizeof(buffer
) - buffer_use
) {
111 fprintf(stderr
, "BUG (buffer too large) -->\n");
112 vfprintf(stdout
, msg
, vl
);
113 fprintf(stderr
, " <--\n");
118 if (ret
< sizeof(buffer
) - buffer_use
)
121 spinlock_unlock(&buffer_lock
);