save.c 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /* save command: write a text string to a file on the FAT12 floppy.
  2. * Usage: save <file> <text...> (text may contain spaces). */
  3. #include "fat.h"
  4. #include "io.h"
  5. #define NAME_BUF_SIZE 12
  6. static void to_8_3(const char *in, unsigned int in_len, char *out)
  7. {
  8. unsigned int i, j, dot;
  9. for (i = 0; i < in_len && in[i] == ' '; i++)
  10. ;
  11. dot = i;
  12. while (dot < in_len && in[dot] != '.' && in[dot] != ' ')
  13. dot++;
  14. j = 0;
  15. while (i < dot && j < 8) {
  16. out[j++] = (char)(in[i] >= 'a' && in[i] <= 'z' ? in[i] - 32 : in[i]);
  17. i++;
  18. }
  19. while (j < 8)
  20. out[j++] = ' ';
  21. if (i < in_len && in[i] == '.')
  22. i++;
  23. while (i < in_len && in[i] != ' ' && j < 11) {
  24. out[j++] = (char)(in[i] >= 'a' && in[i] <= 'z' ? in[i] - 32 : in[i]);
  25. i++;
  26. }
  27. while (j < 11)
  28. out[j++] = ' ';
  29. out[11] = '\0';
  30. }
  31. void program_save_main(const char *arg)
  32. {
  33. char name_8_3[NAME_BUF_SIZE];
  34. const char *text;
  35. unsigned int i, fn_len, text_len;
  36. int r;
  37. if (!arg || !arg[0]) {
  38. io_putstr("save: usage save <file> <text>\n");
  39. return;
  40. }
  41. /* filename = first token */
  42. i = 0;
  43. while (arg[i] == ' ')
  44. i++;
  45. fn_len = 0;
  46. while (arg[i + fn_len] && arg[i + fn_len] != ' ')
  47. fn_len++;
  48. if (fn_len == 0) {
  49. io_putstr("save: usage save <file> <text>\n");
  50. return;
  51. }
  52. to_8_3(arg + i, fn_len, name_8_3);
  53. /* text = the rest (trimmed) */
  54. i += fn_len;
  55. while (arg[i] == ' ')
  56. i++;
  57. text = arg + i;
  58. text_len = 0;
  59. while (text[text_len])
  60. text_len++;
  61. r = fat_init();
  62. if (r == -2) {
  63. io_putstr("save: disk read error\n");
  64. return;
  65. }
  66. if (r != 0) {
  67. io_putstr("save: no FAT filesystem found\n");
  68. return;
  69. }
  70. r = fat_write_file(name_8_3, text, text_len);
  71. if (r == -2) {
  72. io_putstr("save: disk write error\n");
  73. return;
  74. }
  75. if (r != 0) {
  76. io_putstr("save: write failed\n");
  77. return;
  78. }
  79. io_putstr("saved\n");
  80. }