summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJim Meyering <meyering@redhat.com>2009-11-25 14:33:30 +0100
committerJim Meyering <meyering@redhat.com>2009-11-25 14:56:06 +0100
commitf84e1a91fb3c43ba06567cbaeaf641135170d35a (patch)
treea50ef075092438482fd2edf15172a140fc35d5e1
parentef77cd53d352251ed2bc7995ac6951f29f091bad (diff)
downloadgnulib-f84e1a91fb3c43ba06567cbaeaf641135170d35a.tar.gz
pread: new module
* modules/pread: New file. * lib/pread.c (pread): New file. * m4/pread.m4: Likewise. * lib/unistd.in.h (pread): Define/declare. * m4/unistd_h.m4 (gl_UNISTD_H_DEFAULTS): Define defaults. * modules/unistd (Makefile.am): Substitute witnesses. * doc/posix-functions/pread.texi (pread): Update. * MODULES.html.sh: Add pread.
-rw-r--r--ChangeLog11
-rwxr-xr-xMODULES.html.sh1
-rw-r--r--doc/posix-functions/pread.texi6
-rw-r--r--lib/pread.c64
-rw-r--r--lib/unistd.in.h20
-rw-r--r--m4/pread.m415
-rw-r--r--m4/unistd_h.m43
-rw-r--r--modules/pread24
-rw-r--r--modules/unistd3
9 files changed, 144 insertions, 3 deletions
diff --git a/ChangeLog b/ChangeLog
index c0ce45b88c..c255af0348 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,16 @@
2009-11-25 Jim Meyering <meyering@redhat.com>
+ pread: new module
+ * modules/pread: New file.
+ * lib/unistd.in.h (pread): Define/declare.
+ * lib/pread.c (pread): New file.
+ * m4/unistd_h.m4 (gl_UNISTD_H_DEFAULTS): Define defaults.
+ * modules/unistd (Makefile.am): Substitute witnesses.
+ * doc/posix-functions/pread.texi (pread): Update.
+ * MODULES.html.sh: Add pread.
+
+2009-11-25 Jim Meyering <meyering@redhat.com>
+
tests/init.sh: new file to be used via most *.sh tests
* tests/init.sh: New file.
diff --git a/MODULES.html.sh b/MODULES.html.sh
index 7fc55dd8aa..9d5664a2b9 100755
--- a/MODULES.html.sh
+++ b/MODULES.html.sh
@@ -1345,6 +1345,7 @@ h_errno
index
makecontext
mktemp
+pread
pthread_attr_getstackaddr
pthread_attr_setstackaddr
rindex
diff --git a/doc/posix-functions/pread.texi b/doc/posix-functions/pread.texi
index d1d6403a85..a11e36eac7 100644
--- a/doc/posix-functions/pread.texi
+++ b/doc/posix-functions/pread.texi
@@ -8,11 +8,11 @@ Gnulib module: ---
Portability problems fixed by Gnulib:
@itemize
+@item
+This function is missing on some platforms:
+mingw, BeOS.
@end itemize
Portability problems not fixed by Gnulib:
@itemize
-@item
-This function is missing on some platforms:
-mingw, BeOS.
@end itemize
diff --git a/lib/pread.c b/lib/pread.c
new file mode 100644
index 0000000000..632b91497c
--- /dev/null
+++ b/lib/pread.c
@@ -0,0 +1,64 @@
+/* replacement pread function
+ Copyright (C) 2009 Free Software Foundation, Inc.
+
+ This program is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 3 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>. */
+
+#include <config.h>
+
+/* Specification. */
+#include <unistd.h>
+
+#include <errno.h>
+
+#define __libc_lseek(f,o,w) lseek (f, o, w)
+#define __set_errno(Val) errno = (Val)
+
+/* pread substitute for systems that the function, such as mingw32 and BeOS. */
+/* The following is identical to the function from glibc's
+ sysdeps/posix/pread.c */
+
+/* Note: This implementation of pread is not multithread-safe. */
+
+ssize_t
+pread (int fd, void *buf, size_t nbyte, off_t offset)
+{
+ /* Since we must not change the file pointer preserve the value so that
+ we can restore it later. */
+ int save_errno;
+ ssize_t result;
+ off_t old_offset = __libc_lseek (fd, 0, SEEK_CUR);
+ if (old_offset == (off_t) -1)
+ return -1;
+
+ /* Set to wanted position. */
+ if (__libc_lseek (fd, offset, SEEK_SET) == (off_t) -1)
+ return -1;
+
+ /* Write out the data. */
+ result = __libc_read (fd, buf, nbyte);
+
+ /* Now we have to restore the position. If this fails we have to
+ return this as an error. But if the writing also failed we
+ return this error. */
+ save_errno = errno;
+ if (__libc_lseek (fd, old_offset, SEEK_SET) == (off_t) -1)
+ {
+ if (result == -1)
+ __set_errno (save_errno);
+ return -1;
+ }
+ __set_errno (save_errno);
+
+ return result;
+}
diff --git a/lib/unistd.in.h b/lib/unistd.in.h
index 9a9a671115..13d1bbac96 100644
--- a/lib/unistd.in.h
+++ b/lib/unistd.in.h
@@ -664,6 +664,26 @@ extern int pipe2 (int fd[2], int flags);
#endif
+#if @GNULIB_PREAD@
+# if @REPLACE_PREAD@
+# define pread rpl_pread
+# endif
+/* Read at most BUFSIZE bytes from FD into BUF, starting at OFFSET.
+ Return the number of bytes placed into BUF if successful, otherwise
+ set errno and return -1. 0 indicates EOF. See the POSIX:2001
+ specification <http://www.opengroup.org/susv3xsh/pread.html>. */
+# if !@HAVE_PREAD@ || @REPLACE_PREAD@
+ extern ssize_t pread (int fd, char *buf, size_t bufsize, off_t offset);
+# endif
+#elif defined GNULIB_POSIXCHECK
+# undef pread
+# define pread(f,b,s,o) \
+ (GL_LINK_WARNING ("pread is unportable - " \
+ "use gnulib module pread for portability"), \
+ pread (f, b, s, o))
+#endif
+
+
#if @GNULIB_READLINK@
# if @REPLACE_READLINK@
# define readlink rpl_readlink
diff --git a/m4/pread.m4 b/m4/pread.m4
new file mode 100644
index 0000000000..c281362df9
--- /dev/null
+++ b/m4/pread.m4
@@ -0,0 +1,15 @@
+# pread.m4 serial 1
+dnl Copyright (C) 2009 Free Software Foundation, Inc.
+dnl This file is free software; the Free Software Foundation
+dnl gives unlimited permission to copy and/or distribute it,
+dnl with or without modifications, as long as this notice is preserved.
+
+AC_DEFUN([gl_FUNC_PREAD],
+[
+ AC_REQUIRE([gl_UNISTD_H_DEFAULTS])
+ AC_CHECK_FUNCS_ONCE([pread])
+ if test $ac_cv_func_pread = no; then
+ HAVE_PREAD=0
+ AC_LIBOBJ([pread])
+ fi
+])
diff --git a/m4/unistd_h.m4 b/m4/unistd_h.m4
index 0a5b5d5a75..cb50d50e00 100644
--- a/m4/unistd_h.m4
+++ b/m4/unistd_h.m4
@@ -56,6 +56,7 @@ AC_DEFUN([gl_UNISTD_H_DEFAULTS],
GNULIB_LINKAT=0; AC_SUBST([GNULIB_LINKAT])
GNULIB_LSEEK=0; AC_SUBST([GNULIB_LSEEK])
GNULIB_PIPE2=0; AC_SUBST([GNULIB_PIPE2])
+ GNULIB_PREAD=0; AC_SUBST([GNULIB_PREAD])
GNULIB_READLINK=0; AC_SUBST([GNULIB_READLINK])
GNULIB_READLINKAT=0; AC_SUBST([GNULIB_READLINKAT])
GNULIB_RMDIR=0; AC_SUBST([GNULIB_RMDIR])
@@ -87,6 +88,7 @@ AC_DEFUN([gl_UNISTD_H_DEFAULTS],
HAVE_LINK=1; AC_SUBST([HAVE_LINK])
HAVE_LINKAT=1; AC_SUBST([HAVE_LINKAT])
HAVE_PIPE2=1; AC_SUBST([HAVE_PIPE2])
+ HAVE_PREAD=1; AC_SUBST([HAVE_PREAD])
HAVE_READLINK=1; AC_SUBST([HAVE_READLINK])
HAVE_READLINKAT=1; AC_SUBST([HAVE_READLINKAT])
HAVE_SLEEP=1; AC_SUBST([HAVE_SLEEP])
@@ -111,6 +113,7 @@ AC_DEFUN([gl_UNISTD_H_DEFAULTS],
REPLACE_LINK=0; AC_SUBST([REPLACE_LINK])
REPLACE_LINKAT=0; AC_SUBST([REPLACE_LINKAT])
REPLACE_LSEEK=0; AC_SUBST([REPLACE_LSEEK])
+ REPLACE_PREAD=0; AC_SUBST([REPLACE_PREAD])
REPLACE_READLINK=0; AC_SUBST([REPLACE_READLINK])
REPLACE_RMDIR=0; AC_SUBST([REPLACE_RMDIR])
REPLACE_SLEEP=0; AC_SUBST([REPLACE_SLEEP])
diff --git a/modules/pread b/modules/pread
new file mode 100644
index 0000000000..06039e64dd
--- /dev/null
+++ b/modules/pread
@@ -0,0 +1,24 @@
+Description:
+pread() function: read without changing file offset
+
+Files:
+lib/pread.c
+m4/pread.m4
+
+Depends-on:
+unistd
+
+configure.ac:
+gl_FUNC_PREAD
+gl_UNISTD_MODULE_INDICATOR([pread])
+
+Makefile.am:
+
+Include:
+<unistd.h>
+
+License:
+LGPLv2+
+
+Maintainer:
+Jim Meyering
diff --git a/modules/unistd b/modules/unistd
index 1282e5380c..7a5500101e 100644
--- a/modules/unistd
+++ b/modules/unistd
@@ -49,6 +49,7 @@ unistd.h: unistd.in.h
-e 's|@''GNULIB_LINKAT''@|$(GNULIB_LINKAT)|g' \
-e 's|@''GNULIB_LSEEK''@|$(GNULIB_LSEEK)|g' \
-e 's|@''GNULIB_PIPE2''@|$(GNULIB_PIPE2)|g' \
+ -e 's|@''GNULIB_PREAD''@|$(GNULIB_PREAD)|g' \
-e 's|@''GNULIB_READLINK''@|$(GNULIB_READLINK)|g' \
-e 's|@''GNULIB_READLINKAT''@|$(GNULIB_READLINKAT)|g' \
-e 's|@''GNULIB_RMDIR''@|$(GNULIB_RMDIR)|g' \
@@ -79,6 +80,7 @@ unistd.h: unistd.in.h
-e 's|@''HAVE_LINK''@|$(HAVE_LINK)|g' \
-e 's|@''HAVE_LINKAT''@|$(HAVE_LINKAT)|g' \
-e 's|@''HAVE_PIPE2''@|$(HAVE_PIPE2)|g' \
+ -e 's|@''HAVE_PREAD''@|$(HAVE_PREAD)|g' \
-e 's|@''HAVE_READLINK''@|$(HAVE_READLINK)|g' \
-e 's|@''HAVE_READLINKAT''@|$(HAVE_READLINKAT)|g' \
-e 's|@''HAVE_SLEEP''@|$(HAVE_SLEEP)|g' \
@@ -103,6 +105,7 @@ unistd.h: unistd.in.h
-e 's|@''REPLACE_LINK''@|$(REPLACE_LINK)|g' \
-e 's|@''REPLACE_LINKAT''@|$(REPLACE_LINKAT)|g' \
-e 's|@''REPLACE_LSEEK''@|$(REPLACE_LSEEK)|g' \
+ -e 's|@''REPLACE_PREAD''@|$(REPLACE_PREAD)|g' \
-e 's|@''REPLACE_READLINK''@|$(REPLACE_READLINK)|g' \
-e 's|@''REPLACE_RMDIR''@|$(REPLACE_RMDIR)|g' \
-e 's|@''REPLACE_SLEEP''@|$(REPLACE_SLEEP)|g' \