3
0

platform.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /*
  2. * Replacements for common but usually nonstandard functions that aren't
  3. * supplied by all platforms.
  4. *
  5. * Copyright (C) 2009 by Dan Fandrich <dan@coneharvesters.com>, et. al.
  6. *
  7. * Licensed under GPLv2, see file LICENSE in this source tree.
  8. */
  9. #include "libbb.h"
  10. #ifndef HAVE_STRCHRNUL
  11. char* FAST_FUNC strchrnul(const char *s, int c)
  12. {
  13. while (*s != '\0' && *s != c)
  14. s++;
  15. return (char*)s;
  16. }
  17. #endif
  18. #ifndef HAVE_VASPRINTF
  19. int FAST_FUNC vasprintf(char **string_ptr, const char *format, va_list p)
  20. {
  21. int r;
  22. va_list p2;
  23. char buf[128];
  24. va_copy(p2, p);
  25. r = vsnprintf(buf, 128, format, p);
  26. va_end(p);
  27. /* Note: can't use xstrdup/xmalloc, they call vasprintf (us) on failure! */
  28. if (r < 128) {
  29. va_end(p2);
  30. *string_ptr = strdup(buf);
  31. return (*string_ptr ? r : -1);
  32. }
  33. *string_ptr = malloc(r+1);
  34. r = (*string_ptr ? vsnprintf(*string_ptr, r+1, format, p2) : -1);
  35. va_end(p2);
  36. return r;
  37. }
  38. #endif
  39. #ifndef HAVE_DPRINTF
  40. /* dprintf is now part of POSIX.1, but was only added in 2008 */
  41. int dprintf(int fd, const char *format, ...)
  42. {
  43. va_list p;
  44. int r;
  45. char *string_ptr;
  46. va_start(p, format);
  47. r = vasprintf(&string_ptr, format, p);
  48. va_end(p);
  49. if (r >= 0) {
  50. r = full_write(fd, string_ptr, r);
  51. free(string_ptr);
  52. }
  53. return r;
  54. }
  55. #endif
  56. #ifndef HAVE_MEMRCHR
  57. /* Copyright (C) 2005 Free Software Foundation, Inc.
  58. * memrchr() is a GNU function that might not be available everywhere.
  59. * It's basically the inverse of memchr() - search backwards in a
  60. * memory block for a particular character.
  61. */
  62. void* FAST_FUNC memrchr(const void *s, int c, size_t n)
  63. {
  64. const char *start = s, *end = s;
  65. end += n - 1;
  66. while (end >= start) {
  67. if (*end == (char)c)
  68. return (void *) end;
  69. end--;
  70. }
  71. return NULL;
  72. }
  73. #endif
  74. #ifndef HAVE_MKDTEMP
  75. /* This is now actually part of POSIX.1, but was only added in 2008 */
  76. char* FAST_FUNC mkdtemp(char *template)
  77. {
  78. if (mktemp(template) == NULL || mkdir(template, 0700) != 0)
  79. return NULL;
  80. return template;
  81. }
  82. #endif
  83. #ifndef HAVE_STRCASESTR
  84. /* Copyright (c) 1999, 2000 The ht://Dig Group */
  85. char* FAST_FUNC strcasestr(const char *s, const char *pattern)
  86. {
  87. int length = strlen(pattern);
  88. while (*s) {
  89. if (strncasecmp(s, pattern, length) == 0)
  90. return (char *)s;
  91. s++;
  92. }
  93. return 0;
  94. }
  95. #endif
  96. #ifndef HAVE_STRSEP
  97. /* Copyright (C) 2004 Free Software Foundation, Inc. */
  98. char* FAST_FUNC strsep(char **stringp, const char *delim)
  99. {
  100. char *start = *stringp;
  101. char *ptr;
  102. if (!start)
  103. return NULL;
  104. if (!*delim)
  105. ptr = start + strlen(start);
  106. else {
  107. ptr = strpbrk(start, delim);
  108. if (!ptr) {
  109. *stringp = NULL;
  110. return start;
  111. }
  112. }
  113. *ptr = '\0';
  114. *stringp = ptr + 1;
  115. return start;
  116. }
  117. #endif
  118. #ifndef HAVE_STPCPY
  119. char* FAST_FUNC stpcpy(char *p, const char *to_add)
  120. {
  121. while ((*p = *to_add) != '\0') {
  122. p++;
  123. to_add++;
  124. }
  125. return p;
  126. }
  127. #endif
  128. #ifndef HAVE_GETLINE
  129. ssize_t FAST_FUNC getline(char **lineptr, size_t *n, FILE *stream)
  130. {
  131. int ch;
  132. char *line = *lineptr;
  133. size_t alloced = *n;
  134. size_t len = 0;
  135. do {
  136. ch = fgetc(stream);
  137. if (ch == EOF)
  138. break;
  139. if (len + 1 >= alloced) {
  140. alloced += alloced/4 + 64;
  141. line = xrealloc(line, alloced);
  142. }
  143. line[len++] = ch;
  144. } while (ch != '\n');
  145. if (len == 0)
  146. return -1;
  147. line[len] = '\0';
  148. *lineptr = line;
  149. *n = alloced;
  150. return len;
  151. }
  152. #endif