1
0

rm.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. void remove_dir(char* path)
  7. {
  8. struct brfs_dir_entry entries[MAX_DIR_ENTRIES];
  9. word num_entries = fs_readdir(path, entries);
  10. word i;
  11. for (i = 0; i < num_entries; i++)
  12. {
  13. struct brfs_dir_entry entry = entries[i];
  14. char decompressed_filename[17];
  15. strdecompress(decompressed_filename, entry.filename);
  16. if (strcmp(decompressed_filename, ".") == 0 || strcmp(decompressed_filename, "..") == 0)
  17. {
  18. continue;
  19. }
  20. char absolute_path[MAX_PATH_LENGTH];
  21. strcpy(absolute_path, path);
  22. strcat(absolute_path, "/");
  23. strcat(absolute_path, decompressed_filename);
  24. if ((entry.flags & 0x01) == 0)
  25. {
  26. // File
  27. if (fs_delete(absolute_path))
  28. {
  29. // Do not sync flash after each file deletion
  30. }
  31. else
  32. {
  33. bdos_println("Could not delete file");
  34. }
  35. }
  36. else
  37. {
  38. // Directory
  39. remove_dir(absolute_path);
  40. }
  41. }
  42. // Delete directory after contents are deleted
  43. if (fs_delete(path))
  44. {
  45. // Do not sync flash after each directory deletion
  46. }
  47. else
  48. {
  49. bdos_println("Could not delete directory");
  50. }
  51. }
  52. int main()
  53. {
  54. // Read number of arguments
  55. word argc = shell_argc();
  56. if (argc < 2)
  57. {
  58. bdos_println("Usage: rm <file/dir>");
  59. return 1;
  60. }
  61. // Read file/dir name
  62. char** args = shell_argv();
  63. char* fname = args[1];
  64. char absolute_path[MAX_PATH_LENGTH];
  65. // Check if absolute path
  66. if (fname[0] != '/')
  67. {
  68. char* cwd = fs_getcwd();
  69. strcpy(absolute_path, cwd);
  70. if (absolute_path[strlen(absolute_path) - 1] != '/')
  71. {
  72. strcat(absolute_path, "/");
  73. }
  74. strcat(absolute_path, fname);
  75. }
  76. else
  77. {
  78. strcpy(absolute_path, fname);
  79. }
  80. // Check if fname is a file or directory
  81. struct brfs_dir_entry* entry = (struct brfs_dir_entry*)fs_stat(absolute_path);
  82. if ((word)entry == -1)
  83. {
  84. bdos_println("File not found");
  85. return 1;
  86. }
  87. // Check if root directory
  88. if (entry->fat_idx == 0)
  89. {
  90. bdos_println("Cannot delete root directory");
  91. return 1;
  92. }
  93. if ((entry->flags & 0x01) == 0)
  94. {
  95. // File
  96. if (fs_delete(absolute_path))
  97. {
  98. //fs_syncflash();
  99. }
  100. else
  101. {
  102. bdos_println("Could not delete file");
  103. }
  104. }
  105. else
  106. {
  107. // Directory
  108. remove_dir(absolute_path);
  109. //fs_syncflash();
  110. }
  111. return 'q';
  112. }
  113. void interrupt()
  114. {
  115. // Handle all interrupts
  116. word i = get_int_id();
  117. switch(i)
  118. {
  119. case INTID_TIMER1:
  120. timer1Value = 1; // Notify ending of timer1
  121. break;
  122. }
  123. }