Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. #include <stdio.h>
  2. #include <alloca.h>
  3. #include <time.h>
  4.  
  5. // crappy example
  6. char *new_ctime_r(const time_t * timep, char *buf, int buflen)
  7. {
  8.     if (buflen < 26)
  9.         return NULL;
  10.     return ctime_r(timep, buf);
  11. }
  12.  
  13.  
  14. // function called in a multi-threaded application
  15. void printtime()
  16. {
  17.     int buflen = 0;
  18.     char *ptr, *output;
  19.     time_t t;
  20.  
  21.     t = time(NULL);
  22.  
  23.     // ctime converts a time_t value to string
  24.     // ctime_r is the re-entrant version of ctime
  25.  
  26.     do {
  27.         buflen = buflen * 2 + 1;
  28.         output = alloca(buflen);
  29.     }
  30.     while ((ptr = new_ctime_r(&t, output, buflen)) == NULL);
  31.  
  32.     printf("%d bytes was allocated at %p\n", buflen, output);
  33.     printf("The datetime is %s", ptr);
  34. }
  35.  
  36. int main()
  37. {
  38.     printtime();
  39.     return 0;
  40. }