ucmd_save.c 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. /* save: ring-3 command. Writes a text string to a file.
  2. * The terminal resolves the path and passes "<absolute-path> <text...>" as the
  3. * single argument, so this only splits it and calls the kernel service. */
  4. #include "ucmd_lib.h"
  5. void _start(const char *arg)
  6. {
  7. char fn[64];
  8. unsigned int i = 0, fn_len = 0, text_len = 0;
  9. const char *text;
  10. int r;
  11. if (!arg || !arg[0]) {
  12. u_puts("save: usage save /<device>/<file> <text>\n");
  13. u_exit(1);
  14. }
  15. while (arg[i] == ' ')
  16. i++;
  17. while (arg[i + fn_len] && arg[i + fn_len] != ' ' && fn_len < sizeof(fn) - 1)
  18. fn_len++;
  19. {
  20. unsigned int j;
  21. for (j = 0; j < fn_len; j++)
  22. fn[j] = arg[i + j];
  23. }
  24. fn[fn_len] = '\0';
  25. if (fn_len == 0) {
  26. u_puts("save: usage save /<device>/<file> <text>\n");
  27. u_exit(1);
  28. }
  29. i += fn_len;
  30. while (arg[i] == ' ')
  31. i++;
  32. text = arg + i;
  33. while (text[text_len])
  34. text_len++;
  35. r = u_syscall3(SYS_WRITE_FILE, (int)fn, (int)text, (int)text_len);
  36. if (r == SYS_ENODEV || r == SYS_ENOFS) {
  37. u_puts("save: unknown device or invalid path\n");
  38. u_exit(1);
  39. }
  40. if (r == SYS_EIO) {
  41. u_puts("save: disk write error\n");
  42. u_exit(1);
  43. }
  44. if (r != 0) {
  45. u_puts("save: write failed\n");
  46. u_exit(1);
  47. }
  48. u_puts("saved\n");
  49. u_exit(0);
  50. }