@@ -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 []);
@@ -827,6 +828,10 @@ static const struct Nif console_print_nif = {
827828 .base .type = NIFFunctionType ,
828829 .nif_ptr = nif_console_print
829830};
831+ static const struct Nif console_print_err_nif = {
832+ .base .type = NIFFunctionType ,
833+ .nif_ptr = nif_console_print_err
834+ };
830835static const struct Nif base64_encode_nif = {
831836 .base .type = NIFFunctionType ,
832837 .nif_ptr = nif_base64_encode
@@ -5635,16 +5640,14 @@ static term nif_atomvm_get_creation(Context *ctx, int argc, term argv[])
56355640 return term_make_maybe_boxed_int64 (ctx -> global -> creation , & ctx -> heap );
56365641}
56375642
5638- static term nif_console_print ( Context * ctx , int argc , term argv [])
5643+ static term console_print_to ( FILE * stream , Context * ctx , term argv [])
56395644{
5640- UNUSED (argc );
5641-
56425645 term t = argv [0 ];
56435646 if (term_is_binary (t )) {
56445647 const char * data = term_binary_data (t );
5645- unsigned long n = term_binary_size (t );
5646- fprintf ( stdout , "%.*s" , ( int ) n , data );
5647- fflush (stdout );
5648+ size_t n = term_binary_size (t );
5649+ fwrite ( data , 1 , n , stream );
5650+ fflush (stream );
56485651 } else {
56495652 size_t size ;
56505653 switch (interop_iolist_size (t , & size )) {
@@ -5655,6 +5658,10 @@ static term nif_console_print(Context *ctx, int argc, term argv[])
56555658 case InteropBadArg :
56565659 RAISE_ERROR (BADARG_ATOM );
56575660 }
5661+ if (size == 0 ) {
5662+ fflush (stream );
5663+ return OK_ATOM ;
5664+ }
56585665 char * buf = malloc (size );
56595666 if (IS_NULL_PTR (buf )) {
56605667 RAISE_ERROR (OUT_OF_MEMORY_ATOM );
@@ -5669,13 +5676,25 @@ static term nif_console_print(Context *ctx, int argc, term argv[])
56695676 free (buf );
56705677 RAISE_ERROR (BADARG_ATOM );
56715678 }
5672- fprintf ( stdout , "%.*s" , ( int ) size , buf );
5673- fflush (stdout );
5679+ fwrite ( buf , 1 , size , stream );
5680+ fflush (stream );
56745681 free (buf );
56755682 }
56765683 return OK_ATOM ;
56775684}
56785685
5686+ static term nif_console_print (Context * ctx , int argc , term argv [])
5687+ {
5688+ UNUSED (argc );
5689+ return console_print_to (stdout , ctx , argv );
5690+ }
5691+
5692+ static term nif_console_print_err (Context * ctx , int argc , term argv [])
5693+ {
5694+ UNUSED (argc );
5695+ return console_print_to (stderr , ctx , argv );
5696+ }
5697+
56795698// clang-format off
56805699static const char b64_table [64 ] = {'A' , 'B' , 'C' , 'D' , 'E' , 'F' , 'G' , 'H' , 'I' , 'J' ,
56815700 'K' , 'L' , 'M' , 'N' , 'O' , 'P' , 'Q' , 'R' , 'S' , 'T' ,
0 commit comments