Apache HTTPD
unixfilemap.c
Go to the documentation of this file.
1/*
2 __ __ _
3 ___\ \/ /_ __ __ _| |_
4 / _ \\ /| '_ \ / _` | __|
5 | __// \| |_) | (_| | |_
6 \___/_/\_\ .__/ \__,_|\__|
7 |_| XML parser
8
9 Copyright (c) 1997-2000 Thai Open Source Software Center Ltd
10 Copyright (c) 2000 Clark Cooper <[email protected]>
11 Copyright (c) 2001-2002 Fred L. Drake, Jr. <[email protected]>
12 Copyright (c) 2006 Karl Waclawek <[email protected]>
13 Copyright (c) 2016-2017 Sebastian Pipping <[email protected]>
14 Copyright (c) 2017 Rhodri James <[email protected]>
15 Licensed under the MIT license:
16
17 Permission is hereby granted, free of charge, to any person obtaining
18 a copy of this software and associated documentation files (the
19 "Software"), to deal in the Software without restriction, including
20 without limitation the rights to use, copy, modify, merge, publish,
21 distribute, sublicense, and/or sell copies of the Software, and to permit
22 persons to whom the Software is furnished to do so, subject to the
23 following conditions:
24
25 The above copyright notice and this permission notice shall be included
26 in all copies or substantial portions of the Software.
27
28 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
29 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
30 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
31 NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
32 DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
33 OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
34 USE OR OTHER DEALINGS IN THE SOFTWARE.
35*/
36
37#include <sys/types.h>
38#include <sys/mman.h>
39#include <sys/stat.h>
40#include <fcntl.h>
41#include <errno.h>
42#include <string.h>
43#include <stdio.h>
44#include <unistd.h>
45
46#ifndef MAP_FILE
47# define MAP_FILE 0
48#endif
49
50#include "xmltchar.h"
51#include "filemap.h"
52
53#ifdef XML_UNICODE_WCHAR_T
54# define XML_FMT_STR "ls"
55#else
56# define XML_FMT_STR "s"
57#endif
58
59int
61 void (*processor)(const void *, size_t, const tchar *, void *arg),
62 void *arg) {
63 int fd;
64 size_t nbytes;
65 struct stat sb;
66 void *p;
67
69 if (fd < 0) {
71 return 0;
72 }
73 if (fstat(fd, &sb) < 0) {
75 close(fd);
76 return 0;
77 }
78 if (! S_ISREG(sb.st_mode)) {
79 close(fd);
80 fprintf(stderr, "%" XML_FMT_STR ": not a regular file\n", name);
81 return 0;
82 }
83 if (sb.st_size > XML_MAX_CHUNK_LEN) {
84 close(fd);
85 return 2; /* Cannot be passed to XML_Parse in one go */
86 }
87
88 nbytes = sb.st_size;
89 /* mmap fails for zero length files */
90 if (nbytes == 0) {
91 static const char c = '\0';
92 processor(&c, 0, name, arg);
93 close(fd);
94 return 1;
95 }
96 p = (void *)mmap((void *)0, (size_t)nbytes, PROT_READ, MAP_FILE | MAP_PRIVATE,
97 fd, (off_t)0);
98 if (p == (void *)-1) {
100 close(fd);
101 return 0;
102 }
104 munmap((void *)p, nbytes);
105 close(fd);
106 return 1;
107}
#define XML_MAX_CHUNK_LEN
Definition filemap.h:45
void const char * arg
Definition http_vhost.h:63
apr_file_t * fd
apr_size_t size
void apr_size_t * nbytes
apr_vformatter_buff_t * c
Definition apr_lib.h:175
apr_pool_t * p
Definition md_event.c:32
#define S_ISREG(m)
Definition readfilemap.c:71
char * name
#define MAP_FILE
Definition unixfilemap.c:47
int filemap(const char *name, void(*processor)(const void *, size_t, const char *, void *arg), void *arg)
Definition unixfilemap.c:60
#define XML_FMT_STR
Definition unixfilemap.c:56
#define tperror
Definition xmltchar.h:72
#define tchar
Definition xmltchar.h:76
#define topen
Definition xmltchar.h:73