ls.c 927 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. /* ls command: list root directory of FAT12 floppy. */
  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. (void)is_dir;
  11. for (i = 0; i < 11 && name[i]; i++)
  12. io_putchar(name[i] == ' ' ? ' ' : name[i]);
  13. if (i < 11)
  14. io_putchar(' ');
  15. io_putstr(" ");
  16. /* Simple decimal print for size */
  17. n = size;
  18. if (n == 0) {
  19. io_putchar('0');
  20. } else {
  21. buf[0] = '\0';
  22. i = BUF_SIZE - 1;
  23. buf[i] = '\0';
  24. while (n && i > 0) {
  25. buf[--i] = (char)('0' + (n % 10));
  26. n /= 10;
  27. }
  28. io_putstr(buf + i);
  29. }
  30. io_putstr("\n");
  31. }
  32. void program_ls_main(void)
  33. {
  34. int r;
  35. if (fat_init() != 0) {
  36. io_putstr("ls: fat init failed\n");
  37. return;
  38. }
  39. r = fat_list_root(list_cb);
  40. if (r == -2) {
  41. io_putstr("ls: disk read error\n");
  42. return;
  43. }
  44. if (r != 0) {
  45. io_putstr("ls: list failed\n");
  46. return;
  47. }
  48. }