add support for unicode args on windows

Mon, 02 Dec 2019 13:29:36 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Mon, 02 Dec 2019 13:29:36 +0100
changeset 687
9922a349a61a
parent 686
ab159748055c
child 688
d405d2ac78e6

add support for unicode args on windows

dav/main.c file | annotate | diff | comparison | revisions
dav/sync.c file | annotate | diff | comparison | revisions
dav/xattrtool.c file | annotate | diff | comparison | revisions
mingw.mk file | annotate | diff | comparison | revisions
test/main.c file | annotate | diff | comparison | revisions
--- a/dav/main.c	Sun Nov 24 12:40:28 2019 +0100
+++ b/dav/main.c	Mon Dec 02 13:29:36 2019 +0100
@@ -73,7 +73,48 @@
     
 }
 
+int dav_main(int argc, char **argv);
+
+#ifdef _WIN32
+static char* wchar2utf8(const wchar_t *wstr, size_t wlen) {
+    size_t maxlen = wlen * 4;
+    char *ret = malloc(maxlen + 1);
+    int ret_len = WideCharToMultiByte(
+        CP_UTF8,
+        0,
+        wstr,
+        wlen,
+        ret,
+        maxlen,
+        NULL,
+        NULL);
+    ret[ret_len] = 0;
+    return ret;
+}
+
+int wmain(int argc, wchar_t **argv) {
+    char **argv_utf8 = calloc(argc, sizeof(char*));
+    for(int i=0;i<argc;i++) {
+        argv_utf8[i] = wchar2utf8(argv[i], wcslen(argv[i]));
+    }
+    
+    int ret = dav_main(argc, argv_utf8);
+    
+    for(int i=0;i<argc;i++) {
+        free(argv_utf8[i]);
+    }
+    free(argv_utf8);
+    
+    return ret;
+}
+#else
 int main(int argc, char **argv) {
+    return dav_main(argc, argv);
+}
+#endif
+
+
+int dav_main(int argc, char **argv) {
     if(argc < 2) {
         fprintf(stderr, "Missing command\n");
         print_usage(argv[0]);
--- a/dav/sync.c	Sun Nov 24 12:40:28 2019 +0100
+++ b/dav/sync.c	Mon Dec 02 13:29:36 2019 +0100
@@ -112,7 +112,47 @@
     }
 }
 
+int dav_sync_main(int argc, char **argv);
+
+#ifdef _WIN32
+static char* wchar2utf8(const wchar_t *wstr, size_t wlen) {
+    size_t maxlen = wlen * 4;
+    char *ret = malloc(maxlen + 1);
+    int ret_len = WideCharToMultiByte(
+        CP_UTF8,
+        0,
+        wstr,
+        wlen,
+        ret,
+        maxlen,
+        NULL,
+        NULL);
+    ret[ret_len] = 0;
+    return ret;
+}
+
+int wmain(int argc, wchar_t **argv) {
+    char **argv_utf8 = calloc(argc, sizeof(char*));
+    for(int i=0;i<argc;i++) {
+        argv_utf8[i] = wchar2utf8(argv[i], wcslen(argv[i]));
+    }
+    
+    int ret = dav_sync_main(argc, argv_utf8);
+    
+    for(int i=0;i<argc;i++) {
+        free(argv_utf8[i]);
+    }
+    free(argv_utf8);
+    
+    return ret;
+}
+#else
 int main(int argc, char **argv) {
+    return dav_sync_main(argc, argv);
+}
+#endif
+
+int dav_sync_main(int argc, char **argv) {
     if(argc < 2) {
         fprintf(stderr, "Missing command\n");
         print_usage(argv[0]);
@@ -142,10 +182,12 @@
     
     // ignore sigpipe to make sure the program doesn't exit
     // if stdout will be closed (for example by using dav-sync ... | head)
+#ifndef _WIN32
     struct sigaction act;
     memset(&act, 0, sizeof(struct sigaction));
     act.sa_handler = SIG_IGN;
     sigaction(SIGPIPE, &act, NULL);
+#endif
     
     // prepare signal handler thread
     pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
--- a/dav/xattrtool.c	Sun Nov 24 12:40:28 2019 +0100
+++ b/dav/xattrtool.c	Mon Dec 02 13:29:36 2019 +0100
@@ -35,6 +35,10 @@
 #include <string.h>
 #include <errno.h>
 
+#ifdef _WIN32
+#include <Windows.h>
+#endif
+
 #include "libxattr.h"
 
 int attrtool_list(int argc, char **argv, int values);
@@ -51,7 +55,47 @@
     fprintf(stderr, "   remove <file> <name>\n");
 }
 
+int xattr_tool_main(int argc, char **argv);
+
+#ifdef _WIN32
+static char* wchar2utf8(const wchar_t *wstr, size_t wlen) {
+    size_t maxlen = wlen * 4;
+    char *ret = malloc(maxlen + 1);
+    int ret_len = WideCharToMultiByte(
+        CP_UTF8,
+        0,
+        wstr,
+        wlen,
+        ret,
+        maxlen,
+        NULL,
+        NULL);
+    ret[ret_len] = 0;
+    return ret;
+}
+
+int wmain(int argc, wchar_t **argv) {
+    char **argv_utf8 = calloc(argc, sizeof(char*));
+    for(int i=0;i<argc;i++) {
+        argv_utf8[i] = wchar2utf8(argv[i], wcslen(argv[i]));
+    }
+    
+    int ret = xattr_tool_main(argc, argv_utf8);
+    
+    for(int i=0;i<argc;i++) {
+        free(argv_utf8[i]);
+    }
+    free(argv_utf8);
+    
+    return ret;
+}
+#else
 int main(int argc, char **argv) {
+    return xattr_tool_main(argc, argv);
+}
+#endif
+
+int xattr_tool_main(int argc, char **argv) {
     if(argc < 3) {
         print_usage(argv[0]);
         return 1;
--- a/mingw.mk	Sun Nov 24 12:40:28 2019 +0100
+++ b/mingw.mk	Mon Dec 02 13:29:36 2019 +0100
@@ -31,9 +31,9 @@
 AR = ar
 RM = rm
 
-CFLAGS  = -std=gnu99 -g -c 
+CFLAGS  = -std=gnu99 -g -c
 COFLAGS = -o
-LDFLAGS = 
+LDFLAGS = -municode
 LOFLAGS = -o
 ARFLAGS = -r
 RMFLAGS = -f
--- a/test/main.c	Sun Nov 24 12:40:28 2019 +0100
+++ b/test/main.c	Mon Dec 02 13:29:36 2019 +0100
@@ -34,7 +34,17 @@
 #include "base64.h"
 #include "crypto.h"
 
+#ifdef _WIN32
+int wmain(int argc, wchar_t **argv) {
+    return test_main(1, "davtest");
+}
+#else
 int main(int argc, char **argv) {
+    return test_main(argc, argv);
+}
+#endif
+
+int test_main(int argc, char **argv) {
     printf("libidav tests\n");
     printf("-------------\n\n");
     

mercurial