3
0

platform.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. if (r < 128) {
  28. va_end(p2);
  29. *string_ptr = xstrdup(buf);
  30. return r;
  31. }
  32. *string_ptr = xmalloc(r+1);
  33. r = vsnprintf(*string_ptr, r+1, format, p2);
  34. va_end(p2);
  35. return r;
  36. }
  37. #endif
  38. #ifndef HAVE_DPRINTF
  39. /* dprintf is now part of POSIX.1, but was only added in 2008 */
  40. int dprintf(int fd, const char *format, ...)
  41. {
  42. va_list p;
  43. int r;
  44. char *string_ptr;
  45. va_start(p, format);
  46. r = vasprintf(&string_ptr, format, p);
  47. va_end(p);
  48. if (r >= 0) {
  49. r = full_write(fd, string_ptr, r);
  50. free(string_ptr);
  51. }
  52. return r;
  53. }
  54. #endif
  55. #ifndef HAVE_MEMRCHR
  56. /* Copyright (C) 2005 Free Software Foundation, Inc.
  57. * memrchr() is a GNU function that might not be available everywhere.
  58. * It's basically the inverse of memchr() - search backwards in a
  59. * memory block for a particular character.
  60. */
  61. void* FAST_FUNC memrchr(const void *s, int c, size_t n)
  62. {
  63. const char *start = s, *end = s;
  64. end += n - 1;
  65. while (end >= start) {
  66. if (*end == (char)c)
  67. return (void *) end;
  68. end--;
  69. }
  70. return NULL;
  71. }
  72. #endif
  73. #ifndef HAVE_MKDTEMP
  74. /* This is now actually part of POSIX.1, but was only added in 2008 */
  75. char* FAST_FUNC mkdtemp(char *template)
  76. {
  77. if (mktemp(template) == NULL || mkdir(template, 0700) != 0)
  78. return NULL;
  79. return template;
  80. }
  81. #endif
  82. #ifndef HAVE_STRCASESTR
  83. /* Copyright (c) 1999, 2000 The ht://Dig Group */
  84. char* FAST_FUNC strcasestr(const char *s, const char *pattern)
  85. {
  86. int length = strlen(pattern);
  87. while (*s) {
  88. if (strncasecmp(s, pattern, length) == 0)
  89. return (char *)s;
  90. s++;
  91. }
  92. return 0;
  93. }
  94. #endif
  95. #ifndef HAVE_STRSEP
  96. /* Copyright (C) 2004 Free Software Foundation, Inc. */
  97. char* FAST_FUNC strsep(char **stringp, const char *delim)
  98. {
  99. char *start = *stringp;
  100. char *ptr;
  101. if (!start)
  102. return NULL;
  103. if (!*delim)
  104. ptr = start + strlen(start);
  105. else {
  106. ptr = strpbrk(start, delim);
  107. if (!ptr) {
  108. *stringp = NULL;
  109. return start;
  110. }
  111. }
  112. *ptr = '\0';
  113. *stringp = ptr + 1;
  114. return start;
  115. }
  116. #endif
  117. #ifndef HAVE_STPCPY
  118. char* FAST_FUNC stpcpy(char *p, const char *to_add)
  119. {
  120. while ((*p = *to_add) != '\0') {
  121. p++;
  122. to_add++;
  123. }
  124. return p;
  125. }
  126. #endif
  127. #ifndef HAVE_GETLINE
  128. ssize_t FAST_FUNC getline(char **lineptr, size_t *n, FILE *stream)
  129. {
  130. int ch;
  131. char *line = *lineptr;
  132. size_t alloced = *n;
  133. size_t len = 0;
  134. do {
  135. ch = fgetc(stream);
  136. if (ch == EOF)
  137. break;
  138. if (len + 1 >= alloced) {
  139. alloced += alloced/4 + 64;
  140. line = xrealloc(line, alloced);
  141. }
  142. line[len++] = ch;
  143. } while (ch != '\n');
  144. if (len == 0)
  145. return -1;
  146. line[len] = '\0';
  147. *lineptr = line;
  148. *n = alloced;
  149. return len;
  150. }
  151. #endif