home *** CD-ROM | disk | FTP | other *** search
/ Steganos Hacker Tools / SHT151.iso / programme / scanner / nmapNTsp1 / Win_2000.exe / nmapNT-src / snprintf.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-04-05  |  977 b   |  45 lines

  1. #include <stdio.h>
  2.  
  3. #include "error.h"
  4. #include "charpool.h"
  5.  
  6. #define SNPRINTF_BUFSIZE 8192
  7.  
  8. int snprintf ( char *str, size_t n, const char *format, ... ) {
  9. static char *buf = NULL;
  10. static int warning = 0;
  11. int len;
  12. va_list ap;
  13. int res = 0;
  14.  
  15. va_start(ap, format);
  16.  
  17. if (!warning) {
  18.   error("WARNING:  your system apparently does not offer snprintf().  Reverting to less secure version");
  19.   warning = 1;
  20. }
  21.  
  22. if (!buf) {
  23.   buf = (char *) cp_alloc(SNPRINTF_BUFSIZE);
  24. }
  25.  
  26. #ifndef SPRINTF_RETURNS_STRING
  27. res = vsprintf(buf, format, ap);
  28. if (res >= SNPRINTF_BUFSIZE || res < 0) {
  29.   fatal("Our bufferZ may have been overfl0wed!!@#$!@#");
  30. }
  31. #else
  32. res = 4; /* avoid compiler warnings */
  33. vsprintf(buf, format, ap);  /* Oh well -- at least they were warned */
  34. #endif
  35. len = strlen(buf);
  36. if (len >= SNPRINTF_BUFSIZE)
  37.   fatal("Our bufferZ may have been overfl0wed!!@#$!@#");
  38.  
  39.  
  40. Strncpy(str, buf, n);
  41. va_end (ap);
  42. if (len >= n) return -1;
  43. return len;
  44. }
  45.