[PATCH] dmesg.c: cleanups

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Signed-off-by: Marek Polacek <mpolacek@xxxxxxxxxx>
---
 sys-utils/dmesg.c |   86 ++++++++++++++++++++++++-----------------------------
 1 files changed, 39 insertions(+), 47 deletions(-)

diff --git a/sys-utils/dmesg.c b/sys-utils/dmesg.c
index f1a7dcb..d5c399a 100644
--- a/sys-utils/dmesg.c
+++ b/sys-utils/dmesg.c
@@ -8,7 +8,8 @@
  * by Peeter Joot.  This was also suggested by John Hudson.
  * 1999-02-22 Arkadiusz Mi¶kiewicz <misiek@xxxxxxxxxx>
  * - added Native Language Support
- *
+ * 2011-02-03 Marek Polacek <mpolacek@xxxxxxxxxx>
+ * - cleanups
  */
 
 /*
@@ -29,40 +30,37 @@
  * Only function 3 is allowed to non-root processes.
  */
 
-#include <linux/unistd.h>
-#include <stdio.h>
+#include <err.h>
+#include <ctype.h>
 #include <getopt.h>
+#include <stdbool.h>
+#include <stdio.h>
 #include <stdlib.h>
-# include <sys/klog.h>
-
+#include <sys/klog.h>
 #include "nls.h"
+#include "xalloc.h"
 
-static char *progname;
-
-static void
-usage(void) {
-	fprintf(stderr,
-		_("Usage: %s [-c] [-n level] [-r] [-s bufsize]\n"), progname);
+static void __attribute__ ((noreturn)) usage(void)
+{
+	fputs(_("Usage: dmesg [-c] [-n level] [-r] [-s bufsize]\n"), stderr);
+	exit(EXIT_FAILURE);
 }
 
-int
-main(int argc, char *argv[]) {
+int main(int argc, char *argv[])
+{
 	char *buf;
-	int  sz;
-	int  bufsize = 0;
-	int  i;
-	int  n;
-	int  c;
-	int  level = 0;
-	int  lastc;
-	int  cmd = 3;		/* Read all messages in the ring buffer */
-	int  raw = 0;
+	int sz;
+	int bufsize = 0;
+	int n;
+	int c;
+	int level = 0;
+	int cmd = 3;		/* Read all messages in the ring buffer */
+	bool raw = false;
 
 	setlocale(LC_ALL, "");
 	bindtextdomain(PACKAGE, LOCALEDIR);
 	textdomain(PACKAGE);
 
-	progname = argv[0];
 	while ((c = getopt(argc, argv, "crn:s:")) != -1) {
 		switch (c) {
 		case 'c':
@@ -70,37 +68,31 @@ main(int argc, char *argv[]) {
 			break;
 		case 'n':
 			cmd = 8;	/* Set level of messages */
-			level = atoi(optarg);
+			level = strtol(optarg, NULL, 10);
 			break;
 		case 'r':
-			raw = 1;
+			raw = true;
 			break;
 		case 's':
-			bufsize = atoi(optarg);
+			bufsize = strtol(optarg, NULL, 10);
 			if (bufsize < 4096)
 				bufsize = 4096;
 			break;
 		case '?':
 		default:
 			usage();
-			exit(1);
 		}
 	}
 	argc -= optind;
-	argv += optind;
 
-	if (argc > 1) {
+	if (argc > 1)
 		usage();
-		exit(1);
-	}
 
 	if (cmd == 8) {
 		n = klogctl(cmd, NULL, level);
-		if (n < 0) {
-			perror("klogctl");
-			exit(1);
-		}
-		exit(0);
+		if (n < 0)
+			err(EXIT_FAILURE, _("klogctl failed"));
+		return EXIT_SUCCESS;
 	}
 
 	if (!bufsize) {
@@ -111,14 +103,14 @@ main(int argc, char *argv[]) {
 
 	if (bufsize) {
 		sz = bufsize + 8;
-		buf = (char *) malloc(sz * sizeof(char));
+		buf = xmalloc(sz);
 		n = klogctl(cmd, buf, sz);
 	} else {
 		sz = 16392;
 		while (1) {
-			buf = (char *) malloc(sz * sizeof(char));
+			buf = xmalloc(sz);
 			n = klogctl(3, buf, sz);	/* read only */
-			if (n != sz || sz > (1<<28))
+			if (n != sz || sz > (1 << 28))
 				break;
 			free(buf);
 			sz *= 4;
@@ -128,16 +120,14 @@ main(int argc, char *argv[]) {
 			n = klogctl(cmd, buf, sz);	/* read and clear */
 	}
 
-	if (n < 0) {
-		perror("klogctl");
-		exit(1);
-	}
+	if (n < 0)
+		err(EXIT_FAILURE, _("klogctl failed"));
 
-	lastc = '\n';
-	for (i = 0; i < n; i++) {
+	char lastc = '\n';
+	for (int i = 0; i < n; i++) {
 		if (!raw && (i == 0 || buf[i - 1] == '\n') && buf[i] == '<') {
 			i++;
-			while (buf[i] >= '0' && buf[i] <= '9')
+			while (isdigit(buf[i]))
 				i++;
 			if (buf[i] == '>')
 				i++;
@@ -145,8 +135,10 @@ main(int argc, char *argv[]) {
 		lastc = buf[i];
 		putchar(lastc);
 	}
+
 	if (lastc != '\n')
 		putchar('\n');
 	free(buf);
-	return 0;
+
+	return EXIT_SUCCESS;
 }
-- 
1.7.3.4

--
To unsubscribe from this list: send the line "unsubscribe util-linux" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[Index of Archives]     [Netdev]     [Ethernet Bridging]     [Linux Wireless]     [Kernel Newbies]     [Security]     [Linux for Hams]     [Netfilter]     [Bugtraq]     [Yosemite News]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux Admin]     [Samba]

  Powered by Linux