ucmd_ls.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /* ls: ring-3 command. `ls` / `ls /` lists mounted filesystems;
  2. * `ls /<device>[/dir]` lists a directory on that filesystem. */
  3. #include "ucmd_lib.h"
  4. #define VFS_PATH_MAX 64
  5. #define BUF_SIZE 12
  6. static void put_size_sectors(unsigned int sectors)
  7. {
  8. if (sectors >= 2u * 1024u * 1024u) { /* >= 1 GiB */
  9. unsigned int gb = sectors >> 21;
  10. unsigned int tenths = ((sectors & 0x1FFFFFu) * 10u) >> 21;
  11. u_putdec(gb);
  12. if (tenths) {
  13. u_putc('.');
  14. u_putdec(tenths);
  15. }
  16. u_puts(" GB");
  17. } else if (sectors >= 2048u) { /* >= 1 MiB */
  18. unsigned int mb = sectors >> 11;
  19. unsigned int tenths = ((sectors & 0x7FFu) * 10u) >> 11;
  20. u_putdec(mb);
  21. if (tenths) {
  22. u_putc('.');
  23. u_putdec(tenths);
  24. }
  25. u_puts(" MB");
  26. } else {
  27. u_putdec(sectors / 2u);
  28. u_puts(" KB");
  29. }
  30. }
  31. static void print_entry(const struct sys_dirent *d)
  32. {
  33. unsigned int i, n;
  34. char buf[BUF_SIZE];
  35. if (!d->raw_83) {
  36. u_puts(d->name);
  37. if (d->is_dir) {
  38. u_puts(" <DIR>\n");
  39. return;
  40. }
  41. u_putc(' ');
  42. } else {
  43. for (i = 0; i < 11 && d->name[i]; i++)
  44. u_putc(d->name[i] == ' ' ? ' ' : d->name[i]);
  45. if (i < 11)
  46. u_putc(' ');
  47. if (d->is_dir) {
  48. u_puts(" <DIR>\n");
  49. return;
  50. }
  51. u_puts(" ");
  52. }
  53. n = d->size;
  54. if (n == 0) {
  55. u_putc('0');
  56. } else {
  57. i = BUF_SIZE - 1;
  58. buf[i] = '\0';
  59. while (n && i > 0) {
  60. buf[--i] = (char)('0' + (n % 10));
  61. n /= 10;
  62. }
  63. u_puts(buf + i);
  64. }
  65. u_putc('\n');
  66. }
  67. void _start(const char *path)
  68. {
  69. char cwd[VFS_PATH_MAX];
  70. /* The shell passes arguments verbatim; an empty one means the current
  71. * working directory (and "/" is the mount listing). */
  72. if (!path || !path[0]) {
  73. if (u_getcwd(cwd, sizeof(cwd)) != 0)
  74. cwd[0] = '\0';
  75. path = cwd;
  76. }
  77. if (!path || !path[0] || (path[0] == '/' && path[1] == '\0')) {
  78. struct sys_mount m;
  79. unsigned int i = 0;
  80. while (u_syscall2(SYS_LIST_MOUNTS, (int)i, (int)&m) == 1) {
  81. u_puts(m.name);
  82. u_puts(" ");
  83. u_puts(m.type);
  84. u_putc(' ');
  85. put_size_sectors(m.total_sectors);
  86. if (m.label[0]) {
  87. u_puts(" \"");
  88. u_puts(m.label);
  89. u_putc('"');
  90. }
  91. u_putc('\n');
  92. i++;
  93. }
  94. u_exit(0);
  95. }
  96. {
  97. struct sys_dirent d;
  98. unsigned int i = 0;
  99. int r;
  100. while ((r = u_syscall3(SYS_LIST_DIR, (int)path, (int)i,
  101. (int)&d)) == 1) {
  102. print_entry(&d);
  103. i++;
  104. }
  105. if (r == SYS_ENOENT)
  106. u_puts("ls: no such file or directory\n");
  107. else if (r == SYS_ENODEV)
  108. u_puts("ls: no such device\n");
  109. else if (r == SYS_ENOFS)
  110. u_puts("ls: no filesystem on device\n");
  111. else if (r == SYS_EINVAL)
  112. u_puts("ls: not a directory\n");
  113. else if (r == SYS_EIO)
  114. u_puts("ls: disk error\n");
  115. }
  116. u_exit(0);
  117. }