add util files

This commit is contained in:
Gerolf Ziegenhain 2016-10-04 18:34:04 +02:00
parent 1d3b674ecc
commit 8622552f86
3 changed files with 32 additions and 23 deletions

View File

@ -15,14 +15,6 @@
#include <pthread.h>
#include <signal.h>
#include <arpa/inet.h>
#include <time.h>
#include <sys/time.h>
#include <stdio.h>
#ifdef __MACH__
#include <mach/clock.h>
#include <mach/mach.h>
#endif
//#define DEBUG 1
@ -30,6 +22,7 @@
#include "cwprotocol.h"
#include "beep.h"
#include "util.h"
int serial_status = 0, fd_serial, numbytes;
@ -53,21 +46,6 @@ char last_sender[16];
int translate = 1;
int audio_status = 1;
/* portable time, as listed in https://gist.github.com/jbenet/1087739 */
void current_utc_time(struct timespec *ts) {
#ifdef __MACH__ // OS X does not have clock_gettime, use clock_get_time
clock_serv_t cclock;
mach_timespec_t mts;
host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &cclock);
clock_get_time(cclock, &mts);
mach_port_deallocate(mach_task_self(), cclock);
ts->tv_sec = mts.tv_sec;
ts->tv_nsec = mts.tv_nsec;
#else
clock_gettime(CLOCK_REALTIME, ts);
#endif
}
/* a better clock() in milliseconds */
long fastclock(void)
{

19
src/util.c Normal file
View File

@ -0,0 +1,19 @@
#include "util.h"
/* portable time, as listed in https://gist.github.com/jbenet/1087739 */
void current_utc_time(struct timespec *ts) {
#ifdef __MACH__ // OS X does not have clock_gettime, use clock_get_time
clock_serv_t cclock;
mach_timespec_t mts;
host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &cclock);
clock_get_time(cclock, &mts);
mach_port_deallocate(mach_task_self(), cclock);
ts->tv_sec = mts.tv_sec;
ts->tv_nsec = mts.tv_nsec;
#else
clock_gettime(CLOCK_REALTIME, ts);
#endif
}

12
src/util.h Normal file
View File

@ -0,0 +1,12 @@
#include <time.h>
#include <sys/time.h>
#ifdef __MACH__
#include <mach/clock.h>
#include <mach/mach.h>
#endif
void current_utc_time(struct timespec *ts);