@@ -257,6 +257,7 @@ static term nif_atomvm_get_start_beam(Context *ctx, int argc, term argv[]);
257257static term nif_atomvm_read_priv (Context * ctx , int argc , term argv []);
258258static term nif_atomvm_get_creation (Context * ctx , int argc , term argv []);
259259static term nif_console_print (Context * ctx , int argc , term argv []);
260+ static term nif_console_print_err (Context * ctx , int argc , term argv []);
260261static term nif_base64_encode (Context * ctx , int argc , term argv []);
261262static term nif_base64_decode (Context * ctx , int argc , term argv []);
262263static term nif_base64_encode_to_string (Context * ctx , int argc , term argv []);
@@ -829,6 +830,10 @@ static const struct Nif console_print_nif = {
829830 .base .type = NIFFunctionType ,
830831 .nif_ptr = nif_console_print
831832};
833+ static const struct Nif console_print_err_nif = {
834+ .base .type = NIFFunctionType ,
835+ .nif_ptr = nif_console_print_err
836+ };
832837static const struct Nif base64_encode_nif = {
833838 .base .type = NIFFunctionType ,
834839 .nif_ptr = nif_base64_encode
@@ -5796,16 +5801,14 @@ static term nif_atomvm_get_creation(Context *ctx, int argc, term argv[])
57965801 return term_make_maybe_boxed_int64 (ctx -> global -> creation , & ctx -> heap );
57975802}
57985803
5799- static term nif_console_print ( Context * ctx , int argc , term argv [])
5804+ static term console_print_to ( FILE * stream , Context * ctx , term argv [])
58005805{
5801- UNUSED (argc );
5802-
58035806 term t = argv [0 ];
58045807 if (term_is_binary (t )) {
58055808 const char * data = term_binary_data (t );
5806- unsigned long n = term_binary_size (t );
5807- fprintf ( stdout , "%.*s" , ( int ) n , data );
5808- fflush (stdout );
5809+ size_t n = term_binary_size (t );
5810+ fwrite ( data , 1 , n , stream );
5811+ fflush (stream );
58095812 } else {
58105813 size_t size ;
58115814 switch (interop_iolist_size (t , & size )) {
@@ -5816,6 +5819,10 @@ static term nif_console_print(Context *ctx, int argc, term argv[])
58165819 case InteropBadArg :
58175820 RAISE_ERROR (BADARG_ATOM );
58185821 }
5822+ if (size == 0 ) {
5823+ fflush (stream );
5824+ return OK_ATOM ;
5825+ }
58195826 char * buf = malloc (size );
58205827 if (IS_NULL_PTR (buf )) {
58215828 RAISE_ERROR (OUT_OF_MEMORY_ATOM );
@@ -5830,13 +5837,25 @@ static term nif_console_print(Context *ctx, int argc, term argv[])
58305837 free (buf );
58315838 RAISE_ERROR (BADARG_ATOM );
58325839 }
5833- fprintf ( stdout , "%.*s" , ( int ) size , buf );
5834- fflush (stdout );
5840+ fwrite ( buf , 1 , size , stream );
5841+ fflush (stream );
58355842 free (buf );
58365843 }
58375844 return OK_ATOM ;
58385845}
58395846
5847+ static term nif_console_print (Context * ctx , int argc , term argv [])
5848+ {
5849+ UNUSED (argc );
5850+ return console_print_to (stdout , ctx , argv );
5851+ }
5852+
5853+ static term nif_console_print_err (Context * ctx , int argc , term argv [])
5854+ {
5855+ UNUSED (argc );
5856+ return console_print_to (stderr , ctx , argv );
5857+ }
5858+
58405859// clang-format off
58415860static const char b64_table [64 ] = {'A' , 'B' , 'C' , 'D' , 'E' , 'F' , 'G' , 'H' , 'I' , 'J' ,
58425861 'K' , 'L' , 'M' , 'N' , 'O' , 'P' , 'Q' , 'R' , 'S' , 'T' ,
0 commit comments