ls.c 1010 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. r = fat_init();
  36. if (r == -2) {
  37. io_putstr("ls: disk read error\n");
  38. return;
  39. }
  40. if (r != 0) {
  41. io_putstr("ls: no FAT filesystem found\n");
  42. return;
  43. }
  44. r = fat_list_root(list_cb);
  45. if (r == -2) {
  46. io_putstr("ls: disk read error\n");
  47. return;
  48. }
  49. if (r != 0) {
  50. io_putstr("ls: list failed\n");
  51. return;
  52. }
  53. }