aboutsummaryrefslogtreecommitdiff
path: root/expat/xmlwf/unixfilemap.c
diff options
context:
space:
mode:
authormarha <marha@users.sourceforge.net>2013-10-18 13:29:37 +0200
committermarha <marha@users.sourceforge.net>2013-10-18 13:29:37 +0200
commit72483d25d0f41d48a8e6bdc83ab57c971cfb47b4 (patch)
treec8aa56f1e7dc24ce93287592c07a5f354eb6f86d /expat/xmlwf/unixfilemap.c
parentb821fe861f33d7e8a39fe4c7d885b7ff379dc476 (diff)
parent5880b059e9a156336daf32a73bed72def6ba90f2 (diff)
downloadvcxsrv-72483d25d0f41d48a8e6bdc83ab57c971cfb47b4.tar.gz
vcxsrv-72483d25d0f41d48a8e6bdc83ab57c971cfb47b4.tar.bz2
vcxsrv-72483d25d0f41d48a8e6bdc83ab57c971cfb47b4.zip
Merge remote-tracking branch 'origin/released'
* origin/released: Added expat-2.1.0
Diffstat (limited to 'expat/xmlwf/unixfilemap.c')
-rw-r--r--expat/xmlwf/unixfilemap.c65
1 files changed, 65 insertions, 0 deletions
diff --git a/expat/xmlwf/unixfilemap.c b/expat/xmlwf/unixfilemap.c
new file mode 100644
index 000000000..93adce32e
--- /dev/null
+++ b/expat/xmlwf/unixfilemap.c
@@ -0,0 +1,65 @@
+/* Copyright (c) 1998, 1999 Thai Open Source Software Center Ltd
+ See the file COPYING for copying permission.
+*/
+
+#include <sys/types.h>
+#include <sys/mman.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <errno.h>
+#include <string.h>
+#include <stdio.h>
+#include <unistd.h>
+
+#ifndef MAP_FILE
+#define MAP_FILE 0
+#endif
+
+#include "filemap.h"
+
+int
+filemap(const char *name,
+ void (*processor)(const void *, size_t, const char *, void *arg),
+ void *arg)
+{
+ int fd;
+ size_t nbytes;
+ struct stat sb;
+ void *p;
+
+ fd = open(name, O_RDONLY);
+ if (fd < 0) {
+ perror(name);
+ return 0;
+ }
+ if (fstat(fd, &sb) < 0) {
+ perror(name);
+ close(fd);
+ return 0;
+ }
+ if (!S_ISREG(sb.st_mode)) {
+ close(fd);
+ fprintf(stderr, "%s: not a regular file\n", name);
+ return 0;
+ }
+
+ nbytes = sb.st_size;
+ /* mmap fails for zero length files */
+ if (nbytes == 0) {
+ static const char c = '\0';
+ processor(&c, 0, name, arg);
+ close(fd);
+ return 1;
+ }
+ p = (void *)mmap((caddr_t)0, (size_t)nbytes, PROT_READ,
+ MAP_FILE|MAP_PRIVATE, fd, (off_t)0);
+ if (p == (void *)-1) {
+ perror(name);
+ close(fd);
+ return 0;
+ }
+ processor(p, nbytes, name, arg);
+ munmap((caddr_t)p, nbytes);
+ close(fd);
+ return 1;
+}