posix-io.c 1.39 KB
Newer Older
1 2 3 4 5
/*
 * Alessandro Rubini for CERN, 2011 -- public domain
 */
#define _GNU_SOURCE /* for strnlen */
#include <stdio.h>
6
#include <stdlib.h>
7 8
#include <string.h>
#include <time.h>
9
#include <sys/timex.h>
10
#include <pptp/pptp.h>
11

12
const Integer32 PP_ADJ_FREQ_MAX = 512000;
13

14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
void pp_puts(const char *s)
{
	fputs(s, stdout);
}

int pp_strnlen(const char *s, int maxlen)
{
	return strnlen(s, maxlen);
}

void *pp_memcpy(void *d, const void *s, int count)
{
	return memcpy(d, s, count);
}

29
int pp_memcmp(const void *s1, const void *s2, int count)
30 31 32 33
{
	return memcmp(s1, s2, count);
}

34 35 36 37 38
void *pp_memset(void *s, int c, int count)
{
	return memset(s, c, count);
}

39
void pp_get_tstamp(TimeInternal *t)
40
{
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
	struct timespec tp;
	if (clock_gettime(CLOCK_REALTIME, &tp) < 0) {
		/* FIXME diag PERROR("clock_gettime() failed, exiting."); */
		exit(0);
	}
	t->seconds = tp.tv_sec;
	t->nanoseconds = tp.tv_nsec;
}

void pp_set_tstamp(TimeInternal *t)
{
	/* FIXME: what happens with timers? */
	struct timespec tp;
	tp.tv_sec = t->seconds;
	tp.tv_nsec = t->nanoseconds;
	if (clock_settime(CLOCK_REALTIME, &tp) < 0) {
		/* FIXME diag PERROR("clock_settime() failed, exiting."); */
		exit(0);
	}
60
}
61 62 63 64 65

int pp_adj_freq(Integer32 adj)
{
	struct timex t;

66 67 68 69
	if (adj > PP_ADJ_FREQ_MAX)
		adj = PP_ADJ_FREQ_MAX;
	else if (adj < -PP_ADJ_FREQ_MAX)
		adj = -PP_ADJ_FREQ_MAX;
70 71 72 73 74 75

	t.modes = MOD_FREQUENCY;
	t.freq = adj * ((1 << 16) / 1000);

	return !adjtimex(&t);
}