Use double-precision for representing time, as seconds since some
arbitrary epoch. If we're strictly portable then time(3) is all we
have, and we have to convert with difftime(3); otherwise we can have
gettimeofday(2) and convert by hand.
#ifndef PORTABLE
# include <unistd.h>
#ifndef PORTABLE
# include <unistd.h>
#endif
#include <mLib/darray.h>
#endif
#include <mLib/darray.h>
+static double doubletime(void)
+{
+#ifdef PORTABLE
+ static time_t start = (time_t)-1;
+ time_t now = time(0);
+
+ if (start == (time_t)-1) start = now;
+ return difftime(now, start);
+#else
+ struct timeval tv;
+
+ gettimeofday(&tv, 0);
+ return (tv.tv_sec + tv.tv_usec/1000000.0);
+#endif
+}
+
static int generate(grand *r, mp *outsz,
int (*func)(const void *buf, size_t sz, void *p),
void *p)
static int generate(grand *r, mp *outsz,
int (*func)(const void *buf, size_t sz, void *p),
void *p)
unsigned percent = 0;
mp *kb = MP_ZERO, *t = MP_NEW;
dstr d = DSTR_INIT;
unsigned percent = 0;
mp *kb = MP_ZERO, *t = MP_NEW;
dstr d = DSTR_INIT;
static char baton[] = "-\\|/";
char *bp;
int rc;
static char baton[] = "-\\|/";
char *bp;
int rc;
/* --- Spit out random data --- */
/* --- Spit out random data --- */
bp = baton;
if (flags & f_progress) {
char *errbuf = xmalloc(BUFSIZ);
bp = baton;
if (flags & f_progress) {
char *errbuf = xmalloc(BUFSIZ);
/* --- Update the display --- */
if (flags & f_progress) {
/* --- Update the display --- */
if (flags & f_progress) {
if (percent > 100)
up = 1;
if (!outsz) {
if (percent > 100)
up = 1;
if (!outsz) {
- if (difftime(now, last) > 1.0) {
+ if (now - last > 1.0) {
mp_div(&t, 0, t, outsz);
assert(!MP_NEGP(t) && MP_CMP(t, <, MP_UINT_MAX));
pc = mp_touint(t);
mp_div(&t, 0, t, outsz);
assert(!MP_NEGP(t) && MP_CMP(t, <, MP_UINT_MAX));
pc = mp_touint(t);
- if (pc > percent || percent > 100 || difftime(now, last) > 1.0) {
+ if (pc > percent || percent > 100 || now - last > 1.0) {
if (percent > 100)
percent = 0;
percent &= ~1;
if (percent > 100)
percent = 0;
percent &= ~1;