Skip to content

Commit

Permalink
Cygwin: termios: Refactor the function is_console_app().
Browse files Browse the repository at this point in the history
Signed-off-by: Takashi Yano <[email protected]>
  • Loading branch information
tyan0 committed Aug 28, 2023
1 parent eb1584b commit 50ff790
Showing 1 changed file with 8 additions and 10 deletions.
18 changes: 8 additions & 10 deletions winsup/cygwin/fhandler/termios.cc
Original file line number Diff line number Diff line change
Expand Up @@ -704,22 +704,20 @@ static bool
is_console_app (const WCHAR *filename)
{
HANDLE h;
const int id_offset = 92;
h = CreateFileW (filename, GENERIC_READ, FILE_SHARE_READ,
NULL, OPEN_EXISTING, 0, NULL);
char buf[1024];
DWORD n;
ReadFile (h, buf, sizeof (buf), &n, 0);
CloseHandle (h);
char *p = (char *) memmem (buf, n, "PE\0\0", 4);
if (p && p + id_offset < buf + n)
return p[id_offset] == '\003'; /* 02: GUI, 03: console */
else
{
wchar_t *e = wcsrchr (filename, L'.');
if (e && (wcscasecmp (e, L".bat") == 0 || wcscasecmp (e, L".cmd") == 0))
return true;
}
/* The offset of Subsystem is the same for both IMAGE_NT_HEADERS32 and
IMAGE_NT_HEADERS64, so only IMAGE_NT_HEADERS32 is used here. */
IMAGE_NT_HEADERS32 *p = (IMAGE_NT_HEADERS32 *) memmem (buf, n, "PE\0\0", 4);
if (p && (char *) &p->OptionalHeader.DllCharacteristics <= buf + n)
return p->OptionalHeader.Subsystem == IMAGE_SUBSYSTEM_WINDOWS_CUI;
wchar_t *e = wcsrchr (filename, L'.');
if (e && (wcscasecmp (e, L".bat") == 0 || wcscasecmp (e, L".cmd") == 0))
return true;
return false;
}

Expand Down

0 comments on commit 50ff790

Please sign in to comment.