From: Markus Fischer Date: Mon, 3 Jun 2002 15:49:38 +0000 (+0000) Subject: - Fix isatty() and ttyname() (Closes #17323, #17333). X-Git-Tag: RELEASE_0_90~47 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=8060dae548120eafc07a561c1e2cf0a48eefa2ff;p=php - Fix isatty() and ttyname() (Closes #17323, #17333). --- diff --git a/ext/posix/posix.c b/ext/posix/posix.c index d3046b2090..0e65a3a2ed 100644 --- a/ext/posix/posix.c +++ b/ext/posix/posix.c @@ -25,6 +25,7 @@ #include "php.h" #include "ext/standard/info.h" #include "ext/standard/php_string.h" +#include "ext/standard/file.h" #include "php_posix.h" #if HAVE_POSIX @@ -580,19 +581,44 @@ PHP_FUNCTION(posix_ctermid) #endif /* }}} */ +#define STREAM_GET_FD() \ + stream = zend_list_find(Z_LVAL_P(z_fd), &rsrc_type); \ + if (!stream || rsrc_type != php_file_le_stream()) { \ + php_error(E_WARNING, "%s() expects argument 1 to be a valid stream resource", \ + get_active_function_name(TSRMLS_C)); \ + return; \ + } \ + if (php_stream_can_cast(stream, PHP_STREAM_AS_FD) == SUCCESS) { \ + php_stream_cast(stream, PHP_STREAM_AS_FD, (void*)&fd, 0); \ + } else { \ + php_error(E_WARNING, "%s() could not use stream of type '%s'", \ + get_active_function_name(TSRMLS_C), stream->ops->label); \ + return; \ + } + + /* {{{ proto string posix_ttyname(int fd) Determine terminal device name (POSIX.1, 4.7.2) */ PHP_FUNCTION(posix_ttyname) { zval *z_fd; char *p; + php_stream *stream; + int rsrc_type, fd; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &z_fd) == FAILURE) return; - convert_to_long(z_fd); + switch (Z_TYPE_P(z_fd)) { + case IS_RESOURCE: + STREAM_GET_FD(); + break; + default: + convert_to_long(z_fd); + fd = Z_LVAL_P(z_fd); + } - if (NULL == (p = ttyname(Z_LVAL_P(z_fd)))) { + if (NULL == (p = ttyname(fd))) { POSIX_G(last_error) = errno; RETURN_FALSE; } @@ -606,18 +632,26 @@ PHP_FUNCTION(posix_ttyname) PHP_FUNCTION(posix_isatty) { zval *z_fd; - int result; + php_stream *stream; + int rsrc_type, fd; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &z_fd) == FAILURE) return; - convert_to_long(z_fd); + switch (Z_TYPE_P(z_fd)) { + case IS_RESOURCE: + STREAM_GET_FD(); + break; + default: + convert_to_long(z_fd); + fd = Z_LVAL_P(z_fd); + } - result = isatty(Z_LVAL_P(z_fd)); - if (!result) + if (isatty(fd)) { + RETURN_TRUE; + } else { RETURN_FALSE; - - RETURN_TRUE; + } } /* }}} */