platform.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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_USLEEP
  19. int FAST_FUNC usleep(unsigned usec)
  20. {
  21. struct timespec ts;
  22. ts.tv_sec = usec / 1000000u;
  23. ts.tv_nsec = (usec % 1000000u) * 1000u;
  24. /*
  25. * If a signal has non-default handler, nanosleep returns early.
  26. * Our version of usleep doesn't return early
  27. * if interrupted by such signals:
  28. */
  29. while (nanosleep(&ts, &ts) != 0)
  30. continue;
  31. return 0;
  32. }
  33. #endif
  34. #ifndef HAVE_VASPRINTF
  35. int FAST_FUNC vasprintf(char **string_ptr, const char *format, va_list p)
  36. {
  37. int r;
  38. va_list p2;
  39. char buf[128];
  40. va_copy(p2, p);
  41. r = vsnprintf(buf, 128, format, p);
  42. va_end(p);
  43. /* Note: can't use xstrdup/xmalloc, they call vasprintf (us) on failure! */
  44. if (r < 128) {
  45. va_end(p2);
  46. *string_ptr = strdup(buf);
  47. return (*string_ptr ? r : -1);
  48. }
  49. *string_ptr = malloc(r+1);
  50. r = (*string_ptr ? vsnprintf(*string_ptr, r+1, format, p2) : -1);
  51. va_end(p2);
  52. return r;
  53. }
  54. #endif
  55. #ifndef HAVE_DPRINTF
  56. /* dprintf is now part of POSIX.1, but was only added in 2008 */
  57. int dprintf(int fd, const char *format, ...)
  58. {
  59. va_list p;
  60. int r;
  61. char *string_ptr;
  62. va_start(p, format);
  63. r = vasprintf(&string_ptr, format, p);
  64. va_end(p);
  65. if (r >= 0) {
  66. r = full_write(fd, string_ptr, r);
  67. free(string_ptr);
  68. }
  69. return r;
  70. }
  71. #endif
  72. #ifndef HAVE_MEMRCHR
  73. /* Copyright (C) 2005 Free Software Foundation, Inc.
  74. * memrchr() is a GNU function that might not be available everywhere.
  75. * It's basically the inverse of memchr() - search backwards in a
  76. * memory block for a particular character.
  77. */
  78. void* FAST_FUNC memrchr(const void *s, int c, size_t n)
  79. {
  80. const char *start = s, *end = s;
  81. end += n - 1;
  82. while (end >= start) {
  83. if (*end == (char)c)
  84. return (void *) end;
  85. end--;
  86. }
  87. return NULL;
  88. }
  89. #endif
  90. #ifndef HAVE_MKDTEMP
  91. /* This is now actually part of POSIX.1, but was only added in 2008 */
  92. char* FAST_FUNC mkdtemp(char *template)
  93. {
  94. /* NB: on error, mktemp returns an empty string, not NULL */
  95. if (mktemp(template)[0] == '\0' || mkdir(template, 0700) != 0)
  96. return NULL;
  97. return template;
  98. }
  99. #endif
  100. #ifndef HAVE_STRCASESTR
  101. /* Copyright (c) 1999, 2000 The ht://Dig Group */
  102. char* FAST_FUNC strcasestr(const char *s, const char *pattern)
  103. {
  104. int length = strlen(pattern);
  105. while (*s) {
  106. if (strncasecmp(s, pattern, length) == 0)
  107. return (char *)s;
  108. s++;
  109. }
  110. return 0;
  111. }
  112. #endif
  113. #ifndef HAVE_STRSEP
  114. /* Copyright (C) 2004 Free Software Foundation, Inc. */
  115. char* FAST_FUNC strsep(char **stringp, const char *delim)
  116. {
  117. char *start = *stringp;
  118. char *ptr;
  119. if (!start)
  120. return NULL;
  121. if (!*delim)
  122. ptr = start + strlen(start);
  123. else {
  124. ptr = strpbrk(start, delim);
  125. if (!ptr) {
  126. *stringp = NULL;
  127. return start;
  128. }
  129. }
  130. *ptr = '\0';
  131. *stringp = ptr + 1;
  132. return start;
  133. }
  134. #endif
  135. #ifndef HAVE_STPCPY
  136. char* FAST_FUNC stpcpy(char *p, const char *to_add)
  137. {
  138. while ((*p = *to_add) != '\0') {
  139. p++;
  140. to_add++;
  141. }
  142. return p;
  143. }
  144. #endif
  145. #ifndef HAVE_STPNCPY
  146. char* FAST_FUNC stpncpy(char *p, const char *to_add, size_t n)
  147. {
  148. while (n != 0 && (*p = *to_add) != '\0') {
  149. p++;
  150. to_add++;
  151. n--;
  152. }
  153. return p;
  154. }
  155. #endif
  156. #ifndef HAVE_GETLINE
  157. ssize_t FAST_FUNC getline(char **lineptr, size_t *n, FILE *stream)
  158. {
  159. int ch;
  160. char *line = *lineptr;
  161. size_t alloced = *n;
  162. size_t len = 0;
  163. do {
  164. ch = fgetc(stream);
  165. if (ch == EOF)
  166. break;
  167. if (len + 1 >= alloced) {
  168. alloced += alloced/4 + 64;
  169. line = xrealloc(line, alloced);
  170. }
  171. line[len++] = ch;
  172. } while (ch != '\n');
  173. if (len == 0)
  174. return -1;
  175. line[len] = '\0';
  176. *lineptr = line;
  177. *n = alloced;
  178. return len;
  179. }
  180. #endif
  181. #ifndef HAVE_TTYNAME_R
  182. int ttyname_r(int fd, char *buf, size_t buflen)
  183. {
  184. int r;
  185. char path[sizeof("/proc/self/fd/%d") + sizeof(int)*3];
  186. if (!isatty(fd))
  187. return errno == EINVAL ? ENOTTY : errno;
  188. sprintf(path, "/proc/self/fd/%d", fd);
  189. r = readlink(path, buf, buflen);
  190. if (r < 0)
  191. return errno;
  192. if (r >= buflen)
  193. return ERANGE;
  194. buf[r] = '\0';
  195. return 0;
  196. }
  197. #endif