[hackers] [ubase] Simplify dmesg_show() || sin

From: <git_AT_suckless.org>
Date: Mon, 30 Jun 2014 17:50:42 +0200

commit 643a7142561a2760642ac77a9cb3bad6fa387f90
Author: sin <sin_AT_2f30.org>
Date: Mon Jun 30 16:43:25 2014 +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.

diff --git a/dmesg.c b/dmesg.c
index b90d9ac..6498010 100644
--- a/dmesg.c
+++ b/dmesg.c
_AT_@ -27,7 +27,6 @@ main(int argc, char *argv[])
         int n;
         char *buf;
         int cflag = 0;
- int rflag = 0;
         long level;
 
         ARGBEGIN {
_AT_@ -39,7 +38,6 @@ main(int argc, char *argv[])
                 cflag = 1;
                 break;
         case 'r':
- rflag = 1;
                 break;
         case 'n':
                 level = estrtol(EARGF(usage()), 10);
_AT_@ -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:");
_AT_@ -79,30 +72,13 @@ main(int argc, char *argv[])
 static int
 dmesg_show(int fd, const void *buf, size_t n)
 {
- int last = '
';
- 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 == '
' && 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 != '
')
- if (write(fd, "
", 1) != 1)
- return -1;
+ if (p[n - 1] != '
')
+ putchar('
');
         return 0;
 }
Received on Mon Jun 30 2014 - 17:50:42 CEST

This archive was generated by hypermail 2.3.0 : Mon Jun 30 2014 - 18:00:09 CEST