#include #include #include #include #include #include //#include #include // program to test how several string api's function // ** i noticed different behaviour in different platforms/compilers // // // C:\local\cygwin\lib>dumpbin /exports /symbols *.* w32api\*.* | grep "of file\|n[a-zA-Z]*\(cpy\|printf\)" // Dump of file libc.a __snprintf __strncpy _snprintf _strncpy _wcsncpy // Dump of file libcygwin.a __snprintf __strncpy _snprintf _strncpy _wcsncpy // Dump of file w32api\libntdll.a _wcsncpy _strncpy __snwprintf __snprintf // Dump of file w32api\libntoskrnl.a _wcsncpy _strncpy __snwprintf __snprintf // Dump of file w32api\libshlwapi.a _wnsprintfW _wnsprintfA // // C:\Program Files\Microsoft Visual Studio .NET 2003\Vc7>dumpbin /exports /symbols lib\*.* PlatformSDK\Lib\*.* | grep "of file\|n[a-zA-Z]*\(cpy\|printf\)" // Dump of file lib\msvcrt.lib _wcsncpy _strncpy __snwprintf __snprintf __mbsncpy __mbsnbcpy // Dump of file PlatformSDK\Lib\ShLwApi.Lib _wnsprintfA _wnsprintfW // // C:\Program Files\Windows CE Tools\wce420\SMARTPHONE 2003\Lib\Armv4>dumpbin /exports /symbols *.* | grep "of file\|n[a-zA-Z]*\(cpy\|printf\)" // Dump of file coredll.lib _snprintf _snwprintf strncpy wcsncpy #if defined(__CYGWIN__) #define COMPILE_WITH_GCC 1 #elif defined(_WIN32_WCE) #define COMPILE_WITH_EVC 1 #else #define COMPILE_WITH_MSC 1 #endif #define testcp(fn, T, str) \ void tst##fn(int i, int &nCopied, int &nNuls, int &nResult) \ { \ T buf[64]; \ memset(buf, 0xaa, sizeof(buf)); \ fn(buf, str, i); \ nCopied= 0; nNuls= 0; nResult= 0; \ for (int j=0 ; j<64 ; j++) { \ if (buf[j]==0) { \ nNuls++; \ } \ else if (buf[j]==str[j]) { \ nCopied++; \ } \ } \ } #define testpf(fn, T, str) \ void tst##fn(int i, int &nCopied, int &nNuls, int &nResult) \ { \ T buf[64]; \ memset(buf, 0xaa, sizeof(buf)); \ nResult= fn(buf, i, str); \ nCopied= 0; nNuls= 0; \ for (int j=0 ; j<64 ; j++) { \ if (buf[j]==0) { \ nNuls++; \ } \ else if (buf[j]==str[j]) { \ nCopied++; \ } \ } \ } #define testapf(fn) testpf(fn, char, "01234567") #define testwpf(fn) testpf(fn, WCHAR, L"01234567") #define testacp(fn) testcp(fn, char, "01234567") #define testwcp(fn) testcp(fn, WCHAR, L"01234567") #ifdef COMPILE_WITH_GCC testapf(snprintf) #define callsnprintf 1 testacp(strncpy) #define callstrncpy 1 testwcp(wcsncpy) #define callwcsncpy 1 //testapf(_snprintf) //testwpf(_snwprint) //testacp(_strncpy) //testapf(wnsprintf) #endif #ifdef COMPILE_WITH_MSC testwcp(wcsncpy) #define callwcsncpy 1 testacp(strncpy) #define callstrncpy 1 testwpf(_snwprintf) #define call_snwprintf 1 testapf(_snprintf) #define call_snprintf 1 testapf(wnsprintfA) #define callwnsprintfA 1 testwpf(wnsprintfW) #define callwnsprintfW 1 //testacp(_mbsncpy) //testacp(_mbsnbcpy) #endif #ifdef COMPILE_WITH_EVC testapf(_snprintf) #define call_snprintf 1 testwpf(_snwprintf) #define call_snwprintf 1 testacp(strncpy) #define callstrncpy 1 testwcp(wcsncpy) #define callwcsncpy 1 #endif const char *g_logname=NULL; bool g_stdout= false; void DebugStdOut() { g_stdout= true; } void DebugSetLogfile(const char *filename) { g_logname= filename; } void debug(char *fmt, ...) { va_list ap; if (g_logname) { FILE *f=fopen(g_logname, "a"); if (f) { va_start(ap, fmt); vfprintf(f, fmt, ap); va_end(ap); fclose(f); } } if (g_stdout) { va_start(ap, fmt); vprintf(fmt, ap); va_end(ap); } va_end(ap); } #define call(fn) \ debug("tst" #fn ":"); \ for (i=6 ; i<=10 ; i++) { \ tst##fn(i, nCopied, nNuls, nResult); \ debug(" %d:%d:%d", nCopied, nNuls, nResult); \ } \ debug("\n"); void dotests() { int i; int nCopied, nNuls, nResult; #if defined(callstrncpy) call(strncpy) #endif #if defined(call_snprintf) call(_snprintf) #endif #if defined(callsnprintf) call(snprintf) #endif #if defined(callwnsprintfA) call(wnsprintfA) #endif #if defined(callwnsprintfW) call(wnsprintfW) #endif #if defined(callwcsncpy) call(wcsncpy) #endif #if defined(call_snwprintf) call(_snwprintf) #endif } #ifdef _WIN32_WCE int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow) { DebugSetLogfile("tststr.log"); debug("writing test results to logfile\n"); dotests(); debug("done testing\n"); return 0; } #else int main(int , char **) { DebugStdOut(); debug("writing test results to stdout\n"); dotests(); debug("done testing\n"); return 0; } #endif