This is an automated email from the ASF dual-hosted git repository.
andk pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/mynewt-core.git
The following commit(s) were added to refs/heads/master by this push:
new 1a4b85fda fs/fs: Add hex option to 'cat' in fs cli
1a4b85fda is described below
commit 1a4b85fda4009cca6102f8f49563e6bc3ea69a0d
Author: Andrzej Kaczmarek <[email protected]>
AuthorDate: Wed Dec 17 14:27:15 2025 +0100
fs/fs: Add hex option to 'cat' in fs cli
This adds '-x' and '-X' arguments to 'cat' command in filesystem cli.
Both switches enable hex dump for file contents as either plain hex dump
or hex dump with offset markings.
compat> cat -x blob
F9 FB 10 10 20 C6 34 F6 46 56 36 F6 57 07 02 C4 B2 25 40 F9 FB 01 74 ...
20 11 91 F6 AF 21 B1 3B 02 57 7F 00 00 00 00 31 01 61 05 78 1D B0 CE ...
74 AB 92 B4 75 B7 65 02 17 7D FF FF FF FF
compat> cat -X blob
0000: F9 FB 10 10 20 C6 34 F6 46 56 36 F6 57 07 02 4C
0010: B2 25 40 F9 FB 01 74 00 57 E5 5D E8 91 00 81 21
0020: 20 11 91 F6 AF 21 B1 3B 02 57 7F 00 00 00 00 13
0030: 01 61 05 78 1D B0 CE DA C1 D6 6A 66 AB 03 78 06
0040: 74 AB 92 B4 75 B7 65 02 17 7D FF FF FF FF
---
fs/fs/src/fs_cli.c | 45 ++++++++++++++++++++++++++++++++++++---------
1 file changed, 36 insertions(+), 9 deletions(-)
diff --git a/fs/fs/src/fs_cli.c b/fs/fs/src/fs_cli.c
index 9fa932a22..10412d2bc 100644
--- a/fs/fs/src/fs_cli.c
+++ b/fs/fs/src/fs_cli.c
@@ -164,29 +164,56 @@ static int
fs_cat_cmd(int argc, char **argv)
{
int rc;
+ const char *fname;
struct fs_file *file;
char buf[32];
+ bool plain_hex;
+ bool verbose_hex;
+ uint16_t offset = 0;
uint32_t len;
+ int i;
- if (argc != 2) {
- console_printf("cat <filename>\n");
+ if (argc < 2) {
+ console_printf("cat [-x|X] <filename>\n");
return -1;
}
- rc = fs_open(argv[1], FS_ACCESS_READ, &file);
+ plain_hex = !strcmp(argv[1], "-x");
+ verbose_hex = !strcmp(argv[1], "-X");
+
+ fname = plain_hex || verbose_hex ? argv[2] : argv[1];
+
+ rc = fs_open(fname, FS_ACCESS_READ, &file);
if (rc != FS_EOK) {
- console_printf("Error opening %s - %d\n", argv[1], rc);
+ console_printf("Error opening %s - %d\n", fname, rc);
return -1;
}
- do {
- rc = fs_read(file, sizeof(buf), buf, &len);
+ while (1) {
+ rc = fs_read(file, verbose_hex ? 16 : sizeof(buf), buf, &len);
if (rc != FS_EOK) {
- console_printf("\nError reading %s - %d\n", argv[1], rc);
+ console_printf("\nError reading %s - %d\n", fname, rc);
+ break;
+ }
+
+ if (len == 0) {
break;
}
- console_write(buf, len);
- } while (len > 0);
+
+ if (verbose_hex) {
+ console_printf("%04X: ", offset);
+ }
+ if (plain_hex || verbose_hex) {
+ for (i = 0; i < len; i++) {
+ console_printf("%02X ", buf[i]);
+ }
+ console_printf("\n");
+ } else {
+ console_write(buf, len);
+ }
+
+ offset += len;
+ }
fs_close(file);