commit 643a7142561a2760642ac77a9cb3bad6fa387f90
parent 5aede71236bec290b43f09b9bed5d175a76deea0
Author: sin <sin@2f30.org>
Date: Mon, 30 Jun 2014 16:43:25 +0100
Simplify dmesg_show()
The code was way too complicated. We do not currently filter out
the priority level, so we default to raw output.
Diffstat:
M | dmesg.c | | | 38 | +++++++------------------------------- |
1 file changed, 7 insertions(+), 31 deletions(-)
diff --git a/dmesg.c b/dmesg.c
@@ -27,7 +27,6 @@ main(int argc, char *argv[])
int n;
char *buf;
int cflag = 0;
- int rflag = 0;
long level;
ARGBEGIN {
@@ -39,7 +38,6 @@ main(int argc, char *argv[])
cflag = 1;
break;
case 'r':
- rflag = 1;
break;
case 'n':
level = estrtol(EARGF(usage()), 10);
@@ -60,14 +58,9 @@ main(int argc, char *argv[])
if (n < 0)
eprintf("klogctl:");
- if (rflag) {
- if (write(STDOUT_FILENO, buf, n) != n)
- eprintf("write:");
- } else {
- n = dmesg_show(STDOUT_FILENO, buf, n);
- if (n < 0)
- eprintf("dmesg_show:");
- }
+ n = dmesg_show(STDOUT_FILENO, buf, n);
+ if (n < 0)
+ eprintf("dmesg_show:");
if (cflag && klogctl(SYSLOG_ACTION_CLEAR, NULL, 0) < 0)
eprintf("klogctl:");
@@ -79,30 +72,13 @@ main(int argc, char *argv[])
static int
dmesg_show(int fd, const void *buf, size_t n)
{
- int last = '\n';
- char *newbuf, *q;
const char *p = buf;
ssize_t r;
- size_t i;
- newbuf = calloc(n, sizeof(char));
- q = newbuf;
- for (i = 0; i < n; ) {
- if (last == '\n' && p[i] == '<') {
- i += 2;
- if (i + 1 < n && p[i + 1] == '>')
- i++;
- } else {
- *q++ = p[i];
- }
- last = p[i++];
- }
- r = write(fd, newbuf, n);
- free(newbuf);
- if(r < 0 || (size_t)r != n)
+ r = write(fd, p, n);
+ if (r < 0 || (size_t)r != n)
return -1;
- if (last != '\n')
- if (write(fd, "\n", 1) != 1)
- return -1;
+ if (p[n - 1] != '\n')
+ putchar('\n');
return 0;
}