diff --git a/src/engine/cwd.cpp b/src/engine/cwd.cpp index ee0d563c6..29dbf3f55 100644 --- a/src/engine/cwd.cpp +++ b/src/engine/cwd.cpp @@ -10,6 +10,7 @@ #include "jam.h" #include "mem.h" +#include "output.h" #include "pathsys.h" #include @@ -72,7 +73,7 @@ void cwd_init( void ) if ( !cwd_ ) { - perror( "can not get current working directory" ); + errno_puts( "can not get current working directory" ); exit( EXITBAD ); } } diff --git a/src/engine/execunix.cpp b/src/engine/execunix.cpp index 207677998..f1ca14fd8 100644 --- a/src/engine/execunix.cpp +++ b/src/engine/execunix.cpp @@ -215,7 +215,7 @@ void exec_cmd /* Create pipes for collecting child output. */ if ( pipe( out ) < 0 || ( globs.pipe_action && pipe( err ) < 0 ) ) { - perror( "pipe" ); + errno_puts( "pipe" ); exit( EXITBAD ); } @@ -257,7 +257,7 @@ void exec_cmd if ( ( cmdtab[ slot ].pid = vfork() ) == -1 ) { - perror( "vfork" ); + errno_puts( "vfork" ); exit( EXITBAD ); } @@ -294,11 +294,11 @@ void exec_cmd setrlimit( RLIMIT_CPU, &r_limit ); } if (0 != setpgid( pid, pid )) { - perror("setpgid(child)"); + errno_puts("setpgid(child)"); /* exit( EXITBAD ); */ } execvp( argv[ 0 ], (char * *)argv ); - perror( "execvp" ); + errno_puts( "execvp" ); _exit( 127 ); } @@ -324,7 +324,7 @@ void exec_cmd cmdtab[ slot ].stream[ OUT ] = fdopen( cmdtab[ slot ].fd[ OUT ], "rb" ); if ( !cmdtab[ slot ].stream[ OUT ] ) { - perror( "fdopen" ); + errno_puts( "fdopen" ); exit( EXITBAD ); } @@ -335,7 +335,7 @@ void exec_cmd cmdtab[ slot ].stream[ ERR ] = fdopen( cmdtab[ slot ].fd[ ERR ], "rb" ); if ( !cmdtab[ slot ].stream[ ERR ] ) { - perror( "fdopen" ); + errno_puts( "fdopen" ); exit( EXITBAD ); } } diff --git a/src/engine/execvms.cpp b/src/engine/execvms.cpp index 8b281fe52..7a70b365f 100644 --- a/src/engine/execvms.cpp +++ b/src/engine/execvms.cpp @@ -200,7 +200,7 @@ void exec_cmd if ( !cwd ) { - perror( "can not get current working directory" ); + errno_puts( "can not get current working directory" ); exit( EXITBAD ); } diff --git a/src/engine/output.cpp b/src/engine/output.cpp index e015f577b..196082c78 100644 --- a/src/engine/output.cpp +++ b/src/engine/output.cpp @@ -9,6 +9,10 @@ #include #include +#include + +#include +#include #define bjam_out (stdout) @@ -137,6 +141,35 @@ void out_action } +void errno_puts(char const * const s) +{ + const auto e = errno; + err_printf("[errno %d] %s (%s)\n", e, s, strerror(e)); +} + + +void errno_printf(char const * const f, ...) +{ + const auto e = errno; + std::string s = "[errno "+std::to_string(e)+"] "; + { + va_list args; + va_start( args, f ); + int l = vsnprintf( nullptr, 0, f, args ); + va_end( args ); + va_start( args, f ); + std::unique_ptr r(new char[l+1]); + vsnprintf( r.get(), l+1, f, args ); + va_end( args ); + s += r.get(); + } + s += " ("; + s += strerror(e); + s += ")"; + err_puts(s.c_str()); +} + + OBJECT * outf_int( int const value ) { char buffer[ 50 ]; diff --git a/src/engine/output.h b/src/engine/output.h index c33758967..b81a8d5c4 100644 --- a/src/engine/output.h +++ b/src/engine/output.h @@ -35,6 +35,10 @@ void err_data(char const * const s); void out_printf(char const * const f, ...); void err_printf(char const * const f, ...); +// Output current errno value & description along with given string. +void errno_puts(char const * const s); +void errno_printf(char const * const f, ...); + OBJECT * outf_int( int const value ); OBJECT * outf_double( double const value ); OBJECT * outf_time( timestamp const * const value ); diff --git a/src/engine/scan.cpp b/src/engine/scan.cpp index 83a01b260..67948a095 100644 --- a/src/engine/scan.cpp +++ b/src/engine/scan.cpp @@ -166,7 +166,7 @@ int yyline() { FILE * f = stdin; if ( strcmp( object_str( i->fname ), "-" ) && !( f = fopen( object_str( i->fname ), "r" ) ) ) - perror( object_str( i->fname ) ); + errno_puts( object_str( i->fname ) ); i->file = f; }