Commits

Oliver Schneider committed 770b54c

- Still staying minimally invasive, yay!

  • Participants
  • Parent commits 8449113

Comments (0)

Files changed (2)

 #include "win32msvc_glue.c"
 #else
 #include <unistd.h>
+#define _tmain main
+#define _TCHAR char
 #endif // _WIN32 && _MSC_VER
 
 #define MIN(x,y) (((x)<(y)) ? (x) : (y))
 	if(x<0) buf[7]|=0x80;
 }
 
-int main(int argc,char *argv[])
+int _tmain(int argc,_TCHAR *argv[])
 {
 	int fd;
 	u_char *old,*new;
  * POSSIBILITY OF SUCH DAMAGE.
  */
 
+#include <Windows.h>
+#include <tchar.h>
 #include <io.h>
+#include <stdarg.h>
 
-off_t ftello(FILE *stream)
+static off_t ftello(FILE *stream)
 {
     return (off_t)_ftelli64(stream);
 }
 
-int fseeko(FILE *stream, off_t offset, int whence)
+static int fseeko(FILE *stream, off_t offset, int whence)
 {
     return _fseeki64(stream, offset, whence);
 }
 
-void err(int eval, const char *fmt, ...)
+static const _TCHAR* get_program_name()
 {
-    /* phew ... */
+    return _T("bsdiff"); /* faking it for now */
 }
 
-void errx(int eval, const char *fmt, ...)
+#ifdef _UNICODE
+/* We use wchar_t here to force warnings if whoever changes this(including the
+   future me) attempts something silly ;) */
+static wchar_t* get_tchar_from_char(const char* fmt)
 {
-    /* phew ... */
+    if(fmt)
+    {
+        size_t i;
+        size_t const len = strlen(fmt);
+        wchar_t* wfmt = calloc(len + 1, sizeof(wchar_t));
+
+        if(!wfmt)
+            return NULL;
+
+        for(i = 0; i < len; i++)
+        {
+            /* That's right, we ass-u-me that the original string is always
+               ASCII or EASCII and will get bitten by changes that don't adhere
+               to this assumption. */
+            wfmt[i] = (wchar_t)fmt[i];
+        }
+        return wfmt;
+    }
+    return NULL;
 }
 
-#pragma warning(disable: 4244) // possible loss of data
+#define open _topen
+/* Own concoction to handle this special case in the least intrusive manner. */
+#define fopen win32bsdiff_fopen
+
+static FILE* win32bsdiff_fopen(const wchar_t *filename, const char* mode)
+{
+    _TCHAR* tmode = get_tchar_from_char(mode);
+    if(tmode)
+    {
+        FILE* retval = _tfopen(filename, tmode);
+        free(tmode);
+        return retval;
+    }
+    return NULL;
+}
+#else
+#   define get_tchar_from_char(x) x
+#endif // _UNICODE
+
+static void wrap_err_T(int x, int eval, const char *fmt, ...)
+{
+    const int local_errno = errno; // TODO: _doserrno
+    const _TCHAR* progname = get_program_name();
+    _TCHAR* tfmt = get_tchar_from_char(fmt);
+
+    (void)_ftprintf(stderr, _T("%s: "), progname);
+    if (fmt != NULL)
+    {
+        va_list ap;
+
+        va_start(ap, fmt);
+        (void)_vftprintf(stderr, tfmt, ap);
+        va_end(ap);
+    }
+    if(!x)
+        (void)_ftprintf(stderr, _T(": %s\n"), _tcserror(local_errno));
+    else
+        (void)_ftprintf(stderr, _T("\n"));
+    if(fmt != (const char*)tfmt)
+    {
+        free(tfmt);
+    }
+    exit(eval);
+}
+#define err(eval, fmt, ...) wrap_err_T(0, eval, fmt, __VA_ARGS__)
+#define errx(eval, fmt, ...) wrap_err_T(1, eval, fmt, __VA_ARGS__)
+
+#pragma warning(disable: 4244) // possible loss of data inside offtout()