str.c 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. /**
  2. * $Id$
  3. *
  4. * Copyright (C) 2014 Victor Seva <[email protected]>
  5. *
  6. * This file is part of kamailio, a free SIP server.
  7. *
  8. * Permission to use, copy, modify, and distribute this software for any
  9. * purpose with or without fee is hereby granted, provided that the above
  10. * copyright notice and this permission notice appear in all copies.
  11. *
  12. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  13. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  14. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  15. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  16. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  17. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  18. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  19. */
  20. #include <string.h>
  21. #include "str.h"
  22. #include "mem/mem.h"
  23. int str_append(str *orig, str *suffix, str *dest)
  24. {
  25. if(orig == NULL || suffix == NULL || suffix->len == 0 || dest == NULL)
  26. {
  27. LM_ERR("wrong parameters\n");
  28. return -1;
  29. }
  30. dest->len = orig->len + suffix->len;
  31. dest->s = pkg_malloc(sizeof(char)*dest->len);
  32. if(dest->s==NULL)
  33. {
  34. LM_ERR("memory allocation failure\n");
  35. return -1;
  36. }
  37. if(orig->len>0)
  38. {
  39. memcpy(dest->s, orig->s, orig->len);
  40. }
  41. memcpy(dest->s+orig->len, suffix->s, suffix->len);
  42. return 0;
  43. }