home *** CD-ROM | disk | FTP | other *** search
/ RISC DISC 2 / RISC_DISC_2.iso / pd_share / program / code / stubshack / StubsHack / Example / c / Main
Encoding:
Text File  |  1995-01-12  |  2.2 KB  |  114 lines

  1. /* 
  2.  
  3. Example program which intercepts printf and strcat. Note that all printf
  4. calls which don't use floating point are actually sent by the norcroft
  5. compiler to _printf, so we have to intercept both printf and _printf.
  6.  
  7. If you want to intercept fprintf, sprintf, vsprintf etc, the same thing 
  8. applies - you will need to also intercept _fprintf, _sprintf, _vsprintf 
  9. etc etc.
  10.  
  11. */
  12.  
  13. #include <stdio.h>
  14. #include <stdarg.h>
  15. #include <string.h>
  16.  
  17. #include "StubsHack.StubsHack.h"
  18.  
  19.  
  20.  
  21. extern int _printf( const char *fmt, ...);
  22.     /* 
  23.     This is the prototype of a function in stubs which is 
  24.     identical to printf, except it
  25.     doesn't handle floating point. According to the RO3 PRMs 4-310, 
  26.     _printf is present to enable space-optimisation when using the 
  27.     non-shared C library.
  28.     CC sends all printf's to _printf if they don't use floating point,
  29.     so we need to know about it if we are to intercept all printf
  30.     calls.
  31.     */
  32.  
  33. static char    *(*old_strcat)( char *, const char *)        = strcat;
  34. static int    (*old_printf)( const char *, ...)        = printf;
  35.     /* 
  36.     These will always send execution to the SharedCLibrary, even
  37.     after strcat and printf have been redirected.
  38.     */
  39.  
  40.  
  41.  
  42.  
  43. static int    NewPrintf( const char *fmt, ...)
  44. {
  45. va_list args;
  46. int    i;
  47.  
  48. old_printf( "******printf starting*******\n");
  49.  
  50. va_start( args, fmt);
  51. i = vprintf( fmt, args);
  52. va_end( args);
  53.  
  54. old_printf( "******printf finished*******\n\n");
  55.  
  56. return i;
  57.  
  58. }
  59.  
  60.  
  61.  
  62. static char    *NewStrcat( char *a, const char *b)
  63. {
  64. char *c;
  65.  
  66. printf( "'strcat' called with arguments: '%s' '%s'\n", a, b);
  67. c = old_strcat( a, b);
  68. printf( "Result is '%s'\n", c);
  69.  
  70. return c;
  71. }
  72.  
  73.  
  74.  
  75. int    main( void)
  76. {
  77. char s[200] = "aaaaa";
  78.  
  79.  
  80. printf( "Some example text displayed by printf before redirection\n\n");
  81.  
  82. strcat( s, "wwww");
  83.  
  84. StubsHack_RedirectStubsFn(
  85.     (StubsHack_fnptr)    strcat,
  86.     (StubsHack_fnptr)    NewStrcat,
  87.     (StubsHack_fnptr *)    &old_strcat
  88.     );
  89.  
  90. strcat( s, "ppppp");
  91.  
  92.  
  93. StubsHack_RedirectStubsFn(
  94.     (StubsHack_fnptr)    printf,
  95.     (StubsHack_fnptr)    NewPrintf,
  96.     (StubsHack_fnptr *)    &old_printf
  97.     );
  98.  
  99. StubsHack_RedirectStubsFn(
  100.     (StubsHack_fnptr)    _printf,
  101.     (StubsHack_fnptr)    NewPrintf,
  102.     (StubsHack_fnptr *)    NULL        /* We will send all _printf calls to printf    */
  103.     );
  104.  
  105.  
  106. strcat( s, "jjjjjj");
  107.  
  108.  
  109. printf( "finished\n");
  110.  
  111. return 0;
  112. }
  113.  
  114.