platform.c 3.9 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. */
  30. while (nanosleep(&ts, &ts) != 0)
  31. continue;
  32. return 0;
  33. }
  34. #endif
  35. #ifndef HAVE_VASPRINTF
  36. int FAST_FUNC vasprintf(char **string_ptr, const char *format, va_list p)
  37. {
  38. int r;
  39. va_list p2;
  40. char buf[128];
  41. va_copy(p2, p);
  42. r = vsnprintf(buf, 128, format, p);
  43. va_end(p);
  44. /* Note: can't use xstrdup/xmalloc, they call vasprintf (us) on failure! */
  45. if (r < 128) {
  46. va_end(p2);
  47. *string_ptr = strdup(buf);
  48. return (*string_ptr ? r : -1);
  49. }
  50. *string_ptr = malloc(r+1);
  51. r = (*string_ptr ? vsnprintf(*string_ptr, r+1, format, p2) : -1);
  52. va_end(p2);
  53. return r;
  54. }
  55. #endif
  56. #ifndef HAVE_DPRINTF
  57. /* dprintf is now part of POSIX.1, but was only added in 2008 */
  58. int dprintf(int fd, const char *format, ...)
  59. {
  60. va_list p;
  61. int r;
  62. char *string_ptr;
  63. va_start(p, format);
  64. r = vasprintf(&string_ptr, format, p);
  65. va_end(p);
  66. if (r >= 0) {
  67. r = full_write(fd, string_ptr, r);
  68. free(string_ptr);
  69. }
  70. return r;
  71. }
  72. #endif
  73. #ifndef HAVE_MEMRCHR
  74. /* Copyright (C) 2005 Free Software Foundation, Inc.
  75. * memrchr() is a GNU function that might not be available everywhere.
  76. * It's basically the inverse of memchr() - search backwards in a
  77. * memory block for a particular character.
  78. */
  79. void* FAST_FUNC memrchr(const void *s, int c, size_t n)
  80. {
  81. const char *start = s, *end = s;
  82. end += n - 1;
  83. while (end >= start) {
  84. if (*end == (char)c)
  85. return (void *) end;
  86. end--;
  87. }
  88. return NULL;
  89. }
  90. #endif
  91. #ifndef HAVE_MKDTEMP
  92. /* This is now actually part of POSIX.1, but was only added in 2008 */
  93. char* FAST_FUNC mkdtemp(char *template)
  94. {
  95. if (mktemp(template) == NULL || 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