rm.c 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. int main()
  7. {
  8. // Read number of arguments
  9. word argc = shell_argc();
  10. if (argc < 2)
  11. {
  12. bdos_println("Usage: rm <file/dir>");
  13. return 1;
  14. }
  15. // Read file/dir name
  16. char** args = shell_argv();
  17. char* fname = args[1];
  18. char absolute_path[MAX_PATH_LENGTH];
  19. // Check if absolute path
  20. if (fname[0] != '/')
  21. {
  22. char* cwd = fs_getcwd();
  23. strcpy(absolute_path, cwd);
  24. strcat(absolute_path, "/");
  25. strcat(absolute_path, fname);
  26. }
  27. else
  28. {
  29. strcpy(absolute_path, fname);
  30. }
  31. // Check if fname is a file or directory
  32. struct brfs_dir_entry* entry = (struct brfs_dir_entry*)fs_stat(absolute_path);
  33. if ((word)entry == -1)
  34. {
  35. bdos_println("File not found");
  36. return 1;
  37. }
  38. if ((entry->flags & 0x01) == 0)
  39. {
  40. // File
  41. if (fs_delete(absolute_path))
  42. {
  43. fs_syncflash();
  44. }
  45. else
  46. {
  47. bdos_println("Could not delete file");
  48. }
  49. }
  50. else
  51. {
  52. // Directory
  53. uprintln("del dir");
  54. // TODO: Implement recursive deletion of directory
  55. }
  56. return 'q';
  57. }
  58. void interrupt()
  59. {
  60. // Handle all interrupts
  61. word i = get_int_id();
  62. switch(i)
  63. {
  64. case INTID_TIMER1:
  65. timer1Value = 1; // Notify ending of timer1
  66. break;
  67. }
  68. }