int main (int argc, char **argv) { int i; double seconds = 0.0; bool ok = true; initialize_main (&argc, &argv); set_program_name (argv[0]); setlocale (LC_ALL, ""); bindtextdomain (PACKAGE, LOCALEDIR); textdomain (PACKAGE); atexit (close_stdout); parse_long_options (argc, argv, PROGRAM_NAME, PACKAGE_NAME, Version, usage, AUTHORS, (char const *) NULL); if (getopt_long (argc, argv, "", NULL, NULL) != -1) usage (EXIT_FAILURE); if (argc == 1) { error (0, 0, _("missing operand")); usage (EXIT_FAILURE); } for (i = optind; i < argc; i++) { double s; const char *p; if (! xstrtod (argv[i], &p, &s, c_strtod) /* Nonnegative interval. */ || ! (0 <= s) /* No extra chars after the number and an optional s,m,h,d char. */ || (*p && *(p+1)) /* Check any suffix char and update S based on the suffix. */ || ! apply_suffix (&s, *p)) { error (0, 0, _("invalid time interval %s"), quote (argv[i])); ok = false; } seconds += s; } if (!ok) usage (EXIT_FAILURE); if (xnanosleep (seconds)) error (EXIT_FAILURE, errno, _("cannot read realtime clock")); return EXIT_SUCCESS; }
// Returns 0 if cannot parse as a series of decimal digits, possibly followed // by k/m or K/M to denote a multiplier based on 1000 or 1024 respectively. // If allow_neg, may be preceded by `-` int atooff(const char *str, int length, int allow_neg, cc_off_t *outval) { if (!str || length <= 0) return 0; int is_neg = 0; if (*str == '-') { if (!allow_neg) return 0; is_neg = 1; str++; length--; if (length <= 0) return 0; } *outval = 0; int first = 1; for (; length; length--, str++, first = 0) { int digit = *str - '0'; if (digit < 0 || digit > 9) { // abort the digits sequence, and try to apply as a suffix. // Only valid if it's the last and not the first and can be applied return length == 1 && !first && apply_suffix(*outval, *str, outval); } first = 0; if (is_neg) digit = -digit; if (!mult_safe(*outval, 10, outval) || !add_safe(*outval, digit, outval)) return 0; } return 1; }