23
23
#include "zend_compile.h"
24
24
#include "zend_stream.h"
25
25
26
+ #ifndef S_ISREG
27
+ # define S_ISREG (m ) 1
28
+ #endif
29
+
26
30
ZEND_DLIMPORT int isatty (int fd );
27
31
28
32
static size_t zend_stream_stdio_reader (void * handle , char * buf , size_t len ) /* {{{ */
@@ -37,39 +41,6 @@ static void zend_stream_stdio_closer(void *handle) /* {{{ */
37
41
}
38
42
} /* }}} */
39
43
40
- static size_t zend_stream_stdio_fsizer (void * handle ) /* {{{ */
41
- {
42
- zend_stat_t buf ;
43
- if (handle && zend_fstat (fileno ((FILE * )handle ), & buf ) == 0 ) {
44
- #ifdef S_ISREG
45
- if (!S_ISREG (buf .st_mode )) {
46
- return 0 ;
47
- }
48
- #endif
49
- return buf .st_size ;
50
- }
51
- return 0 ;
52
- } /* }}} */
53
-
54
- static size_t zend_stream_fsize (zend_file_handle * file_handle ) /* {{{ */
55
- {
56
- zend_stat_t buf ;
57
-
58
- if (file_handle -> type == ZEND_HANDLE_STREAM ) {
59
- return file_handle -> handle .stream .fsizer (file_handle -> handle .stream .handle );
60
- }
61
- if (file_handle -> handle .fp && zend_fstat (fileno (file_handle -> handle .fp ), & buf ) == 0 ) {
62
- #ifdef S_ISREG
63
- if (!S_ISREG (buf .st_mode )) {
64
- return 0 ;
65
- }
66
- #endif
67
- return buf .st_size ;
68
- }
69
-
70
- return -1 ;
71
- } /* }}} */
72
-
73
44
ZEND_API void zend_stream_init_fp (zend_file_handle * handle , FILE * fp , const char * filename ) {
74
45
memset (handle , 0 , sizeof (zend_file_handle ));
75
46
handle -> type = ZEND_HANDLE_FP ;
@@ -125,8 +96,7 @@ static size_t zend_stream_read(zend_file_handle *file_handle, char *buf, size_t
125
96
126
97
ZEND_API int zend_stream_fixup (zend_file_handle * file_handle , char * * buf , size_t * len ) /* {{{ */
127
98
{
128
- size_t size ;
129
- zend_bool is_fp = 0 ;
99
+ size_t size = 0 ;
130
100
131
101
if (file_handle -> buf ) {
132
102
* buf = file_handle -> buf ;
@@ -141,25 +111,28 @@ ZEND_API int zend_stream_fixup(zend_file_handle *file_handle, char **buf, size_t
141
111
}
142
112
143
113
if (file_handle -> type == ZEND_HANDLE_FP ) {
144
- if (!file_handle -> handle .fp ) {
114
+ FILE * fp = file_handle -> handle .fp ;
115
+ int is_tty ;
116
+ if (!fp ) {
145
117
return FAILURE ;
146
118
}
147
119
148
- is_fp = 1 ;
120
+ is_tty = isatty (fileno (fp ));
121
+ if (!is_tty ) {
122
+ zend_stat_t buf ;
123
+ if (zend_fstat (fileno (fp ), & buf ) == 0 && S_ISREG (buf .st_mode )) {
124
+ size = buf .st_size ;
125
+ }
126
+ }
127
+
149
128
file_handle -> type = ZEND_HANDLE_STREAM ;
150
- file_handle -> handle .stream .handle = file_handle -> handle . fp ;
151
- file_handle -> handle .stream .isatty = isatty ( fileno (( FILE * ) file_handle -> handle . stream . handle )) ;
129
+ file_handle -> handle .stream .handle = fp ;
130
+ file_handle -> handle .stream .isatty = is_tty ;
152
131
file_handle -> handle .stream .reader = (zend_stream_reader_t )zend_stream_stdio_reader ;
153
132
file_handle -> handle .stream .closer = (zend_stream_closer_t )zend_stream_stdio_closer ;
154
- file_handle -> handle .stream .fsizer = (zend_stream_fsizer_t )zend_stream_stdio_fsizer ;
155
- }
156
-
157
- size = zend_stream_fsize (file_handle );
158
- if (size == (size_t )-1 ) {
159
- return FAILURE ;
160
133
}
161
134
162
- if (is_fp && ! file_handle -> handle . stream . isatty && size ) {
135
+ if (size ) {
163
136
file_handle -> buf = * buf = safe_emalloc (1 , size , ZEND_MMAP_AHEAD );
164
137
file_handle -> len = zend_stream_read (file_handle , * buf , size );
165
138
} else {
0 commit comments