From: Zack Weinberg Date: Thu, 6 Apr 2017 18:14:14 +0000 (-0700) Subject: getopt: merge from glibc: repetition reduction X-Git-Tag: v1.0~6292 X-Git-Url: https://gitweb.git.savannah.gnu.org/gitweb/?a=commitdiff_plain;h=d72c5a7bed87ea43863479657285c19dc968c1dd;p=gnulib.git getopt: merge from glibc: repetition reduction The definitions of the entry point functions 'getopt' and '__posix_getopt' can be made substantially less repetitive with a helper macro. While I was merging the const-correctness changes from gnulib into glibc I noticed there are still some unnecessary casts in _getopt_internal_r. * lib/getopt.c (getopt, __posix_getopt): Eliminate repetition with a macro. Consistently cast 'argv' to 'char **' when calling _getopt_internal. (_getopt_internal_r): Remove unnecessary casts when calling exchange. --- diff --git a/ChangeLog b/ChangeLog index 06fe3b81d7..dc573dbec4 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,21 @@ 2017-04-06 Zack Weinberg + getopt: merge from glibc: repetition reduction + + The definitions of the entry point functions 'getopt' and + '__posix_getopt' can be made substantially less repetitive with a + helper macro. + + While I was merging the const-correctness changes from gnulib into + glibc I noticed there are still some unnecessary casts in + _getopt_internal_r. + + * lib/getopt.c (getopt, __posix_getopt): Eliminate repetition with + a macro. Consistently cast 'argv' to 'char **' when calling + _getopt_internal. + (_getopt_internal_r): Remove unnecessary casts when calling exchange. + + getopt: clean up error reporting getopt can print a whole bunch of error messages, and when used diff --git a/lib/getopt.c b/lib/getopt.c index ddf3e4c9f5..7752872897 100644 --- a/lib/getopt.c +++ b/lib/getopt.c @@ -318,7 +318,7 @@ _getopt_internal_r (int argc, char **argv, const char *optstring, if (d->__first_nonopt != d->__last_nonopt && d->__last_nonopt != d->optind) - exchange ((char **) argv, d); + exchange (argv, d); else if (d->__last_nonopt != d->optind) d->__first_nonopt = d->optind; @@ -341,7 +341,7 @@ _getopt_internal_r (int argc, char **argv, const char *optstring, if (d->__first_nonopt != d->__last_nonopt && d->__last_nonopt != d->optind) - exchange ((char **) argv, d); + exchange (argv, d); else if (d->__first_nonopt == d->__last_nonopt) d->__first_nonopt = d->optind; d->__last_nonopt = argc; @@ -824,32 +824,23 @@ _getopt_internal (int argc, char **argv, const char *optstring, return result; } -/* glibc gets a LSB-compliant getopt. - Standalone applications get a POSIX-compliant getopt. */ -#if _LIBC -enum { POSIXLY_CORRECT = 0 }; -#else -enum { POSIXLY_CORRECT = 1 }; -#endif - -int -getopt (int argc, char *const *argv, const char *optstring) -{ - return _getopt_internal (argc, (char **) argv, optstring, - (const struct option *) 0, - (int *) 0, - 0, POSIXLY_CORRECT); -} +/* glibc gets a LSB-compliant getopt and a POSIX-complaint __posix_getopt. + Standalone applications just get a POSIX-compliant getopt. + POSIX and LSB both require these functions to take 'char *const *argv' + even though this is incorrect (because of the permutation). */ +#define GETOPT_ENTRY(NAME, POSIXLY_CORRECT) \ + int \ + NAME (int argc, char *const *argv, const char *optstring) \ + { \ + return _getopt_internal (argc, (char **)argv, optstring, \ + 0, 0, 0, POSIXLY_CORRECT); \ + } #ifdef _LIBC -int -__posix_getopt (int argc, char *const *argv, const char *optstring) -{ - return _getopt_internal (argc, argv, optstring, - (const struct option *) 0, - (int *) 0, - 0, 1); -} +GETOPT_ENTRY(getopt, 0) +GETOPT_ENTRY(__posix_getopt, 1) +#else +GETOPT_ENTRY(getopt, 1) #endif