~ubuntu-branches/ubuntu/precise/kompozer/precise

« back to all changes in this revision

Viewing changes to mozilla/expat/xmlwf/unixfilemap.c

  • Committer: Bazaar Package Importer
  • Author(s): Anthony Yarusso
  • Date: 2007-08-27 01:11:03 UTC
  • Revision ID: james.westby@ubuntu.com-20070827011103-2jgf4s6532gqu2ka
Tags: upstream-0.7.10
ImportĀ upstreamĀ versionĀ 0.7.10

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
Copyright (c) 1998, 1999 Thai Open Source Software Center Ltd
 
3
See the file COPYING for copying permission.
 
4
*/
 
5
 
 
6
#include <sys/types.h>
 
7
#include <sys/mman.h>
 
8
#include <sys/stat.h>
 
9
#include <fcntl.h>
 
10
#include <errno.h>
 
11
#include <string.h>
 
12
#include <stdio.h>
 
13
 
 
14
#ifndef MAP_FILE
 
15
#define MAP_FILE 0
 
16
#endif
 
17
 
 
18
#include "filemap.h"
 
19
 
 
20
int filemap(const char *name,
 
21
            void (*processor)(const void *, size_t, const char *, void *arg),
 
22
            void *arg)
 
23
{
 
24
  int fd;
 
25
  size_t nbytes;
 
26
  struct stat sb;
 
27
  void *p;
 
28
 
 
29
  fd = open(name, O_RDONLY);
 
30
  if (fd < 0) {
 
31
    perror(name);
 
32
    return 0;
 
33
  }
 
34
  if (fstat(fd, &sb) < 0) {
 
35
    perror(name);
 
36
    close(fd);
 
37
    return 0;
 
38
  }
 
39
  if (!S_ISREG(sb.st_mode)) {
 
40
    close(fd);
 
41
    fprintf(stderr, "%s: not a regular file\n", name);
 
42
    return 0;
 
43
  }
 
44
  
 
45
  nbytes = sb.st_size;
 
46
  p = (void *)mmap((caddr_t)0, (size_t)nbytes, PROT_READ,
 
47
                   MAP_FILE|MAP_PRIVATE, fd, (off_t)0);
 
48
  if (p == (void *)-1) {
 
49
    perror(name);
 
50
    close(fd);
 
51
    return 0;
 
52
  }
 
53
  processor(p, nbytes, name, arg);
 
54
  munmap((caddr_t)p, nbytes);
 
55
  close(fd);
 
56
  return 1;
 
57
}