diff options
author | Eric Andersen <andersen@codepoet.org> | 2003-03-07 17:33:40 +0000 |
---|---|---|
committer | Eric Andersen <andersen@codepoet.org> | 2003-03-07 17:33:40 +0000 |
commit | 2afcbe436fcfee169fbbe95984134c0fac03e46d (patch) | |
tree | 8500b1c63861c499ee20dbc9ab009db3549e6502 /util-linux/dmesg.c | |
parent | a9c6bc41e99e0c28d524060805429c7b742546a7 (diff) | |
download | busybox-w32-2afcbe436fcfee169fbbe95984134c0fac03e46d.tar.gz busybox-w32-2afcbe436fcfee169fbbe95984134c0fac03e46d.tar.bz2 busybox-w32-2afcbe436fcfee169fbbe95984134c0fac03e46d.zip |
audit for proper error handling, apply a few size optimizations
-Erik
Diffstat (limited to 'util-linux/dmesg.c')
-rw-r--r-- | util-linux/dmesg.c | 61 |
1 files changed, 34 insertions, 27 deletions
diff --git a/util-linux/dmesg.c b/util-linux/dmesg.c index 73de6d1ae..56e0771f0 100644 --- a/util-linux/dmesg.c +++ b/util-linux/dmesg.c | |||
@@ -13,11 +13,15 @@ | |||
13 | * from util-linux -- adapted for busybox by | 13 | * from util-linux -- adapted for busybox by |
14 | * Erik Andersen <andersee@debian.org>. I ripped out Native Language | 14 | * Erik Andersen <andersee@debian.org>. I ripped out Native Language |
15 | * Support, replaced getopt, added some gotos for redundant stuff. | 15 | * Support, replaced getopt, added some gotos for redundant stuff. |
16 | * | ||
17 | * Audited and cleaned up on 7 March 2003 to reduce size of | ||
18 | * check error handling by Erik Andersen <andersee@debian.org> | ||
16 | */ | 19 | */ |
17 | 20 | ||
18 | #include <stdio.h> | 21 | #include <stdio.h> |
19 | #include <stdlib.h> | 22 | #include <stdlib.h> |
20 | #include <getopt.h> | 23 | #include <getopt.h> |
24 | #include <errno.h> | ||
21 | 25 | ||
22 | #if __GNU_LIBRARY__ < 5 | 26 | #if __GNU_LIBRARY__ < 5 |
23 | # ifdef __alpha__ | 27 | # ifdef __alpha__ |
@@ -32,34 +36,31 @@ | |||
32 | int dmesg_main(int argc, char **argv) | 36 | int dmesg_main(int argc, char **argv) |
33 | { | 37 | { |
34 | char *buf; | 38 | char *buf; |
35 | int c; | ||
36 | int bufsize = 8196; | 39 | int bufsize = 8196; |
37 | int i; | 40 | int i, n; |
38 | int n; | ||
39 | int level = 0; | 41 | int level = 0; |
40 | int lastc; | 42 | int lastc; |
41 | int cmd = 3; | 43 | int cmd = 3; |
42 | 44 | ||
43 | while ((c = getopt(argc, argv, "cn:s:")) != EOF) { | 45 | while ((i = getopt(argc, argv, "cn:s:")) != EOF) { |
44 | switch (c) { | 46 | switch (i) { |
45 | case 'c': | 47 | case 'c': |
46 | cmd = 4; | 48 | cmd = 4; |
47 | break; | 49 | break; |
48 | case 'n': | 50 | case 'n': |
49 | cmd = 8; | 51 | cmd = 8; |
50 | if (optarg == NULL) | 52 | level = bb_xgetlarg(optarg, 10, 0, 10); |
51 | show_usage(); | 53 | break; |
52 | level = atoi(optarg); | 54 | case 's': |
53 | break; | 55 | /* I think a 512k max kernel ring buffer is big enough for |
54 | case 's': | 56 | * anybody, as the default is 16k... Could be wrong though. |
55 | if (optarg == NULL) | 57 | * If so I'm sure I'll hear about it by the enraged masses*/ |
58 | bufsize = bb_xgetlarg(optarg, 10, 4096, 512*1024); | ||
59 | break; | ||
60 | default: | ||
56 | show_usage(); | 61 | show_usage(); |
57 | bufsize = atoi(optarg); | ||
58 | break; | ||
59 | default: | ||
60 | show_usage(); | ||
61 | } | 62 | } |
62 | } | 63 | } |
63 | 64 | ||
64 | if (optind < argc) { | 65 | if (optind < argc) { |
65 | show_usage(); | 66 | show_usage(); |
@@ -67,15 +68,13 @@ int dmesg_main(int argc, char **argv) | |||
67 | 68 | ||
68 | if (cmd == 8) { | 69 | if (cmd == 8) { |
69 | if (klogctl(cmd, NULL, level) < 0) | 70 | if (klogctl(cmd, NULL, level) < 0) |
70 | perror_msg_and_die("klogctl"); | 71 | goto die_the_death; |
71 | return EXIT_SUCCESS; | 72 | goto all_done; |
72 | } | 73 | } |
73 | 74 | ||
74 | if (bufsize < 4096) | 75 | buf = xmalloc(bufsize); |
75 | bufsize = 4096; | ||
76 | buf = (char *) xmalloc(bufsize); | ||
77 | if ((n = klogctl(cmd, buf, bufsize)) < 0) | 76 | if ((n = klogctl(cmd, buf, bufsize)) < 0) |
78 | perror_msg_and_die("klogctl"); | 77 | goto die_the_death; |
79 | 78 | ||
80 | lastc = '\n'; | 79 | lastc = '\n'; |
81 | for (i = 0; i < n; i++) { | 80 | for (i = 0; i < n; i++) { |
@@ -91,5 +90,13 @@ int dmesg_main(int argc, char **argv) | |||
91 | } | 90 | } |
92 | if (lastc != '\n') | 91 | if (lastc != '\n') |
93 | putchar('\n'); | 92 | putchar('\n'); |
93 | all_done: | ||
94 | #ifdef CONFIG_FEATURE_CLEAN_UP | ||
95 | if (buf) { | ||
96 | free(buf); | ||
97 | } | ||
98 | #endif | ||
94 | return EXIT_SUCCESS; | 99 | return EXIT_SUCCESS; |
100 | die_the_death: | ||
101 | perror_msg_and_die("klogctl"); | ||
95 | } | 102 | } |