xref: /freebsd/contrib/expat/xmlwf/readfilemap.c (revision 5dae51da3da0cc94d17bd67b308fad304ebec7e0)
1 /* Copyright (c) 1998, 1999 Thai Open Source Software Center Ltd
2    See the file COPYING for copying permission.
3 */
4 
5 #include <sys/types.h>
6 #include <sys/stat.h>
7 #include <fcntl.h>
8 #include <stdlib.h>
9 #include <stdio.h>
10 
11 /* Functions close(2) and read(2) */
12 #ifdef __WATCOMC__
13 #ifndef __LINUX__
14 #include <io.h>
15 #else
16 #include <unistd.h>
17 #endif
18 #else
19 # if !defined(WIN32) && !defined(_WIN32) && !defined(_WIN64)
20 #  include <unistd.h>
21 # endif
22 #endif
23 
24 #ifndef S_ISREG
25 #ifndef S_IFREG
26 #define S_IFREG _S_IFREG
27 #endif
28 #ifndef S_IFMT
29 #define S_IFMT _S_IFMT
30 #endif
31 #define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
32 #endif /* not S_ISREG */
33 
34 #ifndef O_BINARY
35 #ifdef _O_BINARY
36 #define O_BINARY _O_BINARY
37 #else
38 #define O_BINARY 0
39 #endif
40 #endif
41 
42 #include "filemap.h"
43 
44 int
45 filemap(const char *name,
46         void (*processor)(const void *, size_t, const char *, void *arg),
47         void *arg)
48 {
49   size_t nbytes;
50   int fd;
51   int n;
52   struct stat sb;
53   void *p;
54 
55   fd = open(name, O_RDONLY|O_BINARY);
56   if (fd < 0) {
57     perror(name);
58     return 0;
59   }
60   if (fstat(fd, &sb) < 0) {
61     perror(name);
62     close(fd);
63     return 0;
64   }
65   if (!S_ISREG(sb.st_mode)) {
66     fprintf(stderr, "%s: not a regular file\n", name);
67     close(fd);
68     return 0;
69   }
70   nbytes = sb.st_size;
71   /* malloc will return NULL with nbytes == 0, handle files with size 0 */
72   if (nbytes == 0) {
73     static const char c = '\0';
74     processor(&c, 0, name, arg);
75     close(fd);
76     return 1;
77   }
78   p = malloc(nbytes);
79   if (!p) {
80     fprintf(stderr, "%s: out of memory\n", name);
81     close(fd);
82     return 0;
83   }
84   n = read(fd, p, nbytes);
85   if (n < 0) {
86     perror(name);
87     free(p);
88     close(fd);
89     return 0;
90   }
91   if (n != nbytes) {
92     fprintf(stderr, "%s: read unexpected number of bytes\n", name);
93     free(p);
94     close(fd);
95     return 0;
96   }
97   processor(p, nbytes, name, arg);
98   free(p);
99   close(fd);
100   return 1;
101 }
102