default.rs 1.4 KB

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