]> granicus.if.org Git - php/commitdiff
Use POSIX IO here, mainly to fix interactive mode.
authorSascha Schumann <sas@php.net>
Tue, 13 May 2003 23:36:38 +0000 (23:36 +0000)
committerSascha Schumann <sas@php.net>
Tue, 13 May 2003 23:36:38 +0000 (23:36 +0000)
This also fixes a possible cpu-time hog bug where a file would consist
of "#" and nothing else.

sapi/cli/php_cli.c

index 42cc540fbd54584671b24b26974f677a327a26ba..235f52157fb6f55d10855c0573ac1f5022c54310 100644 (file)
 #if HAVE_SETLOCALE
 #include <locale.h>
 #endif
+#if HAVE_FCNTL_H
+#include <fcntl.h>
+#endif
+
 #include "zend.h"
 #include "zend_extensions.h"
 #include "php_ini.h"
 
 #include "php_getopt.h"
 
+#ifndef O_BINARY
+#define O_BINARY 0
+#endif
+
 #define PHP_MODE_STANDARD    1
 #define PHP_MODE_HIGHLIGHT   2
 #define PHP_MODE_INDENT      3
@@ -716,7 +724,7 @@ int main(int argc, char *argv[])
                        ap_php_optind++;
                }
                if (script_file) {
-                       if (!(file_handle.handle.fp = VCWD_FOPEN(script_file, "rb"))) {
+                       if (!(file_handle.handle.fd = VCWD_OPEN(script_file, O_RDONLY | O_BINARY))) {
                                SG(headers_sent) = 1;
                                SG(request_info).no_headers = 1;
                                php_printf("Could not open input file: %s.\n", script_file);
@@ -725,27 +733,30 @@ int main(int argc, char *argv[])
                        file_handle.filename = script_file;
                        script_filename = script_file;
                        /* #!php support */
-                       c = fgetc(file_handle.handle.fp);
+                       c = 0;
+                       read(file_handle.handle.fd, &c, 1);
                        if (c == '#') {
                                while (c != 10 && c != 13) {
-                                       c = fgetc(file_handle.handle.fp);       /* skip to end of line */
+                                       /* skip to end of line */
+                                       if (read(file_handle.handle.fd, &c, 1) != 1) 
+                                               break;
                                }
                                /* handle situations where line is terminated by \r\n */
                                if (c == 13) {
-                                       if (fgetc(file_handle.handle.fp) != 10) {
+                                       if (read(file_handle.handle.fd, &c, 1) == 1 && c != 10) {
                                                long pos = ftell(file_handle.handle.fp);
                                                fseek(file_handle.handle.fp, pos - 1, SEEK_SET);
                                        }
                                }
                                CG(zend_lineno) = -2;
                        } else {
-                               rewind(file_handle.handle.fp);
+                               lseek(file_handle.handle.fd, 0, SEEK_SET);
                        }
                } else {
                        file_handle.filename = "-";
-                       file_handle.handle.fp = stdin;
+                       file_handle.handle.fd = STDIN_FILENO;
                }
-               file_handle.type = ZEND_HANDLE_FP;
+               file_handle.type = ZEND_HANDLE_FD;
                file_handle.opened_path = NULL;
                file_handle.free_filename = 0;
                php_self = file_handle.filename;
@@ -761,7 +772,7 @@ int main(int argc, char *argv[])
 
                if (php_request_startup(TSRMLS_C)==FAILURE) {
                        *arg_excp = arg_free;
-                       fclose(file_handle.handle.fp);
+                       zend_file_handle_dtor(&file_handle);
                        SG(headers_sent) = 1;
                        SG(request_info).no_headers = 1;
                        php_request_shutdown((void *) 0);
@@ -798,7 +809,7 @@ int main(int argc, char *argv[])
                case PHP_MODE_STRIP:
                        if (open_file_for_scanning(&file_handle TSRMLS_CC)==SUCCESS) {
                                zend_strip(TSRMLS_C);
-                               fclose(file_handle.handle.fp);
+                               zend_file_handle_dtor(&file_handle);
                        }
                        goto out;
                        break;
@@ -809,7 +820,7 @@ int main(int argc, char *argv[])
                                if (open_file_for_scanning(&file_handle TSRMLS_CC)==SUCCESS) {
                                        php_get_highlight_struct(&syntax_highlighter_ini);
                                        zend_highlight(&syntax_highlighter_ini TSRMLS_CC);
-                                       fclose(file_handle.handle.fp);
+                                       zend_file_handle_dtor(&file_handle);
                                }
                                goto out;
                        }
@@ -819,7 +830,7 @@ int main(int argc, char *argv[])
                case PHP_MODE_INDENT:
                        open_file_for_scanning(&file_handle TSRMLS_CC);
                        zend_indent();
-                       fclose(file_handle.handle.fp);
+                       zend_file_handle_dtor(&file_handle);
                        goto out;
                        break;
 #endif