]> granicus.if.org Git - php/commitdiff
First attempt at fd_set() and select()
authorEvan Klinger <evan@php.net>
Wed, 24 Nov 1999 03:05:10 +0000 (03:05 +0000)
committerEvan Klinger <evan@php.net>
Wed, 24 Nov 1999 03:05:10 +0000 (03:05 +0000)
# Please test thoroughly

ext/standard/file.c
ext/standard/file.h

index 2a9d98a2ccebb8b1363493a40dc323e5ec9c8d7c..8f0c1a2ddd447319b9798640450a2fd57cb68796 100644 (file)
@@ -111,6 +111,9 @@ static void _file_upload_dtor(char *file);
 /* sharing globals is *evil* */
 static int le_fopen,le_popen, le_socket, le_uploads; 
 
+static fd_set readfd;
+static int max_fd;
+
 /* }}} */
 /* {{{ tempnam */
 
@@ -241,6 +244,8 @@ function_entry file_functions[] = {
 #if (0 && defined(HAVE_SYS_TIME_H) && HAVE_SETSOCKOPT && defined(SO_SNDTIMEO) && defined(SO_RCVTIMEO))
        PHP_FE(set_socket_timeout,      NULL)
 #endif
+       PHP_FE(fd_set, NULL)
+       PHP_FE(select, NULL)
        {NULL, NULL, NULL}
 };
 
@@ -1661,9 +1666,70 @@ PHP_FUNCTION(fgetcsv) {
 
 /* }}} */
 
-/*<
- * Local variables:
- * tab-width: 4
- * c-basic-offset: 4
- * End:
- */
+PHP_FUNCTION(fd_set)
+{
+        pval **arg;
+        void *what;
+        int type, fd;
+
+        if(ARG_COUNT(ht) <= 0) {
+                php_error(E_WARNING, "fd_set: Must be passed at least one value" );
+                var_uninit(return_value);
+                return;
+        }
+        else if(ARG_COUNT(ht) == 1) {
+                if(getParametersEx(1, &arg) == FAILURE) {
+                        WRONG_PARAM_COUNT;
+                }
+        what = zend_fetch_resource(arg,-1,"Select",&type,3,le_fopen,le_socket,le_popen);
+        ZEND_VERIFY_RESOURCE(what);
+        if(type == le_socket) {
+                fd = *(int *)what;
+        } else {
+                fd = fileno((FILE *)what);
+        }
+        max_fd = fd;
+        FD_ZERO(&readfd);
+        FD_SET(max_fd, &readfd);
+        }
+        else {
+                pval ***args = (pval ***) emalloc(sizeof(pval **) * ARG_COUNT(ht));
+                pval **max, result;
+                int i;
+                if(getParametersArrayEx(ARG_COUNT(ht), args) == FAILURE) {
+                        efree(args);
+                        WRONG_PARAM_COUNT;
+                }
+                FD_ZERO(&readfd);
+                for(i = 0; i < ARG_COUNT(ht); i++) {
+                        what = zend_fetch_resource(*args,-1,"select",&type,3,le_fopen,le_socket,le_popen);
+                        ZEND_VERIFY_RESOURCE(what);
+                        if(type == le_socket) {
+                                fd = *(int *)what;
+                        } else {
+                                fd = fileno((FILE *)what);
+                        }
+                FD_SET(fd, &readfd);
+                if(fd > max_fd) max_fd = fd;
+               }
+       }
+       RETURN_LONG(1);
+}
+
+PHP_FUNCTION(select)
+{
+       pval **timeout;
+       struct timeval tv;
+
+       if(ARG_COUNT(ht) != 1 || getParametersEx(1, &timeout) == FAILURE) {
+               WRONG_PARAM_COUNT;
+       }
+
+       convert_to_long_ex(timeout);
+
+       tv.tv_sec = (*timeout)->value.lval / 1000000;
+       tv.tv_usec = (*timeout)->value.lval % 1000000;
+
+       RETURN_LONG(select(max_fd + 1,&readfd,NULL,NULL,((*timeout)->value.lval <= 0) ? NULL : &tv));
+}
+
index 10824638ebf490323220c9e3ebb78f471fdae305..0f4b521a5b8d521f5aca3f024ac7f8bd15ee92c5 100644 (file)
@@ -68,6 +68,8 @@ PHP_FUNCTION(set_socket_timeout);
 PHP_FUNCTION(set_file_buffer);
 PHP_FUNCTION(get_meta_tags);
 PHP_FUNCTION(flock);
+PHP_FUNCTION(fd_set);
+PHP_FUNCTION(select);
 
 PHPAPI int _php3_set_sock_blocking(int socketd, int block);
 PHPAPI int php_file_le_fopen(void);