(FAIL_ONLY_ONE_WAY): New macro. Factor out some duplication.

(main): Use it.
[case 'a']: Use strtoul rather than strtol to avoid compiler warnings.
This commit is contained in:
Jim Meyering 2002-09-28 16:50:34 +00:00
parent 6fee4452d3
commit fd5bc32835

View File

@ -191,7 +191,7 @@ cwrite (int new_file_flag, const char *bp, int bytes)
if (output_desc < 0) if (output_desc < 0)
error (EXIT_FAILURE, errno, "%s", outfile); error (EXIT_FAILURE, errno, "%s", outfile);
} }
if (full_write (output_desc, bp, bytes) != bytes) if (full_write (output_desc, bp, bytes) != (size_t) bytes)
error (EXIT_FAILURE, errno, "%s", outfile); error (EXIT_FAILURE, errno, "%s", outfile);
} }
@ -358,6 +358,14 @@ line_bytes_split (int nchars)
free (buf); free (buf);
} }
#define FAIL_ONLY_ONE_WAY() \
do \
{ \
error (0, 0, _("cannot split in more than one way")); \
usage (EXIT_FAILURE); \
} \
while (0)
int int
main (int argc, char **argv) main (int argc, char **argv)
{ {
@ -401,21 +409,21 @@ main (int argc, char **argv)
break; break;
case 'a': case 'a':
if (xstrtol (optarg, NULL, 10, &tmp_long, "") != LONGINT_OK {
|| tmp_long < 0 || tmp_long > SIZE_MAX) unsigned long tmp;
{ if (xstrtoul (optarg, NULL, 10, &tmp, "") != LONGINT_OK
error (0, 0, _("%s: invalid suffix length"), optarg); || SIZE_MAX < tmp)
usage (EXIT_FAILURE); {
} error (0, 0, _("%s: invalid suffix length"), optarg);
suffix_length = tmp_long; usage (EXIT_FAILURE);
}
suffix_length = tmp;
}
break; break;
case 'b': case 'b':
if (split_type != type_undef) if (split_type != type_undef)
{ FAIL_ONLY_ONE_WAY ();
error (0, 0, _("cannot split in more than one way"));
usage (EXIT_FAILURE);
}
split_type = type_bytes; split_type = type_bytes;
if (xstrtol (optarg, NULL, 10, &tmp_long, "bkm") != LONGINT_OK if (xstrtol (optarg, NULL, 10, &tmp_long, "bkm") != LONGINT_OK
|| tmp_long < 0 || tmp_long > INT_MAX) || tmp_long < 0 || tmp_long > INT_MAX)
@ -428,10 +436,7 @@ main (int argc, char **argv)
case 'l': case 'l':
if (split_type != type_undef) if (split_type != type_undef)
{ FAIL_ONLY_ONE_WAY ();
error (0, 0, _("cannot split in more than one way"));
usage (EXIT_FAILURE);
}
split_type = type_lines; split_type = type_lines;
if (xstrtol (optarg, NULL, 10, &tmp_long, "") != LONGINT_OK if (xstrtol (optarg, NULL, 10, &tmp_long, "") != LONGINT_OK
|| tmp_long < 0 || tmp_long > INT_MAX) || tmp_long < 0 || tmp_long > INT_MAX)
@ -444,11 +449,7 @@ main (int argc, char **argv)
case 'C': case 'C':
if (split_type != type_undef) if (split_type != type_undef)
{ FAIL_ONLY_ONE_WAY ();
error (0, 0, _("cannot split in more than one way"));
usage (EXIT_FAILURE);
}
split_type = type_byteslines; split_type = type_byteslines;
if (xstrtol (optarg, NULL, 10, &tmp_long, "bkm") != LONGINT_OK if (xstrtol (optarg, NULL, 10, &tmp_long, "bkm") != LONGINT_OK
|| tmp_long < 0 || tmp_long > INT_MAX) || tmp_long < 0 || tmp_long > INT_MAX)
@ -470,10 +471,7 @@ main (int argc, char **argv)
case '8': case '8':
case '9': case '9':
if (split_type != type_undef && split_type != type_digits) if (split_type != type_undef && split_type != type_digits)
{ FAIL_ONLY_ONE_WAY ();
error (0, 0, _("cannot split in more than one way"));
usage (EXIT_FAILURE);
}
if (digits_optind != 0 && digits_optind != this_optind) if (digits_optind != 0 && digits_optind != this_optind)
accum = 0; /* More than one number given; ignore other. */ accum = 0; /* More than one number given; ignore other. */
digits_optind = this_optind; digits_optind = this_optind;