save.c 1.4 KB

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