Add missing space
[llpp.git] / wsel.c
blobc59fbf5817da487742042f0c62034ebd44993bbb
1 #define WIN32_LEAN_AND_MEAN
2 #include <stdio.h>
3 #include <errno.h>
4 #include <stdlib.h>
5 #include <windows.h>
7 #ifdef _MSC_VER
8 #define NORETURN __declspec (noreturn)
9 #elif defined __GNUC__
10 #define NORETURN __attribute__ ((noreturn))
11 #else
12 #define NORETURN
13 #endif
15 static void NORETURN winerr (int exitcode, const char *fmt, ...)
17 va_list ap;
19 va_start (ap, fmt);
20 vfprintf (stderr, fmt, ap);
21 va_end (ap);
22 fprintf (stderr, ": winerror 0x%lx\n", GetLastError ());
23 exit (exitcode);
26 int main (void)
28 HANDLE h;
29 LPWSTR tp;
30 size_t n, m, k;
31 char buf[4096];
33 if (!OpenClipboard (NULL)) winerr (1, "OpenClipboard");
34 EmptyClipboard ();
36 n = fread (buf, 1, 4096, stdin);
37 if (n < 0) {
38 fprintf (stderr, "fread failed: %s\n", strerror (errno));
39 return 2;
41 if (!n) return 3;
43 m = MultiByteToWideChar (
44 CP_UTF8,
45 MB_ERR_INVALID_CHARS,
46 buf,
48 NULL,
51 if (m <= 0) winerr (4, "MultiByteToWideChar %d", m);
53 h = GlobalAlloc (GMEM_MOVEABLE, (m+1) * sizeof (*tp));
54 if (!h) winerr (5, "GlobalAlloc");
56 tp = GlobalLock (h);
57 k = MultiByteToWideChar (
58 CP_UTF8,
59 MB_ERR_INVALID_CHARS,
60 buf,
62 tp,
65 if (k != m) winerr (6, "MultiByteToWideChar %d %d", k, m);
66 tp[k] = 0;
67 GlobalUnlock (h);
68 if (!SetClipboardData (CF_UNICODETEXT, h)) winerr (7, "SetClipboardData");
69 if (GlobalFree (h)) winerr (8, "GlobalFree");
70 if (!CloseClipboard ()) winerr (9, "CloseClipboard");
71 return 0;