commit b8d1f834376392a5ce088e719bb3089b32a75e4b
parent 6e4a0da527d6f063d47e865910692aa80e4bb554
Author: sin <sin@2f30.org>
Date: Mon, 12 Aug 2013 11:26:34 +0100
Optimize dmesg(1) output on Linux
Diffstat:
1 file changed, 6 insertions(+), 2 deletions(-)
diff --git a/linux/dmesg.c b/linux/dmesg.c
@@ -2,6 +2,7 @@
#include <sys/klog.h>
#include <unistd.h>
#include <stdio.h>
+#include <string.h>
enum {
SYSLOG_ACTION_READ_ALL = 3,
@@ -24,20 +25,23 @@ int
dmesg_show(int fd, const void *buf, size_t n)
{
int last = '\n';
+ char newbuf[n], *q = newbuf;
const char *p = buf;
size_t i;
+ memset(newbuf, 0, n);
for (i = 0; i < n; ) {
if (last == '\n' && p[i] == '<') {
i += 2;
if (i + 1 < n && p[i + 1] == '>')
i++;
} else {
- if (write(fd, &p[i], 1) != 1)
- return -1;
+ *q++ = p[i];
}
last = p[i++];
}
+ if (write(fd, newbuf, n) != n)
+ return -1;
if (last != '\n')
if (write(fd, "\n", 1) != 1)
return -1;