cat.c 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /* cat command: print file contents to screen (supports paths). */
  2. #include "fat.h"
  3. #include "io.h"
  4. #define DUMP_BUF_SIZE 4096
  5. void program_cat_main(const char *filename)
  6. {
  7. static unsigned char buf[DUMP_BUF_SIZE];
  8. unsigned int dir_cluster;
  9. char name_8_3[12];
  10. int n;
  11. unsigned int i;
  12. if (!filename || !filename[0]) {
  13. io_putstr("cat: usage cat <file>\n");
  14. return;
  15. }
  16. n = fat_init();
  17. if (n == -2) {
  18. io_putstr("cat: disk read error\n");
  19. return;
  20. }
  21. if (n != 0) {
  22. io_putstr("cat: no FAT filesystem found\n");
  23. return;
  24. }
  25. n = fat_resolve_path(filename, &dir_cluster, name_8_3);
  26. if (n == -2) {
  27. io_putstr("cat: disk error\n");
  28. return;
  29. }
  30. if (n != 0) {
  31. io_putstr("cat: not found\n");
  32. return;
  33. }
  34. n = fat_read_file_in(dir_cluster, name_8_3, buf, DUMP_BUF_SIZE);
  35. if (n == -2) {
  36. io_putstr("cat: disk read error\n");
  37. return;
  38. }
  39. if (n < 0) {
  40. io_putstr("cat: file not found\n");
  41. return;
  42. }
  43. for (i = 0; i < (unsigned int)n; i++) {
  44. unsigned char c = buf[i];
  45. if (c >= 0x20 && c < 0x7F)
  46. io_putchar((char)c);
  47. else if (c == '\n')
  48. io_putchar('\n');
  49. else if (c == '\t')
  50. io_putchar('\t');
  51. else
  52. io_putchar('.');
  53. }
  54. if (n > 0 && buf[n - 1] != '\n')
  55. io_putstr("\n");
  56. }