~ubuntu-branches/ubuntu/hardy/ghostscript/hardy

« back to all changes in this revision

Viewing changes to expat/xmlwf/unixfilemap.c

  • Committer: Bazaar Package Importer
  • Author(s): Till Kamppeter
  • Date: 2007-11-22 12:17:43 UTC
  • mfrom: (1.1.7 upstream)
  • Revision ID: james.westby@ubuntu.com-20071122121743-cd70s3ypq0r243mp
Tags: 8.61.dfsg.1-0ubtuntu1
* New upstream release
  o Final 8.61 release
* debian/patches/09_ijs_krgb_support.dpatch: Adapted to upstream changes.
* debian/rules: Updated CUPS-related variables for "make install" calls.
* debian/rules: Remove /usr/include/ghostscript from the ghostscript
  package, they go into lings-dev.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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/mman.h>
 
7
#include <sys/stat.h>
 
8
#include <fcntl.h>
 
9
#include <errno.h>
 
10
#include <string.h>
 
11
#include <stdio.h>
 
12
#include <unistd.h>
 
13
 
 
14
#ifndef MAP_FILE
 
15
#define MAP_FILE 0
 
16
#endif
 
17
 
 
18
#include "filemap.h"
 
19
 
 
20
int
 
21
filemap(const char *name,
 
22
        void (*processor)(const void *, size_t, const char *, void *arg),
 
23
        void *arg)
 
24
{
 
25
  int fd;
 
26
  size_t nbytes;
 
27
  struct stat sb;
 
28
  void *p;
 
29
 
 
30
  fd = open(name, O_RDONLY);
 
31
  if (fd < 0) {
 
32
    perror(name);
 
33
    return 0;
 
34
  }
 
35
  if (fstat(fd, &sb) < 0) {
 
36
    perror(name);
 
37
    close(fd);
 
38
    return 0;
 
39
  }
 
40
  if (!S_ISREG(sb.st_mode)) {
 
41
    close(fd);
 
42
    fprintf(stderr, "%s: not a regular file\n", name);
 
43
    return 0;
 
44
  }
 
45
 
 
46
  nbytes = sb.st_size;
 
47
  /* mmap fails for zero length files */
 
48
  if (nbytes == 0) {
 
49
    static const char c = '\0';
 
50
    processor(&c, 0, name, arg);
 
51
    close(fd);
 
52
    return 1;
 
53
  }
 
54
  p = (void *)mmap((caddr_t)0, (size_t)nbytes, PROT_READ,
 
55
                   MAP_FILE|MAP_PRIVATE, fd, (off_t)0);
 
56
  if (p == (void *)-1) {
 
57
    perror(name);
 
58
    close(fd);
 
59
    return 0;
 
60
  }
 
61
  processor(p, nbytes, name, arg);
 
62
  munmap((caddr_t)p, nbytes);
 
63
  close(fd);
 
64
  return 1;
 
65
}