40
42
/* Dispays help for sleep in various levels */
41
43
void help_cmd_sleep(unsigned int level)
43
printf("This is the %s help for '%s'.\n",
44
level ? EXT_HELP : SHORT_HELP, cmdname);
45
if (level == HELP_SHORT) {
46
printf("`%s' pauses for a given time interval\n", cmdname);
48
help_cmd_sleep(HELP_SHORT);
50
"Usage: %s <duration>\n"
51
"The duration is a decimal number of seconds.\n",
58
/** Convert string containing decimal seconds to useconds_t.
60
* @param nptr Pointer to string.
61
* @param result Result of the conversion.
62
* @return EOK if conversion was successful.
64
static int decimal_to_useconds(const char *nptr, useconds_t *result)
67
uint64_t whole_seconds;
68
uint64_t frac_seconds;
71
/* Check for whole seconds */
74
endptr = (char *)nptr;
76
ret = str_uint64_t(nptr, &endptr, 10, false, &whole_seconds);
81
/* Check for fractional seconds */
82
if (*endptr == '\0') {
84
} else if (*endptr == '.' && endptr[1] == '\0') {
86
} else if (*endptr == '.') {
88
ret = str_uint64_t(nptr, &endptr, 10, true, &frac_seconds);
92
int ndigits = endptr - nptr;
93
for (; ndigits < 6; ndigits++)
95
for (; ndigits > 6; ndigits--)
101
/* Check for overflow */
102
useconds_t total = whole_seconds * 1000000 + frac_seconds;
103
if (total / 1000000 != whole_seconds)
48
111
/* Main entry point for sleep, accepts an array of arguments */
49
112
int cmd_sleep(char **argv)
51
115
unsigned int argc;
54
118
/* Count the arguments */
55
for (argc = 0; argv[argc] != NULL; argc ++);
57
printf("%s %s\n", TEST_ANNOUNCE, cmdname);
58
printf("%d arguments passed to %s", argc - 1, cmdname);
66
for (i = 1; i < argc; i++)
67
printf("[%d] -> %s\n", i, argv[i]);
119
argc = cli_count_args(argv);
122
printf("%s - incorrect number of arguments. Try `help %s'\n",
127
ret = decimal_to_useconds(argv[1], &duration);
129
printf("%s - invalid duration.\n", cmdname);
133
(void) usleep(duration);
69
135
return CMD_SUCCESS;