1 /* Copyright (c) 1998, 1999 Thai Open Source Software Center Ltd
2 See the file COPYING for copying permission.
11 /* Functions close(2) and read(2) */
19 # if !defined(WIN32) && !defined(_WIN32) && !defined(_WIN64)
26 #define S_IFREG _S_IFREG
29 #define S_IFMT _S_IFMT
31 #define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
32 #endif /* not S_ISREG */
36 #define O_BINARY _O_BINARY
45 filemap(const char *name,
46 void (*processor)(const void *, size_t, const char *, void *arg),
55 fd = open(name, O_RDONLY|O_BINARY);
60 if (fstat(fd, &sb) < 0) {
65 if (!S_ISREG(sb.st_mode)) {
66 fprintf(stderr, "%s: not a regular file\n", name);
71 /* malloc will return NULL with nbytes == 0, handle files with size 0 */
73 static const char c = '\0';
74 processor(&c, 0, name, arg);
80 fprintf(stderr, "%s: out of memory\n", name);
84 n = read(fd, p, nbytes);
92 fprintf(stderr, "%s: read unexpected number of bytes\n", name);
97 processor(p, nbytes, name, arg);