~martin-decky/helenos/rcu

« back to all changes in this revision

Viewing changes to uspace/app/bdsh/cmds/modules/sleep/sleep.c

  • Committer: Vojtech Horky
  • Date: 2012-05-23 12:03:26 UTC
  • mfrom: (1443.1.19 misc)
  • mto: This revision was merged to the branch mainline in revision 1479.
  • Revision ID: vojtechhorky@users.sourceforge.net-20120523120326-jv50stjymxmh598s
Merge GSOC-originated patches 

Merge from lp:~vojtech-horky/helenos/misc.

The merge includes:
 * Switching to previous directory with `cd -' in Bdsh
 * Interactive mode for cp module in Bdsh
 * Implementation of sleep command
 * printf and echo for Bdsh
 * Rewrite of the mkdir module
 * Ctrl-arrow jumps over words in the editor
 * The scripts work with Python 3

Show diffs side-by-side

added added

removed removed

Lines of Context:
26
26
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
27
 */
28
28
 
 
29
#include <errno.h>
29
30
#include <stdio.h>
30
31
#include <stdlib.h>
 
32
#include <unistd.h>
31
33
#include "config.h"
32
34
#include "util.h"
33
35
#include "errors.h"
40
42
/* Dispays help for sleep in various levels */
41
43
void help_cmd_sleep(unsigned int level)
42
44
{
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);
 
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
 
45
55
        return;
46
56
}
47
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
 */
 
64
static 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
 
48
111
/* Main entry point for sleep, accepts an array of arguments */
49
112
int cmd_sleep(char **argv)
50
113
{
 
114
        int ret;
51
115
        unsigned int argc;
52
 
        unsigned int i;
 
116
        useconds_t duration;
53
117
 
54
118
        /* Count the arguments */
55
 
        for (argc = 0; argv[argc] != NULL; argc ++);
56
 
 
57
 
        printf("%s %s\n", TEST_ANNOUNCE, cmdname);
58
 
        printf("%d arguments passed to %s", argc - 1, cmdname);
59
 
 
60
 
        if (argc < 2) {
61
 
                printf("\n");
62
 
                return CMD_SUCCESS;
63
 
        }
64
 
 
65
 
        printf(":\n");
66
 
        for (i = 1; i < argc; i++)
67
 
                printf("[%d] -> %s\n", i, argv[i]);
 
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);
68
134
 
69
135
        return CMD_SUCCESS;
70
136
}