diff options
author | Yauheni Kaliuta <yauheni.kaliuta@redhat.com> | 2017-04-11 15:15:02 +0300 |
---|---|---|
committer | Lucas De Marchi <lucas.demarchi@intel.com> | 2017-04-11 09:04:28 -0700 |
commit | abcd0bf8ec6623b26cddee3c2a9175b786957600 (patch) | |
tree | c9e878b8862f426b00e04a54edfed9cdc663fdab /libkmod/libkmod-module.c | |
parent | 96b88aea2a5601ff8199bdb4c8851bc62bcbcf82 (diff) | |
download | kmod-abcd0bf8ec6623b26cddee3c2a9175b786957600.tar.gz kmod-abcd0bf8ec6623b26cddee3c2a9175b786957600.tar.bz2 kmod-abcd0bf8ec6623b26cddee3c2a9175b786957600.zip |
libkmod: modinfo: implement line splitting in hex_to_str
The key output is usually short, but for signature it is more
readable to output it in several lines.
Implement line splitting. Set line limit hardcoded to 20 hex
numbers (not characters).
Signed-off-by: Yauheni Kaliuta <yauheni.kaliuta@redhat.com>
Diffstat (limited to 'libkmod/libkmod-module.c')
-rw-r--r-- | libkmod/libkmod-module.c | 20 |
1 files changed, 15 insertions, 5 deletions
diff --git a/libkmod/libkmod-module.c b/libkmod/libkmod-module.c index 22c8f4c..9e155f0 100644 --- a/libkmod/libkmod-module.c +++ b/libkmod/libkmod-module.c @@ -2197,15 +2197,25 @@ static char *kmod_module_hex_to_str(const char *hex, size_t len) { char *str; int i; + int j; + const size_t line_limit = 20; + size_t str_len; - str = malloc(len * 3); + str_len = len * 3; /* XX: or XX\0 */ + str_len += ((str_len + line_limit - 1) / line_limit - 1) * 3; /* \n\t\t */ + + str = malloc(str_len); if (str == NULL) return NULL; - for (i = 0; i < (int)len; i++) { - sprintf(str + i * 3, "%02X", (unsigned char)hex[i]); - if (i < (int)len - 1) - str[i * 3 + 2] = ':'; + for (i = 0, j = 0; i < (int)len; i++) { + j += sprintf(str + j, "%02X", (unsigned char)hex[i]); + if (i < (int)len - 1) { + str[j++] = ':'; + + if ((i + 1) % line_limit == 0) + j += sprintf(str + j, "\n\t\t"); + } } return str; } |