Changeset e4fbccd in mainline
- Timestamp:
- 2012-04-01T20:38:11Z (13 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- 081d60f
- Parents:
- 3d367ee2
- Location:
- uspace/app/bdsh/cmds/modules/sleep
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/app/bdsh/cmds/modules/sleep/sleep.c
r3d367ee2 re4fbccd 49 49 printf( 50 50 "Usage: %s <duration>\n" 51 "The duration is a n integernumber of seconds.\n",51 "The duration is a decimal number of seconds.\n", 52 52 cmdname); 53 53 } 54 54 55 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 */ 64 static 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; 56 109 } 57 110 … … 61 114 int ret; 62 115 unsigned int argc; 63 u int32_t duration;116 useconds_t duration; 64 117 65 118 /* Count the arguments */ … … 68 121 if (argc != 2) { 69 122 printf("%s - incorrect number of arguments. Try `help %s'\n", 70 123 cmdname, cmdname); 71 124 return CMD_FAILURE; 72 125 } 73 126 74 ret = str_u int32_t(argv[1], NULL, 10, true, &duration);127 ret = str_useconds_t(argv[1], &duration); 75 128 if (ret != EOK) { 76 129 printf("%s - invalid duration.\n", cmdname); … … 78 131 } 79 132 80 (void) usleep( (useconds_t)duration * 1000000);133 (void) usleep(duration); 81 134 82 135 return CMD_SUCCESS; -
uspace/app/bdsh/cmds/modules/sleep/sleep.h
r3d367ee2 re4fbccd 4 4 /* Prototypes for the sleep command, excluding entry points */ 5 5 6 static int str_useconds_t(const char *nptr, useconds_t *result); 6 7 7 8 #endif /* SLEEP_H */
Note:
See TracChangeset
for help on using the changeset viewer.