Imported Upstream version 1.3.2
[platform/upstream/libzip.git] / lib / compat.h
1 #ifndef _HAD_LIBZIP_COMPAT_H
2 #define _HAD_LIBZIP_COMPAT_H
3
4 /*
5   compat.h -- compatibility defines.
6   Copyright (C) 1999-2016 Dieter Baron and Thomas Klausner
7
8   This file is part of libzip, a library to manipulate ZIP archives.
9   The authors can be contacted at <libzip@nih.at>
10
11   Redistribution and use in source and binary forms, with or without
12   modification, are permitted provided that the following conditions
13   are met:
14   1. Redistributions of source code must retain the above copyright
15      notice, this list of conditions and the following disclaimer.
16   2. Redistributions in binary form must reproduce the above copyright
17      notice, this list of conditions and the following disclaimer in
18      the documentation and/or other materials provided with the
19      distribution.
20   3. The names of the authors may not be used to endorse or promote
21      products derived from this software without specific prior
22      written permission.
23  
24   THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS
25   OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
26   WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27   ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
28   DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29   DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
30   GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31   INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
32   IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
33   OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
34   IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 */
36
37 #ifdef HAVE_CONFIG_H
38 #include "config.h"
39 #endif
40
41 /* to have *_MAX definitions for all types when compiling with g++ */
42 #define __STDC_LIMIT_MACROS
43
44 #ifdef _WIN32
45 #define ZIP_EXTERN __declspec(dllexport)
46 /* for dup(), close(), etc. */
47 #include <io.h>
48 #endif
49
50 #ifdef HAVE_STDBOOL_H
51 #include <stdbool.h>
52 #else
53 typedef char bool;
54 #define true    1
55 #define false   0
56 #endif
57
58 #include <errno.h>
59
60 /* at least MinGW does not provide EOPNOTSUPP, see
61  * http://sourceforge.net/p/mingw/bugs/263/
62  */
63 #ifndef EOPNOTSUPP
64 #define EOPNOTSUPP EINVAL
65 #endif
66
67 /* at least MinGW does not provide EOVERFLOW, see
68  * http://sourceforge.net/p/mingw/bugs/242/
69  */
70 #ifndef EOVERFLOW
71 #define EOVERFLOW EFBIG
72 #endif
73
74 #ifdef _WIN32
75 #if defined(HAVE__CHMOD)
76 #define chmod           _chmod
77 #endif
78 #if defined(HAVE__CLOSE)
79 #define close           _close
80 #endif
81 #if defined(HAVE__DUP)
82 #define dup             _dup
83 #endif
84 /* crashes reported when using fdopen instead of _fdopen on Windows/Visual Studio 10/Win64 */
85 #if defined(HAVE__FDOPEN)
86 #define fdopen          _fdopen
87 #endif
88 #if !defined(HAVE_FILENO) && defined(HAVE__FILENO)
89 #define fileno          _fileno
90 #endif
91 /* Windows' open() doesn't understand Unix permissions */
92 #if defined(HAVE__OPEN)
93 #define open(a, b, c)   _open((a), (b))
94 #endif
95 #if defined(HAVE__SNPRINTF)
96 #define snprintf        _snprintf
97 #endif
98 #if defined(HAVE__STRDUP)
99 #if !defined(HAVE_STRDUP) || defined(_WIN32)
100 #undef strdup
101 #define strdup          _strdup
102 #endif
103 #endif
104 #if !defined(HAVE__SETMODE) && defined(HAVE_SETMODE)
105 #define _setmode        setmode
106 #endif
107 #if !defined(HAVE_STRTOLL) && defined(HAVE__STRTOI64)
108 #define strtoll         _strtoi64
109 #endif
110 #if !defined(HAVE_STRTOULL) && defined(HAVE__STRTOUI64)
111 #define strtoull        _strtoui64
112 #endif
113 #if defined(HAVE__UMASK)
114 #define umask   _umask
115 #endif
116 #if defined(HAVE__UNLINK)
117 #define unlink  _unlink
118 #endif
119 #endif
120
121 #ifndef HAVE_FSEEKO
122 #define fseeko(s, o, w) (fseek((s), (long int)(o), (w)))
123 #endif
124
125 #ifndef HAVE_FTELLO
126 #define ftello(s)       ((long)ftell((s)))
127 #endif
128
129 #ifndef HAVE_MKSTEMP
130 int _zip_mkstemp(char *);
131 #define mkstemp _zip_mkstemp
132 #endif
133
134 #if !defined(HAVE_STRCASECMP)
135 #if defined(HAVE__STRICMP)
136 #define strcasecmp      _stricmp
137 #elif defined(HAVE_STRICMP)
138 #define strcasecmp      stricmp
139 #endif
140 #endif
141
142 #if SIZEOF_OFF_T == 8
143 #define ZIP_OFF_MAX ZIP_INT64_MAX
144 #define ZIP_OFF_MIN ZIP_INT64_MIN
145 #elif SIZEOF_OFF_T == 4
146 #define ZIP_OFF_MAX ZIP_INT32_MAX
147 #define ZIP_OFF_MIN ZIP_INT32_MIN
148 #elif SIZEOF_OFF_T == 2
149 #define ZIP_OFF_MAX ZIP_INT16_MAX
150 #define ZIP_OFF_MIN ZIP_INT16_MIN
151 #else
152 #error unsupported size of off_t
153 #endif
154
155 #if defined(HAVE_FTELLO) && defined(HAVE_FSEEKO)
156 #define ZIP_FSEEK_MAX ZIP_OFF_MAX
157 #define ZIP_FSEEK_MIN ZIP_OFF_MIN
158 #else
159 #include <limits.h>
160 #define ZIP_FSEEK_MAX LONG_MAX
161 #define ZIP_FSEEK_MIN LONG_MIN
162 #endif
163
164 #ifndef SIZE_MAX
165 #if SIZEOF_SIZE_T == 8
166 #define SIZE_MAX ZIP_INT64_MAX
167 #elif SIZEOF_SIZE_T == 4
168 #define SIZE_MAX ZIP_INT32_MAX
169 #elif SIZEOF_SIZE_T == 2
170 #define SIZE_MAX ZIP_INT16_MAX
171 #else
172 #error unsupported size of size_t
173 #endif
174 #endif
175
176 #ifndef PRId64
177 #ifdef _MSC_VER
178 #define PRId64 "I64d"
179 #else
180 #define PRId64 "lld"
181 #endif
182 #endif
183
184 #ifndef PRIu64
185 #ifdef _MSC_VER
186 #define PRIu64 "I64u"
187 #else
188 #define PRIu64 "llu"
189 #endif
190 #endif
191
192 #ifndef S_ISDIR
193 #define S_ISDIR(mode)   (((mode) & S_IFMT) == S_IFDIR)
194 #endif
195
196 #endif /* compat.h */