mod.rs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. use crate::io::{self, Read, Write};
  2. use alloc::vec::Vec;
  3. use core::{fmt, ptr};
  4. pub use self::allocator::*;
  5. #[cfg(not(feature = "ralloc"))]
  6. #[path = "allocator/dlmalloc.rs"]
  7. mod allocator;
  8. #[cfg(feature = "ralloc")]
  9. #[path = "allocator/ralloc.rs"]
  10. mod allocator;
  11. pub use self::pal::{Pal, PalEpoll, PalPtrace, PalSignal, PalSocket};
  12. mod pal;
  13. pub use self::sys::{e, Sys};
  14. #[cfg(all(not(feature = "no_std"), target_os = "linux"))]
  15. #[path = "linux/mod.rs"]
  16. mod sys;
  17. #[cfg(all(not(feature = "no_std"), target_os = "redox"))]
  18. #[path = "redox/mod.rs"]
  19. mod sys;
  20. #[cfg(test)]
  21. mod test;
  22. mod pte;
  23. pub use self::rlb::{Line, RawLineBuffer};
  24. pub mod rlb;
  25. use self::types::*;
  26. pub mod types;
  27. #[thread_local]
  28. #[allow(non_upper_case_globals)]
  29. #[no_mangle]
  30. pub static mut errno: c_int = 0;
  31. #[allow(non_upper_case_globals)]
  32. pub static mut argv: *mut *mut c_char = ptr::null_mut();
  33. #[allow(non_upper_case_globals)]
  34. pub static mut inner_argv: Vec<*mut c_char> = Vec::new();
  35. #[allow(non_upper_case_globals)]
  36. pub static mut program_invocation_name: *mut c_char = ptr::null_mut();
  37. #[allow(non_upper_case_globals)]
  38. pub static mut program_invocation_short_name: *mut c_char = ptr::null_mut();
  39. #[allow(non_upper_case_globals)]
  40. #[no_mangle]
  41. pub static mut environ: *mut *mut c_char = ptr::null_mut();
  42. #[allow(non_upper_case_globals)]
  43. pub static mut inner_environ: Vec<*mut c_char> = Vec::new();
  44. pub trait WriteByte: fmt::Write {
  45. fn write_u8(&mut self, byte: u8) -> fmt::Result;
  46. }
  47. impl<'a, W: WriteByte> WriteByte for &'a mut W {
  48. fn write_u8(&mut self, byte: u8) -> fmt::Result {
  49. (**self).write_u8(byte)
  50. }
  51. }
  52. pub struct FileWriter(pub c_int);
  53. impl FileWriter {
  54. pub fn write(&mut self, buf: &[u8]) -> isize {
  55. Sys::write(self.0, buf)
  56. }
  57. }
  58. impl fmt::Write for FileWriter {
  59. fn write_str(&mut self, s: &str) -> fmt::Result {
  60. self.write(s.as_bytes());
  61. Ok(())
  62. }
  63. }
  64. impl WriteByte for FileWriter {
  65. fn write_u8(&mut self, byte: u8) -> fmt::Result {
  66. self.write(&[byte]);
  67. Ok(())
  68. }
  69. }
  70. pub struct FileReader(pub c_int);
  71. impl FileReader {
  72. pub fn read(&mut self, buf: &mut [u8]) -> isize {
  73. Sys::read(self.0, buf)
  74. }
  75. }
  76. impl Read for FileReader {
  77. fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
  78. let i = Sys::read(self.0, buf);
  79. if i >= 0 {
  80. Ok(i as usize)
  81. } else {
  82. Err(io::Error::from_raw_os_error(-i as i32))
  83. }
  84. }
  85. }
  86. pub struct StringWriter(pub *mut u8, pub usize);
  87. impl Write for StringWriter {
  88. fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
  89. if self.1 > 1 {
  90. let copy_size = buf.len().min(self.1 - 1);
  91. unsafe {
  92. ptr::copy_nonoverlapping(buf.as_ptr(), self.0, copy_size);
  93. self.1 -= copy_size;
  94. self.0 = self.0.add(copy_size);
  95. *self.0 = 0;
  96. }
  97. }
  98. // Pretend the entire slice was written. This is because many functions
  99. // (like snprintf) expects a return value that reflects how many bytes
  100. // *would have* been written. So keeping track of this information is
  101. // good, and then if we want the *actual* written size we can just go
  102. // `cmp::min(written, maxlen)`.
  103. Ok(buf.len())
  104. }
  105. fn flush(&mut self) -> io::Result<()> {
  106. Ok(())
  107. }
  108. }
  109. impl fmt::Write for StringWriter {
  110. fn write_str(&mut self, s: &str) -> fmt::Result {
  111. // can't fail
  112. self.write(s.as_bytes()).unwrap();
  113. Ok(())
  114. }
  115. }
  116. impl WriteByte for StringWriter {
  117. fn write_u8(&mut self, byte: u8) -> fmt::Result {
  118. // can't fail
  119. self.write(&[byte]).unwrap();
  120. Ok(())
  121. }
  122. }
  123. pub struct UnsafeStringWriter(pub *mut u8);
  124. impl Write for UnsafeStringWriter {
  125. fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
  126. unsafe {
  127. ptr::copy_nonoverlapping(buf.as_ptr(), self.0, buf.len());
  128. self.0 = self.0.add(buf.len());
  129. *self.0 = b'\0';
  130. }
  131. Ok(buf.len())
  132. }
  133. fn flush(&mut self) -> io::Result<()> {
  134. Ok(())
  135. }
  136. }
  137. impl fmt::Write for UnsafeStringWriter {
  138. fn write_str(&mut self, s: &str) -> fmt::Result {
  139. // can't fail
  140. self.write(s.as_bytes()).unwrap();
  141. Ok(())
  142. }
  143. }
  144. impl WriteByte for UnsafeStringWriter {
  145. fn write_u8(&mut self, byte: u8) -> fmt::Result {
  146. // can't fail
  147. self.write(&[byte]).unwrap();
  148. Ok(())
  149. }
  150. }
  151. pub struct UnsafeStringReader(pub *const u8);
  152. impl Read for UnsafeStringReader {
  153. fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
  154. unsafe {
  155. for i in 0..buf.len() {
  156. if *self.0 == 0 {
  157. return Ok(i);
  158. }
  159. buf[i] = *self.0;
  160. self.0 = self.0.offset(1);
  161. }
  162. Ok(buf.len())
  163. }
  164. }
  165. }
  166. pub struct CountingWriter<T> {
  167. pub inner: T,
  168. pub written: usize,
  169. }
  170. impl<T> CountingWriter<T> {
  171. pub fn new(writer: T) -> Self {
  172. Self {
  173. inner: writer,
  174. written: 0,
  175. }
  176. }
  177. }
  178. impl<T: fmt::Write> fmt::Write for CountingWriter<T> {
  179. fn write_str(&mut self, s: &str) -> fmt::Result {
  180. self.written += s.len();
  181. self.inner.write_str(s)
  182. }
  183. }
  184. impl<T: WriteByte> WriteByte for CountingWriter<T> {
  185. fn write_u8(&mut self, byte: u8) -> fmt::Result {
  186. self.written += 1;
  187. self.inner.write_u8(byte)
  188. }
  189. }
  190. impl<T: Write> Write for CountingWriter<T> {
  191. fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
  192. let res = self.inner.write(buf);
  193. if let Ok(written) = res {
  194. self.written += written;
  195. }
  196. res
  197. }
  198. fn write_all(&mut self, buf: &[u8]) -> io::Result<()> {
  199. match self.inner.write_all(&buf) {
  200. Ok(()) => (),
  201. Err(ref err) if err.kind() == io::ErrorKind::WriteZero => (),
  202. Err(err) => return Err(err),
  203. }
  204. self.written += buf.len();
  205. Ok(())
  206. }
  207. fn flush(&mut self) -> io::Result<()> {
  208. self.inner.flush()
  209. }
  210. }