1
0

print.c 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. #define word char
  2. #include "lib/math.c"
  3. #include "lib/stdlib.c"
  4. #include "lib/sys.c"
  5. #include "lib/brfs.c"
  6. #define READ_BUFFER_SIZE 64
  7. int main()
  8. {
  9. // Read number of arguments
  10. word argc = shell_argc();
  11. if (argc < 2)
  12. {
  13. bdos_println("Usage: print <file>");
  14. return 1;
  15. }
  16. // Read filename
  17. char** args = shell_argv();
  18. char* filename = args[1];
  19. char absolute_path[MAX_PATH_LENGTH];
  20. // Check if absolute path
  21. if (filename[0] != '/')
  22. {
  23. char* cwd = fs_getcwd();
  24. strcpy(absolute_path, cwd);
  25. strcat(absolute_path, "/");
  26. strcat(absolute_path, filename);
  27. }
  28. else
  29. {
  30. strcpy(absolute_path, filename);
  31. }
  32. // Get file size
  33. struct brfs_dir_entry* entry = (struct brfs_dir_entry*)fs_stat(absolute_path);
  34. if ((word)entry == -1)
  35. {
  36. bdos_println("File not found");
  37. return 1;
  38. }
  39. word filesize = entry->filesize;
  40. // Open file
  41. word fd = fs_open(absolute_path);
  42. if (fd == -1)
  43. {
  44. bdos_println("File not found");
  45. return 1;
  46. }
  47. // Read file in chunks of READ_BUFFER_SIZE
  48. word file_buffer[READ_BUFFER_SIZE + 1]; // +1 for null terminator
  49. word chunk_to_read;
  50. while (filesize > 0)
  51. {
  52. chunk_to_read = filesize > READ_BUFFER_SIZE ? READ_BUFFER_SIZE : filesize;
  53. memset(file_buffer, 0, READ_BUFFER_SIZE + 1);
  54. fs_read(fd, file_buffer, chunk_to_read);
  55. file_buffer[chunk_to_read] = 0; // Null terminator
  56. bdos_print(file_buffer);
  57. filesize -= chunk_to_read;
  58. }
  59. // Close file
  60. fs_close(fd);
  61. return 'q';
  62. }
  63. void interrupt()
  64. {
  65. // Handle all interrupts
  66. word i = get_int_id();
  67. switch(i)
  68. {
  69. case INTID_TIMER1:
  70. timer1Value = 1; // Notify ending of timer1
  71. break;
  72. }
  73. }