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
-
30
26
ZEND_DLIMPORT int isatty (int fd );
31
27
32
28
static size_t zend_stream_stdio_reader (void * handle , char * buf , size_t len ) /* {{{ */
@@ -41,6 +37,39 @@ static void zend_stream_stdio_closer(void *handle) /* {{{ */
41
37
}
42
38
} /* }}} */
43
39
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
+
44
73
ZEND_API void zend_stream_init_fp (zend_file_handle * handle , FILE * fp , const char * filename ) {
45
74
memset (handle , 0 , sizeof (zend_file_handle ));
46
75
handle -> type = ZEND_HANDLE_FP ;
@@ -96,7 +125,8 @@ static size_t zend_stream_read(zend_file_handle *file_handle, char *buf, size_t
96
125
97
126
ZEND_API int zend_stream_fixup (zend_file_handle * file_handle , char * * buf , size_t * len ) /* {{{ */
98
127
{
99
- size_t size = 0 ;
128
+ size_t size ;
129
+ zend_bool is_fp = 0 ;
100
130
101
131
if (file_handle -> buf ) {
102
132
* buf = file_handle -> buf ;
@@ -111,28 +141,25 @@ ZEND_API int zend_stream_fixup(zend_file_handle *file_handle, char **buf, size_t
111
141
}
112
142
113
143
if (file_handle -> type == ZEND_HANDLE_FP ) {
114
- FILE * fp = file_handle -> handle .fp ;
115
- int is_tty ;
116
- if (!fp ) {
144
+ if (!file_handle -> handle .fp ) {
117
145
return FAILURE ;
118
146
}
119
147
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
-
148
+ is_fp = 1 ;
128
149
file_handle -> type = ZEND_HANDLE_STREAM ;
129
- file_handle -> handle .stream .handle = fp ;
130
- file_handle -> handle .stream .isatty = is_tty ;
150
+ file_handle -> handle .stream .handle = file_handle -> handle . fp ;
151
+ file_handle -> handle .stream .isatty = isatty ( fileno (( FILE * ) file_handle -> handle . stream . handle )) ;
131
152
file_handle -> handle .stream .reader = (zend_stream_reader_t )zend_stream_stdio_reader ;
132
153
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 ;
133
160
}
134
161
135
- if (size ) {
162
+ if (is_fp && ! file_handle -> handle . stream . isatty && size ) {
136
163
file_handle -> buf = * buf = safe_emalloc (1 , size , ZEND_MMAP_AHEAD );
137
164
file_handle -> len = zend_stream_read (file_handle , * buf , size );
138
165
} else {
0 commit comments