2 compat: Some compatibility functions and header inclusions.
3 Basic standard C stuff, that may barely be above/around C89.
5 The mpg123 code is determined to keep it's legacy. A legacy of old, old UNIX.
6 It is envisioned to include this compat header instead of any of the "standard" headers, to catch compatibility issues.
7 So, don't include stdlib.h or string.h ... include compat.h.
9 copyright 2007-8 by the mpg123 project - free software under the terms of the LGPL 2.1
10 see COPYING and AUTHORS files in distribution or http://mpg123.org
11 initially written by Thomas Orgis
14 #ifndef MPG123_COMPAT_H
15 #define MPG123_COMPAT_H
19 /* Needed for strdup(), in strict mode ... */
21 #define _XOPEN_SOURCE 500
37 #ifdef HAVE_SYS_SIGNAL_H
38 #include <sys/signal.h>
46 /* Types, types, types. */
47 /* Do we actually need these two in addition to sys/types.h? As replacement? */
48 #ifdef HAVE_SYS_TYPES_H
49 #include <sys/types.h>
51 #ifdef HAVE_INTTYPES_H
57 /* We want SIZE_MAX, etc. */
63 #define SIZE_MAX ((size_t)-1)
66 #define ULONG_MAX ((unsigned long)-1)
80 #ifdef HAVE_SYS_TIME_H
83 /* For select(), I need select.h according to POSIX 2001, else: sys/time.h sys/types.h unistd.h */
84 #ifdef HAVE_SYS_SELECT_H
85 #include <sys/select.h>
88 /* compat_open makes little sense without */
91 /* To parse big numbers... */
93 #define atobigint atoll
95 #define atobigint atol
98 typedef unsigned char byte
;
102 typedef long ssize_t
;
104 #endif /* __REACTOS__ */
106 /* A safe realloc also for very old systems where realloc(NULL, size) returns NULL. */
107 void *safe_realloc(void *ptr
, size_t size
);
108 #ifndef HAVE_STRERROR
109 const char *strerror(int errnum
);
113 char *strdup(const char *s
);
116 /* If we have the size checks enabled, try to derive some sane printfs.
117 Simple start: Use max integer type and format if long is not big enough.
118 I am hesitating to use %ll without making sure that it's there... */
119 #if !(defined PLAIN_C89) && (defined SIZEOF_OFF_T) && (SIZEOF_OFF_T > SIZEOF_LONG) && (defined PRIiMAX)
120 # define OFF_P PRIiMAX
121 typedef intmax_t off_p
;
127 #if !(defined PLAIN_C89) && (defined SIZEOF_SIZE_T) && (SIZEOF_SIZE_T > SIZEOF_LONG) && (defined PRIuMAX)
128 # define SIZE_P PRIuMAX
129 typedef uintmax_t size_p
;
132 typedef unsigned long size_p
;
135 #if !(defined PLAIN_C89) && (defined SIZEOF_SSIZE_T) && (SIZEOF_SSIZE_T > SIZEOF_LONG) && (defined PRIiMAX)
136 # define SSIZE_P PRIuMAX
137 typedef intmax_t ssize_p
;
139 # define SSIZE_P "li"
140 typedef long ssize_p
;
144 * Opening a file handle can be different.
145 * This function here is defined to take a path in native encoding (ISO8859 / UTF-8 / ...), or, when MS Windows Unicode support is enabled, an UTF-8 string that will be converted back to native UCS-2 (wide character) before calling the system's open function.
146 * @param[in] wptr Pointer to wide string.
147 * @param[in] mbptr Pointer to multibyte string.
148 * @return file descriptor (>=0) or error code.
150 int compat_open(const char *filename
, int flags
);
151 FILE* compat_fopen(const char *filename
, const char *mode
);
154 * Closing a file handle can be platform specific.
155 * This function takes a file descriptor that is to be closed.
156 * @param[in] infd File descriptor to be closed.
157 * @return 0 if the file was successfully closed. A return value of -1 indicates an error.
159 int compat_close(int infd
);
160 int compat_fclose(FILE* stream
);
162 /* Those do make sense in a separate file, but I chose to include them in compat.c because that's the one source whose object is shared between mpg123 and libmpg123 -- and both need the functionality internally. */
164 #ifdef WANT_WIN32_UNICODE
167 * Converts a null terminated UCS-2 string to a multibyte (UTF-8) equivalent.
168 * Caller is supposed to free allocated buffer.
169 * @param[in] wptr Pointer to wide string.
170 * @param[out] mbptr Pointer to multibyte string.
171 * @param[out] buflen Optional parameter for length of allocated buffer.
172 * @return status of WideCharToMultiByte conversion.
174 * WideCharToMultiByte - http://msdn.microsoft.com/en-us/library/dd374130(VS.85).aspx
176 int win32_wide_utf8(const wchar_t * const wptr
, char **mbptr
, size_t * buflen
);
180 * Converts a null terminated UTF-8 string to a UCS-2 equivalent.
181 * Caller is supposed to free allocated buffer.
182 * @param[out] mbptr Pointer to multibyte string.
183 * @param[in] wptr Pointer to wide string.
184 * @param[out] buflen Optional parameter for length of allocated buffer.
185 * @return status of WideCharToMultiByte conversion.
187 * MultiByteToWideChar - http://msdn.microsoft.com/en-us/library/dd319072(VS.85).aspx
190 int win32_utf8_wide(const char *const mbptr
, wchar_t **wptr
, size_t *buflen
);
193 /* Blocking write/read of data with signal resilience.
194 Both continue after being interrupted by signals and always return the
195 amount of processed data (shortage indicating actual problem or EOF). */
196 size_t unintr_write(int fd
, void const *buffer
, size_t bytes
);
197 size_t unintr_read (int fd
, void *buffer
, size_t bytes
);
199 /* That one comes from Tellie on OS/2, needed in resolver. */
201 typedef int socklen_t
;
204 /* OSX SDK defines an enum with "normal" as value. That clashes with
207 #define normal mpg123_normal
212 #if (!defined(WIN32) || defined (__CYGWIN__)) && defined(HAVE_SIGNAL_H)
213 void (*catchsignal(int signum
, void(*handler
)()))();