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

1#ifndef UTIL_H2#define UTIL_H34#ifdef WIN325/* For WriteConsoleW() below */6#include <windows.h>7#include <io.h>8#include <limits.h>9#include <processenv.h>10#include <shellapi.h>11#include <stdlib.h>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/*23 * The Windows console does not do UTF-8.  Bytes written to it, with24 * 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).31 *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.36 */3738static void priv_fwrite(const char *s, size_t len, FILE *fout, int is_tty) {39#ifdef WIN3240  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  }62#endif63  fwrite(s, 1, len, fout);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 */