@@ -517,6 +517,7 @@ void ourWriteOut(struct OperationConfig *config, struct per_transfer *per,
517517 bool done = FALSE;
518518 struct curl_certinfo * certinfo ;
519519 CURLcode res = curl_easy_getinfo (per -> curl , CURLINFO_CERTINFO , & certinfo );
520+ bool fclose_stream = FALSE;
520521
521522 if (!writeinfo )
522523 return ;
@@ -556,9 +557,15 @@ void ourWriteOut(struct OperationConfig *config, struct per_transfer *per,
556557 done = TRUE;
557558 break ;
558559 case VAR_STDOUT :
560+ if (fclose_stream )
561+ fclose (stream );
562+ fclose_stream = FALSE;
559563 stream = stdout ;
560564 break ;
561565 case VAR_STDERR :
566+ if (fclose_stream )
567+ fclose (stream );
568+ fclose_stream = FALSE;
562569 stream = stderr ;
563570 break ;
564571 case VAR_JSON :
@@ -600,6 +607,36 @@ void ourWriteOut(struct OperationConfig *config, struct per_transfer *per,
600607 else
601608 fputs ("%header{" , stream );
602609 }
610+ else if (!strncmp ("output{" , & ptr [1 ], 7 )) {
611+ bool append = FALSE;
612+ ptr += 8 ;
613+ if ((ptr [0 ] == '>' ) && (ptr [1 ] == '>' )) {
614+ append = TRUE;
615+ ptr += 2 ;
616+ }
617+ end = strchr (ptr , '}' );
618+ if (end ) {
619+ char fname [512 ]; /* holds the longest file name */
620+ size_t flen = end - ptr ;
621+ if (flen < sizeof (fname )) {
622+ FILE * stream2 ;
623+ memcpy (fname , ptr , flen );
624+ fname [flen ] = 0 ;
625+ stream2 = fopen (fname , append ? FOPEN_APPENDTEXT :
626+ FOPEN_WRITETEXT );
627+ if (stream2 ) {
628+ /* only change if the open worked */
629+ if (fclose_stream )
630+ fclose (stream );
631+ stream = stream2 ;
632+ fclose_stream = TRUE;
633+ }
634+ }
635+ ptr = end + 1 ;
636+ }
637+ else
638+ fputs ("%output{" , stream );
639+ }
603640 else {
604641 /* illegal syntax, then just output the characters that are used */
605642 fputc ('%' , stream );
@@ -632,4 +669,6 @@ void ourWriteOut(struct OperationConfig *config, struct per_transfer *per,
632669 ptr ++ ;
633670 }
634671 }
672+ if (fclose_stream )
673+ fclose (stream );
635674}
0 commit comments