platform.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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_GETLINE
  146. ssize_t FAST_FUNC getline(char **lineptr, size_t *n, FILE *stream)
  147. {
  148. int ch;
  149. char *line = *lineptr;
  150. size_t alloced = *n;
  151. size_t len = 0;
  152. do {
  153. ch = fgetc(stream);
  154. if (ch == EOF)
  155. break;
  156. if (len + 1 >= alloced) {
  157. alloced += alloced/4 + 64;
  158. line = xrealloc(line, alloced);
  159. }
  160. line[len++] = ch;
  161. } while (ch != '\n');
  162. if (len == 0)
  163. return -1;
  164. line[len] = '\0';
  165. *lineptr = line;
  166. *n = alloced;
  167. return len;
  168. }
  169. #endif
  170. #ifndef HAVE_TTYNAME_R
  171. int ttyname_r(int fd, char *buf, size_t buflen)
  172. {
  173. int r;
  174. char path[sizeof("/proc/self/fd/%d") + sizeof(int)*3];
  175. if (!isatty(fd))
  176. return errno == EINVAL ? ENOTTY : errno;
  177. sprintf(path, "/proc/self/fd/%d", fd);
  178. r = readlink(path, buf, buflen);
  179. if (r < 0)
  180. return errno;
  181. if (r >= buflen)
  182. return ERANGE;
  183. buf[r] = '\0';
  184. return 0;
  185. }
  186. #endif