lib.rs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. //! fcntl implementation for Redox, following http://pubs.opengroup.org/onlinepubs/7908799/xsh/fcntl.h.html
  2. #![no_std]
  3. #![allow(non_camel_case_types)]
  4. //TODO #![feature(thread_local)]
  5. #[cfg(all(not(feature = "no_std"), target_os = "linux"))]
  6. #[macro_use]
  7. extern crate sc;
  8. #[cfg(all(not(feature = "no_std"), target_os = "redox"))]
  9. #[macro_use]
  10. extern crate syscall;
  11. pub use sys::*;
  12. #[cfg(all(not(feature = "no_std"), target_os = "linux"))]
  13. #[path = "linux/mod.rs"]
  14. mod sys;
  15. #[cfg(all(not(feature = "no_std"), target_os = "redox"))]
  16. #[path = "redox/mod.rs"]
  17. mod sys;
  18. pub mod types;
  19. use core::fmt;
  20. use types::*;
  21. //TODO #[thread_local]
  22. #[allow(non_upper_case_globals)]
  23. #[no_mangle]
  24. pub static mut errno: c_int = 0;
  25. pub unsafe fn c_str(s: *const c_char) -> &'static [u8] {
  26. use core::usize;
  27. c_str_n(s, usize::MAX)
  28. }
  29. pub unsafe fn c_str_n(s: *const c_char, n: usize) -> &'static [u8] {
  30. use core::slice;
  31. let mut size = 0;
  32. for _ in 0..n {
  33. if *s.offset(size) == 0 {
  34. break;
  35. }
  36. size += 1;
  37. }
  38. slice::from_raw_parts(s as *const u8, size as usize)
  39. }
  40. pub unsafe fn cstr_from_bytes_with_nul_unchecked(bytes: &[u8]) -> *const c_char {
  41. &*(bytes as *const [u8] as *const c_char)
  42. }
  43. pub struct FileWriter(pub c_int);
  44. impl FileWriter {
  45. pub fn write(&mut self, buf: &[u8]) {
  46. write(self.0, buf);
  47. }
  48. }
  49. impl fmt::Write for FileWriter {
  50. fn write_str(&mut self, s: &str) -> fmt::Result {
  51. self.write(s.as_bytes());
  52. Ok(())
  53. }
  54. }
  55. pub struct StringWriter(pub *mut u8, pub usize);
  56. impl StringWriter {
  57. pub unsafe fn write(&mut self, buf: &[u8]) {
  58. for &b in buf.iter() {
  59. if self.1 == 0 {
  60. break;
  61. } else if self.1 == 1 {
  62. *self.0 = b'\0';
  63. } else {
  64. *self.0 = b;
  65. }
  66. self.0 = self.0.offset(1);
  67. self.1 -= 1;
  68. if self.1 > 0 {
  69. *self.0 = b'\0';
  70. }
  71. }
  72. }
  73. }
  74. impl fmt::Write for StringWriter {
  75. fn write_str(&mut self, s: &str) -> fmt::Result {
  76. unsafe { self.write(s.as_bytes()) };
  77. Ok(())
  78. }
  79. }
  80. pub struct UnsafeStringWriter(pub *mut u8);
  81. impl UnsafeStringWriter {
  82. pub unsafe fn write(&mut self, buf: &[u8]) {
  83. for &b in buf.iter() {
  84. *self.0 = b;
  85. self.0 = self.0.offset(1);
  86. *self.0 = b'\0';
  87. }
  88. }
  89. }
  90. impl fmt::Write for UnsafeStringWriter {
  91. fn write_str(&mut self, s: &str) -> fmt::Result {
  92. unsafe { self.write(s.as_bytes()) };
  93. Ok(())
  94. }
  95. }