]> granicus.if.org Git - php/commitdiff
- Fix isatty() and ttyname() (Closes #17323, #17333).
authorMarkus Fischer <mfischer@php.net>
Mon, 3 Jun 2002 15:49:38 +0000 (15:49 +0000)
committerMarkus Fischer <mfischer@php.net>
Mon, 3 Jun 2002 15:49:38 +0000 (15:49 +0000)
ext/posix/posix.c

index d3046b20908e21b28be78b8ecb5dac3a45fc3ab1..0e65a3a2ed6f8b0ca0756ed99e24a21533e7203a 100644 (file)
@@ -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;
+       }
 }
 /* }}} */