Changeset e4fbccd in mainline


Ignore:
Timestamp:
2012-04-01T20:38:11Z (12 years ago)
Author:
Sean Bartell <wingedtachikoma@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
081d60f
Parents:
3d367ee2
Message:

Make `sleep' work with fractional seconds.

Location:
uspace/app/bdsh/cmds/modules/sleep
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • uspace/app/bdsh/cmds/modules/sleep/sleep.c

    r3d367ee2 re4fbccd  
    4949                printf(
    5050                "Usage:  %s <duration>\n"
    51                 "The duration is an integer number of seconds.\n",
     51                "The duration is a decimal number of seconds.\n",
    5252                cmdname);
    5353        }
    5454
    5555        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 str_useconds_t(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;
    56109}
    57110
     
    61114        int ret;
    62115        unsigned int argc;
    63         uint32_t duration;
     116        useconds_t duration;
    64117
    65118        /* Count the arguments */
     
    68121        if (argc != 2) {
    69122                printf("%s - incorrect number of arguments. Try `help %s'\n",
    70                         cmdname, cmdname);
     123                    cmdname, cmdname);
    71124                return CMD_FAILURE;
    72125        }
    73126
    74         ret = str_uint32_t(argv[1], NULL, 10, true, &duration);
     127        ret = str_useconds_t(argv[1], &duration);
    75128        if (ret != EOK) {
    76129                printf("%s - invalid duration.\n", cmdname);
     
    78131        }
    79132
    80         (void) usleep((useconds_t)duration * 1000000);
     133        (void) usleep(duration);
    81134
    82135        return CMD_SUCCESS;
  • uspace/app/bdsh/cmds/modules/sleep/sleep.h

    r3d367ee2 re4fbccd  
    44/* Prototypes for the sleep command, excluding entry points */
    55
     6static int str_useconds_t(const char *nptr, useconds_t *result);
    67
    78#endif /* SLEEP_H */
Note: See TracChangeset for help on using the changeset viewer.