char * mfptoa( u_long fpi, u_long fpf, int ndec ) { int isneg; if (M_ISNEG(fpi, fpf)) { isneg = 1; M_NEG(fpi, fpf); } else isneg = 0; return dolfptoa(fpi, fpf, isneg, ndec, 0); }
static struct tm * get_struct_tm( const vint64 *stamp, int local) { struct tm *tm = NULL; int32 folds = 0; time_t ts; #ifdef HAVE_INT64 int64 tl; ts = tl = stamp->q_s; /* * If there is chance of truncation, try to fix it. Let the * compiler find out if this can happen at all. */ while (ts != tl) { /* truncation? */ if (tl < 0) { if (--folds < MINFOLD) return NULL; tl += SOLAR_CYCLE_SECS; } else { if (++folds > MAXFOLD) return NULL; tl -= SOLAR_CYCLE_SECS; } ts = tl; /* next try... */ } #else /* * since we do not have 64-bit scalars, it's not likely we have * 64-bit time_t. Assume 32 bits and properly reduce the value. */ u_int32 hi, lo; hi = stamp->D_s.hi; lo = stamp->D_s.lo; while ((hi && ~hi) || ((hi ^ lo) & 0x80000000u)) { if (M_ISNEG(hi, lo)) { if (--folds < MINFOLD) return NULL; M_ADD(hi, lo, 0, SOLAR_CYCLE_SECS); } else { if (++folds > MAXFOLD) return NULL; M_SUB(hi, lo, 0, SOLAR_CYCLE_SECS); } } ts = (int32)lo; #endif /* * 'ts' should be a suitable value by now. Just go ahead, but * with care: * * There are some pathological implementations of 'gmtime()' * and 'localtime()' out there. No matter if we have 32-bit or * 64-bit 'time_t', try to fix this by solar cycle warping * again... * * At least the MSDN says that the (Microsoft) Windoze * versions of 'gmtime()' and 'localtime()' will bark on time * stamps < 0. */ while ((tm = (*(local ? localtime : gmtime))(&ts)) == NULL) if (ts < 0) { if (--folds < MINFOLD) return NULL; ts += SOLAR_CYCLE_SECS; } else if (ts >= (time_t)SOLAR_CYCLE_SECS) { if (++folds > MAXFOLD) return NULL; ts -= SOLAR_CYCLE_SECS; } else return NULL; /* That's truly pathological! */ /* 'tm' surely not NULL here! */ NTP_INSIST(tm != NULL); if (folds != 0) { tm->tm_year += folds * SOLAR_CYCLE_YEARS; if (tm->tm_year <= 0 || tm->tm_year >= 200) return NULL; /* left warp range... can't help here! */ } return tm; }