nvme-print-stdout: change stdout_d() to not print all zeros#3046
Merged
igaw merged 2 commits intolinux-nvme:masterfrom Jan 12, 2026
Merged
nvme-print-stdout: change stdout_d() to not print all zeros#3046igaw merged 2 commits intolinux-nvme:masterfrom
igaw merged 2 commits intolinux-nvme:masterfrom
Conversation
Collaborator
|
why not going the full hexdump way? static bool line_equal(const unsigned char *a,
const unsigned char *b,
size_t len)
{
return memcmp(a, b, len) == 0;
}
void stdout_d(unsigned char *buf, int len, int width, int group)
{
size_t offset, i;
unsigned char prev[32];
char ascii[32 + 1];
bool have_prev = false;
bool omitting = false;
assert(width <= sizeof(prev));
assert(width < sizeof(ascii));
assert(group > 0);
/* Header */
printf(" ");
for (i = 0; i < width; i++)
printf("%3zx", i);
printf("\n");
for (offset = 0; offset < len; offset += width) {
size_t line_len = width;
if (offset + line_len > len)
line_len = len - offset;
/* Collapse identical lines (hexdump behavior) */
if (have_prev &&
line_len == width &&
line_equal(buf + offset, prev, width)) {
if (!omitting) {
printf("*\n");
omitting = true;
}
continue;
}
omitting = false;
/* Save current line for next comparison */
if (line_len == width) {
memcpy(prev, buf + offset, width);
have_prev = true;
} else {
have_prev = false;
}
printf("%04zx:", offset);
memset(ascii, 0, sizeof(ascii));
for (i = 0; i < line_len; i++) {
if (i % group)
printf("%02x", buf[offset + i]);
else
printf(" %02x", buf[offset + i]);
ascii[i] = (buf[offset + i] >= '!' &&
buf[offset + i] <= '~') ?
buf[offset + i] : '.';
}
/* Pad short final line */
if (line_len < width) {
size_t missing = width - line_len;
size_t pad = missing * 2 +
missing / group +
(missing % group ? 1 : 0);
printf(" %*s", (int)pad, "");
}
printf(" \"%.*s\"\n", (int)line_len, ascii);
}
/* Final offset line (hexdump prints this always) */
printf("%04zx\n", offset);
}this gives vs[]:
0 1 2 3 4 5 6 7 8 9 a b c d e f
0000: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 "................"
*
00c0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 07 "................"
00d0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 "................"
*
03a0: 01 00 3c 0f 00 00 00 00 00 00 00 00 00 00 00 00 "..<............."
03b0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 "................"
*
0400 |
This is for the id-ns command empty vendor-specific fields. Note: This will change for other commands d() outputs. Signed-off-by: Tokunori Ikegami <[email protected]>
Contributor
Author
|
Just realized as the hexdump not print equal line but not only for the all zero then fixed the patch as mentioned. (Added) |
This is to remain all dump functionality feature. Signed-off-by: Tokunori Ikegami <[email protected]>
Collaborator
|
Thanks! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This is for the id-ns command empty vendor-specific fields.
Note: This will change for other commands d() outputs.