source: trunk/third/tiff/libtiff/tif_unix.c @ 18174

Revision 18174, 4.9 KB checked in by ghudson, 22 years ago (diff)
This commit was generated by cvs2svn to compensate for changes in r18173, which included commits to RCS files with non-trunk default branches.
Line 
1/* $Header: /afs/dev.mit.edu/source/repository/third/tiff/libtiff/tif_unix.c,v 1.1.1.1 2002-12-26 02:38:09 ghudson Exp $ */
2
3/*
4 * Copyright (c) 1988-1997 Sam Leffler
5 * Copyright (c) 1991-1997 Silicon Graphics, Inc.
6 *
7 * Permission to use, copy, modify, distribute, and sell this software and
8 * its documentation for any purpose is hereby granted without fee, provided
9 * that (i) the above copyright notices and this permission notice appear in
10 * all copies of the software and related documentation, and (ii) the names of
11 * Sam Leffler and Silicon Graphics may not be used in any advertising or
12 * publicity relating to the software without the specific, prior written
13 * permission of Sam Leffler and Silicon Graphics.
14 *
15 * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
17 * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. 
18 *
19 * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
20 * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
21 * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
22 * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
23 * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
24 * OF THIS SOFTWARE.
25 */
26
27/*
28 * TIFF Library UNIX-specific Routines.
29 */
30#include "tiffiop.h"
31#include <sys/types.h>
32#include <unistd.h>
33#include <stdlib.h>
34
35static tsize_t
36_tiffReadProc(thandle_t fd, tdata_t buf, tsize_t size)
37{
38        return ((tsize_t) read((int) fd, buf, (size_t) size));
39}
40
41static tsize_t
42_tiffWriteProc(thandle_t fd, tdata_t buf, tsize_t size)
43{
44        return ((tsize_t) write((int) fd, buf, (size_t) size));
45}
46
47static toff_t
48_tiffSeekProc(thandle_t fd, toff_t off, int whence)
49{
50#if USE_64BIT_API == 1
51        return ((toff_t) lseek64((int) fd, (off64_t) off, whence));
52#else
53        return ((toff_t) lseek((int) fd, (off_t) off, whence));
54#endif
55}
56
57static int
58_tiffCloseProc(thandle_t fd)
59{
60        return (close((int) fd));
61}
62
63#include <sys/stat.h>
64
65static toff_t
66_tiffSizeProc(thandle_t fd)
67{
68#ifdef _AM29K
69        long fsize;
70        return ((fsize = lseek((int) fd, 0, SEEK_END)) < 0 ? 0 : fsize);
71#else
72#if USE_64BIT_API == 1
73        struct stat64 sb;
74        return (toff_t) (fstat64((int) fd, &sb) < 0 ? 0 : sb.st_size);
75#else
76        struct stat sb;
77        return (toff_t) (fstat((int) fd, &sb) < 0 ? 0 : sb.st_size);
78#endif
79#endif
80}
81
82#ifdef HAVE_MMAP
83#include <sys/mman.h>
84
85static int
86_tiffMapProc(thandle_t fd, tdata_t* pbase, toff_t* psize)
87{
88        toff_t size = _tiffSizeProc(fd);
89        if (size != (toff_t) -1) {
90                *pbase = (tdata_t)
91                    mmap(0, size, PROT_READ, MAP_SHARED, (int) fd, 0);
92                if (*pbase != (tdata_t) -1) {
93                        *psize = size;
94                        return (1);
95                }
96        }
97        return (0);
98}
99
100static void
101_tiffUnmapProc(thandle_t fd, tdata_t base, toff_t size)
102{
103        (void) fd;
104        (void) munmap(base, (off_t) size);
105}
106#else /* !HAVE_MMAP */
107static int
108_tiffMapProc(thandle_t fd, tdata_t* pbase, toff_t* psize)
109{
110        (void) fd; (void) pbase; (void) psize;
111        return (0);
112}
113
114static void
115_tiffUnmapProc(thandle_t fd, tdata_t base, toff_t size)
116{
117        (void) fd; (void) base; (void) size;
118}
119#endif /* !HAVE_MMAP */
120
121/*
122 * Open a TIFF file descriptor for read/writing.
123 */
124TIFF*
125TIFFFdOpen(int fd, const char* name, const char* mode)
126{
127        TIFF* tif;
128
129        tif = TIFFClientOpen(name, mode,
130            (thandle_t) fd,
131            _tiffReadProc, _tiffWriteProc,
132            _tiffSeekProc, _tiffCloseProc, _tiffSizeProc,
133            _tiffMapProc, _tiffUnmapProc);
134        if (tif)
135                tif->tif_fd = fd;
136        return (tif);
137}
138
139/*
140 * Open a TIFF file for read/writing.
141 */
142TIFF*
143TIFFOpen(const char* name, const char* mode)
144{
145        static const char module[] = "TIFFOpen";
146        int m, fd;
147
148        m = _TIFFgetMode(mode, module);
149        if (m == -1)
150                return ((TIFF*)0);
151
152/* for cygwin */       
153#ifdef O_BINARY
154        m |= O_BINARY;
155#endif       
156       
157#ifdef _AM29K
158        fd = open(name, m);
159#else
160        fd = open(name, m, 0666);
161#endif
162        if (fd < 0) {
163                TIFFError(module, "%s: Cannot open", name);
164                return ((TIFF *)0);
165        }
166        return (TIFFFdOpen(fd, name, mode));
167}
168
169void*
170_TIFFmalloc(tsize_t s)
171{
172        return (malloc((size_t) s));
173}
174
175void
176_TIFFfree(tdata_t p)
177{
178        free(p);
179}
180
181void*
182_TIFFrealloc(tdata_t p, tsize_t s)
183{
184        return (realloc(p, (size_t) s));
185}
186
187void
188_TIFFmemset(tdata_t p, int v, tsize_t c)
189{
190        memset(p, v, (size_t) c);
191}
192
193void
194_TIFFmemcpy(tdata_t d, const tdata_t s, tsize_t c)
195{
196        memcpy(d, s, (size_t) c);
197}
198
199int
200_TIFFmemcmp(const tdata_t p1, const tdata_t p2, tsize_t c)
201{
202        return (memcmp(p1, p2, (size_t) c));
203}
204
205static void
206unixWarningHandler(const char* module, const char* fmt, va_list ap)
207{
208        if (module != NULL)
209                fprintf(stderr, "%s: ", module);
210        fprintf(stderr, "Warning, ");
211        vfprintf(stderr, fmt, ap);
212        fprintf(stderr, ".\n");
213}
214TIFFErrorHandler _TIFFwarningHandler = unixWarningHandler;
215
216static void
217unixErrorHandler(const char* module, const char* fmt, va_list ap)
218{
219        if (module != NULL)
220                fprintf(stderr, "%s: ", module);
221        vfprintf(stderr, fmt, ap);
222        fprintf(stderr, ".\n");
223}
224TIFFErrorHandler _TIFFerrorHandler = unixErrorHandler;
Note: See TracBrowser for help on using the repository browser.