@@ -570,6 +570,38 @@ static int set_proc_descriptor_to_pipe(struct php_proc_open_descriptor_item *des
570
570
return SUCCESS ;
571
571
}
572
572
573
+ static int set_proc_descriptor_to_file (struct php_proc_open_descriptor_item * desc , zval * zfile , zval * zmode )
574
+ {
575
+ php_socket_t fd ;
576
+
577
+ /* try a wrapper */
578
+ php_stream * stream = php_stream_open_wrapper (Z_STRVAL_P (zfile ), Z_STRVAL_P (zmode ),
579
+ REPORT_ERRORS |STREAM_WILL_CAST , NULL );
580
+
581
+ /* force into an fd */
582
+ if (stream == NULL ) {
583
+ return FAILURE ;
584
+ }
585
+ if (php_stream_cast (stream , PHP_STREAM_CAST_RELEASE |PHP_STREAM_AS_FD , (void * * )& fd , REPORT_ERRORS ) == FAILURE ) {
586
+ return FAILURE ;
587
+ }
588
+
589
+ desc -> mode = DESC_FILE ;
590
+
591
+ #ifdef PHP_WIN32
592
+ desc -> childend = dup_fd_as_handle ((int )fd );
593
+ _close ((int )fd );
594
+
595
+ /* simulate the append mode by fseeking to the end of the file
596
+ this introduces a potential race-condition, but it is the best we can do, though */
597
+ if (strchr (Z_STRVAL_P (zmode ), 'a' )) {
598
+ SetFilePointer (desc -> childend , 0 , NULL , FILE_END );
599
+ }
600
+ #else
601
+ desc -> childend = fd ;
602
+ #endif
603
+ }
604
+
573
605
static void close_all_descriptors (struct php_proc_open_descriptor_item * descriptors , int ndesc )
574
606
{
575
607
for (int i = 0 ; i < ndesc ; i ++ ) {
@@ -773,10 +805,6 @@ PHP_FUNCTION(proc_open)
773
805
}
774
806
} else if (strcmp (Z_STRVAL_P (ztype ), "file" ) == 0 ) {
775
807
zval * zfile , * zmode ;
776
- php_socket_t fd ;
777
- php_stream * stream ;
778
-
779
- descriptors [ndesc ].mode = DESC_FILE ;
780
808
781
809
if ((zfile = zend_hash_index_find (Z_ARRVAL_P (descitem ), 1 )) != NULL ) {
782
810
if (!try_convert_to_string (zfile )) {
@@ -796,29 +824,9 @@ PHP_FUNCTION(proc_open)
796
824
goto exit_fail ;
797
825
}
798
826
799
- /* try a wrapper */
800
- stream = php_stream_open_wrapper (Z_STRVAL_P (zfile ), Z_STRVAL_P (zmode ),
801
- REPORT_ERRORS |STREAM_WILL_CAST , NULL );
802
-
803
- /* force into an fd */
804
- if (stream == NULL || FAILURE == php_stream_cast (stream ,
805
- PHP_STREAM_CAST_RELEASE |PHP_STREAM_AS_FD ,
806
- (void * * )& fd , REPORT_ERRORS )) {
827
+ if (set_proc_descriptor_to_file (& descriptors [ndesc ], zfile , zmode ) == FAILURE ) {
807
828
goto exit_fail ;
808
829
}
809
-
810
- #ifdef PHP_WIN32
811
- descriptors [ndesc ].childend = dup_fd_as_handle ((int )fd );
812
- _close ((int )fd );
813
-
814
- /* simulate the append mode by fseeking to the end of the file
815
- this introduces a potential race-condition, but it is the best we can do, though */
816
- if (strchr (Z_STRVAL_P (zmode ), 'a' )) {
817
- SetFilePointer (descriptors [ndesc ].childend , 0 , NULL , FILE_END );
818
- }
819
- #else
820
- descriptors [ndesc ].childend = fd ;
821
- #endif
822
830
} else if (strcmp (Z_STRVAL_P (ztype ), "redirect" ) == 0 ) {
823
831
zval * ztarget = zend_hash_index_find_deref (Z_ARRVAL_P (descitem ), 1 );
824
832
struct php_proc_open_descriptor_item * target = NULL ;
0 commit comments