Remove stray debugging aid
[llpp.git] / wsel.c
blob497d52b82247666093244403accde552a25fa8ec
1 #define WIN32_LEAN_AND_MEAN
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <windows.h>
6 #ifdef _MSC_VER
7 #define NORETURN __declspec (noreturn)
8 #elif defined __GNUC__
9 #define NORETURN __attribute__ ((noreturn))
10 #else
11 #define NORETURN
12 #endif
14 static void NORETURN winerr (int exitcode, const char *fmt, ...)
16 va_list ap;
18 va_start (ap, fmt);
19 vfprintf (stderr, fmt, ap);
20 va_end (ap);
21 fprintf (stderr, ": winerror 0x%lx\n", GetLastError ());
22 exit (exitcode);
25 int main (void)
27 HANDLE h;
28 LPWSTR tp;
29 size_t n, m, k;
30 char buf[4096];
32 if (!OpenClipboard (NULL)) winerr (1, "OpenClipboard");
33 EmptyClipboard ();
35 n = fread (buf, 1, 4096, stdin);
36 if (n < 0) {
37 fprintf (stderr, "fread failed: %s\n", strerror (errno));
38 return 2;
40 if (!n) return 3;
42 m = MultiByteToWideChar (
43 CP_UTF8,
44 MB_ERR_INVALID_CHARS,
45 buf,
47 NULL,
50 if (m <= 0) winerr (4, "MultiByteToWideChar %d", m);
52 h = GlobalAlloc (GMEM_MOVEABLE, (m+1) * sizeof (*tp));
53 if (!h) winerr (5, "GlobalAlloc");
55 tp = GlobalLock (h);
56 k = MultiByteToWideChar (
57 CP_UTF8,
58 MB_ERR_INVALID_CHARS,
59 buf,
61 tp,
64 if (k != m) winerr (6, "MultiByteToWideChar %d %d", k, m);
65 tp[k] = 0;
66 GlobalUnlock (h);
67 if (!SetClipboardData (CF_UNICODETEXT, h)) winerr (7, "SetClipboardData");
68 if (GlobalFree (h)) winerr (8, "GlobalFree");
69 if (!CloseClipboard ()) winerr (9, "CloseClipboard");
70 return 0;