3
0

platform.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 the GPL version 2, see the file LICENSE in this tarball.
  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_FDPRINTF
  39. /* dprintf is now actually part of POSIX.1, but was only added in 2008 */
  40. int fdprintf(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