diff options
author | Denys Vlasenko <vda.linux@googlemail.com> | 2020-07-20 02:03:02 +0200 |
---|---|---|
committer | Denys Vlasenko <vda.linux@googlemail.com> | 2020-07-20 02:03:02 +0200 |
commit | 2dd82f465e16f9df991d5677114370298a2ffade (patch) | |
tree | bbd5cd4e159a1ce6a2bdb9011aec41c19a40c039 | |
parent | 9a2d899273e3a8a58bdb4c3834d65d22658e7821 (diff) | |
download | busybox-w32-2dd82f465e16f9df991d5677114370298a2ffade.tar.gz busybox-w32-2dd82f465e16f9df991d5677114370298a2ffade.tar.bz2 busybox-w32-2dd82f465e16f9df991d5677114370298a2ffade.zip |
lsscsi: code shrink
function old new delta
lsscsi_main 298 302 +4
get_line 56 45 -11
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 1/1 up/down: 4/-11) Total: -7 bytes
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r-- | miscutils/lsscsi.c | 27 |
1 files changed, 12 insertions, 15 deletions
diff --git a/miscutils/lsscsi.c b/miscutils/lsscsi.c index f737d33d9..76c281264 100644 --- a/miscutils/lsscsi.c +++ b/miscutils/lsscsi.c | |||
@@ -27,25 +27,21 @@ | |||
27 | 27 | ||
28 | static const char scsi_dir[] ALIGN1 = "/sys/bus/scsi/devices"; | 28 | static const char scsi_dir[] ALIGN1 = "/sys/bus/scsi/devices"; |
29 | 29 | ||
30 | static char *get_line(const char *filename, char *buf, unsigned *bufsize_p) | 30 | static char *get_line(const char *filename, char *buf, char *bufend) |
31 | { | 31 | { |
32 | unsigned bufsize = *bufsize_p; | 32 | ssize_t sz = bufend - buf - 2; /* -2 for two NULs */ |
33 | ssize_t sz; | ||
34 | 33 | ||
35 | if ((int)(bufsize - 2) <= 0) | 34 | if (sz <= 0) |
36 | return buf; | 35 | return buf; |
37 | 36 | ||
38 | sz = open_read_close(filename, buf, bufsize - 2); | 37 | sz = open_read_close(filename, buf, sz); |
39 | if (sz < 0) | 38 | if (sz < 0) |
40 | sz = 0; | 39 | sz = 0; |
41 | buf[sz] = '\0'; | 40 | buf[sz] = '\0'; |
42 | 41 | ||
43 | sz = (trim(buf) - buf) + 1; | 42 | buf = trim(buf) + 1; |
44 | bufsize -= sz; | ||
45 | buf += sz; | ||
46 | buf[0] = '\0'; | 43 | buf[0] = '\0'; |
47 | 44 | ||
48 | *bufsize_p = bufsize; | ||
49 | return buf; | 45 | return buf; |
50 | } | 46 | } |
51 | 47 | ||
@@ -61,7 +57,6 @@ int lsscsi_main(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) | |||
61 | while ((de = readdir(dir)) != NULL) { | 57 | while ((de = readdir(dir)) != NULL) { |
62 | char buf[256]; | 58 | char buf[256]; |
63 | char *ptr; | 59 | char *ptr; |
64 | unsigned bufsize; | ||
65 | const char *vendor; | 60 | const char *vendor; |
66 | const char *type_str; | 61 | const char *type_str; |
67 | const char *type_name; | 62 | const char *type_name; |
@@ -76,15 +71,17 @@ int lsscsi_main(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) | |||
76 | if (chdir(de->d_name) != 0) | 71 | if (chdir(de->d_name) != 0) |
77 | continue; | 72 | continue; |
78 | 73 | ||
79 | bufsize = sizeof(buf); | ||
80 | vendor = buf; | 74 | vendor = buf; |
81 | ptr = get_line("vendor", buf, &bufsize); | 75 | ptr = get_line("vendor", buf, buf + sizeof(buf)); |
76 | |||
82 | type_str = ptr; | 77 | type_str = ptr; |
83 | ptr = get_line("type", ptr, &bufsize); | 78 | ptr = get_line("type", ptr, buf + sizeof(buf)); |
79 | |||
84 | model = ptr; | 80 | model = ptr; |
85 | ptr = get_line("model", ptr, &bufsize); | 81 | ptr = get_line("model", ptr, buf + sizeof(buf)); |
82 | |||
86 | rev = ptr; | 83 | rev = ptr; |
87 | ptr = get_line("rev", ptr, &bufsize); | 84 | /*ptr =*/ get_line("rev", ptr, buf + sizeof(buf)); |
88 | 85 | ||
89 | printf("[%s]\t", de->d_name); | 86 | printf("[%s]\t", de->d_name); |
90 | 87 | ||