1- #[ cfg( feature = "browser-transport" ) ]
21extern crate alloc;
32
43use alloc:: alloc:: { alloc as sys_alloc, dealloc as sys_dealloc, realloc as sys_realloc, Layout } ;
54use core:: ffi:: { c_char, c_int, c_void} ;
65use core:: mem:: { align_of, size_of} ;
76use core:: ptr:: null_mut;
8- use std:: ffi:: CStr ;
97
10- #[ cfg( feature = "browser-transport" ) ]
118#[ global_allocator]
129static ALLOC : wee_alloc:: WeeAlloc = wee_alloc:: WeeAlloc :: INIT ;
1310
@@ -134,23 +131,17 @@ pub extern "C" fn calloc(nmemb: usize, size: usize) -> *mut c_void {
134131
135132#[ no_mangle]
136133pub extern "C" fn fprintf ( _stream : * mut c_void , _fmt : * const c_char , _arg : * const c_void ) -> c_int {
137- // Pretend 0 chars written.
138- 0
134+ unreachable ! ( "Running in wasm mode. Should not be calling `fprintf`" ) ;
139135}
140136
141137#[ no_mangle]
142138pub extern "C" fn fclose ( _stream : * mut c_void ) -> c_int {
143- 0
139+ unreachable ! ( "Running in wasm mode. Should not be calling `fclose`" ) ;
144140}
145141
146142#[ no_mangle]
147143pub extern "C" fn snprintf ( buf : * mut c_char , n : usize , _fmt : * const c_char , _arg : usize ) -> c_int {
148- unsafe {
149- if !buf. is_null ( ) && n > 0 {
150- * buf = 0 ;
151- }
152- }
153- 0
144+ unreachable ! ( "Running in wasm mode. Should not be calling `sprintf`" ) ;
154145}
155146
156147// Dummy va_list type for signature compatibility
@@ -161,74 +152,26 @@ pub struct va_list__dummy {
161152
162153#[ no_mangle]
163154pub extern "C" fn vsnprintf ( buf : * mut c_char , n : usize , _fmt : * const c_char , _ap : * mut va_list__dummy ) -> c_int {
164- unsafe {
165- if !buf. is_null ( ) && n > 0 {
166- * buf = 0 ;
167- }
168- }
169- 0
155+ unreachable ! ( "Running in wasm mode. Should not be calling `vsprintf`" ) ;
170156}
171157
172158#[ no_mangle]
173159pub extern "C" fn abort ( ) -> ! {
174- #[ cfg( feature = "browser-transport" ) ]
175160 wasm_bindgen:: throw_str ( "abort" ) ;
176161}
177162
178163#[ inline]
179164unsafe fn cstr_prefix_len ( mut p : * const u8 , limit : usize ) -> usize {
180- // Count up to first NUL or `limit` bytes.
181- let mut i = 0usize ;
182- while i < limit {
183- let b = * p;
184- if b == 0 {
185- break ;
186- }
187- i += 1 ;
188- p = p. add ( 1 ) ;
189- }
190- i
165+ unreachable ! ( "Running in wasm mode. Should not be calling `vsprintf`" ) ;
191166}
192167
193168#[ no_mangle]
194169pub extern "C" fn strncmp ( s1 : * const c_char , s2 : * const c_char , n : usize ) -> c_int {
195- if n == 0 {
196- return 0 ;
197- }
198- if s1. is_null ( ) || s2. is_null ( ) {
199- return 0 ;
200- } // benign stub behavior
201-
202- // SAFETY: caller promises valid C strings; we only touch up to `n` bytes or NUL.
203- let a = s1 as * const u8 ;
204- let b = s2 as * const u8 ;
205- let len = unsafe {
206- let l1 = cstr_prefix_len ( a, n) ;
207- let l2 = cstr_prefix_len ( b, n) ;
208- core:: cmp:: min ( core:: cmp:: min ( l1, l2) , n)
209- } ;
210-
211- // Compare common prefix (unsigned char semantics)
212- for i in 0 ..len {
213- let av = unsafe { * a. add ( i) } as u8 ;
214- let bv = unsafe { * b. add ( i) } as u8 ;
215- if av != bv {
216- return ( av as c_int ) - ( bv as c_int ) ;
217- }
218- }
219-
220- // If we stopped early (before n), one string may have ended (NUL) earlier.
221- if len < n {
222- let av = unsafe { * a. add ( len) } as u8 ; // 0 if NUL, else next byte
223- let bv = unsafe { * b. add ( len) } as u8 ;
224- return ( av as c_int ) - ( bv as c_int ) ;
225- }
226- 0
170+ unreachable ! ( "Running in wasm mode. Should not be calling `vsprintf`" ) ;
227171}
228172
229173#[ no_mangle]
230174pub extern "C" fn clock ( ) -> c_int {
231- #[ cfg( feature = "browser-transport" ) ]
232175 {
233176 // js_sys::Date::now() -> f64 milliseconds since epoch
234177
@@ -240,41 +183,22 @@ pub extern "C" fn clock() -> c_int {
240183
241184#[ inline]
242185fn write_host ( _s : & str , _stream : * mut c_void ) {
243- // no-op
186+ unreachable ! ( "Running in wasm mode. Should not be calling `vsprintf`" ) ;
244187}
245188
246189/// int fputc(int c, FILE *stream)
247190#[ no_mangle]
248191pub extern "C" fn fputc ( c : c_int , stream : * mut c_void ) -> c_int {
249- let ch = ( c as u8 ) as char ;
250- let mut buf = [ 0u8 ; 4 ] ;
251- let s = ch. encode_utf8 ( & mut buf) ;
252- write_host ( s, stream) ;
253- // C fputc returns the written character cast to unsigned char as int
254- ( c as u8 ) as c_int
192+ unreachable ! ( "Running in wasm mode. Should not be calling `vsprintf`" ) ;
255193}
256194
257195/// int fputs(const char *s, FILE *stream)
258196#[ no_mangle]
259197pub extern "C" fn fputs ( s : * const c_char , stream : * mut c_void ) -> c_int {
260- if s. is_null ( ) {
261- return -1 ;
262- }
263- // SAFETY: caller promises valid NUL-terminated string
264- let bytes = unsafe { CStr :: from_ptr ( s) } . to_bytes ( ) ;
265- let text = core:: str:: from_utf8 ( bytes) . unwrap_or ( "<invalid utf-8>" ) ;
266- write_host ( text, stream) ;
267- // C fputs returns a nonnegative value on success (commonly a non-portable value, so we return len)
268- bytes. len ( ) as c_int
198+ unreachable ! ( "Running in wasm mode. Should not be calling `vsprintf`" ) ;
269199}
270200
271201#[ no_mangle]
272202pub extern "C" fn fdopen ( fd : c_int , _mode : * const c_char ) -> * mut c_void {
273- // Provide distinct, non-null sentinel handles for stdin/stdout/stderr (0/1/2).
274- // We never dereference these; other stubs ignore `stream` and treat any non-null as valid.
275- if ( 0 ..=2 ) . contains ( & fd) {
276- ( fd as usize + 1 ) as * mut c_void
277- } else {
278- null_mut ( )
279- }
203+ unreachable ! ( "Running in wasm mode. Should not be calling `vsprintf`" ) ;
280204}
0 commit comments