Merge branch 'ungetwc' into 'master'
Implementing ungetwc See merge request redox-os/relibc!390
This commit is contained in:
+18
-3
@@ -237,9 +237,24 @@ pub extern "C" fn swscanf(s: *const wchar_t, format: *const wchar_t, ap: va_list
|
||||
unimplemented!();
|
||||
}
|
||||
|
||||
// #[no_mangle]
|
||||
pub extern "C" fn ungetwc(wc: wint_t, stream: *mut FILE) -> wint_t {
|
||||
unimplemented!();
|
||||
/// Push wide character `wc` back onto `stream` so it'll be read next
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn ungetwc(wc: wint_t, stream: &mut FILE) -> wint_t {
|
||||
if wc == WEOF {
|
||||
return wc;
|
||||
}
|
||||
static mut INTERNAL: mbstate_t = mbstate_t;
|
||||
let mut bytes: [c_char; MB_CUR_MAX as usize] = [0; MB_CUR_MAX as usize];
|
||||
|
||||
let amount = wcrtomb(bytes.as_mut_ptr(), wc as wchar_t, &mut INTERNAL);
|
||||
if amount == usize::MAX {
|
||||
return WEOF;
|
||||
}
|
||||
for i in 0..amount {
|
||||
ungetc(bytes[i] as c_int, &mut *stream);
|
||||
}
|
||||
|
||||
wc
|
||||
}
|
||||
|
||||
// #[no_mangle]
|
||||
|
||||
@@ -43,6 +43,7 @@ EXPECT_NAMES=\
|
||||
stdio/printf_space_pad \
|
||||
stdio/ungetc_ftell \
|
||||
stdio/ungetc_multiple \
|
||||
stdio/ungetwc \
|
||||
stdio/fscanf_offby1 \
|
||||
stdio/fscanf \
|
||||
stdio/printf_neg_pad \
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
#include <wchar.h>
|
||||
#include <wctype.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <assert.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
FILE *stream;
|
||||
wint_t wc;
|
||||
wint_t wc2;
|
||||
|
||||
if (NULL == (stream = fopen("wchar/ungetwc.in", "r+")))
|
||||
return 1;
|
||||
|
||||
while (WEOF != (wc = fgetwc(stream)) && iswdigit(wc)) {}
|
||||
|
||||
if (WEOF != wc)
|
||||
ungetwc(wc, stream);
|
||||
|
||||
wc2 = fgetwc(stream);
|
||||
assert(WEOF != wc2);
|
||||
assert(wc == wc2);
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
123A
|
||||
Reference in New Issue
Block a user