[vlc-devel] [PATCH] compat: add pread
Steve Lhomme
robux4 at videolabs.io
Thu Jan 7 14:29:02 CET 2016
---
compat/pread.c | 53 ++++++++++++++++++++++++++++++++++++++++++++++++++++
configure.ac | 2 +-
include/vlc_fixups.h | 4 ++++
src/misc/block.c | 21 ---------------------
4 files changed, 58 insertions(+), 22 deletions(-)
create mode 100644 compat/pread.c
diff --git a/compat/pread.c b/compat/pread.c
new file mode 100644
index 0000000..aad1813
--- /dev/null
+++ b/compat/pread.c
@@ -0,0 +1,53 @@
+/*****************************************************************************
+ * pread.c: POSIX C pread() replacement
+ *****************************************************************************
+ * Copyright © 2016 VLC authors and VideoLAN
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
+ *****************************************************************************/
+
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif
+
+#ifdef _WIN32
+#include <windows.h>
+
+ssize_t pread (int fd, void *buf, size_t count, off_t offset)
+{
+ HANDLE handle = (HANDLE)(intptr_t)_get_osfhandle (fd);
+ if (handle == INVALID_HANDLE_VALUE)
+ return -1;
+
+ OVERLAPPED olap; olap.Offset = offset; olap.OffsetHigh = (offset >> 32);
+ DWORD written;
+ /* This braindead API will override the file pointer even if we specify
+ * an explicit read offset... So do not expect this to mix well with
+ * regular read() calls. */
+ if (ReadFile (handle, buf, count, &written, &olap))
+ return written;
+ return -1;
+}
+#else
+#include <io.h>
+
+ssize_t pread (int fd, void *buf, size_t count, off_t offset)
+{
+ if ( lseek (fd, offset, SEEK_SET) != offset ) {
+ return -1;
+ }
+ return read( fd, buf, count );
+}
+#endif
diff --git a/configure.ac b/configure.ac
index 7286e27..b8a674b 100644
--- a/configure.ac
+++ b/configure.ac
@@ -565,7 +565,7 @@ need_libc=false
dnl Check for usual libc functions
AC_CHECK_DECLS([nanosleep],,,[#include <time.h>])
AC_CHECK_FUNCS([daemon fcntl fstatvfs fork getenv getpwuid_r isatty lstat memalign mkostemp mmap open_memstream openat pread posix_fadvise posix_madvise setlocale stricmp strnicmp strptime uselocale pthread_cond_timedwait_monotonic_np pthread_condattr_setclock])
-AC_REPLACE_FUNCS([atof atoll dirfd fdopendir ffsll flockfile fsync getdelim getpid lldiv nrand48 poll posix_memalign rewind setenv strcasecmp strcasestr strdup strlcpy strndup strnlen strnstr strsep strtof strtok_r strtoll swab tdestroy timegm timespec_get strverscmp])
+AC_REPLACE_FUNCS([atof atoll dirfd fdopendir ffsll flockfile fsync getdelim getpid lldiv nrand48 poll posix_memalign pread rewind setenv strcasecmp strcasestr strdup strlcpy strndup strnlen strnstr strsep strtof strtok_r strtoll swab tdestroy timegm timespec_get strverscmp])
AC_CHECK_FUNCS(fdatasync,,
[AC_DEFINE(fdatasync, fsync, [Alias fdatasync() to fsync() if missing.])
])
diff --git a/include/vlc_fixups.h b/include/vlc_fixups.h
index c865bee..ee09c18 100644
--- a/include/vlc_fixups.h
+++ b/include/vlc_fixups.h
@@ -112,6 +112,10 @@ ssize_t getdelim (char **, size_t *, int, FILE *);
ssize_t getline (char **, size_t *, FILE *);
#endif
+#ifndef HAVE_PREAD
+ssize_t pread (int fd, void *buf, size_t count, off_t offset);
+#endif
+
#ifndef HAVE_REWIND
void rewind (FILE *);
#endif
diff --git a/src/misc/block.c b/src/misc/block.c
index 610659d..6306bf0 100644
--- a/src/misc/block.c
+++ b/src/misc/block.c
@@ -377,27 +377,6 @@ block_t *block_shm_Alloc (void *addr, size_t length)
#endif
-#ifdef _WIN32
-# include <io.h>
-
-static
-ssize_t pread (int fd, void *buf, size_t count, off_t offset)
-{
- HANDLE handle = (HANDLE)(intptr_t)_get_osfhandle (fd);
- if (handle == INVALID_HANDLE_VALUE)
- return -1;
-
- OVERLAPPED olap; olap.Offset = offset; olap.OffsetHigh = (offset >> 32);
- DWORD written;
- /* This braindead API will override the file pointer even if we specify
- * an explicit read offset... So do not expect this to mix well with
- * regular read() calls. */
- if (ReadFile (handle, buf, count, &written, &olap))
- return written;
- return -1;
-}
-#endif
-
/**
* Loads a file into a block of memory through a file descriptor.
* If possible a private file mapping is created. Otherwise, the file is read
--
2.6.3
More information about the vlc-devel
mailing list