diff --git a/System/FFI/LibC.HC b/System/FFI/LibC.HC index f26949f..d089d7a 100644 --- a/System/FFI/LibC.HC +++ b/System/FFI/LibC.HC @@ -51,30 +51,6 @@ _LONGJMP:: _extern _SETJMP U64 _setjmp(U64 jmp_buf); _extern _LONGJMP U64 longjmp(U64 jmp_buf, U64 ret); -class libc_tm { - I32 tm_sec; /* seconds, range 0 to 59 */ - I32 tm_min; /* minutes, range 0 to 59 */ - I32 tm_hour; /* hours, range 0 to 23 */ - I32 tm_mday; /* day of the month, range 1 to 31 */ - I32 tm_mon; /* month, range 0 to 11 */ - I32 tm_year; /* The number of years since 1900 */ - I32 tm_wday; /* day of the week, range 0 to 6 */ - I32 tm_yday; /* day in the year, range 0 to 365 */ - I32 tm_isdst; /* daylight saving time */ -}; - -#define NIST_TIME_OFFSET -9488 - -I64 CDate2Unix(CDate dt) -{ // TempleOS datetime to Unix timestamp. - return ToI64((dt - Str2Date("1/1/1970")) / CDATE_FREQ + NIST_TIME_OFFSET); -} - -public CDate Unix2CDate(I64 timestamp) -{//Unix timestamp to TempleOS datetime. - return (timestamp-NIST_TIME_OFFSET)*CDATE_FREQ+Str2Date("1/1/1970"); -} - U0 free() { PUSH_SYSV_REGS @@ -106,56 +82,6 @@ U0 localtime() POP_SYSV_REGS } -U64 @gmtime(U64* timep) -{ - CDateStruct ds; - Date2Struct(&ds, Unix2CDate(*timep)); - libc_tm* tm = CAlloc(sizeof(libc_tm)); - - tm->tm_sec = ds.sec; - tm->tm_min = ds.min; - tm->tm_hour = ds.hour; - tm->tm_mday = ds.day_of_mon; - tm->tm_mon = ds.mon; - tm->tm_year = ds.year; - tm->tm_wday = ds.day_of_week; - return tm; -} - -U0 gmtime() -{ - PUSH_SYSV_REGS - GET_SYSV_ARGS - @gmtime(p0); - POP_SYSV_REGS -} - -U64 @mktime(libc_tm* tm) -{ - CDateStruct ds; - MemSet(&ds, 0, sizeof(CDateStruct)); - if (tm) { - ds.sec = tm->tm_sec; - ds.min = tm->tm_min; - ds.hour = tm->tm_hour; - ds.day_of_mon = tm->tm_mday; - ds.mon = tm->tm_mon; - ds.year = tm->tm_year; - ds.day_of_week = tm->tm_wday; - } else { - Date2Struct(&ds, Now); - } - return CDate2Unix(Struct2Date(&ds)); -} - -U0 mktime() -{ - PUSH_SYSV_REGS - GET_SYSV_ARGS - @mktime(p0); - POP_SYSV_REGS -} - I64 @strncmp(U8* s1, U8* s2, I32 n) { U64 u1, u2; @@ -315,6 +241,7 @@ U0 strcmp() { PUSH_SYSV_REGS GET_SYSV_ARGS + //"strcmp: '%s', '%s'\n", p0, p1; StrCmp(p0, p1); POP_SYSV_REGS } @@ -364,6 +291,14 @@ U0 realloc() POP_SYSV_REGS } +#define MY_TIME_OFFSET 9488 + +public +I64 CDate2Unix(CDate dt) +{ // TempleOS datetime to Unix timestamp. + return ToI64((dt - Str2Date("1/1/1970")) / CDATE_FREQ) - MY_TIME_OFFSET; +} + I64 @time(I64* ptr) { no_warn ptr; @@ -450,3 +385,11 @@ U0 strstr() POP_SYSV_REGS } + +U0 sprintf() +{ + PUSH_SYSV_REGS + GET_SYSV_ARGS + StrPrint(p0, p1, p2, p3, p4, p5); + POP_SYSV_REGS +} diff --git a/src/mujs/jsdate.c b/src/mujs/jsdate.c index fd5ab46..bf1d406 100644 --- a/src/mujs/jsdate.c +++ b/src/mujs/jsdate.c @@ -323,7 +323,7 @@ static char *fmtdate(char *buf, double t) int d = DateFromTime(t); if (!isfinite(t)) return "Invalid Date"; - npf_snprintf(buf, 1024, "%04d-%02d-%02d", y, m+1, d); + sprintf(buf, "%04d-%02d-%02d", y, m+1, d); return buf; } @@ -338,11 +338,11 @@ static char *fmttime(char *buf, double t, double tza) if (!isfinite(t)) return "Invalid Date"; if (tza == 0) - npf_snprintf(buf, 1024, "%02d:%02d:%02d.%03dZ", H, M, S, ms); + sprintf(buf, "%02d:%02d:%02d.%03dZ", H, M, S, ms); else if (tza < 0) - npf_snprintf(buf, 1024, "%02d:%02d:%02d.%03d-%02d:%02d", H, M, S, ms, tzh, tzm); + sprintf(buf, "%02d:%02d:%02d.%03d-%02d:%02d", H, M, S, ms, tzh, tzm); else - npf_snprintf(buf, 1024, "%02d:%02d:%02d.%03d+%02d:%02d", H, M, S, ms, tzh, tzm); + sprintf(buf, "%02d:%02d:%02d.%03d+%02d:%02d", H, M, S, ms, tzh, tzm); return buf; } @@ -353,7 +353,7 @@ static char *fmtdatetime(char *buf, double t, double tza) return "Invalid Date"; fmtdate(dbuf, t); fmttime(tbuf, t, tza); - npf_snprintf(buf, 1024, "%sT%s", dbuf, tbuf); + sprintf(buf, "%sT%s", dbuf, tbuf); return buf; } diff --git a/src/mujs/jsnumber.c b/src/mujs/jsnumber.c index 86259da..8e79d34 100644 --- a/src/mujs/jsnumber.c +++ b/src/mujs/jsnumber.c @@ -116,11 +116,11 @@ static void numtostr(js_State *J, const char *fmt, int w, double n) { /* buf needs to fit printf("%.20f", 1e20) */ char buf[50], *e; - npf_snprintf(buf, 1024, fmt, w, n); + sprintf(buf, fmt, w, n); e = strchr(buf, 'e'); if (e) { int exp = atoi(e+1); - npf_snprintf(e, 1024, "e%+d", exp); + sprintf(e, "e%+d", exp); } js_pushstring(J, buf); } diff --git a/src/mujs/jsrun.c b/src/mujs/jsrun.c index e06b212..b956208 100644 --- a/src/mujs/jsrun.c +++ b/src/mujs/jsrun.c @@ -919,11 +919,11 @@ const char *js_ref(js_State *J) s = v->u.boolean ? "_True" : "_False"; break; case JS_TOBJECT: - npf_snprintf(buf, 1024, "%p", (void*)v->u.object); + sprintf(buf, "%p", (void*)v->u.object); s = js_intern(J, buf); break; default: - npf_snprintf(buf, 1024, "%d", J->nextref++); + sprintf(buf, "%d", J->nextref++); s = js_intern(J, buf); break; }