- } else if (argc == 2) {
- /* convert low arg */
- if (read_num_buf(NULL, argv[1]) == NULL) {
- fprintf(stderr, "%s: ouch\n", program);
- exit(1);
- }
- if (sscanf(argv[1], "%ld", &start) != 1) {
- fprintf(stderr, "%s: ouch\n", program);
- exit(1);
- }
-
- } else {
- /* read input until we get a good line */
- if (read_num_buf(stdin, buf) != NULL) {
-
- /* convert the buffer */
- if (sscanf(buf, "%ld", &start) != 1) {
- fprintf(stderr, "%s: ouch\n", program);
- exit(1);
- }
- } else {
- exit(0);
- }
- }
- if (start > stop) {
- fprintf(stderr, "%s: ouch\n", program);
- exit(1);
+ /*
+ * Convert low and high args. Strtoul(3) sets errno to
+ * ERANGE if the number is too large, but, if there's
+ * a leading minus sign it returns the negation of the
+ * result of the conversion, which we'd rather disallow.
+ */
+ switch (argc) {
+ case 2:
+ /* Start and stop supplied on the command line. */
+ if (argv[0][0] == '-' || argv[1][0] == '-')
+ errx(1, "negative numbers aren't permitted.");
+
+ errno = 0;
+ start = strtoul(argv[0], &p, 10);
+ if (errno)
+ err(1, "%s", argv[0]);
+ if (*p != '\0')
+ errx(1, "%s: illegal numeric format.", argv[0]);
+
+ errno = 0;
+ stop = strtoul(argv[1], &p, 10);
+ if (errno)
+ err(1, "%s", argv[1]);
+ if (*p != '\0')
+ errx(1, "%s: illegal numeric format.", argv[1]);
+ break;
+ case 1:
+ /* Start on the command line. */
+ if (argv[0][0] == '-')
+ errx(1, "negative numbers aren't permitted.");
+
+ errno = 0;
+ start = strtoul(argv[0], &p, 10);
+ if (errno)
+ err(1, "%s", argv[0]);
+ if (*p != '\0')
+ errx(1, "%s: illegal numeric format.", argv[0]);
+ break;
+ case 0:
+ start = read_num_buf();
+ break;
+ default:
+ usage();