Mirror of CollapseOS
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

55 lines
1.1KB

  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <fcntl.h>
  4. #include <unistd.h>
  5. #include <string.h>
  6. #include "common.h"
  7. /* Execute code from target file on the target machine.
  8. fname can be "-" for stdin.
  9. */
  10. int main(int argc, char **argv)
  11. {
  12. if (argc != 3) {
  13. fprintf(stderr, "Usage: ./exec device fname\n");
  14. return 1;
  15. }
  16. FILE *fp = stdin;
  17. if (strcmp(argv[2], "-") != 0) {
  18. fp = fopen(argv[2], "r");
  19. if (fp == NULL) {
  20. fprintf(stderr, "Could not open %s\n", argv[2]);
  21. return 1;
  22. }
  23. }
  24. int fd = ttyopen(argv[1]);
  25. if (fd < 0) {
  26. fprintf(stderr, "Could not open %s\n", argv[1]);
  27. return 1;
  28. }
  29. set_blocking(fd, 0);
  30. int c = getc(fp);
  31. while (c != EOF) {
  32. if (c == '\n') c = '\r';
  33. write(fd, &c, 1);
  34. usleep(1000); // let it breathe
  35. while (read(fd, &c, 1) == 1) {
  36. putchar(c);
  37. fflush(stdout);
  38. }
  39. c = getc(fp);
  40. }
  41. if (fd > 0) {
  42. close(fd);
  43. }
  44. if (strcmp(argv[2], "-") != 0) {
  45. fclose(fp);
  46. }
  47. printf("Done!\n");
  48. return 0;
  49. }