ls.c 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /* ls command: list a directory (root by default, or a path). */
  2. #include "fat.h"
  3. #include "io.h"
  4. #define BUF_SIZE 12
  5. static void list_cb(const char *name, int is_dir, unsigned int size)
  6. {
  7. unsigned int i;
  8. char buf[BUF_SIZE];
  9. unsigned int n;
  10. for (i = 0; i < 11 && name[i]; i++)
  11. io_putchar(name[i] == ' ' ? ' ' : name[i]);
  12. if (i < 11)
  13. io_putchar(' ');
  14. if (is_dir) {
  15. io_putstr(" <DIR>\n");
  16. return;
  17. }
  18. io_putstr(" ");
  19. n = size;
  20. if (n == 0) {
  21. io_putchar('0');
  22. } else {
  23. buf[0] = '\0';
  24. i = BUF_SIZE - 1;
  25. buf[i] = '\0';
  26. while (n && i > 0) {
  27. buf[--i] = (char)('0' + (n % 10));
  28. n /= 10;
  29. }
  30. io_putstr(buf + i);
  31. }
  32. io_putstr("\n");
  33. }
  34. void program_ls_main(const char *path)
  35. {
  36. unsigned int dir_cluster = 0;
  37. int r;
  38. r = fat_init();
  39. if (r == -2) {
  40. io_putstr("ls: disk read error\n");
  41. return;
  42. }
  43. if (r != 0) {
  44. io_putstr("ls: no FAT filesystem found\n");
  45. return;
  46. }
  47. if (path && path[0]) {
  48. r = fat_resolve_dir(path, &dir_cluster);
  49. if (r == -2) {
  50. io_putstr("ls: disk error\n");
  51. return;
  52. }
  53. if (r != 0) {
  54. io_putstr("ls: not a directory\n");
  55. return;
  56. }
  57. }
  58. r = fat_list_dir(dir_cluster, list_cb);
  59. if (r == -2) {
  60. io_putstr("ls: disk read error\n");
  61. return;
  62. }
  63. }