Advertisement
agent-0007

Increase text buffer

Jan 7th, 2014
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.56 KB | None | 0 0
  1. void *buffer_alloc(void *buf, char *str) {
  2.     void *tmp = NULL;
  3.  
  4.     if (buf == NULL) {
  5.         tmp = (char*)calloc(strlen(str) + 1, sizeof(*buf));
  6.         if (tmp == NULL) {
  7.             printf("calloc(): failed. Memory full ?\n");
  8.             return ;
  9.         }
  10.         strncat(tmp, str, strlen(str) + 1);
  11.     } else {
  12.         if (print_debug() == true) printf("buffer_alloc: realloc\n");
  13.         if ((tmp = (char *)realloc(buf, strlen(buf) + strlen(str) + 1)) == NULL) {
  14.             printf("realloc(): failed. Memory full ?\n");
  15.             return ;
  16.         } else {
  17.             strncat(tmp, str, strlen(str) + 1);
  18.         }
  19.     }
  20.  
  21.     buf = tmp;
  22.     return buf;
  23. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement