1 /* Copyright (c) 1998, 1999 Thai Open Source Software Center Ltd
2 See the file COPYING for copying permission.
25 #define S_IFREG _S_IFREG
28 #define S_IFMT _S_IFMT
30 #define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
31 #endif /* not S_ISREG */
35 #define O_BINARY _O_BINARY
44 filemap(const char *name,
45 void (*processor)(const void *, size_t, const char *, void *arg),
54 fd = open(name, O_RDONLY|O_BINARY);
59 if (fstat(fd, &sb) < 0) {
63 if (!S_ISREG(sb.st_mode)) {
64 fprintf(stderr, "%s: not a regular file\n", name);
68 /* malloc will return NULL with nbytes == 0, handle files with size 0 */
70 static const char c = '\0';
71 processor(&c, 0, name, arg);
77 fprintf(stderr, "%s: out of memory\n", name);
81 n = read(fd, p, nbytes);
89 fprintf(stderr, "%s: read unexpected number of bytes\n", name);
94 processor(p, nbytes, name, arg);