Sync usage with man page.
[netbsd-mini2440.git] / common / dist / zlib / contrib / minizip / ioapi.c
blobb81e358adc98b9123b5e43923d71e9a2072da2eb
1 /* $NetBSD$ */
3 /* ioapi.c -- IO base function header for compress/uncompress .zip
4 files using zlib + zip or unzip API
6 Version 1.01e, February 12th, 2005
8 Copyright (C) 1998-2005 Gilles Vollant
9 */
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
15 #include "zlib.h"
16 #include "ioapi.h"
20 /* I've found an old Unix (a SunOS 4.1.3_U1) without all SEEK_* defined.... */
22 #ifndef SEEK_CUR
23 #define SEEK_CUR 1
24 #endif
26 #ifndef SEEK_END
27 #define SEEK_END 2
28 #endif
30 #ifndef SEEK_SET
31 #define SEEK_SET 0
32 #endif
34 voidpf ZCALLBACK fopen_file_func OF((
35 voidpf opaque,
36 const char* filename,
37 int mode));
39 uLong ZCALLBACK fread_file_func OF((
40 voidpf opaque,
41 voidpf stream,
42 void* buf,
43 uLong size));
45 uLong ZCALLBACK fwrite_file_func OF((
46 voidpf opaque,
47 voidpf stream,
48 const void* buf,
49 uLong size));
51 long ZCALLBACK ftell_file_func OF((
52 voidpf opaque,
53 voidpf stream));
55 long ZCALLBACK fseek_file_func OF((
56 voidpf opaque,
57 voidpf stream,
58 uLong offset,
59 int origin));
61 int ZCALLBACK fclose_file_func OF((
62 voidpf opaque,
63 voidpf stream));
65 int ZCALLBACK ferror_file_func OF((
66 voidpf opaque,
67 voidpf stream));
70 voidpf ZCALLBACK fopen_file_func (opaque, filename, mode)
71 voidpf opaque;
72 const char* filename;
73 int mode;
75 FILE* file = NULL;
76 const char* mode_fopen = NULL;
77 if ((mode & ZLIB_FILEFUNC_MODE_READWRITEFILTER)==ZLIB_FILEFUNC_MODE_READ)
78 mode_fopen = "rb";
79 else
80 if (mode & ZLIB_FILEFUNC_MODE_EXISTING)
81 mode_fopen = "r+b";
82 else
83 if (mode & ZLIB_FILEFUNC_MODE_CREATE)
84 mode_fopen = "wb";
86 if ((filename!=NULL) && (mode_fopen != NULL))
87 file = fopen(filename, mode_fopen);
88 return file;
92 uLong ZCALLBACK fread_file_func (opaque, stream, buf, size)
93 voidpf opaque;
94 voidpf stream;
95 void* buf;
96 uLong size;
98 uLong ret;
99 ret = (uLong)fread(buf, 1, (size_t)size, (FILE *)stream);
100 return ret;
104 uLong ZCALLBACK fwrite_file_func (opaque, stream, buf, size)
105 voidpf opaque;
106 voidpf stream;
107 const void* buf;
108 uLong size;
110 uLong ret;
111 ret = (uLong)fwrite(buf, 1, (size_t)size, (FILE *)stream);
112 return ret;
115 long ZCALLBACK ftell_file_func (opaque, stream)
116 voidpf opaque;
117 voidpf stream;
119 long ret;
120 ret = ftell((FILE *)stream);
121 return ret;
124 long ZCALLBACK fseek_file_func (opaque, stream, offset, origin)
125 voidpf opaque;
126 voidpf stream;
127 uLong offset;
128 int origin;
130 int fseek_origin=0;
131 long ret;
132 switch (origin)
134 case ZLIB_FILEFUNC_SEEK_CUR :
135 fseek_origin = SEEK_CUR;
136 break;
137 case ZLIB_FILEFUNC_SEEK_END :
138 fseek_origin = SEEK_END;
139 break;
140 case ZLIB_FILEFUNC_SEEK_SET :
141 fseek_origin = SEEK_SET;
142 break;
143 default: return -1;
145 ret = 0;
146 fseek((FILE *)stream, offset, fseek_origin);
147 return ret;
150 int ZCALLBACK fclose_file_func (opaque, stream)
151 voidpf opaque;
152 voidpf stream;
154 int ret;
155 ret = fclose((FILE *)stream);
156 return ret;
159 int ZCALLBACK ferror_file_func (opaque, stream)
160 voidpf opaque;
161 voidpf stream;
163 int ret;
164 ret = ferror((FILE *)stream);
165 return ret;
168 void fill_fopen_filefunc (pzlib_filefunc_def)
169 zlib_filefunc_def* pzlib_filefunc_def;
171 pzlib_filefunc_def->zopen_file = fopen_file_func;
172 pzlib_filefunc_def->zread_file = fread_file_func;
173 pzlib_filefunc_def->zwrite_file = fwrite_file_func;
174 pzlib_filefunc_def->ztell_file = ftell_file_func;
175 pzlib_filefunc_def->zseek_file = fseek_file_func;
176 pzlib_filefunc_def->zclose_file = fclose_file_func;
177 pzlib_filefunc_def->zerror_file = ferror_file_func;
178 pzlib_filefunc_def->opaque = NULL;