public inbox for cygwin@cygwin.com
 help / color / mirror / Atom feed
From: Takashi Yano <takashi.yano@nifty.ne.jp>
To: cygwin@cygwin.com
Subject: Re: [ANNOUNCEMENT] Updated: cygutils 1.4.16-7
Date: Sat, 25 Sep 2021 16:23:54 +0900	[thread overview]
Message-ID: <20210925162354.27f3b28c283470805d965d11@nifty.ne.jp> (raw)
In-Reply-To: <20210925133125.8f894de4e596a71e7f24961f@nifty.ne.jp>

[-- Attachment #1: Type: text/plain, Size: 1069 bytes --]

On Sat, 25 Sep 2021 13:31:25 +0900
Takashi Yano wrote:
> Hi Mark,
> 
> Sometimes, putclip shows error
> "Unable to open the clipboard"
> when I run 'echo A | putclip'.
> 
> I digged into this problem, and found OpenClipboard() sometimes
> fails with error ERROR_ACCESS_DENIED if it is called just after
> SetClipboardData() and CloseClipboard().
> 
> Currently, putclip calls OpenClipboard()/CloseClipboard() four
> times. Is there any reason why closing and reopening clipboard
> several times?
> 
> Is there any problem if you open clipboard once and close it at
> the end of function int putclip() just like the patch attached?
> 
> The problem above disappears if the patch is applied.

I noticed that putclip/getclip have another problem.

These utils assume the charset is always UTF-8, however, cygwin
supports locales which charset is not UTF-8 (such as CP932, EUC-JP,
CP850, CP1251, etc.).

Therefore, what abount using mbstowcs/wcstombs rather than
MultiByteToWideChar/WideCharToMultiByte like the patch attached?

-- 
Takashi Yano <takashi.yano@nifty.ne.jp>

[-- Attachment #2: clip-charset.patch --]
[-- Type: application/octet-stream, Size: 3207 bytes --]

diff --git a/src/cygwin-cygutils/src/clip/getclip.c b/src/cygwin-cygutils/src/clip/getclip.c
index 82e7070..772dfe5 100644
--- a/src/cygwin-cygutils/src/clip/getclip.c
+++ b/src/cygwin-cygutils/src/clip/getclip.c
@@ -32,6 +32,7 @@
 #include <wchar.h>
 #include <sys/cygwin.h>
 #include <w32api/stringapiset.h>
+#include <locale.h>
 
 #define DEBUGGING 0 // Set 1 to display debug output on stderr
 
@@ -198,6 +199,8 @@ main (int argc, const char **argv)
       goto exit;
     }
   rest = poptGetArgs (optCon);
+
+  setlocale (LC_ALL, "");
   if (rest == NULL)
     ec |= getclip (stdout, flags, stderr, program_name);
   else
@@ -425,16 +428,14 @@ getclip (FILE * out, flags_struct flags, FILE * f, char *name)
               int srclen = wcslen (lpwstr); // wide-char count
               int dstlen = srclen << 2; // byte count
               LPSTR dst = (LPSTR) malloc (dstlen);
-              int res = WideCharToMultiByte (CP_UTF8, 0, lpwstr, srclen,
-                                             dst, dstlen, NULL, NULL);
+              int res = wcstombs (dst, lpwstr, dstlen);
               if (res > 0)
                 fwrite (dst, sizeof (char), res, out);
-              else if (res == 0)
+              else if (res == -1)
                 {
 #if DEBUGGING
-                  DWORD err = GetLastError ();
-                  /* look up error code displayed here in w32api/winerror.h */
-                  fprintf (stderr, "WideCharToMultiByte returns %ld\n", err);
+                  /* look up error code displayed here in sys/errno.h */
+                  fprintf (stderr, "wcstombs error (errno=%d)\n", errno);
 #endif
                 }
               free (dst);
diff --git a/src/cygwin-cygutils/src/clip/putclip.c b/src/cygwin-cygutils/src/clip/putclip.c
index abdb8cf..ae58b62 100644
--- a/src/cygwin-cygutils/src/clip/putclip.c
+++ b/src/cygwin-cygutils/src/clip/putclip.c
@@ -32,6 +32,7 @@
 #include <wchar.h>
 #include <sys/cygwin.h>
 #include <w32api/stringapiset.h>
+#include <locale.h>
 
 #define DEBUGGING 0 // Set 1 to display debug output on stderr
 
@@ -198,6 +199,7 @@ main (int argc, const char **argv)
 
   rest = poptGetArgs (optCon);
 
+  setlocale (LC_ALL, "");
   if (rest == NULL)
     ec |= putclip (stdin, flags, stderr, program_name);
   else
@@ -608,17 +610,16 @@ putclip (FILE * in, flags_struct flags, FILE * f, char *name)
           return (PUTCLIP_ERR);
         }
 
-      int res = MultiByteToWideChar (CP_UTF8, 0, convbuf, convlen,
-                                     (LPWSTR) clipbuf, convlen + 1);
-      if (res == 0)
+      convbuf[convlen] = '\0';
+      int res = mbstowcs (clipbuf, convbuf, convlen + 1);
+      if (res == -1)
         {
 #if DEBUGGING
-          DWORD err = GetLastError ();
-          /* look up error code displayed here in w32api/winerror.h */
-          fprintf (stderr, "MultiByteToWideChar returns %ld\n", err);
+          /* look up error code displayed here in sys/errno.h */
+          fprintf (stderr, "mbstowcs error (errno=%d)\n", errno);
 #endif
         }
-      else /* res != 0 */
+      else /* res != -1 */
         {
           *((WCHAR *) clipbuf + res) = 0; /* NULL-terminate just in case */
 #if DEBUGGING

  reply	other threads:[~2021-09-25  7:24 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-09-23  6:37 Mark Geisert
2021-09-25  4:31 ` Takashi Yano
2021-09-25  7:23   ` Takashi Yano [this message]
2021-09-25  8:12     ` Takashi Yano
2021-09-26  9:44       ` Mark Geisert
2021-09-26  9:43     ` Mark Geisert
2021-09-26  9:40   ` Mark Geisert
2021-09-26 18:41     ` Doug Henderson
2021-09-27  0:46       ` Westcoast Human
2021-09-27  6:13         ` Brian Inglis
2021-09-27  6:29           ` Brian Inglis
2021-09-27 20:00           ` Richard Beels
2021-09-27 20:00           ` Richard Beels
2021-09-27 20:00           ` Richard Beels
2021-09-27 10:10         ` Takashi Yano
2021-09-27 11:07           ` Takashi Yano
2021-09-28  3:58             ` Westcoast Human

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210925162354.27f3b28c283470805d965d11@nifty.ne.jp \
    --to=takashi.yano@nifty.ne.jp \
    --cc=cygwin@cygwin.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).