rm.c 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /* rm command: delete a file from the FAT12 floppy. */
  2. #include "fat.h"
  3. #include "io.h"
  4. #define NAME_BUF_SIZE 12
  5. static void to_8_3(const char *in, unsigned int in_len, char *out)
  6. {
  7. unsigned int i, j, dot;
  8. for (i = 0; i < in_len && in[i] == ' '; i++)
  9. ;
  10. dot = i;
  11. while (dot < in_len && in[dot] != '.' && in[dot] != ' ')
  12. dot++;
  13. j = 0;
  14. while (i < dot && j < 8) {
  15. out[j++] = (char)(in[i] >= 'a' && in[i] <= 'z' ? in[i] - 32 : in[i]);
  16. i++;
  17. }
  18. while (j < 8)
  19. out[j++] = ' ';
  20. if (i < in_len && in[i] == '.')
  21. i++;
  22. while (i < in_len && in[i] != ' ' && j < 11) {
  23. out[j++] = (char)(in[i] >= 'a' && in[i] <= 'z' ? in[i] - 32 : in[i]);
  24. i++;
  25. }
  26. while (j < 11)
  27. out[j++] = ' ';
  28. out[11] = '\0';
  29. }
  30. void program_rm_main(const char *arg)
  31. {
  32. char name_8_3[NAME_BUF_SIZE];
  33. unsigned int i, len;
  34. int r;
  35. if (!arg || !arg[0]) {
  36. io_putstr("rm: usage rm <file>\n");
  37. return;
  38. }
  39. i = 0;
  40. while (arg[i] == ' ')
  41. i++;
  42. len = 0;
  43. while (arg[i + len])
  44. len++;
  45. if (len == 0) {
  46. io_putstr("rm: usage rm <file>\n");
  47. return;
  48. }
  49. to_8_3(arg + i, len, name_8_3);
  50. r = fat_init();
  51. if (r == -2) {
  52. io_putstr("rm: disk read error\n");
  53. return;
  54. }
  55. if (r != 0) {
  56. io_putstr("rm: no FAT filesystem found\n");
  57. return;
  58. }
  59. r = fat_delete_file(name_8_3);
  60. if (r == -2) {
  61. io_putstr("rm: disk error\n");
  62. return;
  63. }
  64. if (r != 0) {
  65. io_putstr("rm: file not found\n");
  66. return;
  67. }
  68. io_putstr("removed\n");
  69. }