ls.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. #define word char
  2. #include "lib/math.c"
  3. #include "lib/stdlib.c"
  4. #include "lib/sys.c"
  5. #include "lib/brfs.c"
  6. /**
  7. * List the contents of a directory
  8. * Sort by filename
  9. */
  10. void list_dir(char* path)
  11. {
  12. struct brfs_dir_entry entries[MAX_DIR_ENTRIES];
  13. word num_entries = fs_readdir(path, entries);
  14. // Keep track of the longest filename for formatting
  15. word max_filename_length = 0;
  16. // Sort entries by filename
  17. word i, j;
  18. for (i = 0; i < num_entries - 1; i++)
  19. {
  20. for (j = 0; j < num_entries - i - 1; j++)
  21. {
  22. char decompressed_filename1[17];
  23. strdecompress(decompressed_filename1, entries[j].filename);
  24. char decompressed_filename2[17];
  25. strdecompress(decompressed_filename2, entries[j + 1].filename);
  26. // Update max_filename_length
  27. // This works because . is always the first entry (skipped by this check)
  28. if (strlen(decompressed_filename2) > max_filename_length)
  29. {
  30. max_filename_length = strlen(decompressed_filename2);
  31. }
  32. // Sort by filename
  33. if (strcmp(decompressed_filename1, decompressed_filename2) > 0)
  34. {
  35. // Swap filenames
  36. struct brfs_dir_entry temp = entries[j];
  37. entries[j] = entries[j + 1];
  38. entries[j + 1] = temp;
  39. }
  40. }
  41. }
  42. for (i = 0; i < num_entries; i++)
  43. {
  44. // Create entire line with blank spaces
  45. char output_line[40];
  46. memset(output_line, ' ', 40);
  47. output_line[40] = 0;
  48. // Add filename
  49. struct brfs_dir_entry entry = entries[i];
  50. strdecompress(output_line, entry.filename);
  51. output_line[strlen(output_line)] = ' ';
  52. // Add filesize if file
  53. if ((entry.flags & 0x01) == 0)
  54. {
  55. char buffer[11];
  56. itoa(entry.filesize, buffer);
  57. memcpy(output_line + max_filename_length + 1, buffer, strlen(buffer));
  58. }
  59. bdos_print(output_line);
  60. }
  61. }
  62. int main()
  63. {
  64. // Read number of arguments
  65. word argc = shell_argc();
  66. char** args = shell_argv();
  67. // Read file/dir name if provided
  68. char* fname;
  69. if (argc < 2)
  70. {
  71. fname = fs_getcwd();
  72. }
  73. else
  74. {
  75. fname = args[1];
  76. }
  77. // Create absolute path
  78. char absolute_path[MAX_PATH_LENGTH];
  79. if (fname[0] != '/')
  80. {
  81. char* cwd = fs_getcwd();
  82. strcpy(absolute_path, cwd);
  83. if (absolute_path[strlen(absolute_path) - 1] != '/')
  84. {
  85. strcat(absolute_path, "/");
  86. }
  87. strcat(absolute_path, fname);
  88. }
  89. else
  90. {
  91. strcpy(absolute_path, fname);
  92. }
  93. // If path is root, list root directory
  94. if (strcmp(absolute_path, "/") == 0)
  95. {
  96. list_dir("/");
  97. return 'q';
  98. }
  99. // Check if fname is a file or directory
  100. struct brfs_dir_entry* entry = (struct brfs_dir_entry*)fs_stat(absolute_path);
  101. if ((word)entry == -1)
  102. {
  103. bdos_println("Dir not found");
  104. return 'q';
  105. }
  106. if ((entry->flags & 0x01) == 0)
  107. {
  108. // File
  109. bdos_println("Not a directory");
  110. }
  111. else
  112. {
  113. // Directory
  114. list_dir(absolute_path);
  115. }
  116. return 'q';
  117. }
  118. void interrupt()
  119. {
  120. // Handle all interrupts
  121. word i = get_int_id();
  122. switch(i)
  123. {
  124. case INTID_TIMER1:
  125. timer1Value = 1; // Notify ending of timer1
  126. break;
  127. }
  128. }