home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / OSK / NETWORK / netpbm_src.lzh / NETPBM / LIBTIFF / tif_unix.c < prev    next >
Text File  |  1996-11-24  |  4KB  |  177 lines

  1. #ifndef lint
  2. static char rcsid[] = "$Header: /usr/people/sam/tiff/libtiff/RCS/tif_unix.c,v 1.6 93/08/25 09:34:24 sam Exp $";
  3. #endif
  4.  
  5. /*
  6.  * Copyright (c) 1988, 1989, 1990, 1991, 1992 Sam Leffler
  7.  * Copyright (c) 1991, 1992 Silicon Graphics, Inc.
  8.  *
  9.  * Permission to use, copy, modify, distribute, and sell this software and 
  10.  * its documentation for any purpose is hereby granted without fee, provided
  11.  * that (i) the above copyright notices and this permission notice appear in
  12.  * all copies of the software and related documentation, and (ii) the names of
  13.  * Sam Leffler and Silicon Graphics may not be used in any advertising or
  14.  * publicity relating to the software without the specific, prior written
  15.  * permission of Sam Leffler and Silicon Graphics.
  16.  * 
  17.  * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, 
  18.  * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY 
  19.  * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.  
  20.  * 
  21.  * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
  22.  * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
  23.  * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  24.  * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF 
  25.  * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 
  26.  * OF THIS SOFTWARE.
  27.  */
  28.  
  29. /*
  30.  * TIFF Library UNIX-specific Routines.
  31.  */
  32. #include "tiffiop.h"
  33. #ifndef _OSK
  34. #include <unistd.h>
  35. #else
  36. typedef long off_t;
  37. #endif
  38. #include <stdlib.h>
  39.  
  40. static tsize_t
  41. _tiffReadProc(thandle_t fd, tdata_t buf, tsize_t size)
  42. {
  43.     return (read((int) fd, buf, (size_t) size));
  44. }
  45.  
  46. static tsize_t
  47. _tiffWriteProc(thandle_t fd, tdata_t buf, tsize_t size)
  48. {
  49.     return (write((int) fd, buf, (size_t) size));
  50. }
  51.  
  52. static toff_t
  53. _tiffSeekProc(thandle_t fd, off_t off, int whence)
  54. {
  55.     return ((toff_t) lseek((int) fd, (off_t) off, whence));
  56. }
  57.  
  58. static int
  59. _tiffCloseProc(thandle_t fd)
  60. {
  61.     return (close((int) fd));
  62. }
  63.  
  64. #include <sys/stat.h>
  65.  
  66. static toff_t
  67. _tiffSizeProc(thandle_t fd)
  68. {
  69. #ifdef _AM29K
  70.     long fsize;
  71.     return ((fsize = lseek((int) fd, 0, SEEK_END)) < 0 ? 0 : fsize);
  72. #else
  73.     struct stat sb;
  74.     return (toff_t) (fstat((int) fd, &sb) < 0 ? 0 : sb.st_size);
  75. #endif
  76. }
  77.  
  78. #ifdef MMAP_SUPPORT
  79. #include <sys/mman.h>
  80.  
  81. static int
  82. _tiffMapProc(thandle_t fd, tdata_t* pbase, toff_t* psize)
  83. {
  84.     toff_t size = _tiffSizeProc(fd);
  85.     if (size != (toff_t) -1) {
  86.         *pbase = (tdata_t)
  87.             mmap(0, size, PROT_READ, MAP_SHARED, (int) fd, 0);
  88.         if (*pbase != (tdata_t) -1) {
  89.             *psize = size;
  90.             return (1);
  91.         }
  92.     }
  93.     return (0);
  94. }
  95.  
  96. static void
  97. _tiffUnmapProc(thandle_t fd, tdata_t base, toff_t size)
  98. {
  99.     (void) munmap(base, (off_t) size);
  100. }
  101. #else /* !MMAP_SUPPORT */
  102. static int
  103. _tiffMapProc(thandle_t fd, tdata_t* pbase, toff_t* psize)
  104. {
  105.     return (0);
  106. }
  107.  
  108. static void
  109. _tiffUnmapProc(thandle_t fd, tdata_t base, toff_t size)
  110. {
  111. }
  112. #endif /* !MMAP_SUPPORT */
  113.  
  114. /*
  115.  * Open a TIFF file descriptor for read/writing.
  116.  */
  117. TIFF*
  118. TIFFFdOpen(int fd, const char* name, const char* mode)
  119. {
  120.     TIFF* tif;
  121.  
  122.     tif = TIFFClientOpen(name, mode,
  123.         (thandle_t) fd,
  124.         _tiffReadProc, _tiffWriteProc,
  125.         _tiffSeekProc, _tiffCloseProc, _tiffSizeProc,
  126.         _tiffMapProc, _tiffUnmapProc);
  127.     if (tif)
  128.         tif->tif_fd = fd;
  129.     return (tif);
  130. }
  131.  
  132. /*
  133.  * Open a TIFF file for read/writing.
  134.  */
  135. TIFF*
  136. TIFFOpen(const char* name, const char* mode)
  137. {
  138.     static const char module[] = "TIFFOpen";
  139.     int m, fd;
  140.  
  141.     m = _TIFFgetMode(mode, module);
  142.     if (m == -1)
  143.         return ((TIFF*)0);
  144. #ifdef _AM29K
  145.     fd = open(name, m);
  146. #else
  147. #ifndef _OSK
  148.     fd = open(name, m, 0666);
  149. #else
  150.     fd = open(name, m);
  151. #endif
  152. #endif
  153.     if (fd < 0) {
  154.         TIFFError(module, "%s: Cannot open", name);
  155.         return ((TIFF *)0);
  156.     }
  157.     return (TIFFFdOpen(fd, name, mode));
  158. }
  159.  
  160. void*
  161. _TIFFmalloc(size_t s)
  162. {
  163.     return (malloc(s));
  164. }
  165.  
  166. void
  167. _TIFFfree(void* p)
  168. {
  169.     free(p);
  170. }
  171.  
  172. void*
  173. _TIFFrealloc(void* p, size_t s)
  174. {
  175.     return (realloc(p, s));
  176. }
  177.