ucmd_ls.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. for (i = 0; i < 11 && d->name[i]; i++)
  36. u_putc(d->name[i] == ' ' ? ' ' : d->name[i]);
  37. if (i < 11)
  38. u_putc(' ');
  39. if (d->is_dir) {
  40. u_puts(" <DIR>\n");
  41. return;
  42. }
  43. u_puts(" ");
  44. n = d->size;
  45. if (n == 0) {
  46. u_putc('0');
  47. } else {
  48. i = BUF_SIZE - 1;
  49. buf[i] = '\0';
  50. while (n && i > 0) {
  51. buf[--i] = (char)('0' + (n % 10));
  52. n /= 10;
  53. }
  54. u_puts(buf + i);
  55. }
  56. u_putc('\n');
  57. }
  58. void _start(const char *path)
  59. {
  60. char cwd[VFS_PATH_MAX];
  61. /* The shell passes arguments verbatim; an empty one means the current
  62. * working directory (and "/" is the mount listing). */
  63. if (!path || !path[0]) {
  64. if (u_getcwd(cwd, sizeof(cwd)) != 0)
  65. cwd[0] = '\0';
  66. path = cwd;
  67. }
  68. if (!path || !path[0] || (path[0] == '/' && path[1] == '\0')) {
  69. struct sys_mount m;
  70. unsigned int i = 0;
  71. while (u_syscall2(SYS_LIST_MOUNTS, (int)i, (int)&m) == 1) {
  72. u_puts(m.name);
  73. u_puts(" ");
  74. u_puts(m.type);
  75. u_putc(' ');
  76. put_size_sectors(m.total_sectors);
  77. if (m.label[0]) {
  78. u_puts(" \"");
  79. u_puts(m.label);
  80. u_putc('"');
  81. }
  82. u_putc('\n');
  83. i++;
  84. }
  85. u_exit(0);
  86. }
  87. {
  88. struct sys_dirent d;
  89. unsigned int i = 0;
  90. int r;
  91. while ((r = u_syscall3(SYS_LIST_DIR, (int)path, (int)i,
  92. (int)&d)) == 1) {
  93. print_entry(&d);
  94. i++;
  95. }
  96. if (r == SYS_ENOENT)
  97. u_puts("ls: no such file or directory\n");
  98. else if (r == SYS_ENODEV)
  99. u_puts("ls: no such device\n");
  100. else if (r == SYS_ENOFS)
  101. u_puts("ls: no filesystem on device\n");
  102. else if (r == SYS_EINVAL)
  103. u_puts("ls: not a directory\n");
  104. else if (r == SYS_EIO)
  105. u_puts("ls: disk error\n");
  106. }
  107. u_exit(0);
  108. }