symbian-qemu-0.9.1-12/expat-2.0.0/xmlwf/readfilemap.c
changeset 1 2fb8b9db1c86
equal deleted inserted replaced
0:ffa851df0825 1:2fb8b9db1c86
       
     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 #ifdef __BEOS__
       
    12 #include <unistd.h>
       
    13 #endif
       
    14 
       
    15 #ifndef S_ISREG
       
    16 #ifndef S_IFREG
       
    17 #define S_IFREG _S_IFREG
       
    18 #endif
       
    19 #ifndef S_IFMT
       
    20 #define S_IFMT _S_IFMT
       
    21 #endif
       
    22 #define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
       
    23 #endif /* not S_ISREG */
       
    24 
       
    25 #ifndef O_BINARY
       
    26 #ifdef _O_BINARY
       
    27 #define O_BINARY _O_BINARY
       
    28 #else
       
    29 #define O_BINARY 0
       
    30 #endif
       
    31 #endif
       
    32 
       
    33 #include "filemap.h"
       
    34 
       
    35 int
       
    36 filemap(const char *name,
       
    37         void (*processor)(const void *, size_t, const char *, void *arg),
       
    38         void *arg)
       
    39 {
       
    40   size_t nbytes;
       
    41   int fd;
       
    42   int n;
       
    43   struct stat sb;
       
    44   void *p;
       
    45 
       
    46   fd = open(name, O_RDONLY|O_BINARY);
       
    47   if (fd < 0) {
       
    48     perror(name);
       
    49     return 0;
       
    50   }
       
    51   if (fstat(fd, &sb) < 0) {
       
    52     perror(name);
       
    53     return 0;
       
    54   }
       
    55   if (!S_ISREG(sb.st_mode)) {
       
    56     fprintf(stderr, "%s: not a regular file\n", name);
       
    57     return 0;
       
    58   }
       
    59   nbytes = sb.st_size;
       
    60   p = malloc(nbytes);
       
    61   if (!p) {
       
    62     fprintf(stderr, "%s: out of memory\n", name);
       
    63     return 0;
       
    64   }
       
    65   n = read(fd, p, nbytes);
       
    66   if (n < 0) {
       
    67     perror(name);
       
    68     free(p);
       
    69     close(fd);
       
    70     return 0;
       
    71   }
       
    72   if (n != nbytes) {
       
    73     fprintf(stderr, "%s: read unexpected number of bytes\n", name);
       
    74     free(p);
       
    75     close(fd);
       
    76     return 0;
       
    77   }
       
    78   processor(p, nbytes, name, arg);
       
    79   free(p);
       
    80   close(fd);
       
    81   return 1;
       
    82 }