2 * Replacements for common but usually nonstandard functions that aren't
3 * supplied by all platforms.
5 * Copyright (C) 2009 by Dan Fandrich <dan@coneharvesters.com>, et. al.
7 * Licensed under GPLv2, see file LICENSE in this source tree.
11 #ifndef HAVE_STRCHRNUL
12 char* FAST_FUNC
strchrnul(const char *s
, int c
)
14 while (*s
!= '\0' && *s
!= c
)
21 int FAST_FUNC
usleep(unsigned usec
)
24 ts
.tv_sec
= usec
/ 1000000u;
25 ts
.tv_nsec
= (usec
% 1000000u) * 1000u;
27 * If a signal has non-default handler, nanosleep returns early.
28 * Our version of usleep doesn't return early
29 * if interrupted by such signals:
31 while (nanosleep(&ts
, &ts
) != 0)
37 #ifndef HAVE_VASPRINTF
38 int FAST_FUNC
vasprintf(char **string_ptr
, const char *format
, va_list p
)
45 r
= vsnprintf(buf
, 128, format
, p
);
48 /* Note: can't use xstrdup/xmalloc, they call vasprintf (us) on failure! */
52 *string_ptr
= strdup(buf
);
53 return (*string_ptr
? r
: -1);
56 *string_ptr
= malloc(r
+1);
57 r
= (*string_ptr
? vsnprintf(*string_ptr
, r
+1, format
, p2
) : -1);
65 /* dprintf is now part of POSIX.1, but was only added in 2008 */
66 int dprintf(int fd
, const char *format
, ...)
73 r
= vasprintf(&string_ptr
, format
, p
);
76 r
= full_write(fd
, string_ptr
, r
);
84 /* Copyright (C) 2005 Free Software Foundation, Inc.
85 * memrchr() is a GNU function that might not be available everywhere.
86 * It's basically the inverse of memchr() - search backwards in a
87 * memory block for a particular character.
89 void* FAST_FUNC
memrchr(const void *s
, int c
, size_t n
)
91 const char *start
= s
, *end
= s
;
95 while (end
>= start
) {
106 /* This is now actually part of POSIX.1, but was only added in 2008 */
107 char* FAST_FUNC
mkdtemp(char *template)
109 /* NB: on error, mktemp returns an empty string, not NULL */
110 if (mktemp(template)[0] == '\0' || mkdir(template, 0700) != 0)
116 #ifndef HAVE_STRCASESTR
117 /* Copyright (c) 1999, 2000 The ht://Dig Group */
118 char* FAST_FUNC
strcasestr(const char *s
, const char *pattern
)
120 int length
= strlen(pattern
);
123 if (strncasecmp(s
, pattern
, length
) == 0)
132 /* Copyright (C) 2004 Free Software Foundation, Inc. */
133 char* FAST_FUNC
strsep(char **stringp
, const char *delim
)
135 char *start
= *stringp
;
142 ptr
= start
+ strlen(start
);
144 ptr
= strpbrk(start
, delim
);
159 char* FAST_FUNC
stpcpy(char *p
, const char *to_add
)
161 while ((*p
= *to_add
) != '\0') {
170 char* FAST_FUNC
stpncpy(char *p
, const char *to_add
, size_t n
)
172 while (n
!= 0 && (*p
= *to_add
) != '\0') {
182 ssize_t FAST_FUNC
getline(char **lineptr
, size_t *n
, FILE *stream
)
185 char *line
= *lineptr
;
193 if (len
+ 1 >= alloced
) {
194 alloced
+= alloced
/4 + 64;
195 line
= xrealloc(line
, alloced
);
198 } while (ch
!= '\n');
210 #ifndef HAVE_TTYNAME_R
211 int ttyname_r(int fd
, char *buf
, size_t buflen
)
214 char path
[sizeof("/proc/self/fd/%d") + sizeof(int)*3];
217 return errno
== EINVAL
? ENOTTY
: errno
;
218 sprintf(path
, "/proc/self/fd/%d", fd
);
219 r
= readlink(path
, buf
, buflen
);