Bruno Haible [Fri, 5 May 2017 19:50:57 +0000 (21:50 +0200)]
inttypes-c++-tests: New module.
* tests/test-inttypes-c++.cc: New file.
(strtoimax): Declare, missing since 2012-01-05.
(strtoumax): Declare, missing since 2012-01-05.
* modules/inttypes-c++-tests: New file.
Paul Eggert [Tue, 2 May 2017 07:40:41 +0000 (00:40 -0700)]
utimens: port to Emacs + MS-Windows
Skip the new MS-Windows-specific code if Emacs.
* lib/utimens.c [EMACS_CONFIGUATION]:
Avoid new MS-Windows-specific code.
(USE_SETFILETIME): New macro.
(fdutimens): Use it.
Bruno Haible [Mon, 1 May 2017 15:27:53 +0000 (17:27 +0200)]
tzset: Work around TZ problem on native Windows.
* m4/tzset.m4 (gl_FUNC_TZSET): Require AC_CANONICAL_HOST. On native
Windows, set REPLACE_TZSET to 1.
* lib/tzset.c (tzset): On native Windows, fix TZ if necessary, and
invoke '_tzset' instead of 'tzset'.
* doc/posix-functions/tzset.texi: Mention the native Windows workaround.
* modules/time_rz (Depends-on): Add tzset.
* lib/time_rz.c (tzset): Remove fallback definition.
* m4/time_rz.m4 (gl_TIME_RZ): Don't test for tzset.
Bruno Haible [Mon, 1 May 2017 11:27:57 +0000 (13:27 +0200)]
copy-file: Preserve sub-second time stamps.
* lib/copy-file.c: Include stat-time.h, utimens.h instead of <utime.h>.
(qcopy_file_preserving): Use 'struct timespec' and utimens() to
transport the time stamps from the original file to the destination
file.
* m4/copy-file.m4 (gl_COPY_FILE): Don't test for utime, utimes.
* modules/copy-file (Depends-on): Add stat-time, utimns instead of
utime-h.
Bruno Haible [Mon, 1 May 2017 10:36:02 +0000 (12:36 +0200)]
wchar: Fix compilation error with the original mingw.org mingw.
* lib/wchar.in.h (rpl_wint_t): If <crtdefs.h> does not exist, include
<stddef.h> instead.
* m4/wint_t.m4 (gl_TYPE_WINT_T_PREREQ): New macro, extracted from
gl_WCTYPE_H.
* m4/wctype_h.m4 (gl_WCTYPE_H): Don't set HAVE_CRTDEFS_H here; require
gl_TYPE_WINT_T_PREREQ instead.
* m4/wchar_h.m4 (gl_WCHAR_H): Require gl_TYPE_WINT_T_PREREQ.
* modules/wchar (Makefile.am): Substitute HAVE_CRTDEFS_H.
Reported by Eli Zaretskii <eliz@gnu.org>.
Bruno Haible [Mon, 1 May 2017 09:50:08 +0000 (11:50 +0200)]
utimens: Add support for native Windows.
* lib/utimens.c: Include <windows.h>, msvc-nothrow.h.
(fdutimens): Provide a native Windows implementation, like utime.c with
added tv_nsec support.
* modules/utimens (Depends-on): Add msvc-nothrow, utime.
Suggested by Tim Rühsen <tim.ruehsen@gmx.de>.
Bruno Haible [Sun, 30 Apr 2017 14:34:54 +0000 (16:34 +0200)]
mktime: Work around TZ problem on native Windows.
* lib/mktime.c: Add #ifs to make the algorithmic workaround independent
from the native Windows workaround.
* m4/mktime.m4 (gl_FUNC_MKTIME_WORKS): New macro, extracted from
gl_FUNC_MKTIME. If guessing, set gl_cv_func_working_mktime to
'guessing no'.
(gl_FUNC_MKTIME): Require it. Require AC_CANONICAL_HOST.
Set REPLACE_MKTIME to 1 on native Windows. Define NEED_MKTIME_WORKING,
NEED_MKTIME_WINDOWS.
(gl_FUNC_MKTIME_INTERNAL): Require gl_FUNC_MKTIME_WORKS, not
gl_FUNC_MKTIME. Set WANT_MKTIME_INTERNAL, not REPLACE_MKTIME. Define
NEED_MKTIME_INTERNAL.
* m4/timegm.m4 (gl_FUNC_TIMEGM): Require gl_FUNC_MKTIME_WORKS, not
gl_FUNC_MKTIME. Cope with 'guessing yes' value.
* modules/mktime-internal (configure.ac): Test WANT_MKTIME_INTERNAL,
not REPLACE_MKTIME.
* doc/posix-functions/mktime.texi: Mention the native Windows
workaround.
Bruno Haible [Sun, 30 Apr 2017 11:32:40 +0000 (13:32 +0200)]
localtime: New module.
* lib/time.in.h (localtime): Declare also if requested by module
'localtime'.
* lib/localtime.c: New file.
* m4/localtime.m4: New file.
* m4/time_h.m4 (gl_HEADER_TIME_H_DEFAULTS): Initialize GNULIB_LOCALTIME.
* modules/time (Makefile.am): Substitute GNULIB_LOCALTIME.
* modules/localtime: New file.
* doc/posix-functions/localtime.texi: Mention the new module.
Bruno Haible [Sun, 30 Apr 2017 10:15:08 +0000 (12:15 +0200)]
gettimeofday: Provide higher resolution on native Windows.
* lib/gettimeofday.c: Don't include <sys/timeb.h>.
(GetSystemTimePreciseAsFileTimeFuncType): New variable.
(initialize): Initialize it.
(gettimeofday) [WINDOWS_NATIVE]: Use it, and convert from FILETIME to
'struct timeval'. Don't use _ftime().
* m4/gettimeofday.m4 (gl_PREREQ_GETTIMEOFDAY): Don't test for
<sys/timeb.h> and _ftime.
Bruno Haible [Sat, 29 Apr 2017 23:26:05 +0000 (01:26 +0200)]
utime-h: Modernize handling of 'struct utimbuf'.
* lib/utime.in.h: Include next <utime.h> if it exists.
(utimbuf): Define to _utimbuf on native Windows.
* m4/utime-h.m4 (gl_UTIME_H): Check for prerequisites of include_next.
Set UTIME_H on native Windows.
(gl_UTIME_MODULE_INDICATOR, gl_HEADER_UTIME_H_DEFAULTS): New macros.
* modules/utime-h (Depends-on): Add include_next.
(Makefile.am): Substitute also HAVE_UTIME_H, INCLUDE_NEXT,
PRAGMA_SYSTEM_HEADER, PRAGMA_COLUMNS, NEXT_UTIME_H.
Bruno Haible [Sat, 29 Apr 2017 13:05:15 +0000 (15:05 +0200)]
stat: Fix time_t values and other problems on native Windows platforms.
* doc/posix-functions/stat.texi: Mention the problem with the Microsoft
implementations of stat().
* lib/stat.c: Include filename.h instead of dosname.h. Include
malloca.h, stat-w32.h.
(is_unc_root): New function.
(rpl_stat): New implementation for native Windows. Remove
REPLACE_FUNC_STAT_DIR code.
* m4/stat.m4 (gl_FUNC_STAT): On native Windows, set REPLACE_STAT always.
Don't define REPLACE_FUNC_STAT_DIR.
(gl_PREREQ_STAT): Require gl_HEADER_SYS_STAT_H.
* modules/stat (Files): Add lib/stat-w32.h, lib/stat-w32.c.
(Depends-on): Remove dosname. Add filename, malloca.
(configure.ac): Also compile lib/stat-w32.c.
Bruno Haible [Sat, 29 Apr 2017 12:55:22 +0000 (14:55 +0200)]
fstat: Fix time_t values on native Windows platforms.
* doc/posix-functions/fstat.texi: Mention the problem with st_*time.
* lib/stat-w32.h: New file.
* lib/stat-w32.c: New file.
* lib/fstat.c: Don't include msvc-inval.h. Include msvc-nothrow.h,
stat-w32.h instead.
(fstat_nothrow): Remove function.
(rpl_fstat): Implement by means of _gl_fstat_by_handle.
* m4/fstat.m4 (gl_FUNC_FSTAT): On native Windows, set REPLACE_FSTAT
always.
(gl_PREREQ_FSTAT): Require gl_HEADER_SYS_STAT_H.
* modules/fstat (Files): Add lib/stat-w32.h, lib/stat-w32.c.
(Depends-on): Remove msvc-inval. Add pathmax, msvc-nothrow.
(configure.ac): Also compile lib/stat-w32.c.
Paul Eggert [Sat, 29 Apr 2017 18:09:39 +0000 (11:09 -0700)]
getopt: port to Solaris 10 with circa-1997 glibc getopt.h
Problem reported by Assaf Gordon and Gavin Smith in:
http://lists.gnu.org/archive/html/bug-gnulib/2017-04/msg00157.html
* lib/getopt-pfx-ext.h (_getopt_internal) [__GETOPT_PREFIX]:
#define this, too.
Bruno Haible [Sat, 29 Apr 2017 11:23:28 +0000 (13:23 +0200)]
strerror_r-posix: Fixes for MSVC 14.
* lib/strerror_r.c: Include <stdarg.h>.
(strerror_r): Provide error messages for errno values 100...140.
* doc/posix-functions/strerror_r.texi: Mention the MSVC 14 problem.
Bruno Haible [Wed, 26 Apr 2017 21:25:11 +0000 (23:25 +0200)]
noreturn: New module.
* lib/noreturn.h: New file.
* modules/noreturn: New file.
* tests/test-noreturn.c: New file.
* modules/noreturn-tests: New file.
* tests/test-noreturn-c++.cc: New file.
* modules/noreturn-c++-tests: New file.
Bruno Haible [Thu, 27 Apr 2017 16:59:24 +0000 (18:59 +0200)]
wctype-h: Fix compilation error with the original mingw.org mingw.
* m4/wctype_h.m4 (gl_WCTYPE_H): Test for <crtdefs.h>. Set
HAVE_CRTDEFS_H.
* modules/wctype-h (Makefile.am): Substitute HAVE_CRTDEFS_H.
* lib/wctype.in.h (rpl_wint_t): If <crtdefs.h> does not exist, include
<stddef.h> instead.
Reported and proposed by Eli Zaretskii <eliz@gnu.org>.
This issue has been assigned CVE-2017-7476 and was
detected with American Fuzzy Lop 2.41b run on the
coreutils date(1) program with ASAN enabled.
ERROR: AddressSanitizer: heap-buffer-overflow on address 0x...
WRITE of size 8 at 0x60d00000cff8 thread T0
#1 0x443020 in extend_abbrs lib/time_rz.c:88
#2 0x443356 in save_abbr lib/time_rz.c:155
#3 0x44393f in localtime_rz lib/time_rz.c:290
#4 0x41e4fe in parse_datetime2 lib/parse-datetime.y:1798
A minimized reproducer is the following 120 byte TZ value,
which goes beyond the value of ABBR_SIZE_MIN (119) on x86_64.
Extend the aa...b portion to overwrite more of the heap.
localtime_rz and mktime_z were affected since commit 4bc76593.
parse_datetime was affected since commit 4e6e16b3f.
* lib/time_rz.c (save_abbr): Rearrange the calculation determining
whether there is enough buffer space available. The rearrangement
ensures we're only dealing with positive numbers, thus avoiding
the problematic promotion of signed to unsigned causing an invalid
comparison when zone_copy is more than ABBR_SIZE_MIN bytes beyond
the start of the buffer.
* tests/test-parse-datetime.c (main): Add a test case written by
Paul Eggert, which overwrites enough of the heap so that
standard glibc will fail with "free(): invalid pointer"
without the patch applied.
Reported and analyzed at https://bugzilla.redhat.com/1444774
Paul Eggert [Tue, 25 Apr 2017 18:58:30 +0000 (11:58 -0700)]
parse-datetime: make it standalone
* lib/parse-datetime.y: Include <stdarg.h>, for va_start etc.
(_GL_ATTRIBUTE_FORMAT): New macro.
These are needed to get './gnulib-tool --test parse-datetime' to work.
Bruno Haible [Sun, 23 Apr 2017 17:19:35 +0000 (19:19 +0200)]
nap.h: Port to native Windows.
* tests/nap.h (nap_get_stat): Renamed from get_stat. Remove argument fd;
use nap_fd instead. On native Windows, close and reopen nap_fd.
(nap_works): Don't compare the ctimes, because on native Windows, these
are the creation times.
(nap): Update.
Paul Eggert [Mon, 24 Apr 2017 03:54:35 +0000 (20:54 -0700)]
Target a C99 subset, not a C89 subset
For many years Gnulib has targeted C89 and has resisted using C99
features, as some Gnulib-using programs still wanted to target
C89. As this no longer seems to be the case, relax the porting
requirements to allow some C99 features. This is merely a change
to the documentation, to give other Gnulib developers a chance to
weigh in on the topic.
* doc/extern-inline.texi (extern inline):
* doc/gnulib-readme.texi (Portability guidelines):
* doc/gnulib-tool.texi (Initial import):
* doc/gnulib.texi (Header files):
Modernize to talk about C99 and C11 instead of C89 and C99.
* doc/gnulib-readme.texi (Portability guidelines):
Now a section, not merely a subsection, so that it
can be split up. Modernize a bit.
(C language versions, C99 features assumed)
(C99 features avoided):
New sections.
Paul Eggert [Sun, 23 Apr 2017 01:48:39 +0000 (18:48 -0700)]
poll: improve fast check for out-of-range NFD
* lib/poll.c: Do not include intprops.h.
(poll): Compare NFD to INT_MAX, not to TYPE_MAXIMUM (nfds_t) / 2.
* modules/poll (Depends-on): Remove intprops.
Bruno Haible [Sun, 23 Apr 2017 00:45:19 +0000 (02:45 +0200)]
getlogin_r: Work around bug in Mac OS X 10.12.
* m4/getlogin_r.m4 (gl_FUNC_GETLOGIN_R): Test also against the Mac OS X
bug.
* lib/getlogin_r.c (getlogin_r): When getlogin_r returns a string of the
given size minus 1, call getlogin_r a second time, on a larger buffer.
* modules/getlogin_r (Depends-on): Add malloca.
* doc/posix-functions/getlogin_r.texi: Mention the Mac OS X bug.
Paul Eggert [Sat, 22 Apr 2017 22:13:24 +0000 (15:13 -0700)]
parse-datetime: fix %z and prefer signed int
%z problem reported by Pádraig Brady in:
http://lists.gnu.org/archive/html/bug-gnulib/2017-04/msg00103.html
While fixing it, I decided to prefer signed ints to size_t, as
they are less error-prone (e.g., ubsan catches overflow).
* lib/parse-datetime.y (textint, parser_control, lookup_word, yylex)
(parse_datetime2): Prefer ptrdiff_t to size_t for sizes and object
counts, since signed integers make for better debugging.
(date): Don’t assume %z works in printf formats.
(debug_strfdatetime, debug_strfdate, debug_strftime): Use int for
sizes of buffers known to be small, e.g., because we’re using snprintf.
(parse_datetime2): Simplify call to debug_mktime_not_ok.
Bruno Haible [Sat, 22 Apr 2017 14:27:45 +0000 (16:27 +0200)]
*printf: Work around rounding bug on Mac OS X.
* m4/printf.m4 (gl_PRINTF_DIRECTIVE_A): Test for Mac OS X 10.12 bug.
* doc/posix-functions/*printf.texi: Mention the rounding bugs of
Mac OS X and FreeBSD.
* doc/glibc-functions/*printf.texi: Likewise.
Bruno Haible [Sat, 22 Apr 2017 11:51:02 +0000 (13:51 +0200)]
get-rusage-data: Avoid warnings on Mac OS X.
* lib/get-rusage-data.c: On Mac OS X, don't define
get_rusage_data_via_setrlimit nor get_rusage_data_via_iterator.
(get_rusage_data) [Mac OS X]: Just return 0.
Bruno Haible [Sat, 22 Apr 2017 10:02:38 +0000 (12:02 +0200)]
xbinary-io: Fix build error.
* modules/xbinary-io (Depends-on): Add gettext-h.
* lib/xbinary-io.c: Include gettext.h and define _().
Reported by Gisle Vanem <gisle.vanem@gmail.com> in
<https://lists.gnu.org/archive/html/bug-gnulib/2017-04/msg00089.html>.
Paul Eggert [Sat, 22 Apr 2017 09:11:26 +0000 (02:11 -0700)]
parse-datetime: overflow and debug cleanups
This long patch was triggered by this bug report from Ruediger Meier:
http://lists.gnu.org/archive/html/bug-gnulib/2017-04/msg00028.html
I fixed the bug he noted, then found some others nearby, and then
still others. Oh my goodness, there were a lot of bugs. I cleaned
up some of the code to follow GNU standards while I was at it.
* lib/parse-datetime.y (ISDIGIT): Remove; all callers changed to
use c_isdigit.
(EPOCH_YEAR): Remove; unused.
(TM_YEAR_BASE): Now an enum rather than a macro.
(HOUR, debug_strfdatetime): Multiply hour by 3600, not 60, to get
time zone offset, since timezones now are in terms of seconds and
not minutes.
(long_time_t): Remove. All uses replaced by time_t or intmax_t as
appropriate. Verify that intmax_t is wide enough.
(time_overflow, time_zone_str): New functions, used to deal
more reliably with overflow.
(dbg_printf): Add printf attribute, to help catch integer width errors.
(textint, relative_time, parser_control, time_zone_hhmm, set_hhmmss)
(%union, to_hour, yylex, parse_datetime2):
Use intmax_t instead of long int and/or long_time_t.
All uses changed.
(DBGBUFSIZE): Move earlier.
(relative_time, set_hhmmss, parser_control):
Just use int for nanoseconds and for time zones; that’s wide enough.
(parser_control): Use bool for members like year_seen that can
be booleans instead of counters. All uses changed.
Remove debug_default_input_timezone; no longer needed.
All uses removed.
(apply_relative_time): Return a bool overflow flag.
All uses changed to check for overflow.
(apply_relative_time, zone, date, relunit, relunit_snumber)
(signed_seconds, unsigned_seconds, yylex, parse_datetime2):
Check for integer overflow portably.
(str_days): Use just int for N, as it’s wide enough.
Prefer 2D char arrays to arrays of char * when it looks like
2D is a win on typical platforms.
Prefer snprintf to strncpy/strncat, for simplicity;
all buffers are smaller than INT_MAX so this is safe.
(TIME_ZONE_BUFSiZE, TM_YEAR_BUFSIZE): New constants.
(debug_print_current_time): Don’t assume tv_nsec is of type long,
as this is not true on x32. Output "." before any nanoseconds.
(debug_print_current_time, parse_datetime2):
Output local zones using a more-consistent format.
(debug_print_current_time, date, parse_datetime2):
(main) [TEST]:
Don’t assume time_t is the same width as long.
(print_rel_part): New function, replacing ...
(PRINT_REL_PART): ... this macro, which was removed. All uses changed.
(debug_print_relative_time): Use bool for boolean.
(local_zone): dsts_seen now counts only tDST instances.
(date): Fix printf of size_t to use %z. Do not assume numeric
tokens have negative values merely because the context suggests
a syntax with "-" separating tokens.
(time_zone_hhmm): Return bool success indicator, which checks for
overflow. Store result into PC->time_zone instead. All callers
changed.
(tm_year_str): New function. Return a bool success indicator and
store the result into a buffer. All callers changed. Output the
numerically correct string even if adding 1900 to the year would
overflow.
(to_tm_year): New function, replacing the old to_year. All
callers changed.
(tm_diff): Sync with glibc.
(lookup_word): Use to_uchar instead of doing it by hand.
(TZBUFSIZE): Now local to the only function that needs it.
(debug_strfdatetime): Simplify now that time zones are int seconds.
(debug_strfdate): Work even if tm_year + 1900 would overflow.
(get_effective_timezone): Remove. All uses removed.
(parse_datetime2): Use fprintf in pieces instead of snprintfing
to a fixed-size buffer. Don’t assume that gmtime succeeds iff
localtime succeeds. Use tm_gmtoff if available. Simplify how
‘goto fail;’ works in conjunction with the ‘ok’ flag.
* m4/parse-datetime.m4 (gl_PARSE_DATETIME): Don’t define
TIME_T_FITS_IN_LONG_INT, as it is no longer needed.
* modules/parse-datetime (Depends-on): Add inttypes.
Bruno Haible [Fri, 21 Apr 2017 23:37:22 +0000 (01:37 +0200)]
gettext-h: Avoid -Wundef warning.
* lib/gettext.h: Test the value of ENABLE_NLS only if it is defined.
Reported by Tim Rühsen <tim.ruehsen@gmx.de> in
<https://lists.gnu.org/archive/html/bug-gnulib/2017-04/msg00022.html>.
Bruno Haible [Fri, 21 Apr 2017 15:24:48 +0000 (17:24 +0200)]
vasnprintf: Fix for MSVC 14.
* lib/vasnprintf.c (USE_MSVC__SNPRINTF): New macro.
Everywhere, use !HAVE_SNPRINTF_RETVAL_C99 || USE_MSVC__SNPRINTF instead
of !HAVE_SNPRINTF_RETVAL_C99.
Bruno Haible [Fri, 21 Apr 2017 14:43:01 +0000 (16:43 +0200)]
mbsinit: Fix for MSVC 14.
* lib/mbsinit.c (mbsinit): If GNULIB_defined_mbstate_t, provide an
implementation that is in sync with mbrtowc.c. On other platforms, use
an adequate ad-hoc implementation.
Bruno Haible [Thu, 20 Apr 2017 19:27:40 +0000 (21:27 +0200)]
verify tests: Fix spurious failure with parallel make.
* gnulib-tool (func_emit_tests_Makefile_am): Emit initialization of
EXTRA_PROGRAMS.
* tests/test-verify.sh: Build test-verify-try.o, not test-verify.o.
* tests/test-verify-try.c: New file.
* modules/verify-tests (Files): Add it.
(EXTRA_PROGRAMS): Add test-verify-try.
(MOSTLYCLEANFILES): Update accordingly.
Reported by Adam James Stewart <ajstewart@anl.gov>.
Bruno Haible [Tue, 18 Apr 2017 23:03:34 +0000 (01:03 +0200)]
vma-iter: Fix conflict with module 'largefile' on 32-bit Solaris 9.
* modules/vma-iter (configure.ac): Test whether <sys/procfs.h> can be
included.
* lib/vma-iter.c: On Solaris, test HAVE_SYS_PROCFS_H before including
<sys/procfs.h>.
* lib/vma-iter.h (VMA_ITERATE_SUPPORTED): Don't define on Solaris when
<sys/procfs.h> cannot be included.
Reported by Tom G. Christensen <tgc@jupiterrise.com>.
Paul Eggert [Sun, 16 Apr 2017 19:43:20 +0000 (12:43 -0700)]
regex: port better to Solaris 10
Solaris 10 <locale.h> includes <libintl.h>, which #defines
gettext, and this causes a double #define.
Problem reported by Gavin Smith in:
http://lists.gnu.org/archive/html/bug-gnulib/2017-04/msg00056.html
* lib/regex_internal.h (gettext): #undef before #defining.
Paul Eggert [Sat, 15 Apr 2017 21:27:11 +0000 (14:27 -0700)]
intprops: improve comments
* lib/intprops.h: Improve and shorten commentary.
For the record, if we ever run into a pedantic compiler that
behaves differently from GCC when converting an out-of-range value
to a signed integer, we can work around the problem with something
like the following code, where UCT is the signed counterpart of T
(UCT is sometimes narrower than UT) and all callers are changed
accordingly:
((t) ((ut) (a) op (ut) (b)))
(TYPE_MINIMUM (t) <= (uct) ((ut) (a) op (ut) (b)) \
? ((t) (uct) (((ut) (a) op (ut) (b)) - TYPE_MINIMUM (t)) \
+ TYPE_MINIMUM (t)) \
: (t) (uct) ((ut) (a) op (ut) (b)))
Paul Eggert [Sat, 15 Apr 2017 01:55:42 +0000 (18:55 -0700)]
intprops: try to avoid tickling similar bugs
* lib/intprops.h (_GL_INT_OP_CALC): Document that UT no longer
needs to be the same width as T; it can be wider.
Change callers so that UT is at least as wide as unsigned int,
as I suspect that this is less likely to run into compiler bugs.
Paul Eggert [Sat, 15 Apr 2017 00:38:58 +0000 (17:38 -0700)]
intprops: port to Oracle Studio 12.3 x86
Problem reported by Gavin Smith in:
http://lists.gnu.org/archive/html/bug-gnulib/2017-04/msg00049.html
* lib/intprops.h (_GL_INT_OP_WRAPV_VIA_UNSIGNED):
Convert unsigned to signed via the usual rather than the standard way,
to avoid a compiler bug in Oracle Studio 12.3 x86.
Paul Eggert [Sun, 9 Apr 2017 06:48:02 +0000 (23:48 -0700)]
getopt: prefer - to _ in new file names
* lib/getopt-cdefs.in.h: Rename from lib/getopt_cdefs.in.h.
* lib/getopt-core.h: Rename from lib/getopt_core.h.
* lib/getopt-ext.h: Rename from lib/getopt_ext.h.
* lib/getopt-pfx-core.h: Rename from lib/getopt_pfx_core.h.
* lib/getopt-pfx-ext.h: Rename from lib/getopt_pfx_ext.h.
All uses changed.
Paul Eggert [Sun, 9 Apr 2017 00:26:03 +0000 (17:26 -0700)]
getopt: port recent getopt changes to macOS
Problem reported by Harald Maier (Bug#26398).
The macOS C compiler uses __nonnull for its own purposes and that
clashes with glibc's __nonnull.
* lib/getopt.in.h: Add comment for _GL_ARG_NONNULL snippet.
* lib/getopt_cdefs.in.h (__nonnull): Remove.
* lib/getopt_core.h (getopt):
* lib/getopt_ext.h (getopt_long, getopt_long_only):
Use _GL_ARG_NONNULL, not __nonnull.
* lib/unistd.in.h: Move snippet hooks to before where the getopt
.h files are included, so that _GL_ARG_NONNULL is defined in time.
* modules/getopt-posix (Depends-on): Add snippet/arg-nonnull.
(getopt.h): Interpolate _GL_ARG_NONNULL snippet.
Paul Eggert [Thu, 6 Apr 2017 22:41:05 +0000 (15:41 -0700)]
getopt-gnu: omit some duplicate code
* m4/getopt.m4 (gl_FUNC_GETOPT_GNU): Don’t require
gl_FUNC_GETOPT_POSIX, as the configure.ac code generated by
gnulib-tool already does this.
* modules/getopt-gnu (configure.ac): Omit code duplicated from
getopt-posix, which we depend on.
As the final act in this patchset, adjust the message at the top of
each file to indicate which files are synced with glibc. (This has
already been done for most of the headers.)
* lib/getopt.c, lib/getopt1.c, lib/getopt_int.h:
Mention in top-of-file boilerplate that these files are shared
between glibc and gnulib.
getopt: split up getopt.in.h and eliminate __need_getopt
Over in glibc, all of the __need macros are being phased out in favor
of small headers that declare only the necessary components, as this
is much simpler and less prone to bugs. As getopt is shared with
glibc, gnulib needs to do the same for __need_getopt.
__need_getopt is misnamed; what it really means is "we want only the
getopt features specified in POSIX, not the GNU extensions". glibc
placed the "meat" of getopt.h into getopt_core.h and getopt_ext.h;
these files can be shared verbatim with gnulib. The portability
wrapper, on the other hand, they have renounced altogether; glibc's
getopt.h will no longer be shared with gnulib at all. In exchange,
certain glibc-specific quirks (having to do with __posix_getopt) no
longer need appear in gnulib's headers at all.
This patch merges getopt_core.h and getopt_ext.h from glibc, and
splits up the current gnulib-side portability wrapper into three
additional headers: getopt_pfx_core.h and getopt_pfx_ext.h handle
__GETOPT_PREFIX for their respective headers, getopt_cdefs.in.h
handles things like __BEGIN_DECLS and __THROW, and getopt.in.h and
unistd.in.h just use them. All new files are clearly marked with
whether they are shared with glibc.
* lib/getopt.in.h: Eliminate __need_getopt. Break up into ...
* lib/getopt_core.h, lib/getopt_ext.h: ... these new files shared
with glibc, and ...
* lib/getopt_cdefs.in.h, lib/getopt_pfx_core.h
* lib/getopt_pfx_ext.h: ... these new files not shared with glibc.
* lib/unistd.in.h: Include getopt_cdefs.h and getopt_pfx_core.h,
instead of defining __need_getopt and including the full getopt.h.
* m4/getopt.m4 (gl_GETOPT_SUBSTITUTE_HEADER): Check for sys/cdefs.h.
Define substitution variables GETOPT_CDEFS_H and HAVE_SYS_CDEFS_H.
* modules/getopt-posix (Files): Add new headers and sort list.
(Depends-on): No longer need snippet/arg-nonnull.
(Makefile.am): Generate getopt_cdefs.h.
glibc's getopt uses alloca to construct a linked list of possibilities
for an "ambiguous" long option. In gnulib, malloc should be used
instead. Providing for both cases complicates things a fair bit.
This patch rewrites ambiguous-option handling to use a boolean vector
instead of a linked list. There is then only one allocation that
might need freeing; in glibc it can honor __libc_use_alloca as usual,
and in gnulib we define __libc_use_alloca to always be false, so we
don't need ifdefs in the middle of the function. This should also be
slightly more efficient in the normal case of long options being fully
spelled out -- I think most people aren't even aware they _can_
sometimes abbreviate long options.
One interesting consequence is that the list of possibilities is now
printed in exactly the order they appear in the list of long options,
instead of the first possibility being shuffled to the end.
(The patch looks bigger than it really is because there's a fair bit
of reindentation and code rearrangement.)
* lib/getopt.c: When used standalone, define __libc_use_alloca
as always false and alloca to abort if called.
(process_long_option): Rewrite handling of ambiguous long options
to use a single boolean vector, not a linked list; use
__libc_use_alloca to decide whether to allocate this using alloca.
There were two copies of the bulk of the code to handle long options.
Now there is only one.
This change temporarily removes the logic to avoid using alloca when
standalone; the next patch in the series will restore it.
* lib/getopt.c (process_long_option): New function split out
from _getopt_internal_r.
(_getopt_internal_r): Replace both copies of the long-option
processing code with calls to process_long_option.
_getopt_data.__posixly_correct is completely redundant to
_getopt_data.__ordering, and some work that logically belongs in
_getopt_initialize was being done by _getopt_internal_r, making the
code harder to understand.
As a side effect, getenv will no longer be called if the first
character of the options string is '+' or '-', which is probably a
Good Thing. (Perhaps we should have a flag character that
specifically asks for the permutation behavior?)
* lib/getopt_int.h (_getopt_data): Remove __posixly_correct field.
* lib/getopt.c (_getopt_internal_r): Move some initialization code...
(_getopt_initialize): ...here. Don't set d->__posixly_correct.
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.