dmesg.c: cleanups -- use err(), xalloc()
Signed-off-by: Marek Polacek <mpolacek@redhat.com> Signed-off-by: Karel Zak <kzak@redhat.com>
This commit is contained in:
parent
d8fb986bf5
commit
15103c4bc6
|
@ -34,21 +34,25 @@
|
||||||
#include <getopt.h>
|
#include <getopt.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <sys/klog.h>
|
#include <sys/klog.h>
|
||||||
|
#include <ctype.h>
|
||||||
|
|
||||||
|
#include "c.h"
|
||||||
#include "nls.h"
|
#include "nls.h"
|
||||||
#include "strutils.h"
|
#include "strutils.h"
|
||||||
|
#include "xalloc.h"
|
||||||
|
|
||||||
static char *progname;
|
static void __attribute__ ((noreturn)) usage(void)
|
||||||
|
{
|
||||||
static void
|
|
||||||
usage(void) {
|
|
||||||
fprintf(stderr,
|
fprintf(stderr,
|
||||||
_("Usage: %s [-c] [-n level] [-r] [-s bufsize]\n"), progname);
|
_("Usage: %s [-c] [-n level] [-r] [-s bufsize]\n"),
|
||||||
|
program_invocation_short_name);
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
int main(int argc, char *argv[])
|
||||||
main(int argc, char *argv[]) {
|
{
|
||||||
char *buf;
|
char *buf = NULL;
|
||||||
int sz;
|
int sz;
|
||||||
int bufsize = 0;
|
int bufsize = 0;
|
||||||
int i;
|
int i;
|
||||||
|
@ -63,7 +67,6 @@ main(int argc, char *argv[]) {
|
||||||
bindtextdomain(PACKAGE, LOCALEDIR);
|
bindtextdomain(PACKAGE, LOCALEDIR);
|
||||||
textdomain(PACKAGE);
|
textdomain(PACKAGE);
|
||||||
|
|
||||||
progname = argv[0];
|
|
||||||
while ((c = getopt(argc, argv, "crn:s:")) != -1) {
|
while ((c = getopt(argc, argv, "crn:s:")) != -1) {
|
||||||
switch (c) {
|
switch (c) {
|
||||||
case 'c':
|
case 'c':
|
||||||
|
@ -84,24 +87,20 @@ main(int argc, char *argv[]) {
|
||||||
case '?':
|
case '?':
|
||||||
default:
|
default:
|
||||||
usage();
|
usage();
|
||||||
exit(EXIT_FAILURE);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
argc -= optind;
|
argc -= optind;
|
||||||
argv += optind;
|
argv += optind;
|
||||||
|
|
||||||
if (argc > 1) {
|
if (argc > 1)
|
||||||
usage();
|
usage();
|
||||||
exit(EXIT_FAILURE);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (cmd == 8) {
|
if (cmd == 8) {
|
||||||
n = klogctl(cmd, NULL, level);
|
n = klogctl(cmd, NULL, level);
|
||||||
if (n < 0) {
|
if (n < 0)
|
||||||
perror("klogctl");
|
err(EXIT_FAILURE, _("klogctl failed"));
|
||||||
exit(EXIT_FAILURE);
|
|
||||||
}
|
return EXIT_SUCCESS;
|
||||||
exit(EXIT_SUCCESS);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!bufsize) {
|
if (!bufsize) {
|
||||||
|
@ -112,14 +111,14 @@ main(int argc, char *argv[]) {
|
||||||
|
|
||||||
if (bufsize) {
|
if (bufsize) {
|
||||||
sz = bufsize + 8;
|
sz = bufsize + 8;
|
||||||
buf = (char *) malloc(sz * sizeof(char));
|
buf = xmalloc(sz * sizeof(char));
|
||||||
n = klogctl(cmd, buf, sz);
|
n = klogctl(cmd, buf, sz);
|
||||||
} else {
|
} else {
|
||||||
sz = 16392;
|
sz = 16392;
|
||||||
while (1) {
|
while (1) {
|
||||||
buf = (char *) malloc(sz * sizeof(char));
|
buf = xmalloc(sz * sizeof(char));
|
||||||
n = klogctl(3, buf, sz); /* read only */
|
n = klogctl(3, buf, sz); /* read only */
|
||||||
if (n != sz || sz > (1<<28))
|
if (n != sz || sz > (1 << 28))
|
||||||
break;
|
break;
|
||||||
free(buf);
|
free(buf);
|
||||||
sz *= 4;
|
sz *= 4;
|
||||||
|
@ -129,16 +128,14 @@ main(int argc, char *argv[]) {
|
||||||
n = klogctl(cmd, buf, sz); /* read and clear */
|
n = klogctl(cmd, buf, sz); /* read and clear */
|
||||||
}
|
}
|
||||||
|
|
||||||
if (n < 0) {
|
if (n < 0)
|
||||||
perror("klogctl");
|
err(EXIT_FAILURE, _("klogctl failed"));
|
||||||
exit(EXIT_FAILURE);
|
|
||||||
}
|
|
||||||
|
|
||||||
lastc = '\n';
|
lastc = '\n';
|
||||||
for (i = 0; i < n; i++) {
|
for (i = 0; i < n; i++) {
|
||||||
if (!raw && (i == 0 || buf[i - 1] == '\n') && buf[i] == '<') {
|
if (!raw && (i == 0 || buf[i - 1] == '\n') && buf[i] == '<') {
|
||||||
i++;
|
i++;
|
||||||
while (buf[i] >= '0' && buf[i] <= '9')
|
while (isdigit(buf[i]))
|
||||||
i++;
|
i++;
|
||||||
if (buf[i] == '>')
|
if (buf[i] == '>')
|
||||||
i++;
|
i++;
|
||||||
|
@ -149,5 +146,6 @@ main(int argc, char *argv[]) {
|
||||||
if (lastc != '\n')
|
if (lastc != '\n')
|
||||||
putchar('\n');
|
putchar('\n');
|
||||||
free(buf);
|
free(buf);
|
||||||
return 0;
|
|
||||||
|
return EXIT_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue