]> granicus.if.org Git - php/commitdiff
- Implemented request #26158/bug #53465 (open arbitrary file descriptor with fopen)
authorGustavo André dos Santos Lopes <cataphract@php.net>
Sat, 11 Dec 2010 01:52:13 +0000 (01:52 +0000)
committerGustavo André dos Santos Lopes <cataphract@php.net>
Sat, 11 Dec 2010 01:52:13 +0000 (01:52 +0000)
NEWS
ext/standard/php_fopen_wrapper.c

diff --git a/NEWS b/NEWS
index 69f34c55f671bbdab2b5cc12b4a51a4417562edd..2b1eacbbaf12372291080dd460a513d206035aec 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -21,6 +21,9 @@
   . Fixed bug #53515 (property_exists incorrect on ArrayObject null and 0
     values). (Felipe)
 
+- Streams
+  . Implemented FR #26158 (open arbitrary file descriptor with fopen)
+
 09 Dec 2010, PHP 5.3.4
 - Upgraded bundled Sqlite3 to version 3.7.3. (Ilia)
 - Upgraded bundled PCRE to version 8.10. (Ilia)
index 8340da42a8f3c71f850cea5f6b1bbe28159a4fd7..2a7a2793c703a8d3f86b70b67f7eef647db8d336 100644 (file)
@@ -257,6 +257,39 @@ php_stream * php_stream_url_wrap_php(php_stream_wrapper *wrapper, char *path, ch
                } else {
                        fd = dup(STDERR_FILENO);
                }
+       } else if (!strncasecmp(path, "fd/", 3)) {
+               char       *start,
+                                  *end;
+               long       fildes_ori;
+               int                dtablesize;
+
+               start = &path[3];
+               fildes_ori = strtol(start, &end, 10);
+               if (end == start || (*end != '\0' && *end != '/')) {
+                       php_stream_wrapper_log_error(wrapper, options TSRMLS_CC,
+                               "php://fd/ stream must be specified in the form php://fd/<orig fd>");
+                       return NULL;
+               }
+
+#if HAVE_UNISTD_H
+               dtablesize = getdtablesize();
+#else
+               dtablesize = INT_MAX;
+#endif
+
+               if (fildes_ori < 0 || fildes_ori >= dtablesize) {
+                       php_stream_wrapper_log_error(wrapper, options TSRMLS_CC,
+                               "The file descriptors must be non-negative numbers smaller than %d", dtablesize);
+                       return NULL;
+               }
+               
+               fd = dup(fildes_ori);
+               if (fd == -1) {
+                       php_stream_wrapper_log_error(wrapper, options TSRMLS_CC,
+                               "Error duping file descriptor %d; possibly it doesn't exist: "
+                               "[%d]: %s", fildes_ori, errno, strerror(errno));
+                       return NULL;
+               }
        } else if (!strncasecmp(path, "filter/", 7)) {
                /* Save time/memory when chain isn't specified */
                if (strchr(mode, 'r') || strchr(mode, '+')) {