| 1 | /*
|
|---|
| 2 | * Copyright (c) 2008 Tim Post
|
|---|
| 3 | * All rights reserved.
|
|---|
| 4 | *
|
|---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
|---|
| 6 | * modification, are permitted provided that the following conditions
|
|---|
| 7 | * are met:
|
|---|
| 8 | *
|
|---|
| 9 | * - Redistributions of source code must retain the above copyright
|
|---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
|---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
|---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
|---|
| 13 | * documentation and/or other materials provided with the distribution.
|
|---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
|---|
| 15 | * derived from this software without specific prior written permission.
|
|---|
| 16 | *
|
|---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|---|
| 27 | */
|
|---|
| 28 |
|
|---|
| 29 | #include <async.h>
|
|---|
| 30 | #include <errno.h>
|
|---|
| 31 | #include <stdio.h>
|
|---|
| 32 | #include <stdlib.h>
|
|---|
| 33 | #include <str.h>
|
|---|
| 34 | #include "config.h"
|
|---|
| 35 | #include "util.h"
|
|---|
| 36 | #include "errors.h"
|
|---|
| 37 | #include "entry.h"
|
|---|
| 38 | #include "sleep.h"
|
|---|
| 39 | #include "cmds.h"
|
|---|
| 40 |
|
|---|
| 41 | static const char *cmdname = "sleep";
|
|---|
| 42 |
|
|---|
| 43 | /* Dispays help for sleep in various levels */
|
|---|
| 44 | void help_cmd_sleep(unsigned int level)
|
|---|
| 45 | {
|
|---|
| 46 | if (level == HELP_SHORT) {
|
|---|
| 47 | printf("`%s' pauses for a given time interval\n", cmdname);
|
|---|
| 48 | } else {
|
|---|
| 49 | help_cmd_sleep(HELP_SHORT);
|
|---|
| 50 | printf(
|
|---|
| 51 | "Usage: %s <duration>\n"
|
|---|
| 52 | "The duration is a decimal number of seconds.\n",
|
|---|
| 53 | cmdname);
|
|---|
| 54 | }
|
|---|
| 55 |
|
|---|
| 56 | return;
|
|---|
| 57 | }
|
|---|
| 58 |
|
|---|
| 59 | /** Convert string containing decimal seconds to useconds_t.
|
|---|
| 60 | *
|
|---|
| 61 | * @param nptr Pointer to string.
|
|---|
| 62 | * @param result Result of the conversion.
|
|---|
| 63 | * @return EOK if conversion was successful.
|
|---|
| 64 | */
|
|---|
| 65 | static errno_t decimal_to_useconds(const char *nptr, useconds_t *result)
|
|---|
| 66 | {
|
|---|
| 67 | errno_t ret;
|
|---|
| 68 | uint64_t whole_seconds;
|
|---|
| 69 | uint64_t frac_seconds;
|
|---|
| 70 | const char *endptr;
|
|---|
| 71 |
|
|---|
| 72 | /* Check for whole seconds */
|
|---|
| 73 | if (*nptr == '.') {
|
|---|
| 74 | whole_seconds = 0;
|
|---|
| 75 | endptr = (char *)nptr;
|
|---|
| 76 | } else {
|
|---|
| 77 | ret = str_uint64_t(nptr, &endptr, 10, false, &whole_seconds);
|
|---|
| 78 | if (ret != EOK)
|
|---|
| 79 | return ret;
|
|---|
| 80 | }
|
|---|
| 81 |
|
|---|
| 82 | /* Check for fractional seconds */
|
|---|
| 83 | if (*endptr == '\0') {
|
|---|
| 84 | frac_seconds = 0;
|
|---|
| 85 | } else if (*endptr == '.' && endptr[1] == '\0') {
|
|---|
| 86 | frac_seconds = 0;
|
|---|
| 87 | } else if (*endptr == '.') {
|
|---|
| 88 | nptr = endptr + 1;
|
|---|
| 89 | ret = str_uint64_t(nptr, &endptr, 10, true, &frac_seconds);
|
|---|
| 90 | if (ret != EOK)
|
|---|
| 91 | return ret;
|
|---|
| 92 |
|
|---|
| 93 | int ndigits = endptr - nptr;
|
|---|
| 94 | for (; ndigits < 6; ndigits++)
|
|---|
| 95 | frac_seconds *= 10;
|
|---|
| 96 | for (; ndigits > 6; ndigits--)
|
|---|
| 97 | frac_seconds /= 10;
|
|---|
| 98 | } else {
|
|---|
| 99 | return EINVAL;
|
|---|
| 100 | }
|
|---|
| 101 |
|
|---|
| 102 | /* Check for overflow */
|
|---|
| 103 | useconds_t total = whole_seconds * 1000000 + frac_seconds;
|
|---|
| 104 | if (total / 1000000 != whole_seconds)
|
|---|
| 105 | return EOVERFLOW;
|
|---|
| 106 |
|
|---|
| 107 | *result = total;
|
|---|
| 108 |
|
|---|
| 109 | return EOK;
|
|---|
| 110 | }
|
|---|
| 111 |
|
|---|
| 112 | /* Main entry point for sleep, accepts an array of arguments */
|
|---|
| 113 | int cmd_sleep(char **argv)
|
|---|
| 114 | {
|
|---|
| 115 | errno_t ret;
|
|---|
| 116 | unsigned int argc;
|
|---|
| 117 | useconds_t duration = 0;
|
|---|
| 118 |
|
|---|
| 119 | /* Count the arguments */
|
|---|
| 120 | argc = cli_count_args(argv);
|
|---|
| 121 |
|
|---|
| 122 | if (argc != 2) {
|
|---|
| 123 | printf("%s - incorrect number of arguments. Try `help %s'\n",
|
|---|
| 124 | cmdname, cmdname);
|
|---|
| 125 | return CMD_FAILURE;
|
|---|
| 126 | }
|
|---|
| 127 |
|
|---|
| 128 | ret = decimal_to_useconds(argv[1], &duration);
|
|---|
| 129 | if (ret != EOK) {
|
|---|
| 130 | printf("%s - invalid duration.\n", cmdname);
|
|---|
| 131 | return CMD_FAILURE;
|
|---|
| 132 | }
|
|---|
| 133 |
|
|---|
| 134 | async_usleep(duration);
|
|---|
| 135 |
|
|---|
| 136 | return CMD_SUCCESS;
|
|---|
| 137 | }
|
|---|
| 138 |
|
|---|