Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. Common.php

function Common::isStdinATTY

Check if STDIN is a TTY.

Return value

boolean

1 call to Common::isStdinATTY()
Config::__construct in vendor/squizlabs/php_codesniffer/src/Config.php
Creates a Config object and populates it with command line values.

File

vendor/squizlabs/php_codesniffer/src/Util/Common.php, line 191

Class

Common

Namespace

PHP_CodeSniffer\Util

Code

public static function isStdinATTY() {
    // The check is slow (especially calling `tty`) so we static
    // cache the result.
    static $isTTY = null;
    if ($isTTY !== null) {
        return $isTTY;
    }
    if (defined('STDIN') === false) {
        return false;
    }
    // If PHP has the POSIX extensions we will use them.
    if (function_exists('posix_isatty') === true) {
        $isTTY = posix_isatty(STDIN) === true;
        return $isTTY;
    }
    // Next try is detecting whether we have `tty` installed and use that.
    if (defined('PHP_WINDOWS_VERSION_PLATFORM') === true) {
        $devnull = 'NUL';
        $which = 'where';
    }
    else {
        $devnull = '/dev/null';
        $which = 'which';
    }
    $tty = trim(shell_exec("{$which} tty 2> {$devnull}"));
    if (empty($tty) === false) {
        exec("tty -s 2> {$devnull}", $output, $returnValue);
        $isTTY = $returnValue === 0;
        return $isTTY;
    }
    // Finally we will use fstat.  The solution borrowed from
    // https://stackoverflow.com/questions/11327367/detect-if-a-php-script-is-being-run-interactively-or-not
    // This doesn't work on Mingw/Cygwin/... using Mintty but they
    // have `tty` installed.
    $type = [
        'S_IFMT' => 0170000,
        'S_IFIFO' => 010000,
    ];
    $stat = fstat(STDIN);
    $mode = $stat['mode'] & $type['S_IFMT'];
    $isTTY = $mode !== $type['S_IFIFO'];
    return $isTTY;
}

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal