28#define ASTMM_LIBC ASTMM_IGNORE
33#include <sys/resource.h>
44char *
strsep(
char **
str,
const char *delims)
54 while (**
str !=
'\0') {
55 if (strchr(delims, **
str)) {
76 if (!overwrite && getenv(
name))
79 buflen = strlen(
name) + strlen(
value) + 2;
95#ifndef HAVE_STRCASESTR
96static char *upper(
const char *orig,
char *
buf,
int bufsize)
100 while (i < (bufsize - 1) && orig[i]) {
101 buf[i] = toupper(orig[i]);
110char *
strcasestr(
const char *haystack,
const char *needle)
114 int u1len = strlen(haystack) + 1, u2len = strlen(needle) + 1;
122 offset = strstr(upper(haystack, u1, u1len), upper(needle, u2, u2len));
125 return ((
char *)((
unsigned long)haystack + (
unsigned long)(offset - u1)));
133size_t strnlen(
const char *s,
size_t n)
145#if !defined(HAVE_STRNDUP)
146char *
strndup(
const char *s,
size_t n)
155 return memcpy(
new, s,
len);
159#if !defined(HAVE_VASPRINTF)
160int vasprintf(
char **strp,
const char *fmt, va_list ap)
168 size = vsnprintf(&s, 1, fmt, ap2);
173 vsnprintf(*strp, size + 1, fmt, ap);
180void timersub(
struct timeval *tvend,
struct timeval *tvstart,
struct timeval *tvdiff)
182 tvdiff->tv_sec = tvend->tv_sec - tvstart->tv_sec;
183 tvdiff->tv_usec = tvend->tv_usec - tvstart->tv_usec;
184 if (tvdiff->tv_usec < 0) {
186 tvdiff->tv_usec += 1000000;
211#if !defined(HAVE_ASPRINTF)
228#define LONG_MIN (-9223372036854775807L-1L)
232#define LONG_MAX 9223372036854775807L
242uint64_t
strtoq(
const char *nptr,
char **endptr,
int base)
247 uint64_t qbase, cutoff;
248 int neg, any, cutlim;
258 }
while (isspace(
c));
267 if ((base == 0 || base == 16) &&
268 c ==
'\0' && (*s ==
'x' || *s ==
'X')) {
274 base =
c ==
'\0' ? 8 : 10;
294 qbase = (unsigned)base;
295 cutoff = neg ? (uint64_t)-(LONG_MIN + LONG_MAX) + LONG_MAX : LONG_MAX;
296 cutlim = cutoff % qbase;
298 for (acc = 0, any = 0;;
c = *s++) {
304 c -= isupper(
c) ?
'A' - 10 :
'a' - 10;
309 if (any < 0 || acc > cutoff || (acc == cutoff &&
c > cutlim))
318 acc = neg ? LONG_MIN : LONG_MAX;
322 *((
const char **)endptr) = any ? s - 1 : nptr;
327#ifndef HAVE_GETLOADAVG
333 double avg[3] = { 0.0, 0.0, 0.0 };
336 if ((LOADAVG = fopen(
"/proc/loadavg",
"r"))) {
337 fscanf(LOADAVG,
"%lf %lf %lf", &avg[0], &avg[1], &avg[2]);
342 for (i = 0; (i < nelem) && (i < 3); i++) {
355 for (i = 0; i < nelem; i++) {
366#if BYTE_ORDER == BIG_ENDIAN
368#elif BYTE_ORDER == LITTLE_ENDIAN
375 (((uint64_t)
number.c[0]) << 56) |
376 (((uint64_t)
number.c[1]) << 48) |
377 (((uint64_t)
number.c[2]) << 40) |
378 (((uint64_t)
number.c[3]) << 32) |
379 (((uint64_t)
number.c[4]) << 24) |
380 (((uint64_t)
number.c[5]) << 16) |
381 (((uint64_t)
number.c[6]) << 8) |
382 (((uint64_t)
number.c[7]) << 0);
384 #error "Unknown byte order"
392#if BYTE_ORDER == BIG_ENDIAN
394#elif BYTE_ORDER == LITTLE_ENDIAN
401 (((uint64_t)
number.c[0]) << 56) |
402 (((uint64_t)
number.c[1]) << 48) |
403 (((uint64_t)
number.c[2]) << 40) |
404 (((uint64_t)
number.c[3]) << 32) |
405 (((uint64_t)
number.c[4]) << 24) |
406 (((uint64_t)
number.c[5]) << 16) |
407 (((uint64_t)
number.c[6]) << 8) |
408 (((uint64_t)
number.c[7]) << 0);
410 #error "Unknown byte order"
416int ffsll(
long long n)
419 for (i = 0; i < 64; i++) {
420 if ((1LL << i) & n) {
428#ifndef HAVE_CLOSEFROM
436 int fd=n, loopmax, i;
442 if (getrlimit(RLIMIT_NOFILE, &rl) == -1) {
448 maxfd = sysconf (_SC_OPEN_MAX);
451 if (maxfd == -1 || maxfd > 65536) {
466 loopmax = maxfd - fd;
470 for (i = 0; i < loopmax; i++) {
474 poll(fds, loopmax, 0);
475 for (i = 0; i < loopmax; i++) {
476 if (fds[i].revents == POLLNVAL) {
486 flags = fcntl(fds[i].fd, F_GETFD);
487 if (flags == -1 &&
errno == EBADF) {
490 fcntl(fds[i].fd, F_SETFD, flags | FD_CLOEXEC);
521#define TEMPCHARS "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_."
522#define NUM_CHARS (sizeof(TEMPCHARS) - 1)
524static int mktemp_internal(
char *path,
int slen,
int mode)
526 char *start, *cp, *ep;
527 const char *tempchars = TEMPCHARS;
528 unsigned int r, tries;
534 if (
len == 0 || slen >=
len) {
538 ep = path +
len - slen;
541 for (start = ep; start > path && start[-1] ==
'X'; start--) {
542 if (tries < INT_MAX / NUM_CHARS) {
549 for (cp = start; cp != ep; cp++) {
556 if (lstat(path, &sb) != 0) {
557 return (
errno == ENOENT ? 0 : -1);
561 fd = open(path, O_CREAT | O_EXCL | O_RDWR, S_IRUSR | S_IWUSR);
562 if (fd != -1 ||
errno != EEXIST) {
567 if (mkdir(path, S_IRUSR | S_IWUSR | S_IXUSR) == 0) {
570 if (
errno != EEXIST) {
583 return mktemp_internal(path, 0, MKTEMP_DIR) ?
NULL : path;
591 return (
float)(int)((x) - 0.5);
593 return (
float)(int)((x) + 0.5);
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)
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)
uint64_t ntohll(uint64_t net64)
uint64_t htonll(uint64_t host64)
long int ast_random(void)