diff --git a/Makefile b/Makefile index 70475df..a9f198d 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,10 @@ PREFIX=/usr/local TEST_CFLAGS=-D_QRINTF_COUNT_CALL=1 -Wall -g -Werror +AOUT=./test +ifeq (Windows_NT, $(OS)) +#AOUT=.\\\test.exe +AOUT=".\test.exe" +endif all: @@ -15,9 +20,9 @@ install: test: test-cc test-cxx test-cc: - bin/qrintf $(CC) $(TEST_CFLAGS) t/test.c deps/picotest/picotest.c -o ./test && ./test + perl bin/qrintf $(CC) $(TEST_CFLAGS) t/test.c deps/picotest/picotest.c -o $(AOUT) && $(AOUT) test-cxx: - bin/qrintf $(CXX) -x c++ $(TEST_CFLAGS) t/test.c deps/picotest/picotest.c -o ./test && ./test + perl bin/qrintf $(CXX) -x c++ $(TEST_CFLAGS) t/test.c deps/picotest/picotest.c -o $(AOUT) && $(AOUT) .PHONY: gen install test test-cc test-cxx diff --git a/bin/qrintf b/bin/qrintf index 4b91eff..9402728 100755 --- a/bin/qrintf +++ b/bin/qrintf @@ -25,7 +25,7 @@ use warnings; use Digest::SHA qw(sha1_hex); use File::Basename qw(basename dirname); use File::Temp qw(tempdir); -use POSIX qw(WIFEXITED WEXITSTATUS WTERMSIG); +use File::Spec::Functions qw(rel2abs); my %EXT = ( c => 'i', @@ -42,14 +42,16 @@ if (@ARGV && $ARGV[0] eq '--version') { exit 0; } -my $pwd = dirname($0); +my $pwd = dirname(rel2abs($0)); my $cc = shift @ARGV; if (basename($cc) =~ m{^g(?:cc|\+\+)}) { # is GCC, use "-wrapper" - exec "$cc", qw(-no-integrated-cpp -wrapper), "$pwd/../share/qrintf/gcc-wrapper", @ARGV; - die "failed to exec $cc:$!"; + if (system("$cc", qw(-no-integrated-cpp -wrapper), "$pwd/../share/qrintf/gcc-wrapper", @ARGV) != 0) { + die "failed to exec $cc:$!"; + } + exit 0; } else { # mimic GCC's "-no-integrated-cpp -wrapper" @@ -87,6 +89,7 @@ if (basename($cc) =~ m{^g(?:cc|\+\+)}) { # preprocess the source files for my $fn (@files) { run_cmd( + $^X, "$pwd/../share/qrintf/gcc-wrapper", $cc, '-E', (grep { $_ ne '-c' } @ARGV), @@ -112,10 +115,10 @@ sub run_cmd { system(@argv) == 0 and return; if ($? == -1) { die "failed to exec $argv[0]:$!"; - } elsif (WIFEXITED($?)) { - exit WEXITSTATUS($?); + } elsif ($? & 255) { + exit $? >> 8; } else { - die "$argv[0] exitted due to signal @{[WTERMSIG($?)]}\n"; + die "$argv[0] exitted due to signal @{[$? & 255]}\n"; } } diff --git a/bin/qrintf-pp b/bin/qrintf-pp index e0f8535..008d8e9 100755 --- a/bin/qrintf-pp +++ b/bin/qrintf-pp @@ -27,7 +27,7 @@ use File::Basename qw(dirname); if (@ARGV && $ARGV[0] eq '--version') { my $cmd = dirname($0) . "/qrintf"; - exec $cmd, "--version"; + exec $^X, $cmd, "--version"; die "failed to execute: $cmd:$!"; } diff --git a/include/qrintf.h b/include/qrintf.h index 95c1a35..33a4fb3 100644 --- a/include/qrintf.h +++ b/include/qrintf.h @@ -45,8 +45,10 @@ extern "C" { #undef sprintf #define sprintf(...) _qp_sprintf(__VA_ARGS__) -#undef snprintf -#define snprintf(...) _qp_snprintf(__VA_ARGS__) +#ifndef _WIN32 +# undef snprintf +# define snprintf(...) _qp_snprintf(__VA_ARGS__) +#endif #if _QRINTF_COUNT_CALL extern size_t _qrintf_call_cnt; diff --git a/share/qrintf/gcc-wrapper b/share/qrintf/gcc-wrapper index ca1865f..d60cea1 100755 --- a/share/qrintf/gcc-wrapper +++ b/share/qrintf/gcc-wrapper @@ -26,8 +26,10 @@ use File::Basename qw(dirname); use POSIX qw(WIFEXITED WEXITSTATUS); if (! grep { $_ eq '-E' } @ARGV) { - exec @ARGV; - die "failed to exec $ARGV[0]:$!"; + if (system(@ARGV) != 0) { + die "failed to exec $ARGV[0]:$!"; + } + exit 0; } my $pwd = dirname($0); @@ -35,7 +37,7 @@ my $pwd = dirname($0); # invoke cpp my ($cpp_cmd, @cpp_args) = @ARGV; system($cpp_cmd, '-DQRINTF=1', '-include', "$pwd/../../include/qrintf.h", @cpp_args) == 0 - or exit(WIFEXITED($?) ? WEXITSTATUS($?) : 1); + or exit($? >> 8 ? $? >> 8 : 1); # read the source file my $src_fn = $ARGV[$#ARGV]; @@ -45,8 +47,8 @@ my $src = do { }; # filter and print -open my $fh, "| $pwd/../../bin/qrintf-pp > $src_fn" +open my $fh, "| $^X $pwd/../../bin/qrintf-pp > $src_fn" or die "failed to invoke $pwd/qrintf-pp:$!"; print $fh $src; close $fh - or exit(WIFEXITED($?) ? WEXITSTATUS($?) : 1); + or exit($? >> 8 ? $? >> 8 : 1); diff --git a/t/test.c b/t/test.c index bde4171..defb826 100644 --- a/t/test.c +++ b/t/test.c @@ -112,19 +112,25 @@ static void test_simple(void) CHECK_MULTI(unsigned , "%u", 0, UINT_MAX); CHECK_MULTI(unsigned long, "%lu", 0, ULONG_MAX); CHECK_MULTI(unsigned long long, "%llu", 0, ULLONG_MAX); +#ifndef _WIN32 CHECK_MULTI(size_t, "%zu", 0, SIZE_MAX); +#endif CHECK_MULTI(unsigned short, "%hx", 0, USHRT_MAX); CHECK_MULTI(unsigned , "%x", 0, UINT_MAX); CHECK_MULTI(unsigned long, "%lx", 0, ULONG_MAX); CHECK_MULTI(unsigned long long, "%llx", 0, ULLONG_MAX); +#ifndef _WIN32 CHECK_MULTI(size_t, "%zx", 0, SIZE_MAX); +#endif CHECK_MULTI(unsigned short, "%hX", 0, USHRT_MAX); CHECK_MULTI(unsigned , "%X", 0, UINT_MAX); CHECK_MULTI(unsigned long, "%lX", 0, ULONG_MAX); CHECK_MULTI(unsigned long long, "%llX", 0, ULLONG_MAX); +#ifndef _WIN32 CHECK_MULTI(size_t, "%zX", 0, SIZE_MAX); +#endif CHECK_MULTI(int, "%7d", INT_MIN, INT_MAX); CHECK_MULTI(int, "%07d", INT_MIN, INT_MAX); @@ -133,6 +139,7 @@ static void test_simple(void) CHECK_MULTI(unsigned, "%7x", 0, UINT_MAX); CHECK_MULTI(unsigned, "%07x", 0, UINT_MAX); +#ifndef _WIN32 CHECK_SNPRINTF(8, "%s", "abcdef"); /* below the bounds */ CHECK_SNPRINTF(8, "1234%s", "abcdef"); /* partial write */ CHECK_SNPRINTF(8, "1234567890%s", "abcdef"); /* no write */ @@ -145,6 +152,7 @@ static void test_simple(void) CHECK_SNPRINTF(3, "%x", UINT_MAX); CHECK_SNPRINTF(3, "%lx", ULONG_MAX); CHECK_SNPRINTF(3, "%llx", ULLONG_MAX); +#endif } static void test_composite(void)