libc: Add gettimeofday()

This commit is contained in:
apio 2023-05-20 12:05:22 +02:00
parent dc169124cc
commit a7c6163e7c
Signed by: apio
GPG Key ID: B8A7D06E42258954
4 changed files with 48 additions and 1 deletions

View File

@ -1,4 +1,4 @@
/* bits/timespec.h: The timespec structure. */
/* bits/timespec.h: The timespec and timeval structures. */
#ifndef _BITS_TIMESPEC_H
#define _BITS_TIMESPEC_H
@ -11,4 +11,10 @@ struct timespec
long tv_nsec;
};
struct timeval
{
time_t tv_sec;
suseconds_t tv_usec;
};
#endif

21
libc/include/sys/time.h Normal file
View File

@ -0,0 +1,21 @@
/* sys/time.h: POSIX time types. */
#ifndef _SYS_TIME_H
#define _SYS_TIME_H
#include <bits/attrs.h>
#include <bits/timespec.h>
#ifdef __cplusplus
extern "C"
{
#endif
/* Get the current time of day. */
__deprecated int gettimeofday(struct timeval* tp, void* timezone);
#ifdef __cplusplus
}
#endif
#endif

View File

@ -19,6 +19,7 @@ typedef __u64_t ino_t;
typedef __u32_t uid_t;
typedef __u32_t gid_t;
typedef __u64_t nlink_t;
typedef __i64_t suseconds_t;
typedef off_t fpos_t;

View File

@ -1,6 +1,8 @@
#include <bits/errno-return.h>
#include <luna/Check.h>
#include <luna/Format.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/syscall.h>
#include <time.h>
#include <unistd.h>
@ -148,4 +150,21 @@ extern "C"
{
return asctime(localtime(tp));
}
int gettimeofday(struct timeval* tp, void* timezone)
{
if (timezone)
{
fputs("gettimeofday: timezone was not NULL, abort\n", stderr);
abort();
}
struct timespec ts;
if (clock_gettime(CLOCK_REALTIME, &ts) < 0) return -1;
tp->tv_sec = ts.tv_sec;
tp->tv_usec = ts.tv_nsec / 1000;
return 0;
}
}