source: mainline/uspace/app/bdsh/cmds/modules/sleep/sleep.c@ c4f7bf6

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since c4f7bf6 was c4f7bf6, checked in by Jakub Jermar <jakub@…>, 12 years ago

Silence compiler warnings when optimized for size.

  • Property mode set to 100644
File size: 3.7 KB
Line 
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 <errno.h>
30#include <stdio.h>
31#include <stdlib.h>
32#include <unistd.h>
33#include "config.h"
34#include "util.h"
35#include "errors.h"
36#include "entry.h"
37#include "sleep.h"
38#include "cmds.h"
39
40static const char *cmdname = "sleep";
41
42/* Dispays help for sleep in various levels */
43void help_cmd_sleep(unsigned int level)
44{
45 if (level == HELP_SHORT) {
46 printf("`%s' pauses for a given time interval\n", cmdname);
47 } else {
48 help_cmd_sleep(HELP_SHORT);
49 printf(
50 "Usage: %s <duration>\n"
51 "The duration is a decimal number of seconds.\n",
52 cmdname);
53 }
54
55 return;
56}
57
58/** Convert string containing decimal seconds to useconds_t.
59 *
60 * @param nptr Pointer to string.
61 * @param result Result of the conversion.
62 * @return EOK if conversion was successful.
63 */
64static int decimal_to_useconds(const char *nptr, useconds_t *result)
65{
66 int ret;
67 uint64_t whole_seconds;
68 uint64_t frac_seconds;
69 char *endptr;
70
71 /* Check for whole seconds */
72 if (*nptr == '.') {
73 whole_seconds = 0;
74 endptr = (char *)nptr;
75 } else {
76 ret = str_uint64_t(nptr, &endptr, 10, false, &whole_seconds);
77 if (ret != EOK)
78 return ret;
79 }
80
81 /* Check for fractional seconds */
82 if (*endptr == '\0') {
83 frac_seconds = 0;
84 } else if (*endptr == '.' && endptr[1] == '\0') {
85 frac_seconds = 0;
86 } else if (*endptr == '.') {
87 nptr = endptr + 1;
88 ret = str_uint64_t(nptr, &endptr, 10, true, &frac_seconds);
89 if (ret != EOK)
90 return ret;
91
92 int ndigits = endptr - nptr;
93 for (; ndigits < 6; ndigits++)
94 frac_seconds *= 10;
95 for (; ndigits > 6; ndigits--)
96 frac_seconds /= 10;
97 } else {
98 return EINVAL;
99 }
100
101 /* Check for overflow */
102 useconds_t total = whole_seconds * 1000000 + frac_seconds;
103 if (total / 1000000 != whole_seconds)
104 return EOVERFLOW;
105
106 *result = total;
107
108 return EOK;
109}
110
111/* Main entry point for sleep, accepts an array of arguments */
112int cmd_sleep(char **argv)
113{
114 int ret;
115 unsigned int argc;
116 useconds_t duration = 0;
117
118 /* Count the arguments */
119 argc = cli_count_args(argv);
120
121 if (argc != 2) {
122 printf("%s - incorrect number of arguments. Try `help %s'\n",
123 cmdname, cmdname);
124 return CMD_FAILURE;
125 }
126
127 ret = decimal_to_useconds(argv[1], &duration);
128 if (ret != EOK) {
129 printf("%s - invalid duration.\n", cmdname);
130 return CMD_FAILURE;
131 }
132
133 (void) usleep(duration);
134
135 return CMD_SUCCESS;
136}
137
Note: See TracBrowser for help on using the repository browser.