version 1.72, 2020/01/12 20:51:08 |
version 1.73, 2021/01/30 08:44:42 |
|
|
#include <errno.h> |
#include <errno.h> |
#include <fcntl.h> |
#include <fcntl.h> |
#include <limits.h> |
#include <limits.h> |
|
#include <math.h> |
#include <netdb.h> |
#include <netdb.h> |
#include <signal.h> |
#include <signal.h> |
#include <stdio.h> |
#include <stdio.h> |
|
#include <stdint.h> |
#include <stdlib.h> |
#include <stdlib.h> |
#include <string.h> |
#include <string.h> |
#include <stdarg.h> |
#include <stdarg.h> |
|
|
|
|
int ucount(void); |
int ucount(void); |
void usage(void); |
void usage(void); |
|
double strtodnum(const char *, double, double, const char **); |
|
|
/* command prompt */ |
/* command prompt */ |
|
|
|
|
cmd_delay(const char *buf) |
cmd_delay(const char *buf) |
{ |
{ |
double del; |
double del; |
del = atof(buf); |
const char *errstr; |
|
|
if (del > 0) { |
if (buf[0] == '\0') |
|
return; |
|
del = strtodnum(buf, 0, UINT32_MAX / 1000000, &errstr); |
|
if (errstr != NULL) |
|
error("s: \"%s\": delay value is %s", buf, errstr); |
|
else { |
udelay = (useconds_t)(del * 1000000); |
udelay = (useconds_t)(del * 1000000); |
gotsig_alarm = 1; |
gotsig_alarm = 1; |
naptime = del; |
naptime = del; |
|
|
hz = cinf.hz; |
hz = cinf.hz; |
} |
} |
|
|
|
#define INVALID 1 |
|
#define TOOSMALL 2 |
|
#define TOOLARGE 3 |
|
|
|
double |
|
strtodnum(const char *nptr, double minval, double maxval, const char **errstrp) |
|
{ |
|
double d = 0; |
|
int error = 0; |
|
char *ep; |
|
struct errval { |
|
const char *errstr; |
|
int err; |
|
} ev[4] = { |
|
{ NULL, 0 }, |
|
{ "invalid", EINVAL }, |
|
{ "too small", ERANGE }, |
|
{ "too large", ERANGE }, |
|
}; |
|
|
|
ev[0].err = errno; |
|
errno = 0; |
|
if (minval > maxval) { |
|
error = INVALID; |
|
} else { |
|
d = strtod(nptr, &ep); |
|
if (nptr == ep || *ep != '\0') |
|
error = INVALID; |
|
else if ((d == -HUGE_VAL && errno == ERANGE) || d < minval) |
|
error = TOOSMALL; |
|
else if ((d == HUGE_VAL && errno == ERANGE) || d > maxval) |
|
error = TOOLARGE; |
|
} |
|
if (errstrp != NULL) |
|
*errstrp = ev[error].errstr; |
|
errno = ev[error].err; |
|
if (error) |
|
d = 0; |
|
|
|
return (d); |
|
} |
|
|
int |
int |
main(int argc, char *argv[]) |
main(int argc, char *argv[]) |
{ |
{ |
|
|
const char *errstr; |
const char *errstr; |
extern char *optarg; |
extern char *optarg; |
extern int optind; |
extern int optind; |
double delay = 5; |
double delay = 5, del; |
|
|
char *viewstr = NULL; |
char *viewstr = NULL; |
|
|
|
|
nflag = 1; |
nflag = 1; |
break; |
break; |
case 's': |
case 's': |
delay = atof(optarg); |
delay = strtodnum(optarg, 0, UINT32_MAX / 1000000, |
if (delay <= 0) |
&errstr); |
delay = 5; |
if (errstr != NULL) |
|
errx(1, "-s \"%s\": delay value is %s", optarg, |
|
errstr); |
break; |
break; |
case 'w': |
case 'w': |
rawwidth = strtonum(optarg, 1, MAX_LINE_BUF-1, &errstr); |
rawwidth = strtonum(optarg, 1, MAX_LINE_BUF-1, &errstr); |
|
|
argv += optind; |
argv += optind; |
|
|
if (argc == 1) { |
if (argc == 1) { |
double del = atof(argv[0]); |
del = strtodnum(argv[0], 0, UINT32_MAX / 1000000, &errstr); |
if (del == 0) |
if (errstr != NULL) |
viewstr = argv[0]; |
viewstr = argv[0]; |
else |
else |
delay = del; |
delay = del; |
} else if (argc == 2) { |
} else if (argc == 2) { |
viewstr = argv[0]; |
viewstr = argv[0]; |
delay = atof(argv[1]); |
delay = strtodnum(argv[1], 0, UINT32_MAX / 1000000, &errstr); |
if (delay <= 0) |
if (errstr != NULL) |
delay = 5; |
errx(1, "\"%s\": delay value is %s", argv[1], errstr); |
} |
} |
|
|
udelay = (useconds_t)(delay * 1000000.0); |
udelay = (useconds_t)(delay * 1000000.0); |