save.c 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. /* save command: write a text string to a file. Usage: save <file> <text...>. */
  2. #include "fat.h"
  3. #include "io.h"
  4. void program_save_main(const char *arg)
  5. {
  6. char fn[64];
  7. unsigned int i, fn_len, text_len;
  8. unsigned int dir_cluster;
  9. char name_8_3[12];
  10. const char *text;
  11. int r;
  12. if (!arg || !arg[0]) {
  13. io_putstr("save: usage save <file> <text>\n");
  14. return;
  15. }
  16. /* filename = first token */
  17. i = 0;
  18. while (arg[i] == ' ')
  19. i++;
  20. fn_len = 0;
  21. while (arg[i + fn_len] && arg[i + fn_len] != ' ' && fn_len < sizeof(fn) - 1)
  22. fn_len++;
  23. {
  24. unsigned int j;
  25. for (j = 0; j < fn_len; j++)
  26. fn[j] = arg[i + j];
  27. }
  28. fn[fn_len] = '\0';
  29. if (fn_len == 0) {
  30. io_putstr("save: usage save <file> <text>\n");
  31. return;
  32. }
  33. /* text = the rest */
  34. i += fn_len;
  35. while (arg[i] == ' ')
  36. i++;
  37. text = arg + i;
  38. text_len = 0;
  39. while (text[text_len])
  40. text_len++;
  41. r = fat_init();
  42. if (r == -2) { io_putstr("save: disk read error\n"); return; }
  43. if (r != 0) { io_putstr("save: no FAT filesystem found\n"); return; }
  44. r = fat_resolve_path(fn, &dir_cluster, name_8_3);
  45. if (r == -2) { io_putstr("save: disk error\n"); return; }
  46. if (r != 0) { io_putstr("save: invalid path\n"); return; }
  47. r = fat_write_file_in(dir_cluster, name_8_3, text, text_len);
  48. if (r == -2) { io_putstr("save: disk write error\n"); return; }
  49. if (r != 0) { io_putstr("save: write failed\n"); return; }
  50. io_putstr("saved\n");
  51. }