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
21 #define term_curr_size (get_tty_size() - term_trailing_size)
23 static char buffer
[1024];
25 static volatile size_t buffer_use
= 0;
27 static struct spinlock buffer_lock
;
29 static inline void __tprintf_flush_newline(void)
34 for (i
= 0; i
< term_starting_size
; ++i
)
38 static inline int __tprintf_flush_skip(char *buffer
, int i
, size_t max
)
42 if (val
== ' ' || val
== ',')
48 static void __tprintf_flush(void)
51 static ssize_t line_count
= 0;
52 size_t term_len
= term_curr_size
;
54 for (i
= 0; i
< buffer_use
; ++i
) {
55 if (buffer
[i
] == '\n') {
56 term_len
= term_curr_size
;
60 if (line_count
== term_len
) {
61 __tprintf_flush_newline();
62 line_count
= term_starting_size
;
64 while (i
< buffer_use
&&
65 __tprintf_flush_skip(buffer
, i
, buffer_use
))
69 fputc(buffer
[i
], stdout
);
77 void tprintf_flush(void)
79 spinlock_lock(&buffer_lock
);
81 spinlock_unlock(&buffer_lock
);
84 void tprintf_init(void)
86 spinlock_init(&buffer_lock
);
88 setvbuf(stdout
, NULL
, _IONBF
, 0);
89 setvbuf(stderr
, NULL
, _IONBF
, 0);
92 void tprintf_cleanup(void)
95 spinlock_destroy(&buffer_lock
);
98 void tprintf(char *msg
, ...)
104 spinlock_lock(&buffer_lock
);
106 avail
= sizeof(buffer
) - buffer_use
;
110 ret
= vsnprintf(buffer
+ buffer_use
, avail
, msg
, vl
);
114 panic("vsnprintf screwed up in tprintf!\n");
115 if (ret
> sizeof(buffer
))
116 panic("No mem in tprintf left!\n");
120 avail
= sizeof(buffer
) - buffer_use
;
124 ret
= vsnprintf(buffer
+ buffer_use
, avail
, msg
, vl
);
128 panic("vsnprintf screwed up in tprintf!\n");
133 spinlock_unlock(&buffer_lock
);