[36ab7c7] | 1 | /*
|
---|
| 2 | * Copyright (c) 2008 Tim Post
|
---|
[39463ff] | 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
[36ab7c7] | 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
[39463ff] | 8 | *
|
---|
[36ab7c7] | 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.
|
---|
[39463ff] | 16 | *
|
---|
[36ab7c7] | 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.
|
---|
[39463ff] | 27 | */
|
---|
| 28 |
|
---|
[3d367ee2] | 29 | #include <errno.h>
|
---|
[fec7ba0] | 30 | #include <fibril.h>
|
---|
[39463ff] | 31 | #include <stdio.h>
|
---|
| 32 | #include <stdlib.h>
|
---|
[1d6dd2a] | 33 | #include <str.h>
|
---|
[39463ff] | 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 |
|
---|
[a000878c] | 41 | static const char *cmdname = "sleep";
|
---|
[39463ff] | 42 |
|
---|
| 43 | /* Dispays help for sleep in various levels */
|
---|
| 44 | void help_cmd_sleep(unsigned int level)
|
---|
| 45 | {
|
---|
[3d367ee2] | 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(
|
---|
[081d60f] | 51 | "Usage: %s <duration>\n"
|
---|
| 52 | "The duration is a decimal number of seconds.\n",
|
---|
| 53 | cmdname);
|
---|
[3d367ee2] | 54 | }
|
---|
| 55 |
|
---|
[39463ff] | 56 | return;
|
---|
| 57 | }
|
---|
| 58 |
|
---|
[bd41ac52] | 59 | /** Convert string containing decimal seconds to usec_t.
|
---|
[e4fbccd] | 60 | *
|
---|
| 61 | * @param nptr Pointer to string.
|
---|
| 62 | * @param result Result of the conversion.
|
---|
| 63 | * @return EOK if conversion was successful.
|
---|
| 64 | */
|
---|
[bd41ac52] | 65 | static errno_t decimal_to_useconds(const char *nptr, usec_t *result)
|
---|
[e4fbccd] | 66 | {
|
---|
[b7fd2a0] | 67 | errno_t ret;
|
---|
[3f7fe9e] | 68 | int64_t whole_seconds;
|
---|
| 69 | int64_t frac_seconds;
|
---|
[b49d872] | 70 | const char *endptr;
|
---|
[e4fbccd] | 71 |
|
---|
| 72 | /* Check for whole seconds */
|
---|
| 73 | if (*nptr == '.') {
|
---|
| 74 | whole_seconds = 0;
|
---|
| 75 | endptr = (char *)nptr;
|
---|
| 76 | } else {
|
---|
[bd41ac52] | 77 | ret = str_int64_t(nptr, &endptr, 10, false, &whole_seconds);
|
---|
[e4fbccd] | 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;
|
---|
[bd41ac52] | 89 | ret = str_int64_t(nptr, &endptr, 10, true, &frac_seconds);
|
---|
[e4fbccd] | 90 | if (ret != EOK)
|
---|
| 91 | return ret;
|
---|
| 92 |
|
---|
| 93 | int ndigits = endptr - nptr;
|
---|
[1b20da0] | 94 | for (; ndigits < 6; ndigits++)
|
---|
[e4fbccd] | 95 | frac_seconds *= 10;
|
---|
| 96 | for (; ndigits > 6; ndigits--)
|
---|
| 97 | frac_seconds /= 10;
|
---|
| 98 | } else {
|
---|
| 99 | return EINVAL;
|
---|
| 100 | }
|
---|
| 101 |
|
---|
| 102 | /* Check for overflow */
|
---|
[bd41ac52] | 103 | usec_t total = SEC2USEC(whole_seconds) + frac_seconds;
|
---|
| 104 | if (USEC2SEC(total) != whole_seconds)
|
---|
[e4fbccd] | 105 | return EOVERFLOW;
|
---|
| 106 |
|
---|
| 107 | *result = total;
|
---|
| 108 |
|
---|
| 109 | return EOK;
|
---|
| 110 | }
|
---|
| 111 |
|
---|
[39463ff] | 112 | /* Main entry point for sleep, accepts an array of arguments */
|
---|
| 113 | int cmd_sleep(char **argv)
|
---|
| 114 | {
|
---|
[b7fd2a0] | 115 | errno_t ret;
|
---|
[39463ff] | 116 | unsigned int argc;
|
---|
[bd41ac52] | 117 | usec_t duration = 0;
|
---|
[39463ff] | 118 |
|
---|
| 119 | /* Count the arguments */
|
---|
[3d367ee2] | 120 | argc = cli_count_args(argv);
|
---|
[39463ff] | 121 |
|
---|
[3d367ee2] | 122 | if (argc != 2) {
|
---|
| 123 | printf("%s - incorrect number of arguments. Try `help %s'\n",
|
---|
[e4fbccd] | 124 | cmdname, cmdname);
|
---|
[3d367ee2] | 125 | return CMD_FAILURE;
|
---|
| 126 | }
|
---|
[39463ff] | 127 |
|
---|
[081d60f] | 128 | ret = decimal_to_useconds(argv[1], &duration);
|
---|
[3d367ee2] | 129 | if (ret != EOK) {
|
---|
| 130 | printf("%s - invalid duration.\n", cmdname);
|
---|
| 131 | return CMD_FAILURE;
|
---|
[39463ff] | 132 | }
|
---|
| 133 |
|
---|
[5f97ef44] | 134 | fibril_usleep(duration);
|
---|
[39463ff] | 135 |
|
---|
| 136 | return CMD_SUCCESS;
|
---|
| 137 | }
|
---|