Compare commits
5 Commits
dd64e2060e
...
dafd2fecf7
Author | SHA1 | Date |
---|---|---|
Bananymous | dafd2fecf7 | |
Bananymous | 9c5cca784e | |
Bananymous | 1138165308 | |
Bananymous | d7eb321d58 | |
Bananymous | 15f8c7014f |
|
@ -22,7 +22,7 @@
|
|||
do { \
|
||||
BAN::Formatter::print(__debug_putchar, "\e[33m"); \
|
||||
BAN::Formatter::print(__debug_putchar, __VA_ARGS__); \
|
||||
BAN::Formatter::print(__debug_putchar, "\e[m"); \
|
||||
BAN::Formatter::print(__debug_putchar, "\e[m\r\n"); \
|
||||
fflush(stddbg); \
|
||||
} while(false)
|
||||
|
||||
|
@ -30,7 +30,7 @@
|
|||
do { \
|
||||
BAN::Formatter::print(__debug_putchar, "\e[31m"); \
|
||||
BAN::Formatter::print(__debug_putchar, __VA_ARGS__); \
|
||||
BAN::Formatter::print(__debug_putchar, "\e[m"); \
|
||||
BAN::Formatter::print(__debug_putchar, "\e[m\r\n"); \
|
||||
fflush(stddbg); \
|
||||
} while(false)
|
||||
|
||||
|
|
|
@ -6,9 +6,9 @@
|
|||
#include <sys/cdefs.h>
|
||||
|
||||
#ifdef NDEBUG
|
||||
#define assert(ignore)((void) 0)
|
||||
#define assert(ignore) ((void)0)
|
||||
#else
|
||||
#define assert(expr) do { if (!(expr)) __assert_fail(#expr, __FILE__, __LINE__, __func__); } while (0)
|
||||
#define assert(expr) ((expr) ? (void)0 : __assert_fail(#expr, __FILE__, __LINE__, __func__))
|
||||
#endif
|
||||
|
||||
__BEGIN_DECLS
|
||||
|
|
|
@ -107,6 +107,11 @@ struct linger
|
|||
#define AF_INET6 2
|
||||
#define AF_UNIX 3
|
||||
|
||||
#define PF_UNSPEC AF_UNSPEC
|
||||
#define PF_INET AF_INET
|
||||
#define PF_INET6 AF_INET6
|
||||
#define PF_UNIX AF_UNIX
|
||||
|
||||
#define SHUT_RD 0x01
|
||||
#define SHUT_WR 0x02
|
||||
#define SHUT_RDWR (SHUT_RD | SHUT_WR)
|
||||
|
|
|
@ -495,6 +495,32 @@ int putenv(char* string)
|
|||
return 0;
|
||||
}
|
||||
|
||||
void* bsearch(const void* key, const void* base, size_t nel, size_t width, int (*compar)(const void*, const void*))
|
||||
{
|
||||
if (nel == 0)
|
||||
return nullptr;
|
||||
|
||||
const uint8_t* base_u8 = reinterpret_cast<const uint8_t*>(base);
|
||||
|
||||
size_t l = 0;
|
||||
size_t r = nel - 1;
|
||||
while (l <= r)
|
||||
{
|
||||
const size_t mid = (l + r) / 2;
|
||||
|
||||
int res = compar(key, base_u8 + mid * width);
|
||||
if (res == 0)
|
||||
return const_cast<uint8_t*>(base_u8 + mid * width);
|
||||
|
||||
if (res < 0)
|
||||
r = mid - 1;
|
||||
else
|
||||
l = mid + 1;
|
||||
}
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
static void qsort_swap(void* lhs, void* rhs, size_t width)
|
||||
{
|
||||
uint8_t buffer[64];
|
||||
|
|
|
@ -21,3 +21,57 @@ time_t time(time_t* tloc)
|
|||
*tloc = tp.tv_sec;
|
||||
return tp.tv_sec;
|
||||
}
|
||||
|
||||
struct tm* gmtime(const time_t* timer)
|
||||
{
|
||||
static struct tm tm;
|
||||
|
||||
constexpr auto is_leap_year =
|
||||
[](time_t year) -> bool
|
||||
{
|
||||
if (year % 400 == 0)
|
||||
return true;
|
||||
if (year % 100 == 0)
|
||||
return false;
|
||||
if (year % 4 == 0)
|
||||
return true;
|
||||
return false;
|
||||
};
|
||||
|
||||
constexpr uint64_t month_days[] { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 };
|
||||
|
||||
time_t time = *timer;
|
||||
|
||||
tm.tm_sec = time % 60; time /= 60;
|
||||
tm.tm_min = time % 60; time /= 60;
|
||||
tm.tm_hour = time % 24; time /= 24;
|
||||
|
||||
time_t total_days = time;
|
||||
tm.tm_wday = (total_days + 4) % 7;
|
||||
tm.tm_year = 1970;
|
||||
while (total_days >= 365U + is_leap_year(tm.tm_year))
|
||||
{
|
||||
total_days -= 365U + is_leap_year(tm.tm_year);
|
||||
tm.tm_year++;
|
||||
}
|
||||
|
||||
bool is_leap_day = is_leap_year(tm.tm_year) && total_days == month_days[2];
|
||||
bool had_leap_day = is_leap_year(tm.tm_year) && total_days > month_days[2];
|
||||
|
||||
for (tm.tm_mon = 0; tm.tm_mon < 12; tm.tm_mon++)
|
||||
if (total_days < month_days[tm.tm_mon + 1] + (is_leap_day || had_leap_day))
|
||||
break;
|
||||
|
||||
tm.tm_mday = total_days - month_days[tm.tm_mon] + !had_leap_day;
|
||||
tm.tm_yday = total_days;
|
||||
tm.tm_year -= 1900;
|
||||
tm.tm_isdst = 0;
|
||||
|
||||
return &tm;
|
||||
}
|
||||
|
||||
struct tm* localtime(const time_t* timer)
|
||||
{
|
||||
// FIXME: support timezones
|
||||
return gmtime(timer);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue