
#include <starlet.h>
#include <unixlib.h>

struct timeval {
    long	tv_sec;
    long	tv_usec;
};

void gettimeofday(struct timeval *tv, struct timeval *NULL)
{
    /*
     * Emulate unix's gettimeofday call
     *
     * This is not good enough since we round up
     * to the nearest second.
     */
    char tbuff[32];
    short lt;
    int flag = 1;			/* Return time, no date */
    long quadTime[2];
    struct {
    	int l;
    	void *a;
    } dt;

    sys$gettim(quadTime);
    tv->tv_sec = decc$fix_time(quadTime);

    /*
     * This is my attempt at extracting the fractional parts
     * of a second from a VMS quadtime. lib$ediv does not work
     * on a Vax if the quotient overflows.
     *
     * I assume that the fractional part of a second will not
     * change due to the convertions from VMS time to Unix time
     * since they have different epochs.
     */
    dt.l = sizeof(tbuff);
    dt.a = tbuff;
    sys$asctim(&lt, &dt, quadTime, flag);
    tbuff[lt-1] = 0;
    sscanf(tbuff, "%*d:%*d:%*d.%d", &tv->tv_usec);
    tv->tv_usec *= 10000;
}
