summaryrefslogtreecommitdiff
path: root/driver/utils/isMinTTY.c
blob: 3b3ae27bf397b958a4462f9a5534ae7bf07714e5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
/*
 * We need Vista headers to use the GetFileInformationByHandleEx function and
 * the FILE_NAME_INFO struct.
 */
#define WINVER 0x0600
#define _WIN32_WINNT 0x0600

#include <stdbool.h>
#include <windows.h>
#include "isMINTTY.h"

bool isMinTTY() {
  const HANDLE h = GetStdHandle(STD_ERROR_HANDLE);
  if (h == NULL || h == INVALID_HANDLE_VALUE) {
    return false;
  } else if (GetFileType(h) != FILE_TYPE_PIPE) {
    return false;
  }

  const unsigned long bufSize = sizeof(DWORD) + MAX_PATH * sizeof(WCHAR);
  BYTE buf[bufSize];
  PFILE_NAME_INFO pfni = (PFILE_NAME_INFO) buf;

  if (!GetFileInformationByHandleEx(h, FileNameInfo, buf, bufSize)) {
    return false;
  }

  PWSTR fn = pfni->FileName;
  fn[pfni->FileNameLength] = L'\0';

  return ((wcsstr(fn, L"\\cygwin-") || wcsstr(fn, L"\\msys-")) &&
           wcsstr(fn, L"-pty") && wcsstr(fn, L"-master"));
}