Skip to content
Myr

util.h

This file

Path

src/util.h

88 lines · 2.6 KiB · C

Last changed by

Use WriteConsoleW() in priv_fwrite() too (#3607)Bumsu Hyeonbefore adoptiondd409de

History of this file

Take it

The walk stopped after 200 commits: lines older than fa6a2ff point at it.

website: remove unused root path on building website (#3248)itchynybefore adoptionfa6a2ff

lines 1–4why, one step back
1#ifndef UTIL_H
2#define UTIL_H34#ifdef WIN32

Use WriteConsoleW() in priv_fwrite() too (#3607)Bumsu Hyeonbefore adoptiondd409de

line 5why, one step back
5/* For WriteConsoleW() below */

website: remove unused root path on building website (#3248)itchynybefore adoptionfa6a2ff

lines 6–7why, one step back
6#include <windows.h>
7#include <io.h>

Use WriteConsoleW() in priv_fwrite() too (#3607)Bumsu Hyeonbefore adoptiondd409de

line 8why, one step back
8#include <limits.h>

website: remove unused root path on building website (#3248)itchynybefore adoptionfa6a2ff

lines 9–10why, one step back
9#include <processenv.h>
10#include <shellapi.h>

Use WriteConsoleW() in priv_fwrite() too (#3607)Bumsu Hyeonbefore adoptiondd409de

line 11why, one step back
11#include <stdlib.h>

website: remove unused root path on building website (#3248)itchynybefore adoptionfa6a2ff

lines 12–22why, one step back
12#include <wchar.h>
13#include <wtypes.h>14#endif1516#include "jv.h"1718jv expand_path(jv);19jv get_home(void);20jv jq_realpath(jv);2122/*

Use WriteConsoleW() in priv_fwrite() too (#3607)Bumsu Hyeonbefore adoptiondd409de

lines 23–30why, one step back
23 * The Windows console does not do UTF-8. Bytes written to it, with
24 * fwrite() or with WriteFile() alike, are decoded with the console's25 * output code page, so anything but ASCII comes out as mojibake unless26 * the user happened to run `chcp 65001` first. The wide character27 * console API is code page independent, so convert to UTF-16 and use28 * WriteConsoleW() instead. We must not call SetConsoleOutputCP(): the29 * code page belongs to the console rather than to us, so changing it30 * also changes the shell we were started from (see #1121 and #1184).

website: remove unused root path on building website (#3248)itchynybefore adoptionfa6a2ff

line 31why, one step back
31 *

Use WriteConsoleW() in priv_fwrite() too (#3607)Bumsu Hyeonbefore adoptiondd409de

lines 32–35why, one step back
32 * Only a console needs this; a file or a pipe wants the UTF-8 bytes.
33 * The is_tty flag alone cannot tell the two apart, as it is computed34 * once, from stdout, and passed along even for writes to stderr, so35 * ask the handle we are about to write to.

website: remove unused root path on building website (#3248)itchynybefore adoptionfa6a2ff

lines 36–39why, one step back
36 */
3738static void priv_fwrite(const char *s, size_t len, FILE *fout, int is_tty) {39#ifdef WIN32

Use WriteConsoleW() in priv_fwrite() too (#3607)Bumsu Hyeonbefore adoptiondd409de

lines 40–61why, one step back
40 if (is_tty && len > 0 && len <= INT_MAX / sizeof(wchar_t)) {
41 HANDLE h = (HANDLE)_get_osfhandle(fileno(fout));42 DWORD mode;43 if (GetConsoleMode(h, &mode)) {44 int wlen = MultiByteToWideChar(CP_UTF8, 0, s, (int)len, NULL, 0);45 wchar_t *ws = wlen > 0 ? malloc(wlen * sizeof(*ws)) : NULL;46 if (ws != NULL) {47 wlen = MultiByteToWideChar(CP_UTF8, 0, s, (int)len, ws, wlen);48 fflush(fout); /* keep our order with stdio writes to this stream */49 for (int done = 0; done < wlen;) {50 DWORD written;51 /* WriteConsoleW() may write fewer characters than asked for. */52 if (!WriteConsoleW(h, ws + done, wlen - done, &written, NULL) ||53 written == 0)54 break;55 done += written;56 }57 free(ws);58 return;59 }60 }61 }

website: remove unused root path on building website (#3248)itchynybefore adoptionfa6a2ff

line 62why, one step back
62#endif

Use WriteConsoleW() in priv_fwrite() too (#3607)Bumsu Hyeonbefore adoptiondd409de

line 63why, one step back
63 fwrite(s, 1, len, fout);

website: remove unused root path on building website (#3248)itchynybefore adoptionfa6a2ff

lines 64–88why, one step back
64}
6566const void *_jq_memmem(const void *haystack, size_t haystacklen,67 const void *needle, size_t needlelen);6869#ifndef MIN70#define MIN(a,b) \71 ({ __typeof__ (a) _a = (a); \72 __typeof__ (b) _b = (b); \73 _a < _b ? _a : _b; })74#endif75#ifndef MAX76#define MAX(a,b) \77 ({ __typeof__ (a) _a = (a); \78 __typeof__ (b) _b = (b); \79 _a > _b ? _a : _b; })80#endif8182#include <time.h>8384#ifndef HAVE_STRPTIME85char* strptime(const char *buf, const char *fmt, struct tm *tm);86#endif8788#endif /* UTIL_H */