strcompat.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 1999 - 2006, Digium, Inc.
  5. *
  6. * See http://www.asterisk.org for more information about
  7. * the Asterisk project. Please do not directly contact
  8. * any of the maintainers of this project for assistance;
  9. * the project provides a web site, mailing lists and IRC
  10. * channels for your use.
  11. *
  12. * This program is free software, distributed under the terms of
  13. * the GNU General Public License Version 2. See the LICENSE file
  14. * at the top of the source tree.
  15. */
  16. /*! \file
  17. *
  18. * \brief Compatibility functions for strsep and strtoq missing on Solaris
  19. */
  20. #include "autoconfig.h"
  21. #include <sys/types.h>
  22. #include <ctype.h>
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. #include <string.h>
  26. #include <stdarg.h>
  27. #ifdef HAVE_ALLOCA_H
  28. #include <alloca.h>
  29. #endif
  30. #ifndef HAVE_STRSEP
  31. char *strsep(char **str, const char *delims)
  32. {
  33. char *token;
  34. if (!*str) {
  35. /* No more tokens */
  36. return NULL;
  37. }
  38. token = *str;
  39. while (**str != '\0') {
  40. if (strchr(delims, **str)) {
  41. **str = '\0';
  42. (*str)++;
  43. return token;
  44. }
  45. (*str)++;
  46. }
  47. /* There is no other token */
  48. *str = NULL;
  49. return token;
  50. }
  51. #endif
  52. #ifndef HAVE_SETENV
  53. int setenv(const char *name, const char *value, int overwrite)
  54. {
  55. unsigned char *buf;
  56. int buflen;
  57. buflen = strlen(name) + strlen(value) + 2;
  58. buf = alloca(buflen);
  59. if (!overwrite && getenv(name))
  60. return 0;
  61. snprintf(buf, buflen, "%s=%s", name, value);
  62. return putenv(buf);
  63. }
  64. #endif
  65. #ifndef HAVE_UNSETENV
  66. int unsetenv(const char *name)
  67. {
  68. return setenv(name, "", 0);
  69. }
  70. #endif
  71. #ifndef HAVE_STRCASESTR
  72. static char *upper(const char *orig, char *buf, int bufsize)
  73. {
  74. int i = 0;
  75. while (i < (bufsize - 1) && orig[i]) {
  76. buf[i] = toupper(orig[i]);
  77. i++;
  78. }
  79. buf[i] = '\0';
  80. return buf;
  81. }
  82. char *strcasestr(const char *haystack, const char *needle)
  83. {
  84. char *u1, *u2;
  85. int u1len = strlen(haystack) + 1, u2len = strlen(needle) + 1;
  86. u1 = alloca(u1len);
  87. u2 = alloca(u2len);
  88. if (u1 && u2) {
  89. char *offset;
  90. if (u2len > u1len) {
  91. /* Needle bigger than haystack */
  92. return NULL;
  93. }
  94. offset = strstr(upper(haystack, u1, u1len), upper(needle, u2, u2len));
  95. if (offset) {
  96. /* Return the offset into the original string */
  97. return ((char *)((unsigned long)haystack + (unsigned long)(offset - u1)));
  98. } else {
  99. return NULL;
  100. }
  101. } else {
  102. return NULL;
  103. }
  104. }
  105. #endif /* !HAVE_STRCASESTR */
  106. #ifndef HAVE_STRNLEN
  107. size_t strnlen(const char *s, size_t n)
  108. {
  109. size_t len;
  110. for (len = 0; len < n; len++)
  111. if (s[len] == '\0')
  112. break;
  113. return len;
  114. }
  115. #endif /* !HAVE_STRNLEN */
  116. #if !defined(HAVE_STRNDUP) && !defined(__AST_DEBUG_MALLOC)
  117. char *strndup(const char *s, size_t n)
  118. {
  119. size_t len = strnlen(s, n);
  120. char *new = malloc(len + 1);
  121. if (!new)
  122. return NULL;
  123. new[len] = '\0';
  124. return memcpy(new, s, len);
  125. }
  126. #endif /* !defined(HAVE_STRNDUP) && !defined(__AST_DEBUG_MALLOC) */
  127. #if !defined(HAVE_VASPRINTF) && !defined(__AST_DEBUG_MALLOC)
  128. int vasprintf(char **strp, const char *fmt, va_list ap)
  129. {
  130. int size;
  131. va_list ap2;
  132. char s;
  133. *strp = NULL;
  134. va_copy(ap2, ap);
  135. size = vsnprintf(&s, 1, fmt, ap2);
  136. va_end(ap2);
  137. *strp = malloc(size + 1);
  138. if (!*strp)
  139. return -1;
  140. vsnprintf(*strp, size + 1, fmt, ap);
  141. return size;
  142. }
  143. #endif /* !defined(HAVE_VASPRINTF) && !defined(__AST_DEBUG_MALLOC) */
  144. /*
  145. * Based on Code from bsd-asprintf from OpenSSH
  146. * Copyright (c) 2004 Darren Tucker.
  147. *
  148. * Based originally on asprintf.c from OpenBSD:
  149. * Copyright (c) 1997 Todd C. Miller <Todd.Miller@courtesan.com>
  150. *
  151. * Permission to use, copy, modify, and distribute this software for any
  152. * purpose with or without fee is hereby granted, provided that the above
  153. * copyright notice and this permission notice appear in all copies.
  154. *
  155. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  156. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  157. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  158. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  159. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  160. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  161. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  162. */
  163. #if !defined(HAVE_ASPRINTF) && !defined(__AST_DEBUG_MALLOC)
  164. int asprintf(char **str, const char *fmt, ...)
  165. {
  166. va_list ap;
  167. int ret;
  168. *str = NULL;
  169. va_start(ap, fmt);
  170. ret = vasprintf(str, fmt, ap);
  171. va_end(ap);
  172. return ret;
  173. }
  174. #endif /* !defined(HAVE_ASPRINTF) && !defined(__AST_DEBUG_MALLOC) */
  175. #ifndef HAVE_GETLOADAVG
  176. #ifdef linux
  177. /*! \brief Alternative method of getting load avg on Linux only */
  178. int getloadavg(double *list, int nelem)
  179. {
  180. FILE *LOADAVG;
  181. double avg[3] = { 0.0, 0.0, 0.0 };
  182. int i, res = -1;
  183. if ((LOADAVG = fopen("/proc/loadavg", "r"))) {
  184. fscanf(LOADAVG, "%lf %lf %lf", &avg[0], &avg[1], &avg[2]);
  185. res = 0;
  186. fclose(LOADAVG);
  187. }
  188. for (i = 0; (i < nelem) && (i < 3); i++) {
  189. list[i] = avg[i];
  190. }
  191. return res;
  192. }
  193. #else /* !linux */
  194. /*! \brief Return something that won't cancel the call, but still return -1, in case
  195. * we correct the implementation to check return value */
  196. int getloadavg(double *list, int nelem)
  197. {
  198. int i;
  199. for (i = 0; i < nelem; i++) {
  200. list[i] = 0.1;
  201. }
  202. return -1;
  203. }
  204. #endif /* linux */
  205. #endif /* !HAVE_GETLOADAVG */