Skip to content

Commit a2e5f05

Browse files
cat read command updated.
1 parent 6f54623 commit a2e5f05

2 files changed

Lines changed: 34 additions & 2 deletions

File tree

disk.img

0 Bytes
Binary file not shown.

source/kernel/C/shell/commands/cat.c

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,47 @@
1010
*/
1111

1212
#include <commands/commands.h>
13+
#include <filesystems/vfs.h>
14+
#include <basics.h>
15+
16+
#define CAT_BUF_SIZE 512
1317

1418
int cmd_cat(int argc, char** argv)
1519
{
1620
if (argc < 2) {
1721
printf("cat: missing file operand");
1822
return 1;
1923
}
20-
21-
printf("%s", read_file(global_fs, argv[1]));
2224

25+
uint8_t buf[CAT_BUF_SIZE];
26+
27+
for (int i = 1; i < argc; i++) {
28+
vfs_file_t file;
29+
30+
/* Open file */
31+
if (vfs_open(argv[i], &file) != 0) {
32+
printf("cat: %s: No such file or directory\n", argv[i]);
33+
continue;
34+
}
35+
36+
/* Read loop */
37+
while (1) {
38+
int r = vfs_read(&file, buf, CAT_BUF_SIZE);
39+
if (r < 0) {
40+
printf("cat: %s: read error", argv[i]);
41+
break;
42+
}
43+
44+
if (r == 0)
45+
break; /* EOF */
46+
47+
for (int j = 0; j < r; j++)
48+
printfnoln("%c", buf[j]);
49+
}
50+
51+
vfs_close(&file);
52+
}
53+
54+
print("\n");
2355
return 0;
2456
}

0 commit comments

Comments
 (0)