home *** CD-ROM | disk | FTP | other *** search
- /*
-
- Example program which intercepts printf and strcat. Note that all printf
- calls which don't use floating point are actually sent by the norcroft
- compiler to _printf, so we have to intercept both printf and _printf.
-
- If you want to intercept fprintf, sprintf, vsprintf etc, the same thing
- applies - you will need to also intercept _fprintf, _sprintf, _vsprintf
- etc etc.
-
- */
-
- #include <stdio.h>
- #include <stdarg.h>
- #include <string.h>
-
- #include "StubsHack.StubsHack.h"
-
-
-
- extern int _printf( const char *fmt, ...);
- /*
- This is the prototype of a function in stubs which is
- identical to printf, except it
- doesn't handle floating point. According to the RO3 PRMs 4-310,
- _printf is present to enable space-optimisation when using the
- non-shared C library.
- CC sends all printf's to _printf if they don't use floating point,
- so we need to know about it if we are to intercept all printf
- calls.
- */
-
- static char *(*old_strcat)( char *, const char *) = strcat;
- static int (*old_printf)( const char *, ...) = printf;
- /*
- These will always send execution to the SharedCLibrary, even
- after strcat and printf have been redirected.
- */
-
-
-
-
- static int NewPrintf( const char *fmt, ...)
- {
- va_list args;
- int i;
-
- old_printf( "******printf starting*******\n");
-
- va_start( args, fmt);
- i = vprintf( fmt, args);
- va_end( args);
-
- old_printf( "******printf finished*******\n\n");
-
- return i;
-
- }
-
-
-
- static char *NewStrcat( char *a, const char *b)
- {
- char *c;
-
- printf( "'strcat' called with arguments: '%s' '%s'\n", a, b);
- c = old_strcat( a, b);
- printf( "Result is '%s'\n", c);
-
- return c;
- }
-
-
-
- int main( void)
- {
- char s[200] = "aaaaa";
-
-
- printf( "Some example text displayed by printf before redirection\n\n");
-
- strcat( s, "wwww");
-
- StubsHack_RedirectStubsFn(
- (StubsHack_fnptr) strcat,
- (StubsHack_fnptr) NewStrcat,
- (StubsHack_fnptr *) &old_strcat
- );
-
- strcat( s, "ppppp");
-
-
- StubsHack_RedirectStubsFn(
- (StubsHack_fnptr) printf,
- (StubsHack_fnptr) NewPrintf,
- (StubsHack_fnptr *) &old_printf
- );
-
- StubsHack_RedirectStubsFn(
- (StubsHack_fnptr) _printf,
- (StubsHack_fnptr) NewPrintf,
- (StubsHack_fnptr *) NULL /* We will send all _printf calls to printf */
- );
-
-
- strcat( s, "jjjjjj");
-
-
- printf( "finished\n");
-
- return 0;
- }
-
-