Hi Jim, Here's a proposal to modernize the 'ftruncate' module. It's triggered by link errors that I got on MSVC 9, because the 'ftruncate' module, while present in gnulib, is marked as 'obsolete' and therefore not included by default. This classification was based on the knowledge that all modern Unix platforms have ftruncate(), mingw too, and MSVC was not yet a portability target.
But now that MSVC is worth considering (because we have a standard 'compile' script for it in GNU, and there is demand from Octave), the actions that make sense are: - drop the old code for SVR2 platforms, - keep the code for native Windows platforms, - un-deprecate the module. Additional improvements: - Include <io.h>, for the declaration of chsize(). - Depend on 'largefile', for automatic use of ftruncate64. 2011-09-21 Bruno Haible <br...@clisp.org> ftruncate: Un-deprecate, concentrate on Win32 support. * modules/ftruncate (Status, Notice): Remove sections. (Depends-on): Add largefile. * m4/ftruncate.m4 (gl_FUNC_FTRUNCATE): Drop failure message on non-mingw platforms. * lib/ftruncate.c: Remove code for the older platforms. For Win32, include <io.h>. * modules/perror-tests (Depends-on): Add ftruncate. * doc/posix-functions/ftruncate.texi: Mention the MSVC problem and the 'ftruncate' module. --- doc/posix-functions/ftruncate.texi.orig Thu Sep 22 01:49:40 2011 +++ doc/posix-functions/ftruncate.texi Thu Sep 22 00:07:10 2011 @@ -4,16 +4,19 @@ POSIX specification:@* @url{http://www.opengroup.org/onlinepubs/9699919799/functions/ftruncate.html} -Gnulib module: --- +Gnulib module: ftruncate Portability problems fixed by Gnulib: @itemize -@end itemize - -Portability problems not fixed by Gnulib: -@itemize +@item +This function is missing on some platforms: +MSVC 9. @item On platforms where @code{off_t} is a 32-bit type, this function is not applicable to arbitrary lengths for files larger than 2 GB. The fix is to use the @code{AC_SYS_LARGEFILE} macro. @end itemize + +Portability problems not fixed by Gnulib: +@itemize +@end itemize --- lib/ftruncate.c.orig Thu Sep 22 01:49:41 2011 +++ lib/ftruncate.c Thu Sep 22 00:10:08 2011 @@ -1,4 +1,4 @@ -/* ftruncate emulations that work on some System V's. +/* ftruncate emulations for native Windows. This file is in the public domain. */ #include <config.h> @@ -6,67 +6,9 @@ /* Specification. */ #include <unistd.h> -#include <sys/types.h> -#include <fcntl.h> +#if HAVE_CHSIZE -#ifdef F_CHSIZE - -int -ftruncate (int fd, off_t length) -{ - return fcntl (fd, F_CHSIZE, length); -} - -#else /* not F_CHSIZE */ -# ifdef F_FREESP - -/* By William Kucharski <kucha...@netcom.com>. */ - -# include <sys/stat.h> -# include <errno.h> - -int -ftruncate (int fd, off_t length) -{ - struct flock fl; - struct stat filebuf; - - if (fstat (fd, &filebuf) < 0) - return -1; - - if (filebuf.st_size < length) - { - /* Extend file length. */ - if (lseek (fd, (length - 1), SEEK_SET) < 0) - return -1; - - /* Write a "0" byte. */ - if (write (fd, "", 1) != 1) - return -1; - } - else - { - - /* Truncate length. */ - - fl.l_whence = 0; - fl.l_len = 0; - fl.l_start = length; - fl.l_type = F_WRLCK; /* write lock on file space */ - - /* This relies on the *undocumented* F_FREESP argument to fcntl, - which truncates the file so that it ends at the position - indicated by fl.l_start. Will minor miracles never cease? */ - - if (fcntl (fd, F_FREESP, &fl) < 0) - return -1; - } - - return 0; -} - -# else /* not F_CHSIZE nor F_FREESP */ -# if HAVE_CHSIZE /* native Windows, e.g. mingw */ +# include <io.h> int ftruncate (int fd, off_t length) @@ -74,17 +16,4 @@ return chsize (fd, length); } -# else /* not F_CHSIZE nor F_FREESP nor HAVE_CHSIZE */ - -# include <errno.h> - -int -ftruncate (int fd, off_t length) -{ - errno = EIO; - return -1; -} - -# endif /* not HAVE_CHSIZE */ -# endif /* not F_FREESP */ -#endif /* not F_CHSIZE */ +#endif --- m4/ftruncate.m4.orig Thu Sep 22 01:49:41 2011 +++ m4/ftruncate.m4 Thu Sep 22 00:11:06 2011 @@ -1,41 +1,18 @@ -# serial 16 +# serial 17 -# See if we need to emulate a missing ftruncate function using fcntl or chsize. +# See if we need to emulate a missing ftruncate function using chsize. # Copyright (C) 2000-2001, 2003-2007, 2009-2011 Free Software Foundation, Inc. # This file is free software; the Free Software Foundation # gives unlimited permission to copy and/or distribute it, # with or without modifications, as long as this notice is preserved. -# FIXME: remove this macro, along with all uses of HAVE_FTRUNCATE in 2012, -# if the check below provokes no more reports. So far, the only report -# arose from a test build of this gnulib module, cross-compiling to mingw: -# <http://thread.gmane.org/gmane.comp.lib.gnulib.bugs/9203> -# Now (in 2010), MSVC has been raised as a possible target: -# <http://thread.gmane.org/gmane.comp.lib.gnulib.bugs/21394/focus=21396> - AC_DEFUN([gl_FUNC_FTRUNCATE], [ AC_REQUIRE([gl_UNISTD_H_DEFAULTS]) - AC_REQUIRE([AC_CANONICAL_HOST]) AC_CHECK_FUNCS_ONCE([ftruncate]) if test $ac_cv_func_ftruncate = no; then HAVE_FTRUNCATE=0 - case "$host_os" in - mingw*) - # Yes, we know mingw lacks ftruncate. - ;; - *) - # If someone lacks ftruncate, make configure fail, and request - # a bug report to inform us about it. - if test x"$SKIP_FTRUNCATE_CHECK" != xyes; then - AC_MSG_FAILURE([Your system lacks the ftruncate function. - Please report this, along with the output of "uname -a", to the - bug-coreut...@gnu.org mailing list. To continue past this point, - rerun configure with SKIP_FTRUNCATE_CHECK=yes. - E.g., ./configure SKIP_FTRUNCATE_CHECK=yes]) - fi - esac fi ]) --- modules/ftruncate.orig Thu Sep 22 01:49:41 2011 +++ modules/ftruncate Thu Sep 22 00:07:12 2011 @@ -1,18 +1,13 @@ Description: ftruncate() function: truncate an open file to a specified length. -Status: -obsolete - -Notice: -This module is obsolete. - Files: lib/ftruncate.c m4/ftruncate.m4 Depends-on: unistd +largefile configure.ac: gl_FUNC_FTRUNCATE --- modules/perror-tests.orig Thu Sep 22 01:49:41 2011 +++ modules/perror-tests Wed Sep 21 23:32:33 2011 @@ -8,6 +8,7 @@ Depends-on: dup2 +ftruncate strerror configure.ac: -- In memoriam Orlando Letelier <http://en.wikipedia.org/wiki/Orlando_Letelier>