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