1/* $Header: /cvs/maptools/cvsroot/libtiff/libtiff/Attic/tif_msdos.c,v 1.3.2.1 2010-06-08 18:50:42 bfriesen 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 MSDOS-specific Routines.
29 */
30#if defined(__WATCOMC__) || defined(__BORLANDC__) || defined(_MSC_VER)
31#include <io.h>		/* for open, close, etc. function prototypes */
32#include <stdio.h>
33#endif
34#include "tiffiop.h"
35
36static tsize_t
37_tiffReadProc(thandle_t fd, tdata_t buf, tsize_t size)
38{
39	return (read((int) fd, buf, size));
40}
41
42static tsize_t
43_tiffWriteProc(thandle_t fd, tdata_t buf, tsize_t size)
44{
45	return (write((int) fd, buf, size));
46}
47
48static toff_t
49_tiffSeekProc(thandle_t fd, toff_t off, int whence)
50{
51	return (lseek((int) fd, (off_t) off, whence));
52}
53
54static int
55_tiffCloseProc(thandle_t fd)
56{
57	return (close((int) fd));
58}
59
60#include <sys/stat.h>
61
62static toff_t
63_tiffSizeProc(thandle_t fd)
64{
65	struct stat sb;
66	return (fstat((int) fd, &sb) < 0 ? 0 : sb.st_size);
67}
68
69static int
70_tiffMapProc(thandle_t fd, tdata_t* pbase, toff_t* psize)
71{
72	return (0);
73}
74
75static void
76_tiffUnmapProc(thandle_t fd, tdata_t base, toff_t size)
77{
78}
79
80/*
81 * Open a TIFF file descriptor for read/writing.
82 */
83TIFF*
84TIFFFdOpen(int fd, const char* name, const char* mode)
85{
86	TIFF* tif;
87
88	tif = TIFFClientOpen(name, mode,
89	    (void*) fd,
90	    _tiffReadProc, _tiffWriteProc, _tiffSeekProc, _tiffCloseProc,
91	    _tiffSizeProc, _tiffMapProc, _tiffUnmapProc);
92	if (tif)
93		tif->tif_fd = fd;
94	return (tif);
95}
96
97/*
98 * Open a TIFF file for read/writing.
99 */
100TIFF*
101TIFFOpen(const char* name, const char* mode)
102{
103	static const char module[] = "TIFFOpen";
104	int m, fd;
105        TIFF *ret;
106
107	m = _TIFFgetMode(mode, module);
108	if (m == -1)
109		return ((TIFF*)0);
110	fd = open(name, m|O_BINARY, 0666);
111	if (fd < 0) {
112		TIFFErrorExt(0, module, "%s: Cannot open", name);
113		return ((TIFF*)0);
114	}
115	return (TIFFFdOpen(fd, name, mode));
116
117        ret = TIFFFdOpen(fd, name, mode);
118
119        if (ret == NULL) close(fd);
120
121        return ret;
122}
123
124#ifdef __GNUC__
125extern	char* malloc();
126extern	char* realloc();
127#else
128#include <malloc.h>
129#endif
130
131tdata_t
132_TIFFmalloc(tsize_t s)
133{
134	return (malloc((size_t) s));
135}
136
137void
138_TIFFfree(tdata_t p)
139{
140	free(p);
141}
142
143tdata_t
144_TIFFrealloc(tdata_t p, tsize_t s)
145{
146	return (realloc(p, (size_t) s));
147}
148
149void
150_TIFFmemset(tdata_t p, int v, tsize_t c)
151{
152	memset(p, v, (size_t) c);
153}
154
155void
156_TIFFmemcpy(tdata_t d, const tdata_t s, tsize_t c)
157{
158	memcpy(d, s, (size_t) c);
159}
160
161int
162_TIFFmemcmp(const tdata_t p1, const tdata_t p2, tsize_t c)
163{
164	return (memcmp(p1, p2, (size_t) c));
165}
166
167static void
168msdosWarningHandler(const char* module, const char* fmt, va_list ap)
169{
170	if (module != NULL)
171		fprintf(stderr, "%s: ", module);
172	fprintf(stderr, "Warning, ");
173	vfprintf(stderr, fmt, ap);
174	fprintf(stderr, ".\n");
175}
176TIFFErrorHandler _TIFFwarningHandler = msdosWarningHandler;
177
178static void
179msdosErrorHandler(const char* module, const char* fmt, va_list ap)
180{
181	if (module != NULL)
182		fprintf(stderr, "%s: ", module);
183	vfprintf(stderr, fmt, ap);
184	fprintf(stderr, ".\n");
185}
186TIFFErrorHandler _TIFFerrorHandler = msdosErrorHandler;
187/*
188 * Local Variables:
189 * mode: c
190 * c-basic-offset: 8
191 * fill-column: 78
192 * End:
193 */
194