@@ -242,35 +242,38 @@ PHPAPI php_stream *_php_stream_fopen_tmpfile(int dummy STREAMS_DC)
242
242
return php_stream_fopen_temporary_file (NULL , "php" , NULL );
243
243
}
244
244
245
+ static void detect_is_pipe (php_stdio_stream_data * self ) {
246
+ #if defined(S_ISFIFO ) && defined(S_ISCHR )
247
+ if (self -> fd >= 0 && do_fstat (self , 0 ) == 0 ) {
248
+ self -> is_pipe = S_ISFIFO (self -> sb .st_mode ) || S_ISCHR (self -> sb .st_mode );
249
+ }
250
+ #elif defined(PHP_WIN32 )
251
+ zend_uintptr_t handle = _get_osfhandle (self -> fd );
252
+
253
+ if (handle != (zend_uintptr_t )INVALID_HANDLE_VALUE ) {
254
+ DWORD file_type = GetFileType ((HANDLE )handle );
255
+
256
+ self -> is_pipe = file_type == FILE_TYPE_PIPE || file_type == FILE_TYPE_CHAR ;
257
+ }
258
+ #endif
259
+ }
260
+
245
261
PHPAPI php_stream * _php_stream_fopen_from_fd (int fd , const char * mode , const char * persistent_id STREAMS_DC )
246
262
{
247
263
php_stream * stream = php_stream_fopen_from_fd_int_rel (fd , mode , persistent_id );
248
264
249
265
if (stream ) {
250
266
php_stdio_stream_data * self = (php_stdio_stream_data * )stream -> abstract ;
251
267
252
- #ifdef S_ISFIFO
253
- /* detect if this is a pipe */
254
- if (self -> fd >= 0 ) {
255
- self -> is_pipe = (do_fstat (self , 0 ) == 0 && S_ISFIFO (self -> sb .st_mode )) ? 1 : 0 ;
256
- }
257
- #elif defined(PHP_WIN32 )
258
- {
259
- zend_uintptr_t handle = _get_osfhandle (self -> fd );
260
-
261
- if (handle != (zend_uintptr_t )INVALID_HANDLE_VALUE ) {
262
- self -> is_pipe = GetFileType ((HANDLE )handle ) == FILE_TYPE_PIPE ;
263
- }
264
- }
265
- #endif
266
-
268
+ detect_is_pipe (self );
267
269
if (self -> is_pipe ) {
268
270
stream -> flags |= PHP_STREAM_FLAG_NO_SEEK ;
271
+ stream -> position = -1 ;
269
272
} else {
270
273
stream -> position = zend_lseek (self -> fd , 0 , SEEK_CUR );
271
274
#ifdef ESPIPE
275
+ /* FIXME: Is this code still needed? */
272
276
if (stream -> position == (zend_off_t )- 1 && errno == ESPIPE ) {
273
- stream -> position = 0 ;
274
277
stream -> flags |= PHP_STREAM_FLAG_NO_SEEK ;
275
278
self -> is_pipe = 1 ;
276
279
}
@@ -288,23 +291,10 @@ PHPAPI php_stream *_php_stream_fopen_from_file(FILE *file, const char *mode STRE
288
291
if (stream ) {
289
292
php_stdio_stream_data * self = (php_stdio_stream_data * )stream -> abstract ;
290
293
291
- #ifdef S_ISFIFO
292
- /* detect if this is a pipe */
293
- if (self -> fd >= 0 ) {
294
- self -> is_pipe = (do_fstat (self , 0 ) == 0 && S_ISFIFO (self -> sb .st_mode )) ? 1 : 0 ;
295
- }
296
- #elif defined(PHP_WIN32 )
297
- {
298
- zend_uintptr_t handle = _get_osfhandle (self -> fd );
299
-
300
- if (handle != (zend_uintptr_t )INVALID_HANDLE_VALUE ) {
301
- self -> is_pipe = GetFileType ((HANDLE )handle ) == FILE_TYPE_PIPE ;
302
- }
303
- }
304
- #endif
305
-
294
+ detect_is_pipe (self );
306
295
if (self -> is_pipe ) {
307
296
stream -> flags |= PHP_STREAM_FLAG_NO_SEEK ;
297
+ stream -> position = -1 ;
308
298
} else {
309
299
stream -> position = zend_ftell (file );
310
300
}
0 commit comments