initial load of upstream version 1.06.32
[xmlrpc-c] / lib / expat / xmlwf / unixfilemap.c
1 /*
2 Copyright (c) 1998, 1999 Thai Open Source Software Center Ltd
3 See the file copying.txt for copying permission.
4 */
5
6 #include <sys/types.h>
7 #include <sys/mman.h>
8 #include <sys/stat.h>
9 #include <fcntl.h>
10 #include <errno.h>
11 #include <string.h>
12 #include <stdio.h>
13
14 #ifndef MAP_FILE
15 #define MAP_FILE 0
16 #endif
17
18 #include "filemap.h"
19
20 int filemap(const char *name,
21             void (*processor)(const void *, size_t, const char *, void *arg),
22             void *arg)
23 {
24   int fd;
25   size_t nbytes;
26   struct stat sb;
27   void *p;
28
29   fd = open(name, O_RDONLY);
30   if (fd < 0) {
31     perror(name);
32     return 0;
33   }
34   if (fstat(fd, &sb) < 0) {
35     perror(name);
36     close(fd);
37     return 0;
38   }
39   if (!S_ISREG(sb.st_mode)) {
40     close(fd);
41     fprintf(stderr, "%s: not a regular file\n", name);
42     return 0;
43   }
44   
45   nbytes = sb.st_size;
46   p = (void *)mmap((caddr_t)0, (size_t)nbytes, PROT_READ,
47                    MAP_FILE|MAP_PRIVATE, fd, (off_t)0);
48   if (p == (void *)-1) {
49     perror(name);
50     close(fd);
51     return 0;
52   }
53   processor(p, nbytes, name, arg);
54   munmap((caddr_t)p, nbytes);
55   close(fd);
56   return 1;
57 }