From: Sascha Schumann Date: Tue, 13 May 2003 23:36:38 +0000 (+0000) Subject: Use POSIX IO here, mainly to fix interactive mode. X-Git-Tag: php-4.3.2RC3~29 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=bbf39825e17ad241f446c7e7425b4cc0f5b0f0a4;p=php Use POSIX IO here, mainly to fix interactive mode. This also fixes a possible cpu-time hog bug where a file would consist of "#" and nothing else. --- diff --git a/sapi/cli/php_cli.c b/sapi/cli/php_cli.c index 42cc540fbd..235f52157f 100644 --- a/sapi/cli/php_cli.c +++ b/sapi/cli/php_cli.c @@ -46,6 +46,10 @@ #if HAVE_SETLOCALE #include #endif +#if HAVE_FCNTL_H +#include +#endif + #include "zend.h" #include "zend_extensions.h" #include "php_ini.h" @@ -75,6 +79,10 @@ #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