Eric Blake [Mon, 21 Dec 2009 18:14:18 +0000 (11:14 -0700)]
getsubopt: work around cygwin bug
On cygwin, compiling with -DGNULIB_POSIXCHECK failed because
the getsubopt link warning definition interfered with the
inclusion of the system header. The fix, as always in these
types of problems, is to ensure that system headers are
completely included before doing any overrides.
* lib/stdlib.in.h (includes): Move unistd inclusion sooner, to
avoid conflicting with system getsubopt.
* doc/posix-functions/getsubopt.texi (getsubopt): Document the
bug.
Eric Blake [Wed, 2 Dec 2009 22:48:08 +0000 (15:48 -0700)]
test-getopt: test stderr behavior
Portions of this commit are commented out because they tickle
glibc bugs. For a real-life example of the bug:
$ env -ua -:
env: invalid option -- :
Try `env --help' for more information.
$ env -:
env: invalid option -- :
Try `env --help' for more information.
$ env -+
env: invalid option -- +
Try `env --help' for more information.
$ env -ua -+
Try `env --help' for more information.
Notice that when -+ is not given as the first argument, the
error message is mistakenly suppressed.
* modules/getopt-posix-tests (Depends-on): Add dup2.
* tests/test-getopt.c (ASSERT): Avoid stderr.
(main): Move stderr to a temporary file.
* tests/test-getopt.h (getopt_loop): No longer manipulate opterr.
Instead, add parameter to inform caller if output occurred.
(test_getopt): Adjust all tests to expect silence, and add new
tests of leading ":".
* doc/glibc-functions/getopt_long.texi (getopt_long): Document
glibc shortcomings with leading "-:" or "+:" in optstring.
* doc/glibc-functions/getopt_long_only.texi (getopt_long_only):
Likewise.
* doc/posix-functions/getopt.texi (getopt): Likewise.
Eric Blake [Wed, 2 Dec 2009 00:21:34 +0000 (17:21 -0700)]
test-getopt: enhance test
Add coverage of optind==0 for getopt_long, since coreutils
depends on it. Also test an optstring containing "W;", since
that tends to expose corner-case bugs (even in glibc, so the
test is weaker than it could be).
* m4/getopt.m4 (gl_GETOPT_CHECK_HEADERS): Require that getopt_long
supports optind=0.
* tests/test-getopt.c (OPTIND_MIN): Move...
* tests/test-getopt.h (OPTIND_MIN): ...here.
* tests/test-getopt_long.h (test_getopt_long): Add more coverage.
Require that optind=0 works, since modern BSD supports it in
addition to optreset, and since coreutils expects it.
(test_getopt_long_only): New test.
* doc/glibc-functions/getopt_long.texi (getopt_long): Document
glibc shortcomings with 'W;', and enforcement of optind=0.
* doc/glibc-functions/getopt_long_only.texi (getopt_long_only):
Likewise.
Eric Blake [Thu, 17 Dec 2009 14:32:00 +0000 (07:32 -0700)]
futimens, utimensat: work around Linux bug
futimens is trivial - let fdutimens do the work. utimensat
is tougher: we don't want to call into local_utimensat,
because that can cause unnecessary chdir. So we have to
repeat the logic from utimens.c.
* m4/futimens.m4 (gl_FUNC_FUTIMENS): Detect ctime bug.
* m4/utimensat.m4 (gl_FUNC_UTIMENSAT): Likewise.
* lib/utimensat.c (rpl_utimensat): Work around it.
* lib/futimens.c (rpl_futimens): Adjust comment.
Eric Blake [Thu, 17 Dec 2009 23:57:37 +0000 (16:57 -0700)]
utimens: work around Linux ctime bug
Force a ctime update by using stat() before any utimensat call
with mtime of UTIME_OMIT. But avoid extra stat()s in later
calls, by doing extra work on the first instance in order to
cache whether the bug is actually present.
* lib/utimens.c (detect_ctime_bug): New helper function.
(update_timespec): Differentiate between workaround needed for
this bug vs. what is needed for systems that lack utimensat.
(fdutimens, lutimens): Work around bug.
Eric Blake [Sat, 19 Dec 2009 16:28:36 +0000 (09:28 -0700)]
canonicalize: reduce memory usage
canonicalize was returning a 4k buffer for everything, even though
the majority of canonical path names are much shorter. This
resulted in a lot of unused memory, which in turn made tar run
out of memory when tracking a lot of files:
http://lists.gnu.org/archive/html/bug-tar/2009-12/msg00011.html
* lib/canonicalize.c (canonicalize_filename_mode): Trim the
allocation to size.
Reported by Solar Designer <solar@openwall.com>.
Eric Blake [Wed, 16 Dec 2009 16:11:32 +0000 (09:11 -0700)]
fcntl: port portions of fcntl to mingw
Borrow ideas from dup_cloexec and dup3 to implement F_DUPFD and
F_DUPFD_CLOEXEC. Support querying the inheritance status via
F_GETFD, but for now, no support for changing with F_SETFD.
The remaining portions of fcntl fail with EINVAL.
* m4/fcntl.m4 (gl_FUNC_FCNTL): Also build fcntl.c on mingw.
* lib/fcntl.c (fcntl) <F_DUPFD, F_DUPFD_CLOEXEC, F_GETFD>: Provide
replacement for mingw.
* modules/fcntl (Description): Update.
(Depends-on): Add dup2.
* m4/fcntl_h.m4 (gl_FCNTL_H_DEFAULTS): Add witness.
* modules/fcntl-h (Makefile.am): Substitute it.
* lib/fcntl.in.h (fcntl): Update declaration.
(F_DUPFD, F_GETFD): New macros, when needed.
* doc/posix-headers/fcntl.texi (fcntl.h): Update documentation.
* doc/posix-functions/fcntl.texi (fcntl): Likewise.
* tests/test-fcntl.c (check_flags, main): Enhance test for items
we now guarantee.
Eric Blake [Tue, 8 Dec 2009 19:10:52 +0000 (12:10 -0700)]
fcntl: work around cygwin bug in F_DUPFD
fcntl(0,F_DUPFD,10000000) mistakenly failed with EMFILE
instead of EINVAL, and fcntl(0,F_DUPFD,-1) mistakenly passed.
* m4/fcntl.m4 (gl_REPLACE_FCNTL): New macro.
(gl_FUNC_FCNTL): Use it. Test for F_DUPFD bug.
* lib/fcntl.c (rpl_fcntl) <F_DUPFD>: Work around it.
<F_DUPFD_CLOEXEC>: Reduce calls to _gl_register_dup.
* doc/posix-functions/fcntl.texi (fcntl): Document it.
Eric Blake [Mon, 7 Dec 2009 18:50:59 +0000 (11:50 -0700)]
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Implement F_DUPFD_CLOEXEC. The unit test still fails on systems
with other fcntl bugs (such as cygwin 1.5 mishandling F_DUPFD,
or mingw lacking fcntl altogether). Passes on Linux, both with
and without kernel support, and on cygwin 1.7.
* modules/fcntl (Files): List new files.
(configure.ac): Run a test.
* m4/fcntl.m4 (gl_FUNC_FCNTL): New file.
* lib/fcntl.c (rpl_fcntl): Likewise.
* m4/fcntl_h.m4 (gl_FCNTL_H_DEFAULTS): Add witness defaults.
(gl_FCNTL_H): Always replace fcntl.h.
* modules/fcntl-h (Makefile.am): Substitute witnesses.
* lib/fcntl.in.h (fcntl): Declare replacement.
(F_DUPFD_CLOEXEC, GNULIB_defined_F_DUPFD_CLOEXEC): New macro when
needed, plus a witness.
* doc/posix-functions/fcntl.texi (fcntl): Document this.
* doc/posix-headers/fcntl.texi (fcntl.h): Likewise.
* tests/test-fcntl.c: New file.
* modules/fcntl-tests: Likewise.
Eric Blake [Mon, 14 Dec 2009 22:42:13 +0000 (15:42 -0700)]
fflush: avoid compilation error on NetBSD
On NetBSD, the system <stdio.h> header contains:
|#if (!defined(_ANSI_SOURCE) && !defined(__STRICT_ANSI__)) || defined(_LIBC)
|typedef __off_t fpos_t;
|#else
|typedef struct __sfpos {
| __off_t _pos;
|} fpos_t;
|#endif
Thus, based on compiler flags (such as using 'gcc -ansi' or the
Intel compiler), it is an error to directly set fpos_t=off_t.
* lib/fflush.c (update_fpos_cache): Use a union to safely convert
between off_t and fpos_t, since the latter is sometimes a struct.
* lib/fseeko.c (rpl_fseeko): Likewise.
Reported by Alexander Nasonov <alnsn@yandex.ru>.
Eric Blake [Tue, 15 Dec 2009 13:51:24 +0000 (06:51 -0700)]
fcntl-h, stdio, sys_ioctl: fix declarations
We cannot use a function-like macro to provide a link-time warning
of a variadic function during GNULIB_POSIXCHECK; at least, not
without assuming C99 variadic macros. We can, however,
use an object-like macro (as was already done for printf).
On the other hand, this patch is only a stop-gap measure to fix
an obvious bug; a more complete patch that switches from a
link-time warning (specific to GNU ld, gcc, and ELF image) to
a compile-time attribute is in the works.
* lib/stdio.in.h (dprintf): Use of link warning on a variadic
function must not take arguments.
* lib/sys_ioctl.in.h (ioctl): Likewise.
* lib/fcntl.in.h (openat): Likewise. Declare extern.
(open): Add a link warning.
Alfred M. Szmidt [Sun, 13 Dec 2009 14:11:14 +0000 (15:11 +0100)]
maint.mk: allow a project to override release-prep commands
* top/maint.mk (alpha, beta, stable): Move release-preparatory
commands into a new rule.
(release-prep): New rule.
(release-prep-hook): New overridable variable.
Eric Blake [Thu, 10 Dec 2009 19:18:31 +0000 (12:18 -0700)]
setenv: relax requirement in light of POSIX ruling
Requiring that {un,}setenv gracefully reject NULL is just a waste
of processing power; POSIX agreed to this argument in
http://austingroupbugs.net/view.php?id=185
so we no longer worry whether a native implementation handles NULL.
* m4/setenv.m4 (gl_FUNC_SETENV_SEPARATE): Test handling of "" but
not NULL.
* tests/test-setenv.c (main): Relax test.
* tests/test-unsetenv.c (main): Likewise.
* doc/posix-functions/setenv.texi (setenv): Document this.
* doc/posix-functions/unsetenv.texi (unsetenv): Likewise.
Jim Meyering [Thu, 10 Dec 2009 11:17:19 +0000 (12:17 +0100)]
mgetgroups: do not write bytes beyond end of malloc'd buffer
* lib/mgetgroups.c: Fix an off-by-one error. When we have no
username, we call getgroups with a one-element-shorter buffer,
but still told it the length was original, max_n_groups.
Eric Blake [Thu, 10 Dec 2009 00:14:12 +0000 (17:14 -0700)]
cloexec: relax license
cloexec is a thin wrapper around fcntl, which is a glibc function
and therefore reasonable for LGPL. Jim Meyering and Bruno Haible
agreed to the change.
* modules/cloexec (Maintainer): Add myself.
(License): Use LGPL, not GPL.
Jim Meyering [Tue, 8 Dec 2009 19:37:50 +0000 (20:37 +0100)]
init.sh: avoid Solaris 10 /bin/sh portability problem
Solaris 10's /bin/sh does not pass '.' arguments 2..N to the
sourced script:
$ printf 'echo "$@"\n' > f; /bin/sh -c '. ./f bar'
$ printf 'echo "$@"\n' > f; bash -c '. ./f bar'
bar
tests/init.sh relied on that, accepting a --set-path=DIR argument,
and two tests used that idiom.
* tests/init.sh: Update suggested usage comments.
(path_prepend_): New function, to be used in place of
the --src-path=DIR option.
Disallow empty strings and strings containing ":".
(setup_): Move PATH-prepending code into path_prepend_.
* tests/test-pread.sh: Adapt to new usage.
* tests/test-xalloc-die.sh: Likewise.
Eric Blake [Tue, 8 Dec 2009 13:13:05 +0000 (06:13 -0700)]
fchdir: fix logic bugs
Configuring with ac_cv_func_fchdir=no on a system that has fchdir
and where open handles directories, just to test out the replacement
capabilities, uncovered an m4 test bug and a link failure on rpl_fstat.
* m4/fchdir.m4 (gl_FUNC_FCHDIR): Fix logic bug.
* tests/test-fchdir.c (main): Enhance test.
* lib/fchdir.c (rpl_fstat): Always provide if fchdir replacement
is in use.
Eric Blake [Tue, 8 Dec 2009 04:08:17 +0000 (21:08 -0700)]
dup2: fix logic bugs
If the platform has dup2, don't register with fchdir if the
destination was -1.
If the platform lacks dup2 (are there any these days?), then don't
close the destination unless the source is valid, make sure errno
is correct, and only register with fchdir on fcntl (since dup is
already overridden to do a registration).
* lib/dup2.c (dup2): Fix logic bugs. Use HAVE_DUP2 rather than
REPLACE_DUP2 to decide when rpl_dup2 is needed.
* m4/dup2.m4 (gl_REPLACE_DUP2): Only define REPLACE_DUP2 when dup2
exists.
(gl_FUNC_DUP2): Drop unneeded AC_SUBST.
Eric Blake [Mon, 7 Dec 2009 17:17:07 +0000 (10:17 -0700)]
unistd-safer: add unit test
Add more unit tests. Meanwhile, fix compilation error on mingw when
testing unistd-safer and fchdir together; and avoid gcc warning on
platforms without setmode.
Eric Blake [Mon, 7 Dec 2009 13:53:59 +0000 (06:53 -0700)]
cloexec: preserve text vs. binary across dup_cloexec
On mingw, dup_cloexec mistakenly converted a text fd into a
binary fd. Cygwin copied the source mode. Most other platforms
don't distinguish between modes.
* lib/cloexec.c (dup_cloexec) [W32]: Query and use translation
mode.
* modules/dup2-tests (Depends-on): Add binary-io.
* modules/cloexec-tests (Depends-on): Likewise.
* tests/test-dup2.c (setmode, is_mode): New helpers.
(main): Add tests that translation mode is preserved.
* tests/test-cloexec.c (setmode, is_mode, main): Likewise.
Reported by Bruno Haible.
Eric Blake [Fri, 4 Dec 2009 21:07:58 +0000 (14:07 -0700)]
mgetgroups: reduce duplicate listings
POSIX doesn't guarantee whether the effective gid is included in
the list of supplementary groups returned by getgroups. On the
other hand, some platforms include the effective gid twice in
the list. Meanwhile, mgetgroups can independently add a duplicate.
Rather than spend a full-blown O(n log n) cleanup, we just remove
the most common forms of duplicate groups with an O(n) pass.
* lib/mgetgroups.c (mgetgroups): Reduce duplicates from the
resulting array.
* tests/test-chown.h (test_chown): Simplify client.
* tests/test-lchown.h (test_lchown): Likewise.
Eric Blake [Sat, 5 Dec 2009 13:39:09 +0000 (06:39 -0700)]
pipe2-safer: new module
pipe2 deserves a *_safer variant. It also makes the code in
pipe.c look simpler.
* modules/pipe2-safer: New file.
* lib/unistd-safer.h (pipe2_safer): New prototype.
* lib/unistd--.h (pipe2): New wrapper.
* lib/pipe-safer.c (pipe2_safer): New function.
* modules/pipe (Depends-on): Add pipe2-safer.
* lib/pipe.c (create_pipe) [WIN32]: Let pipe2_safer do the work.
Eric Blake [Sat, 5 Dec 2009 05:10:44 +0000 (22:10 -0700)]
cloexec: add dup_cloexec
This is needed to enforce correct semantics of mkostemp_safer.
Meanwhile, it is one step closer to providing O_CLOEXEC support
to open, as well as implementing portions of fcntl for mingw.
* lib/cloexec.h (dup_cloexec): New prototype. Add copyright
header and comments.
* lib/cloexec.c (set_cloexec_flag): Add comments.
(dup_cloexec): New function, with mingw implementation borrowed
from...
* lib/w32spawn.h (dup_noinherit): ...here.
* modules/execute (Depends-on): Add cloexec.
* modules/pipe (Depends-on): Likewise.
* modules/cloexec (Depends-on): Add dup2.
* modules/cloexec-tests (Files): New file.
* tests/test-cloexec.c: Likewise.
Eric Blake [Sat, 5 Dec 2009 16:24:54 +0000 (09:24 -0700)]
test-xalloc-die: fix test for mingw
When cross-compiling on mingw to target mingw, argv[0] still includes
a .exe suffix. Rather than worry about ${EXEEXT}, just add a sed
post-process. Also, simplify the script by using tests/init.sh.
* modules/xalloc-die-tests (Files): Add tests/init.sh.
* tests/test-xalloc-die.sh: Rewrite to use init.sh. Strip
directory and .exe suffix off argv[0] output.
Jim Meyering [Sat, 5 Dec 2009 08:35:28 +0000 (09:35 +0100)]
maint.mk: news-check: use grep -E
* top/maint.mk (today): Define a Make variable, not a...
(news-date-check): ...shell variable.
(news-check-regexp): Use the Make variable.
Use grep's -E option. Change the failing diagnostic to mention
the variable, $(news-check-regexp).
(news-check): Rename target from news-date-check. Update sole use.
Eric Blake [Fri, 4 Dec 2009 15:26:23 +0000 (08:26 -0700)]
mgetgroups: add xgetgroups, and avoid ENOSYS failures
ENOSYS implies that there are no supplemental groups, so we can
treat it the same as a return of 0 from getgroups rather than
exposing failure to the user. This in turn fixes a crash in
coreutils' id, which freed an uninitialized pointer.
* lib/mgetgroups.h (xgetgroups): New prototype.
* lib/mgetgroups.c (xgetgroups): New wrapper.
(mgetgroups): Handle ENOSYS.
* modules/mgetgroups (Depends-on): Add realloc.
Reported by Scott Harrison <scott.gnu.2009@scottrix.co.uk>.
Jim Meyering [Tue, 1 Dec 2009 11:06:34 +0000 (12:06 +0100)]
fts: fts_open: do not let an empty string cause immediate failure
This is required in support of GNU rm, for which the command
"rm A '' B" must process and remove both A and B, in spite of
the empty string argument.
* lib/fts.c (fts_open): Do not let the presence of an empty string
cause fts_open to fail immediately. Most fts-using tools must be
able to process all arguments, in order, and can be expected to
diagnose such arguments themselves.
Also, move declaration of local, "len", "down" to initialization.