32 #include <sys/resource.h>
33 #include <sys/types.h>
41 char *
strsep(
char **
str,
const char *delims)
51 while (**str !=
'\0') {
52 if (strchr(delims, **str)) {
73 if (!overwrite && getenv(name))
76 buflen = strlen(name) + strlen(value) + 2;
79 snprintf(buf, buflen,
"%s=%s", name, value);
88 return setenv(name,
"", 0);
92 #ifndef HAVE_STRCASESTR
93 static char *upper(
const char *orig,
char *buf,
int bufsize)
97 while (i < (bufsize - 1) && orig[i]) {
98 buf[i] = toupper(orig[i]);
107 char *
strcasestr(
const char *haystack,
const char *needle)
111 int u1len = strlen(haystack) + 1, u2len = strlen(needle) + 1;
119 offset = strstr(upper(haystack, u1, u1len), upper(needle, u2, u2len));
122 return ((
char *)((
unsigned long)haystack + (
unsigned long)(offset - u1)));
130 size_t strnlen(
const char *s,
size_t n)
134 for (len = 0; len < n; len++)
142 #if !defined(HAVE_STRNDUP) && !defined(__AST_DEBUG_MALLOC)
143 char *
strndup(
const char *s,
size_t n)
146 char *
new =
malloc(len + 1);
152 return memcpy(
new, s, len);
156 #if !defined(HAVE_VASPRINTF) && !defined(__AST_DEBUG_MALLOC)
157 int vasprintf(
char **strp,
const char *fmt, va_list ap)
165 size = vsnprintf(&s, 1, fmt, ap2);
170 vsnprintf(*strp, size + 1, fmt, ap);
176 #ifndef HAVE_TIMERSUB
177 void timersub(
struct timeval *tvend,
struct timeval *tvstart,
struct timeval *tvdiff)
179 tvdiff->tv_sec = tvend->tv_sec - tvstart->tv_sec;
180 tvdiff->tv_usec = tvend->tv_usec - tvstart->tv_usec;
181 if (tvdiff->tv_usec < 0) {
183 tvdiff->tv_usec += 1000000;
208 #if !defined(HAVE_ASPRINTF) && !defined(__AST_DEBUG_MALLOC)
209 int asprintf(
char **str,
const char *fmt, ...)
225 #define LONG_MIN (-9223372036854775807L-1L)
229 #define LONG_MAX 9223372036854775807L
239 uint64_t
strtoq(
const char *nptr,
char **endptr,
int base)
244 uint64_t qbase, cutoff;
245 int neg, any, cutlim;
255 }
while (isspace(c));
264 if ((base == 0 || base == 16) &&
265 c ==
'\0' && (*s ==
'x' || *s ==
'X')) {
271 base = c ==
'\0' ? 8 : 10;
291 qbase = (unsigned)base;
292 cutoff = neg ? (uint64_t)-(LONG_MIN + LONG_MAX) + LONG_MAX : LONG_MAX;
293 cutlim = cutoff % qbase;
295 for (acc = 0, any = 0;; c = *s++) {
301 c -= isupper(c) ?
'A' - 10 :
'a' - 10;
306 if (any < 0 || acc > cutoff || (acc == cutoff && c > cutlim))
315 acc = neg ? LONG_MIN : LONG_MAX;
319 *((
const char **)endptr) = any ? s - 1 : nptr;
324 #ifndef HAVE_GETLOADAVG
330 double avg[3] = { 0.0, 0.0, 0.0 };
333 if ((LOADAVG = fopen(
"/proc/loadavg",
"r"))) {
334 fscanf(LOADAVG,
"%lf %lf %lf", &avg[0], &avg[1], &avg[2]);
339 for (i = 0; (i < nelem) && (i < 3); i++) {
352 for (i = 0; i < nelem; i++) {
363 #if BYTE_ORDER == BIG_ENDIAN
365 #elif BYTE_ORDER == LITTLE_ENDIAN
372 (((uint64_t)
number.c[0]) << 56) |
373 (((uint64_t)
number.c[1]) << 48) |
374 (((uint64_t)
number.c[2]) << 40) |
375 (((uint64_t)
number.c[3]) << 32) |
376 (((uint64_t)
number.c[4]) << 24) |
377 (((uint64_t)
number.c[5]) << 16) |
378 (((uint64_t)
number.c[6]) << 8) |
379 (((uint64_t)
number.c[7]) << 0);
381 #error "Unknown byte order"
389 #if BYTE_ORDER == BIG_ENDIAN
391 #elif BYTE_ORDER == LITTLE_ENDIAN
398 (((uint64_t)
number.c[0]) << 56) |
399 (((uint64_t)
number.c[1]) << 48) |
400 (((uint64_t)
number.c[2]) << 40) |
401 (((uint64_t)
number.c[3]) << 32) |
402 (((uint64_t)
number.c[4]) << 24) |
403 (((uint64_t)
number.c[5]) << 16) |
404 (((uint64_t)
number.c[6]) << 8) |
405 (((uint64_t)
number.c[7]) << 0);
407 #error "Unknown byte order"
413 int ffsll(
long long n)
416 for (i = 0; i < 64; i++) {
417 if ((1LL << i) & n) {
425 #ifndef HAVE_CLOSEFROM
431 char path[16], *result;
432 struct dirent *entry;
434 snprintf(path,
sizeof(path),
"/proc/%d/fd", (
int) getpid());
435 if ((dir = opendir(path))) {
436 while ((entry = readdir(dir))) {
438 if (entry->d_name[0] ==
'.') {
441 if ((x = strtol(entry->d_name, &result, 10)) && x >= n) {
449 long flags = fcntl(x, F_GETFD);
450 if (flags == -1 &&
errno == EBADF) {
453 fcntl(x, F_SETFD, flags | FD_CLOEXEC);
459 getrlimit(RLIMIT_NOFILE, &rl);
460 if (rl.rlim_cur > 65535) {
470 for (x = n; x < rl.rlim_cur; x++) {
474 long flags = fcntl(x, F_GETFD);
475 if (flags == -1 &&
errno == EBADF) {
478 fcntl(x, F_SETFD, flags | FD_CLOEXEC);
504 #define MKTEMP_NAME 0
505 #define MKTEMP_FILE 1
508 #define TEMPCHARS "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_."
509 #define NUM_CHARS (sizeof(TEMPCHARS) - 1)
511 static int mktemp_internal(
char *path,
int slen,
int mode)
513 char *start, *cp, *ep;
514 const char *tempchars = TEMPCHARS;
515 unsigned int r, tries;
521 if (len == 0 || slen >= len) {
525 ep = path + len - slen;
528 for (start = ep; start > path && start[-1] ==
'X'; start--) {
529 if (tries < INT_MAX / NUM_CHARS) {
536 for (cp = start; cp != ep; cp++) {
543 if (lstat(path, &sb) != 0) {
544 return (
errno == ENOENT ? 0 : -1);
548 fd = open(path, O_CREAT | O_EXCL | O_RDWR, S_IRUSR | S_IWUSR);
549 if (fd != -1 ||
errno != EEXIST) {
554 if (mkdir(path, S_IRUSR | S_IWUSR | S_IXUSR) == 0) {
557 if (
errno != EEXIST) {
570 return mktemp_internal(path, 0, MKTEMP_DIR) ? NULL : path;
578 return (
float)(int)((x) - 0.5);
580 return (
float)(int)((x) + 0.5);
int vasprintf(char **strp, const char *fmt, va_list ap)
Asterisk main include file. File version handling, generic pbx functions.
char * strsep(char **str, const char *delims)
#define ast_alloca(size)
call __builtin_alloca to ensure we get gcc builtin semantics
uint64_t ntohll(uint64_t net64)
uint64_t strtoq(const char *nptr, char **endptr, int base)
uint64_t htonll(uint64_t host64)
void closefrom(int lowfd)
int asprintf(char **str, const char *fmt,...)
long int ast_random(void)
char * mkdtemp(char *template_s)
void timersub(struct timeval *tvend, struct timeval *tvstart, struct timeval *tvdiff)
int setenv(const char *name, const char *value, int overwrite)
static int len(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)
size_t strnlen(const char *, size_t)
char * strcasestr(const char *, const char *)
int getloadavg(double *list, int nelem)
int unsetenv(const char *name)
char * strndup(const char *, size_t)