summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorVincent Sanders <vince@kyllikki.org>2015-04-02 14:51:39 (GMT)
committer Vincent Sanders <vince@kyllikki.org>2015-04-02 14:51:39 (GMT)
commit2d6282f46bf44ea82ea9cc562f62af4456714052 (patch)
tree9366c5be3e0e91b219f55fc99f2fed0e7ef66861
parentee700511bdd5af06304382abf0d6ff4cc9bd43f0 (diff)
downloadlibnsutils-2d6282f46bf44ea82ea9cc562f62af4456714052.tar.gz
libnsutils-2d6282f46bf44ea82ea9cc562f62af4456714052.tar.bz2
Added pread and pwrite wrappers.
This adds wrappers for the pread and pwrite standard calls with fixed semantics across all supported platforms. The main variation (so far) is some platforms not having the calls at all and AmigaOS not allowing seeks beyond existing extents.
-rw-r--r--Makefile9
-rw-r--r--include/nsutils/time.h2
-rw-r--r--include/nsutils/unistd.h55
-rw-r--r--src/Makefile2
-rw-r--r--src/unistd.c67
5 files changed, 132 insertions, 3 deletions
diff --git a/Makefile b/Makefile
index fa44370..10b2120 100644
--- a/Makefile
+++ b/Makefile
@@ -1,6 +1,12 @@
+#!/bin/make
+#
+# Makefile for libnsutils
+#
+# Copyright 2014-1015 Vincent Sanders <vince@netsurf-browser.org>
+
# Component settings
COMPONENT := nsutils
-COMPONENT_VERSION := 0.0.1
+COMPONENT_VERSION := 0.0.2
# Default to a static library
COMPONENT_TYPE ?= lib-static
@@ -16,6 +22,7 @@ TESTRUNNER = test/runtest.sh $(BUILDDIR) $(EXEEXT)
WARNFLAGS := -Wall -W -Wundef -Wpointer-arith -Wcast-align \
-Wwrite-strings -Wstrict-prototypes -Wmissing-prototypes \
-Wmissing-declarations -Wnested-externs
+
CFLAGS := -I$(CURDIR)/include/ -I$(CURDIR)/src $(WARNFLAGS) $(CFLAGS)
ifneq ($(GCCVER),2)
CFLAGS := $(CFLAGS) -std=c99
diff --git a/include/nsutils/time.h b/include/nsutils/time.h
index 715d3e4..db60914 100644
--- a/include/nsutils/time.h
+++ b/include/nsutils/time.h
@@ -9,7 +9,7 @@
/**
* \file
- * Base64 encoding and decoding interface.
+ * Time related operations.
*/
#ifndef NSUTILS_TIME_H_
diff --git a/include/nsutils/unistd.h b/include/nsutils/unistd.h
new file mode 100644
index 0000000..be11382
--- a/dev/null
+++ b/include/nsutils/unistd.h
@@ -0,0 +1,55 @@
+/*
+ * Copyright 2015 Vincent Sanders <vince@netsurf-browser.org>
+ *
+ * This file is part of libnsutils.
+ *
+ * Licensed under the MIT License,
+ * http://www.opensource.org/licenses/mit-license.php
+ */
+
+/**
+ * \file
+ * Time related operations.
+ */
+
+#ifndef NSUTILS_UNISTD_H_
+#define NSUTILS_UNISTD_H_
+
+/**
+ * Perform a write operation at a specific offset
+ *
+ * This writes the data into the file specifid by teh file handle at teh given
+ * offset. The offset may be beyond the existing file extent.
+ *
+ * This provides a uniform interface to the pwrite operation without system
+ * specific implementation details.
+ *
+ * \note The write pointer on the fd may be moved by this operation which
+ * differs from the posix version.
+ *
+ * \param fd The file descriptor.
+ * \param buf The data to write.
+ * \param count The length of data in \a buf to write.
+ * \param offset The offset into the file to write the data.
+ * \return the number of bytes written or -1 on error and errno set.
+ */
+ssize_t nsu_pwrite(int fd, const void *buf, size_t count, off_t offset);
+
+/**
+ * Perform a read from a specific offset.
+ *
+ * This provides a uniform interface to the POSIX pread operation without
+ * system specific implementation details.
+ *
+ * \note The read pointer on the fd may be moved by this operation which
+ * differs from the POSIX version.
+ *
+ * \param fd The file descriptor.
+ * \param buf The buffer to place the data into.
+ * \param count The length of data to read.
+ * \param offset The offset into the file to read the data from.
+ * \return the number of bytes read or -1 on error and errno set.
+ */
+ssize_t nsu_pread(int fd, void *buf, size_t count, off_t offset);
+
+#endif
diff --git a/src/Makefile b/src/Makefile
index 28bfc5f..b4fd96a 100644
--- a/src/Makefile
+++ b/src/Makefile
@@ -1,3 +1,3 @@
-DIR_SOURCES := base64.c time.c
+DIR_SOURCES := base64.c time.c unistd.c
include $(NSBUILD)/Makefile.subdir
diff --git a/src/unistd.c b/src/unistd.c
new file mode 100644
index 0000000..219e9ec
--- a/dev/null
+++ b/src/unistd.c
@@ -0,0 +1,67 @@
+/*
+ * Copyright 2015 Vincent Sanders <vince@netsurf-browser.org>
+ *
+ * This file is part of libnsutils.
+ *
+ * Licensed under the MIT License,
+ * http://www.opensource.org/licenses/mit-license.php
+ */
+
+/**
+ * \file
+ * unistd style operations.
+ */
+
+#include <unistd.h>
+#include <errno.h>
+
+#include "nsutils/unistd.h"
+
+/* exported interface documented in nsutils/unistd.h */
+ssize_t nsu_pwrite(int fd, const void *buf, size_t count, off_t offset)
+{
+#if defined(__riscos) || defined(__amiga)
+ off_t sk;
+
+ sk = lseek(fd, offset, SEEK_SET);
+ if (sk == (off_t)-1) {
+#if defined(__amiga)
+ /* amigaos cannot seek past the end of the existing file so use
+ * ftruncate to make the file long enough and retry the seek.
+ */
+ int ret;
+ if (errno == ESPIPE) {
+ ret = ftruncate(fd, offset);
+ if (ret == -1) {
+ return -1;
+ }
+ sk = lseek(fd, offset, SEEK_SET);
+ if (sk == (off_t)-1) {
+ return -1;
+ }
+ } else
+#endif
+ return -1;
+ }
+ return write(fd, buf, count);
+#else
+ return pwrite(fd, buf, count, offset);
+#endif
+}
+
+/* exported interface documented in nsutils/unistd.h */
+ssize_t nsu_pread(int fd, void *buf, size_t count, off_t offset)
+{
+#if defined(__riscos)
+ off_t sk;
+
+ sk = lseek(fd, offset, SEEK_SET);
+ if (sk == -1) {
+ return (off_t)-1;
+ }
+ return read(fd, buf, count);
+#else
+ return pread(fd, buf, count, offset);
+#endif
+
+}