Skip to content

Commit e5b007d

Browse files
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 00 57 E5 5D E8 91 00 81 21 20 11 91 F6 AF 21 B1 3B 02 57 7F 00 00 00 00 31 01 61 05 78 1D B0 CE DA C1 D6 6A 66 AB 03 78 06 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
1 parent 828b84d commit e5b007d

File tree

1 file changed

+36
-9
lines changed

1 file changed

+36
-9
lines changed

fs/fs/src/fs_cli.c

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -164,29 +164,56 @@ static int
164164
fs_cat_cmd(int argc, char **argv)
165165
{
166166
int rc;
167+
const char *fname;
167168
struct fs_file *file;
168169
char buf[32];
170+
bool plain_hex = false;
171+
bool verbose_hex = false;
172+
uint16_t offset = 0;
169173
uint32_t len;
174+
int i;
170175

171-
if (argc != 2) {
172-
console_printf("cat <filename>\n");
176+
if (argc < 2) {
177+
console_printf("cat [-x|X] <filename>\n");
173178
return -1;
174179
}
175180

176-
rc = fs_open(argv[1], FS_ACCESS_READ, &file);
181+
plain_hex = !strcmp(argv[1], "-x");
182+
verbose_hex = !strcmp(argv[1], "-X");
183+
184+
fname = plain_hex || verbose_hex ? argv[2] : argv[1];
185+
186+
rc = fs_open(fname, FS_ACCESS_READ, &file);
177187
if (rc != FS_EOK) {
178-
console_printf("Error opening %s - %d\n", argv[1], rc);
188+
console_printf("Error opening %s - %d\n", fname, rc);
179189
return -1;
180190
}
181191

182-
do {
183-
rc = fs_read(file, sizeof(buf), buf, &len);
192+
while (1) {
193+
rc = fs_read(file, verbose_hex ? 16 : sizeof(buf), buf, &len);
184194
if (rc != FS_EOK) {
185-
console_printf("\nError reading %s - %d\n", argv[1], rc);
195+
console_printf("\nError reading %s - %d\n", fname, rc);
196+
break;
197+
}
198+
199+
if (len == 0) {
186200
break;
187201
}
188-
console_write(buf, len);
189-
} while (len > 0);
202+
203+
if (verbose_hex) {
204+
console_printf("%04x: ", offset);
205+
}
206+
if (plain_hex || verbose_hex) {
207+
for (i = 0; i < len; i++) {
208+
console_printf("%02X ", buf[i]);
209+
}
210+
console_printf("\n");
211+
} else {
212+
console_write(buf, len);
213+
}
214+
215+
offset += len;
216+
}
190217

191218
fs_close(file);
192219

0 commit comments

Comments
 (0)