28#define ASTMM_LIBC ASTMM_IGNORE
33#include <sys/resource.h>
45char *
strsep(
char **
str,
const char *delims)
55 while (**
str !=
'\0') {
56 if (strchr(delims, **
str)) {
77 if (!overwrite && getenv(
name))
80 buflen = strlen(
name) + strlen(
value) + 2;
96#ifndef HAVE_STRCASESTR
97static char *upper(
const char *orig,
char *
buf,
int bufsize)
101 while (i < (bufsize - 1) && orig[i]) {
102 buf[i] = toupper(orig[i]);
111char *
strcasestr(
const char *haystack,
const char *needle)
115 int u1len = strlen(haystack) + 1, u2len = strlen(needle) + 1;
123 offset = strstr(upper(haystack, u1, u1len), upper(needle, u2, u2len));
126 return ((
char *)((
unsigned long)haystack + (
unsigned long)(offset - u1)));
134size_t strnlen(
const char *s,
size_t n)
146#if !defined(HAVE_STRNDUP)
147char *
strndup(
const char *s,
size_t n)
156 return memcpy(
new, s,
len);
160#if !defined(HAVE_VASPRINTF)
161int vasprintf(
char **strp,
const char *fmt, va_list ap)
169 size = vsnprintf(&s, 1, fmt, ap2);
174 vsnprintf(*strp, size + 1, fmt, ap);
181void timersub(
struct timeval *tvend,
struct timeval *tvstart,
struct timeval *tvdiff)
183 tvdiff->tv_sec = tvend->tv_sec - tvstart->tv_sec;
184 tvdiff->tv_usec = tvend->tv_usec - tvstart->tv_usec;
185 if (tvdiff->tv_usec < 0) {
187 tvdiff->tv_usec += 1000000;
212#if !defined(HAVE_ASPRINTF)
229#define LONG_MIN (-9223372036854775807L-1L)
233#define LONG_MAX 9223372036854775807L
243uint64_t
strtoq(
const char *nptr,
char **endptr,
int base)
248 uint64_t qbase, cutoff;
249 int neg, any, cutlim;
259 }
while (isspace(
c));
268 if ((base == 0 || base == 16) &&
269 c ==
'\0' && (*s ==
'x' || *s ==
'X')) {
275 base =
c ==
'\0' ? 8 : 10;
295 qbase = (unsigned)base;
296 cutoff = neg ? (uint64_t)-(LONG_MIN + LONG_MAX) + LONG_MAX : LONG_MAX;
297 cutlim = cutoff % qbase;
299 for (acc = 0, any = 0;;
c = *s++) {
305 c -= isupper(
c) ?
'A' - 10 :
'a' - 10;
310 if (any < 0 || acc > cutoff || (acc == cutoff &&
c > cutlim))
319 acc = neg ? LONG_MIN : LONG_MAX;
323 *((
const char **)endptr) = any ? s - 1 : nptr;
328#ifndef HAVE_GETLOADAVG
334 double avg[3] = { 0.0, 0.0, 0.0 };
337 if ((LOADAVG = fopen(
"/proc/loadavg",
"r"))) {
338 fscanf(LOADAVG,
"%lf %lf %lf", &avg[0], &avg[1], &avg[2]);
343 for (i = 0; (i < nelem) && (i < 3); i++) {
356 for (i = 0; i < nelem; i++) {
367#if BYTE_ORDER == BIG_ENDIAN
369#elif BYTE_ORDER == LITTLE_ENDIAN
376 (((uint64_t)
number.c[0]) << 56) |
377 (((uint64_t)
number.c[1]) << 48) |
378 (((uint64_t)
number.c[2]) << 40) |
379 (((uint64_t)
number.c[3]) << 32) |
380 (((uint64_t)
number.c[4]) << 24) |
381 (((uint64_t)
number.c[5]) << 16) |
382 (((uint64_t)
number.c[6]) << 8) |
383 (((uint64_t)
number.c[7]) << 0);
385 #error "Unknown byte order"
393#if BYTE_ORDER == BIG_ENDIAN
395#elif BYTE_ORDER == LITTLE_ENDIAN
402 (((uint64_t)
number.c[0]) << 56) |
403 (((uint64_t)
number.c[1]) << 48) |
404 (((uint64_t)
number.c[2]) << 40) |
405 (((uint64_t)
number.c[3]) << 32) |
406 (((uint64_t)
number.c[4]) << 24) |
407 (((uint64_t)
number.c[5]) << 16) |
408 (((uint64_t)
number.c[6]) << 8) |
409 (((uint64_t)
number.c[7]) << 0);
411 #error "Unknown byte order"
417int ffsll(
long long n)
420 for (i = 0; i < 64; i++) {
421 if ((1LL << i) & n) {
429#ifndef HAVE_CLOSEFROM
437 int fd=n, loopmax, i;
443 if (getrlimit(RLIMIT_NOFILE, &rl) == -1) {
449 maxfd = sysconf (_SC_OPEN_MAX);
452 if (maxfd == -1 || maxfd > 65536) {
467 loopmax = maxfd - fd;
471 for (i = 0; i < loopmax; i++) {
476 for (i = 0; i < loopmax; i++) {
477 if (fds[i].revents == POLLNVAL) {
487 flags = fcntl(fds[i].fd, F_GETFD);
488 if (flags == -1 &&
errno == EBADF) {
491 fcntl(fds[i].fd, F_SETFD, flags | FD_CLOEXEC);
522#define TEMPCHARS "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_."
523#define NUM_CHARS (sizeof(TEMPCHARS) - 1)
525static int mktemp_internal(
char *path,
int slen,
int mode)
527 char *start, *cp, *ep;
528 const char *tempchars = TEMPCHARS;
529 unsigned int r, tries;
535 if (
len == 0 || slen >=
len) {
539 ep = path +
len - slen;
542 for (start = ep; start > path && start[-1] ==
'X'; start--) {
543 if (tries < INT_MAX / NUM_CHARS) {
550 for (cp = start; cp != ep; cp++) {
557 if (lstat(path, &sb) != 0) {
558 return (
errno == ENOENT ? 0 : -1);
562 fd = open(path, O_CREAT | O_EXCL | O_RDWR, S_IRUSR | S_IWUSR);
563 if (fd != -1 ||
errno != EEXIST) {
568 if (mkdir(path, S_IRUSR | S_IWUSR | S_IXUSR) == 0) {
571 if (
errno != EEXIST) {
584 return mktemp_internal(path, 0, MKTEMP_DIR) ?
NULL : path;
592 return (
float)(int)((x) - 0.5);
594 return (
float)(int)((x) + 0.5);
int unsetenv(const char *name)
int setenv(const char *name, const char *value, int overwrite)
int getloadavg(double *list, int nelem)
int vasprintf(char **strp, const char *fmt, va_list ap)
void timersub(struct timeval *tvend, struct timeval *tvstart, struct timeval *tvdiff)
char * strsep(char **str, const char *delims)
char * mkdtemp(char *template_s)
void closefrom(int lowfd)
int asprintf(char **str, const char *fmt,...)
char * strndup(const char *, size_t)
uint64_t strtoq(const char *nptr, char **endptr, int base)
char * strcasestr(const char *, const char *)
size_t strnlen(const char *, size_t)
Asterisk main include file. File version handling, generic pbx functions.
#define ast_alloca(size)
call __builtin_alloca to ensure we get gcc builtin semantics
static int len(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)
#define ast_poll(a, b, c)
uint64_t ntohll(uint64_t net64)
uint64_t htonll(uint64_t host64)
long int ast_random(void)