FreeBSD/usr.sbin/watchdogd/watchdogd.c
/*
 * Convert a timeout in seconds to N where 2^N nanoseconds is close to
 * "seconds".
 *
 * The kernel expects the timeouts for watchdogs in "2^N nanosecond format".
 */
Convert a timeout in seconds to N where 2^N nanoseconds is close to
"seconds".

The kernel expects the timeouts for watchdogs in "2^N nanosecond format".
Convert a timeout in seconds to N where 2^N nanoseconds is close to " seconds " . The kernel expects the timeouts for watchdogs in " 2^N nanosecond format " .
static u_int
parse_timeout_to_pow2ns(char opt, const char *longopt, const char *myoptarg)
{
	double a;
	u_int rv;
	struct timespec ts;
	struct timeval tv;
	int ticks;
	char shortopt[] = "- ";
	if (!longopt)
		shortopt[1] = opt;
	a = fetchtimeout(opt, longopt, myoptarg, 1);
	if (a == 0)
		rv = WD_TO_NEVER;
	else
		rv = seconds_to_pow2ns(a);
	pow2ns_to_ts(rv, &ts);
	tstotv(&tv, &ts);
	ticks = tvtohz(&tv);
	if (debugging) {
		printf("Timeout for %s%s "
		    "is 2^%d nanoseconds "
		    "(in: %s sec -> out: %jd sec %ld ns -> %d ticks)\n",
		    longopt ? "-" : "", longopt ? longopt : shortopt,
		    rv,
		    myoptarg, (intmax_t)ts.tv_sec, ts.tv_nsec, ticks);
	}
	if (ticks <= 0) {
		errx(1, "Timeout for %s%s is too small, please choose a higher timeout.", longopt ? "-" : "", longopt ? longopt : shortopt);
	}
	return (rv);
}
static u_int parse_timeout_to_pow2ns ( char opt , const char * longopt , const char * myoptarg ) { double a ; u_int rv ; struct timespec ts ; struct timeval tv ; int ticks ; char shortopt [ ] = " " ; if ( ! longopt ) shortopt [ 1 ] = opt ; a = fetchtimeout ( opt , longopt , myoptarg , 1 ) ; if ( a == 0 ) rv = WD_TO_NEVER ; else rv = seconds_to_pow2ns ( a ) ; pow2ns_to_ts ( rv , & ts ) ; tstotv ( & tv , & ts ) ; ticks = tvtohz ( & tv ) ; if ( debugging ) { printf ( " " " " " \n " , longopt ? " " : " " , longopt ? longopt : shortopt , rv , myoptarg , ( intmax_t ) ts . tv_sec , ts . tv_nsec , ticks ) ; } if ( ticks <= 0 ) { errx ( 1 , " " , longopt ? " " : " " , longopt ? longopt : shortopt ) ; } return ( rv ) ; }
Convert a timeout in seconds to N where 2^N nanoseconds is close to
 "seconds".
Convert a timeout in seconds to N where 2^N nanoseconds is close to " seconds " .