#ifndef _WIN32CRAP_H_
#define _WIN32CRAP_H_

#if _MSC_VER >= 1400
#include <errno.h>
#include <windows.h>
#include <direct.h>
#include <share.h>
#include <io.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#define lseek _lseek
#define read _read
#define write _write
#define snprintf(dest, len, format, ...)  _snprintf_s(dest, len, _TRUNCATE, format, __VA_ARGS__);
#define strncpy(dest, src, len); if (strncpy_s(dest, len, src, _TRUNCATE) == STRUNCATE) { \
g_print("strncpy_s discovered an truncate; src = %s\n", src); \
}
#define FOPEN(fp, filename, mode) fopen_s(&fp, filename, mode)
#define OPEN(fp, filename, mode) _sopen_s(&fp, filename, mode, _SH_DENYWR, _S_IREAD | _S_IWRITE)
#define close(fp) _close(fp)
#define sscanf sscanf_s
#define LOCALTIME(ret, src) localtime_s(&ret, src)
#define unlink _unlink
#define STRERROR(buf, len) strerror_s(buf, len, errno)
#define MKDIR(dir, mode) _mkdir(dir)
#define strcasecmp _stricmp
#define strncasecmp _strnicmp
#define S_ISDIR(m)      (((m) & S_IFMT) == S_IFDIR)
#else
#if !defined(__CEGCC__) && !defined(_MSC_VER)
#include <unistd.h>
#define SOCKET int
#define FOPEN(fp, filename, mode) fp = fopen(filename, mode)
#endif
#define OPEN(fp, filename, mode) fp = open(filename, mode)
#define LOCALTIME(ret, src) localtime_r(src, &ret)
#define STRERROR(buf, len) { char *ret = strerror_r(errno, buf, len); if (ret) { strncpy(buf, ret, len); } else { memset(buf, 0, len); } }
#define MKDIR(dir, mode) mkdir(dir, mode)
#endif

#endif /* _WIN32CRAP_H_ */

