default.rs 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. use super::{constants, Buffer, BUFSIZ, FILE};
  2. use core::{cell::UnsafeCell, ptr};
  3. use crate::{fs::File, io::LineWriter, platform::types::*, sync::Mutex};
  4. use alloc::vec::Vec;
  5. pub struct GlobalFile(UnsafeCell<FILE>);
  6. impl GlobalFile {
  7. fn new(file: c_int, flags: c_int) -> Self {
  8. let file = File::new(file);
  9. let writer = LineWriter::new(unsafe { file.get_ref() });
  10. GlobalFile(UnsafeCell::new(FILE {
  11. lock: Mutex::new(()),
  12. file,
  13. flags: constants::F_PERM | flags,
  14. read_buf: Buffer::Owned(vec![0; BUFSIZ as usize]),
  15. read_pos: 0,
  16. read_size: 0,
  17. unget: Vec::new(),
  18. writer,
  19. pid: None,
  20. orientation: 0,
  21. }))
  22. }
  23. pub fn get(&self) -> *mut FILE {
  24. self.0.get()
  25. }
  26. }
  27. // statics need to be Sync
  28. unsafe impl Sync for GlobalFile {}
  29. lazy_static! {
  30. #[allow(non_upper_case_globals)]
  31. pub static ref default_stdin: GlobalFile = GlobalFile::new(0, constants::F_NOWR);
  32. #[allow(non_upper_case_globals)]
  33. pub static ref default_stdout: GlobalFile = GlobalFile::new(1, constants::F_NORD);
  34. #[allow(non_upper_case_globals)]
  35. pub static ref default_stderr: GlobalFile = GlobalFile::new(2, constants::F_NORD);
  36. }
  37. #[no_mangle]
  38. pub static mut stdin: *mut FILE = ptr::null_mut();
  39. #[no_mangle]
  40. pub static mut stdout: *mut FILE = ptr::null_mut();
  41. #[no_mangle]
  42. pub static mut stderr: *mut FILE = ptr::null_mut();