A dynamic tracer for Linux

printxf_test.c 830B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. #include <stdarg.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include "printxf.h"
  6. int printxf_test(const char *fmt, ...)
  7. {
  8. va_list ap, xap;
  9. FILE *fp, *xfp;
  10. char *buf, *xbuf;
  11. size_t loc, xloc;
  12. int len, xlen;
  13. fp = open_memstream( &buf, &loc);
  14. xfp = open_memstream(&xbuf, &xloc);
  15. va_start(ap, fmt);
  16. va_copy(xap, ap);
  17. len = vfprintf ( fp, fmt, ap);
  18. xlen = vfprintxf(NULL, xfp, fmt, xap);
  19. va_end(xap);
  20. va_end( ap);
  21. fclose(xfp);
  22. fclose( fp);
  23. if ((xlen == len)
  24. && (xloc == loc)
  25. && !strcmp(xbuf, buf)) {
  26. free(xbuf);
  27. free( buf);
  28. return 0;
  29. }
  30. printf("FAIL len:%2d loc:%2zd buf:\"%s\"\n", len, loc, buf);
  31. printf(" xlen:%2d xloc:%2zd xbuf:\"%s\"\n", xlen, xloc, xbuf);
  32. return -1;
  33. }
  34. int main(void)
  35. {
  36. printxf_test("testing %% %.*s %d\n%", 3, "w000t", 13);
  37. }