49 lines
726 B
C
49 lines
726 B
C
#include "includes.h"
|
|
#include "common.h"
|
|
#include <time.h>
|
|
|
|
void *os_zalloc(size_t size)
|
|
{
|
|
return calloc(1, size);
|
|
}
|
|
|
|
size_t os_strlcpy(char *dest, const char *src, size_t siz)
|
|
{
|
|
const char *s = src;
|
|
size_t left = siz;
|
|
|
|
if (left) {
|
|
while (--left != 0) {
|
|
if ((*dest++ = *s++) == '\0')
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (left == 0) {
|
|
if (siz != 0)
|
|
*dest = '\0';
|
|
while (*s++)
|
|
;
|
|
}
|
|
|
|
return s - src - 1;
|
|
}
|
|
|
|
int os_get_reltime(struct os_reltime *t)
|
|
{
|
|
struct timespec ts;
|
|
|
|
if (clock_gettime(CLOCK_MONOTONIC, &ts) < 0)
|
|
return -1;
|
|
|
|
t->sec = ts.tv_sec;
|
|
t->usec = ts.tv_nsec / 1000;
|
|
return 0;
|
|
}
|
|
|
|
void os_sleep(os_time_t sec, os_time_t usec)
|
|
{
|
|
const struct timespec req = { sec, usec * 1000 };
|
|
nanosleep(&req, NULL);
|
|
}
|