diff --git a/src/stdlib/src/lib.rs b/src/stdlib/src/lib.rs index 09f1d0a6125e8e3ed869b85bec6c6c3453129cd5..e1364530384d9e8d79bbce665bbde762236b32d2 100644 --- a/src/stdlib/src/lib.rs +++ b/src/stdlib/src/lib.rs @@ -353,14 +353,16 @@ pub unsafe extern "C" fn mblen(s: *const c_char, n: size_t) -> c_int { result as i32 } -// #[no_mangle] -pub extern "C" fn mbstowcs(pwcs: *mut wchar_t, s: *const c_char, n: size_t) -> size_t { - unimplemented!(); +#[no_mangle] +pub unsafe extern "C" fn mbstowcs(pwcs: *mut wchar_t, mut s: *const c_char, n: size_t) -> size_t { + let mut state: mbstate_t = mbstate_t {}; + mbsrtowcs(pwcs, &mut s, n, &mut state) } -// #[no_mangle] -pub extern "C" fn mbtowc(pwc: *mut wchar_t, s: *const c_char, n: size_t) -> c_int { - unimplemented!(); +#[no_mangle] +pub unsafe extern "C" fn mbtowc(pwc: *mut wchar_t, s: *const c_char, n: size_t) -> c_int { + let mut state: mbstate_t = mbstate_t {}; + mbrtowc(pwc, s, n, &mut state) as c_int } #[no_mangle] diff --git a/src/time/src/lib.rs b/src/time/src/lib.rs index 43014c275eb35db72bb12fa09fd03cc85b93e9b6..096bc599b589c77d0b6f44d845ac797c13d5d440 100644 --- a/src/time/src/lib.rs +++ b/src/time/src/lib.rs @@ -198,70 +198,132 @@ pub unsafe extern "C" fn localtime(clock: *const time_t) -> *mut tm { localtime_r(clock, &mut TM) } +const MONTH_DAYS: [[c_int; 12]; 2] = [ + // Non-leap years: + [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31], + // Leap years: + [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31], +]; + +fn leap_year(year: c_int) -> bool { + year % 4 == 0 && (year % 100 != 0 || year % 400 == 0) +} + #[no_mangle] -pub unsafe extern "C" fn localtime_r(clock: *const time_t, r: *mut tm) -> *mut tm { - fn leap_year(year: c_int) -> bool { - year % 4 == 0 && (year % 100 != 0 || year % 400 == 0) +pub unsafe extern "C" fn localtime_r(clock: *const time_t, t: *mut tm) -> *mut tm { + let clock = *clock; + + let mut day = (clock / (60 * 60 * 24)) as c_int; + if clock < 0 && clock % (60 * 60 * 24) != 0 { + // -1 because for negative values round upwards + // -0.3 == 0, but we want -1 + day -= 1; } - let mut clock = *clock; - if clock < 0 { - unimplemented!("localtime_r with a negative time is to be implemented"); + (*t).tm_sec = (clock % 60) as c_int; + (*t).tm_min = ((clock / 60) % 60) as c_int; + (*t).tm_hour = ((clock / (60 * 60)) % 24) as c_int; + + while (*t).tm_sec < 0 { + (*t).tm_sec += 60; + (*t).tm_min -= 1; + } + while (*t).tm_min < 0 { + (*t).tm_min += 60; + (*t).tm_hour -= 1; + } + while (*t).tm_hour < 0 { + (*t).tm_hour += 24; } - let mut days = (clock / (60 * 60 * 24)) as c_int; + // Jan 1th was a thursday, 4th of a zero-indexed week. + (*t).tm_wday = (day + 4) % 7; + if (*t).tm_wday < 0 { + (*t).tm_wday += 7; + } - // Epoch, Jan 1 1970, was on a thursday. - // Jan 5th was a monday. - (*r).tm_wday = (days + 4) % 7; + let mut year = 1970; + if day < 0 { + while day < 0 { + let days_in_year = if leap_year(year) { 366 } else { 365 }; - (*r).tm_year = 1970; + day += days_in_year; + year -= 1; + } + (*t).tm_year = year - 1900; + (*t).tm_yday = day + 1; + } else { + loop { + let days_in_year = if leap_year(year) { 366 } else { 365 }; + + if day < days_in_year { + break; + } + + day -= days_in_year; + year += 1; + } + (*t).tm_year = year - 1900; + (*t).tm_yday = day; + } + let leap = if leap_year(year) { 1 } else { 0 }; + (*t).tm_mon = 0; loop { - let days_in_year = if leap_year((*r).tm_year) { 366 } else { 365 }; + let days_in_month = MONTH_DAYS[leap][(*t).tm_mon as usize]; - if days < days_in_year { + if day < days_in_month { break; } - days -= days_in_year; - (*r).tm_year += 1; + day -= days_in_month; + (*t).tm_mon += 1; } + (*t).tm_mday = 1 + day as c_int; + (*t).tm_isdst = 0; - (*r).tm_yday = days; + t +} - (*r).tm_sec = (clock % 60) as c_int; - (*r).tm_min = ((clock % (60 * 60)) / 60) as c_int; - (*r).tm_hour = (clock / (60 * 60)) as c_int; +#[no_mangle] +pub unsafe extern "C" fn mktime(t: *mut tm) -> time_t { + let mut year = (*t).tm_year + 1900; + let mut month = (*t).tm_mon; + let mut day = (*t).tm_mday as i64 - 1; - const MONTH_DAYS: [[c_int; 12]; 2] = [ - // Non-leap years: - [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31], - // Leap years: - [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31], - ]; + let leap = if leap_year(year) { 1 } else { 0 }; - let leap = if leap_year((*r).tm_year) { 1 } else { 0 }; + if year < 1970 { + day = MONTH_DAYS[if leap_year(year) { 1 } else { 0 }][(*t).tm_mon as usize] as i64 - day; - loop { - let days_in_month = MONTH_DAYS[leap][(*r).tm_mon as usize]; + while year < 1969 { + year += 1; + day += if leap_year(year) { 366 } else { 365 }; + } - if days < (*r).tm_mon { - break; + while month < 11 { + month += 1; + day += MONTH_DAYS[leap][month as usize] as i64; } - days -= days_in_month; - (*r).tm_mon += 1; - } - (*r).tm_mday = days as c_int; - (*r).tm_isdst = 0; + -(day * (60 * 60 * 24) + - (((*t).tm_hour as i64) * (60 * 60) + ((*t).tm_min as i64) * 60 + (*t).tm_sec as i64)) + } else { + while year > 1970 { + year -= 1; + day += if leap_year(year) { 366 } else { 365 }; + } - r -} + while month > 0 { + month -= 1; + day += MONTH_DAYS[leap][month as usize] as i64; + } -// #[no_mangle] -pub extern "C" fn mktime(timeptr: *mut tm) -> time_t { - unimplemented!(); + (day * (60 * 60 * 24) + + ((*t).tm_hour as i64) * (60 * 60) + + ((*t).tm_min as i64) * 60 + + (*t).tm_sec as i64) + } } #[no_mangle] diff --git a/src/wchar/src/lib.rs b/src/wchar/src/lib.rs index 91cae381c6a364961987f16257c1c67fb63b3004..02c679cd278e2d2677a0bb774940c3df5bae5495 100644 --- a/src/wchar/src/lib.rs +++ b/src/wchar/src/lib.rs @@ -10,7 +10,6 @@ extern crate time; extern crate va_list as vl; use core::ptr; -use core::usize; use platform::types::*; use stdio::*; use time::*; @@ -95,7 +94,7 @@ pub unsafe extern "C" fn mbsinit(ps: *const mbstate_t) -> c_int { } #[no_mangle] -pub unsafe extern "C" fn mbrlen(s: *const c_char, n: usize, ps: *mut mbstate_t) -> usize { +pub unsafe extern "C" fn mbrlen(s: *const c_char, n: size_t, ps: *mut mbstate_t) -> size_t { static mut INTERNAL: mbstate_t = mbstate_t; mbrtowc(ptr::null_mut(), s, n, &mut INTERNAL) } @@ -105,9 +104,9 @@ pub unsafe extern "C" fn mbrlen(s: *const c_char, n: usize, ps: *mut mbstate_t) pub unsafe extern "C" fn mbrtowc( pwc: *mut wchar_t, s: *const c_char, - n: usize, + n: size_t, ps: *mut mbstate_t, -) -> usize { +) -> size_t { static mut INTERNAL: mbstate_t = mbstate_t; if ps.is_null() { @@ -127,10 +126,10 @@ pub unsafe extern "C" fn mbrtowc( pub unsafe extern "C" fn mbsnrtowcs( dst_ptr: *mut wchar_t, src_ptr: *mut *const c_char, - src_len: usize, - dst_len: usize, + src_len: size_t, + dst_len: size_t, ps: *mut mbstate_t, -) -> usize { +) -> size_t { static mut INTERNAL: mbstate_t = mbstate_t; if ps.is_null() { @@ -191,10 +190,10 @@ pub unsafe extern "C" fn mbsnrtowcs( pub extern "C" fn mbsrtowcs( dst: *mut wchar_t, src: *mut *const c_char, - len: usize, + len: size_t, ps: *mut mbstate_t, -) -> usize { - unsafe { mbsnrtowcs(dst, src, usize::max_value(), len, ps) } +) -> size_t { + unsafe { mbsnrtowcs(dst, src, size_t::max_value(), len, ps) } } #[no_mangle] @@ -210,7 +209,7 @@ pub unsafe extern "C" fn putwchar(wc: wchar_t) -> wint_t { // #[no_mangle] pub extern "C" fn swprintf( s: *mut wchar_t, - n: usize, + n: size_t, format: *const wchar_t, mut ap: va_list, ) -> c_int { @@ -250,7 +249,7 @@ pub extern "C" fn vwprintf(format: *const wchar_t, arg: va_list) -> c_int { // #[no_mangle] pub extern "C" fn vswprintf( s: *mut wchar_t, - n: usize, + n: size_t, format: *const wchar_t, arg: va_list, ) -> c_int { @@ -259,7 +258,7 @@ pub extern "C" fn vswprintf( //widechar to multibyte #[no_mangle] -pub extern "C" fn wcrtomb(s: *mut c_char, wc: wchar_t, ps: *mut mbstate_t) -> usize { +pub extern "C" fn wcrtomb(s: *mut c_char, wc: wchar_t, ps: *mut mbstate_t) -> size_t { let mut buffer: [c_char; MB_CUR_MAX as usize] = [0; MB_CUR_MAX as usize]; let mut wc_cpy = wc; let mut s_cpy = s; @@ -298,17 +297,17 @@ pub extern "C" fn wcscpy(ws1: *mut wchar_t, ws2: *const wchar_t) -> *mut wchar_t } // #[no_mangle] -pub extern "C" fn wcscspn(ws1: *const wchar_t, ws2: *const wchar_t) -> usize { +pub extern "C" fn wcscspn(ws1: *const wchar_t, ws2: *const wchar_t) -> size_t { unimplemented!(); } // #[no_mangle] pub extern "C" fn wcsftime( wcs: *mut wchar_t, - maxsize: usize, + maxsize: size_t, format: *const wchar_t, timptr: *mut tm, -) -> usize { +) -> size_t { unimplemented!(); } @@ -318,17 +317,17 @@ pub extern "C" fn wcslen(ws: *const wchar_t) -> c_ulong { } // #[no_mangle] -pub extern "C" fn wcsncat(ws1: *mut wchar_t, ws2: *const wchar_t, n: usize) -> *mut wchar_t { +pub extern "C" fn wcsncat(ws1: *mut wchar_t, ws2: *const wchar_t, n: size_t) -> *mut wchar_t { unimplemented!(); } // #[no_mangle] -pub extern "C" fn wcsncmp(ws1: *const wchar_t, ws2: *const wchar_t, n: usize) -> c_int { +pub extern "C" fn wcsncmp(ws1: *const wchar_t, ws2: *const wchar_t, n: size_t) -> c_int { unimplemented!(); } // #[no_mangle] -pub extern "C" fn wcsncpy(ws1: *mut wchar_t, ws2: *const wchar_t, n: usize) -> *mut wchar_t { +pub extern "C" fn wcsncpy(ws1: *mut wchar_t, ws2: *const wchar_t, n: size_t) -> *mut wchar_t { unimplemented!(); } @@ -346,14 +345,14 @@ pub extern "C" fn wcsrchr(ws1: *const wchar_t, ws2: wchar_t) -> *mut wchar_t { pub extern "C" fn wcsrtombs( dst: *mut c_char, src: *mut *const wchar_t, - len: usize, + len: size_t, ps: *mut mbstate_t, -) -> usize { +) -> size_t { unimplemented!(); } // #[no_mangle] -pub extern "C" fn wcsspn(ws1: *const wchar_t, ws2: *const wchar_t) -> usize { +pub extern "C" fn wcsspn(ws1: *const wchar_t, ws2: *const wchar_t) -> size_t { unimplemented!(); } @@ -392,12 +391,12 @@ pub extern "C" fn wcswcs(ws1: *const wchar_t, ws2: *const wchar_t) -> *mut wchar } // #[no_mangle] -pub extern "C" fn wcswidth(pwcs: *const wchar_t, n: usize) -> c_int { +pub extern "C" fn wcswidth(pwcs: *const wchar_t, n: size_t) -> c_int { unimplemented!(); } // #[no_mangle] -pub extern "C" fn wcsxfrm(ws1: *mut wchar_t, ws2: *const wchar_t, n: usize) -> usize { +pub extern "C" fn wcsxfrm(ws1: *mut wchar_t, ws2: *const wchar_t, n: size_t) -> size_t { unimplemented!(); } @@ -412,27 +411,27 @@ pub extern "C" fn wcwidth(wc: wchar_t) -> c_int { } // #[no_mangle] -pub extern "C" fn wmemchr(ws: *const wchar_t, wc: wchar_t, n: usize) -> *mut c_int { +pub extern "C" fn wmemchr(ws: *const wchar_t, wc: wchar_t, n: size_t) -> *mut c_int { unimplemented!(); } // #[no_mangle] -pub extern "C" fn wmemcmp(ws1: *const wchar_t, ws2: *const wchar_t, n: usize) -> c_int { +pub extern "C" fn wmemcmp(ws1: *const wchar_t, ws2: *const wchar_t, n: size_t) -> c_int { unimplemented!(); } // #[no_mangle] -pub extern "C" fn wmemcpy(ws1: *mut wchar_t, ws2: *const wchar_t, n: usize) -> *mut wchar_t { +pub extern "C" fn wmemcpy(ws1: *mut wchar_t, ws2: *const wchar_t, n: size_t) -> *mut wchar_t { unimplemented!(); } // #[no_mangle] -pub extern "C" fn wmemmove(ws1: *mut wchar_t, ws2: *const wchar_t, n: usize) -> *mut wchar_t { +pub extern "C" fn wmemmove(ws1: *mut wchar_t, ws2: *const wchar_t, n: size_t) -> *mut wchar_t { unimplemented!(); } // #[no_mangle] -pub extern "C" fn wmemset(ws1: *mut wchar_t, ws2: wchar_t, n: usize) -> *mut wchar_t { +pub extern "C" fn wmemset(ws1: *mut wchar_t, ws2: wchar_t, n: size_t) -> *mut wchar_t { unimplemented!(); } diff --git a/tests/.gitignore b/tests/.gitignore index 3fb46de6578136d9bb07eb548487cb6165261e25..6b46660daf97cd2687e304112d1536052e348342 100644 --- a/tests/.gitignore +++ b/tests/.gitignore @@ -17,8 +17,10 @@ fsync ftruncate getc_unget locale +localtime math mem +mktime pipe printf rename diff --git a/tests/Makefile b/tests/Makefile index 3584543252a530e7827002a324e225c867c873fe..0f348118b8593f5d372c9f6af3ecb617e05f8a34 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -16,8 +16,10 @@ EXPECT_BINS=\ ftruncate \ getc_unget \ locale \ + localtime \ math \ mem \ + mktime \ pipe \ printf \ rename \ diff --git a/tests/expected/localtime.stderr b/tests/expected/localtime.stderr new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/tests/expected/localtime.stdout b/tests/expected/localtime.stdout new file mode 100644 index 0000000000000000000000000000000000000000..12a3072c69a462b1c99a1be6b8c9854a45d56571 --- /dev/null +++ b/tests/expected/localtime.stdout @@ -0,0 +1,6 @@ +Year 69, Day of year: 333, Month 10, Day of month: 29, Day of week: 6, 0:0:0 +Year 69, Day of year: 365, Month 11, Day of month: 31, Day of week: 3, 0:0:0 +Year 69, Day of year: 365, Month 11, Day of month: 31, Day of week: 3, 23:51:40 +Year 70, Day of year: 0, Month 0, Day of month: 1, Day of week: 4, 0:0:0 +Year 118, Day of year: 193, Month 6, Day of month: 13, Day of week: 5, 4:9:10 +Fri Jul 13 06:03:43 2018 diff --git a/tests/expected/mktime.stderr b/tests/expected/mktime.stderr new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/tests/expected/mktime.stdout b/tests/expected/mktime.stdout new file mode 100644 index 0000000000000000000000000000000000000000..c9b309e63c948ab69eb02512ba52ffdd8fdf58cf --- /dev/null +++ b/tests/expected/mktime.stdout @@ -0,0 +1,6 @@ +31536000 +-2851200 = -2851200 +-86400 = -86400 +-500 = -500 +0 = 0 +1531454950 = 1531454950 diff --git a/tests/localtime.c b/tests/localtime.c new file mode 100644 index 0000000000000000000000000000000000000000..edce6cf64e84c7d90ff9fa0a9d8003047ff07f80 --- /dev/null +++ b/tests/localtime.c @@ -0,0 +1,18 @@ +#include <stdio.h> +#include <time.h> + +int main() { + int day = 60 * 60 * 24; + time_t inputs[] = { -(day * 33), -day, -500, 0, 1531454950 }; + for (int i = 0; i < 5; i += 1) { + struct tm* t = localtime(&inputs[i]); + + printf( + "Year %d, Day of year: %d, Month %d, Day of month: %d, Day of week: %d, %d:%d:%d\n", + t->tm_year, t->tm_yday, t->tm_mon, t->tm_mday, t->tm_wday, t->tm_hour, t->tm_min, t->tm_sec + ); + } + + time_t input = 1531461823; + fputs(ctime(&input), stdout); // Omit newline +} diff --git a/tests/mktime.c b/tests/mktime.c new file mode 100644 index 0000000000000000000000000000000000000000..9b9d94069378aa4db623451b160b4cb60ee15f2c --- /dev/null +++ b/tests/mktime.c @@ -0,0 +1,48 @@ +#include <assert.h> +#include <stdio.h> +#include <stdlib.h> +#include <time.h> + +int check(time_t input) { + struct tm* t = localtime(&input); + + printf("%ld = %ld\n", input, mktime(t)); + + if (input != mktime(t)) { + printf( + "Year %d, Day of year: %d, Month %d, Day of month: %d, Day of week: %d, %d:%d:%d\n", + t->tm_year, t->tm_yday, t->tm_mon, t->tm_mday, t->tm_wday, t->tm_hour, t->tm_min, t->tm_sec + ); + puts("Failed!"); + return -1; + } + return 0; +} +int main() { + struct tm t = {}; + + t.tm_year = 71; + t.tm_mday = 1; + + printf("%ld\n", mktime(&t)); + + int day = 60 * 60 * 24; + time_t inputs[] = { -(day * 33), -day, -500, 0, 1531454950 }; + for (int i = 0; i < 5; i += 1) { + if (check(inputs[i])) { + return -1; + } + } + + srand(time(NULL)); + + for (int i = 0; i < 10; i += 1) { + time_t input = (time_t) rand(); + struct tm* time = localtime(&input); + time_t output = mktime(time); + if (input != output) { + // asctime has newline + printf("Comparison %ld == %ld failed. Time: %s", input, output, asctime(time)); + } + } +}