lib.rs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. //! string implementation for Redox, following http://pubs.opengroup.org/onlinepubs/7908799/xsh/string.h.html
  2. #![no_std]
  3. extern crate errno;
  4. extern crate platform;
  5. extern crate stdlib;
  6. use platform::types::*;
  7. use errno::*;
  8. use core::cmp;
  9. use core::usize;
  10. #[no_mangle]
  11. pub extern "C" fn memccpy(s1: *mut c_void, s2: *const c_void, c: c_int, n: usize) -> *mut c_void {
  12. unimplemented!();
  13. }
  14. #[no_mangle]
  15. pub extern "C" fn memchr(s: *const c_void, c: c_int, n: usize) -> *mut c_void {
  16. unimplemented!();
  17. }
  18. // #[no_mangle]
  19. // pub extern "C" fn memcmp(
  20. // s1: *const c_void,
  21. // s2: *const c_void,
  22. // n: usize,
  23. // ) -> c_int {
  24. // unimplemented!();
  25. // }
  26. // #[no_mangle]
  27. // pub extern "C" fn memcpy(
  28. // s1: *mut c_void,
  29. // s2: *const c_void,
  30. // n: usize,
  31. // ) -> *mut c_void {
  32. // unimplemented!();
  33. // }
  34. // #[no_mangle]
  35. // pub extern "C" fn memmove(
  36. // s1: *mut c_void,
  37. // s2: *const c_void,
  38. // n: usize,
  39. // ) -> *mut c_void {
  40. // unimplemented!();
  41. // }
  42. // #[no_mangle]
  43. // pub extern "C" fn memset(
  44. // s: *mut c_void,
  45. // c: c_int,
  46. // n: usize,
  47. // ) -> *mut c_void {
  48. // unimplemented!();
  49. // }
  50. #[no_mangle]
  51. pub unsafe extern "C" fn strcat(s1: *mut c_char, s2: *const c_char) -> *mut c_char {
  52. strncat(s1, s2, usize::MAX)
  53. }
  54. #[no_mangle]
  55. pub extern "C" fn strchr(s: *const c_char, c: c_int) -> *mut c_char {
  56. unimplemented!();
  57. }
  58. #[no_mangle]
  59. pub unsafe extern "C" fn strcmp(s1: *const c_char, s2: *const c_char) -> c_int {
  60. strncmp(s1, s2, usize::MAX)
  61. }
  62. #[no_mangle]
  63. pub extern "C" fn strcoll(s1: *const c_char, s2: *const c_char) -> c_int {
  64. unimplemented!();
  65. }
  66. #[no_mangle]
  67. pub unsafe extern "C" fn strcpy(s1: *mut c_char, s2: *const c_char) -> *mut c_char {
  68. strncpy(s1, s2, usize::MAX)
  69. }
  70. #[no_mangle]
  71. pub extern "C" fn strcspn(s1: *const c_char, s2: *const c_char) -> c_ulong {
  72. unimplemented!();
  73. }
  74. #[no_mangle]
  75. pub unsafe extern "C" fn strdup(s1: *const c_char) -> *mut c_char {
  76. strndup(s1, usize::MAX)
  77. }
  78. #[no_mangle]
  79. pub unsafe extern "C" fn strndup(s1: *const c_char, size: usize) -> *mut c_char {
  80. let len = strnlen(s1, size);
  81. // the "+ 1" is to account for the NUL byte
  82. let buffer = stdlib::malloc(len + 1) as *mut c_char;
  83. if buffer.is_null() {
  84. platform::errno = ENOMEM as c_int;
  85. } else {
  86. //memcpy(buffer, s1, len)
  87. for i in 0..len as isize {
  88. *buffer.offset(i) = *s1.offset(i);
  89. }
  90. *buffer.offset(len as isize) = 0;
  91. }
  92. buffer
  93. }
  94. #[no_mangle]
  95. pub unsafe extern "C" fn strerror(errnum: c_int) -> *mut c_char {
  96. use core::fmt::Write;
  97. static mut strerror_buf: [u8; 256] = [0; 256];
  98. let mut w = platform::StringWriter(strerror_buf.as_mut_ptr(), strerror_buf.len());
  99. if errnum >= 0 && errnum < STR_ERROR.len() as c_int {
  100. w.write_str(STR_ERROR[errnum as usize]);
  101. } else {
  102. w.write_fmt(format_args!("Unknown error {}", errnum));
  103. }
  104. strerror_buf.as_mut_ptr() as *mut c_char
  105. }
  106. #[no_mangle]
  107. pub unsafe extern "C" fn strlen(s: *const c_char) -> size_t {
  108. strnlen(s, usize::MAX)
  109. }
  110. #[no_mangle]
  111. pub unsafe extern "C" fn strnlen(s: *const c_char, size: usize) -> size_t {
  112. platform::c_str_n(s, size).len() as size_t
  113. }
  114. #[no_mangle]
  115. pub unsafe extern "C" fn strncat(s1: *mut c_char, s2: *const c_char, n: usize) -> *mut c_char {
  116. let mut idx = strlen(s1 as *const _) as isize;
  117. for i in 0..n as isize {
  118. if *s2.offset(i) == 0 {
  119. break;
  120. }
  121. *s1.offset(idx) = *s2.offset(i);
  122. idx += 1;
  123. }
  124. *s1.offset(idx) = 0;
  125. s1
  126. }
  127. #[no_mangle]
  128. pub unsafe extern "C" fn strncmp(s1: *const c_char, s2: *const c_char, n: usize) -> c_int {
  129. let s1 = platform::c_str_n(s1, n);
  130. let s2 = platform::c_str_n(s2, n);
  131. let min_len = n.min(s1.len()).min(s2.len());
  132. for i in 0..min_len {
  133. let val = s1[i] - s2[i];
  134. if val != 0 {
  135. return val as c_int;
  136. }
  137. }
  138. // we can't just check for the NUL byte in the loop as c_str_n() removes it
  139. if s1.len() > s2.len() {
  140. s1[min_len] as c_int
  141. } else if s1.len() < s2.len() {
  142. -(s2[min_len] as c_int)
  143. } else {
  144. 0
  145. }
  146. }
  147. #[no_mangle]
  148. pub unsafe extern "C" fn strncpy(s1: *mut c_char, s2: *const c_char, n: usize) -> *mut c_char {
  149. let s2_slice = platform::c_str_n(s2, n);
  150. let s2_len = s2_slice.len();
  151. //memcpy(s1 as *mut _, s2 as *const _, cmp::min(n, s2_len));
  152. let mut idx = 0;
  153. for _ in 0..cmp::min(n, s2_len) {
  154. *s1.offset(idx as isize) = s2_slice[idx] as c_char;
  155. idx += 1;
  156. }
  157. // if length of s2 < n, pad s1 with zeroes
  158. for _ in cmp::min(n, s2_len)..n {
  159. *s1.offset(idx as isize) = 0;
  160. idx += 1;
  161. }
  162. s1
  163. }
  164. #[no_mangle]
  165. pub extern "C" fn strpbrk(s1: *const c_char, s2: *const c_char) -> *mut c_char {
  166. unimplemented!();
  167. }
  168. #[no_mangle]
  169. pub extern "C" fn strrchr(s: *const c_char, c: c_int) -> *mut c_char {
  170. unimplemented!();
  171. }
  172. #[no_mangle]
  173. pub extern "C" fn strspn(s1: *const c_char, s2: *const c_char) -> c_ulong {
  174. unimplemented!();
  175. }
  176. #[no_mangle]
  177. pub extern "C" fn strstr(s1: *const c_char, s2: *const c_char) -> *mut c_char {
  178. unimplemented!();
  179. }
  180. #[no_mangle]
  181. pub extern "C" fn strtok(s1: *mut c_char, s2: *const c_char) -> *mut c_char {
  182. unimplemented!();
  183. }
  184. #[no_mangle]
  185. pub extern "C" fn strtok_r(
  186. s: *mut c_char,
  187. sep: *const c_char,
  188. lasts: *mut *mut c_char,
  189. ) -> *mut c_char {
  190. unimplemented!();
  191. }
  192. #[no_mangle]
  193. pub extern "C" fn strxfrm(s1: *mut c_char, s2: *const c_char, n: usize) -> c_ulong {
  194. unimplemented!();
  195. }
  196. /*
  197. #[no_mangle]
  198. pub extern "C" fn func(args) -> c_int {
  199. unimplemented!();
  200. }
  201. */