strfunc.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /* Character string handling functions */
  2. #include "config.h"
  3. #include <string.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <stdarg.h>
  7. #include "strfunc.h"
  8. #include "mem_util.h"
  9. /*************************************************************************
  10. my_strcat:
  11. In: two strings
  12. Out: the two strings concatenated, in a newly malloced block of memory,
  13. or NULL if there isn't enough memory.
  14. Note: the calling function is responsible for freeing the memory.
  15. *************************************************************************/
  16. char *my_strcat(char const *s1, char const *s2)
  17. {
  18. char *temp = NULL;
  19. size_t len1,len2;
  20. len1 = (s1==NULL) ? 0 : strlen(s1);
  21. len2 = (s2==NULL) ? 0 : strlen(s2);
  22. if ((temp=(char *)malloc(len1+len2+1))!=NULL) {
  23. if (len1) strcpy(temp,s1);
  24. if (len2) strcpy(temp+len1,s2);
  25. temp[len1+len2] = '\0';
  26. }
  27. return(temp);
  28. }
  29. /*************************************************************************
  30. my_strclump:
  31. In: a NULL-terminated array of strings
  32. Out: all of the strings concatenated, in a newly malloced block of
  33. memory, or NULL if there isn't enough memory.
  34. Note: the calling function is responsible for freeing the memory.
  35. *************************************************************************/
  36. char *my_strclump(char **slist)
  37. {
  38. int i,j,n,len;
  39. char **sp = NULL;
  40. char *s = NULL;
  41. char *temp = NULL;
  42. for (sp=slist,n=0 ; *sp!=NULL ; sp++,n++);
  43. for (i=0,len=0;i<n;i++) len += strlen(slist[i]);
  44. temp = (char*) malloc(len+1);
  45. if (temp==NULL) return NULL;
  46. j=0;
  47. for (sp=slist;*sp!=NULL;sp++)
  48. {
  49. for (s=*sp ; *s!=0 ; s++)
  50. {
  51. temp[j++] = *s;
  52. if (j==len) { temp[j]=0; return temp; }
  53. }
  54. }
  55. temp[j]=0;
  56. return temp;
  57. }
  58. /*************************************************************************
  59. strip_quotes:
  60. In: a string that must be at least two characters long
  61. Out: a copy of the string, newly malloced, missing the first and
  62. last characters (might even be quotes!); NULL is returned if
  63. malloc fails.
  64. Note: this function does NOT do any error checking!
  65. *************************************************************************/
  66. char *strip_quotes(char const *s)
  67. {
  68. char *temp = NULL;
  69. int len = strlen(s);
  70. if ((temp=(char *)malloc(len-1))!=NULL) {
  71. strncpy(temp,s+1,len-2);
  72. temp[len-2]='\0';
  73. }
  74. return(temp);
  75. }
  76. /*
  77. * Format a string into an allocated buffer.
  78. */
  79. char *alloc_vsprintf(char const *fmt, va_list args)
  80. {
  81. char stack_buffer[256];
  82. int len;
  83. char *retval = NULL;
  84. va_list saved_args;
  85. va_copy(saved_args, args);
  86. len = vsnprintf(stack_buffer, sizeof(stack_buffer), fmt, args);
  87. if (len >= (int) sizeof(stack_buffer))
  88. {
  89. retval = (char*)malloc(len + 1);
  90. if (retval != NULL)
  91. vsnprintf(retval, len + 1, fmt, saved_args);
  92. }
  93. else
  94. retval = strdup(stack_buffer);
  95. return retval;
  96. }
  97. /*
  98. * Format a string into an allocated buffer.
  99. */
  100. char *alloc_sprintf(char const *fmt, ...)
  101. {
  102. char *retval;
  103. va_list args;
  104. va_start(args, fmt);
  105. retval = alloc_vsprintf(fmt, args);
  106. va_end(args);
  107. return retval;
  108. }