00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #include "asterisk.h"
00029
00030 #include <ctype.h>
00031 #include <sys/time.h>
00032 #include <sys/resource.h>
00033 #include <sys/types.h>
00034 #include <dirent.h>
00035 #include <unistd.h>
00036 #include <fcntl.h>
00037
00038 #include "asterisk/utils.h"
00039
00040 #ifndef HAVE_STRSEP
00041 char *strsep(char **str, const char *delims)
00042 {
00043 char *token;
00044
00045 if (!*str) {
00046
00047 return NULL;
00048 }
00049
00050 token = *str;
00051 while (**str != '\0') {
00052 if (strchr(delims, **str)) {
00053 **str = '\0';
00054 (*str)++;
00055 return token;
00056 }
00057 (*str)++;
00058 }
00059
00060
00061 *str = NULL;
00062
00063 return token;
00064 }
00065 #endif
00066
00067 #ifndef HAVE_SETENV
00068 int setenv(const char *name, const char *value, int overwrite)
00069 {
00070 unsigned char *buf;
00071 int buflen;
00072
00073 if (!overwrite && getenv(name))
00074 return 0;
00075
00076 buflen = strlen(name) + strlen(value) + 2;
00077 buf = ast_alloca(buflen);
00078
00079 snprintf(buf, buflen, "%s=%s", name, value);
00080
00081 return putenv(buf);
00082 }
00083 #endif
00084
00085 #ifndef HAVE_UNSETENV
00086 int unsetenv(const char *name)
00087 {
00088 return setenv(name, "", 0);
00089 }
00090 #endif
00091
00092 #ifndef HAVE_STRCASESTR
00093 static char *upper(const char *orig, char *buf, int bufsize)
00094 {
00095 int i = 0;
00096
00097 while (i < (bufsize - 1) && orig[i]) {
00098 buf[i] = toupper(orig[i]);
00099 i++;
00100 }
00101
00102 buf[i] = '\0';
00103
00104 return buf;
00105 }
00106
00107 char *strcasestr(const char *haystack, const char *needle)
00108 {
00109 char *u1, *u2;
00110 char *offset;
00111 int u1len = strlen(haystack) + 1, u2len = strlen(needle) + 1;
00112
00113 if (u2len > u1len) {
00114
00115 return NULL;
00116 }
00117 u1 = ast_alloca(u1len);
00118 u2 = ast_alloca(u2len);
00119 offset = strstr(upper(haystack, u1, u1len), upper(needle, u2, u2len));
00120 if (offset) {
00121
00122 return ((char *)((unsigned long)haystack + (unsigned long)(offset - u1)));
00123 } else {
00124 return NULL;
00125 }
00126 }
00127 #endif
00128
00129 #ifndef HAVE_STRNLEN
00130 size_t strnlen(const char *s, size_t n)
00131 {
00132 size_t len;
00133
00134 for (len = 0; len < n; len++)
00135 if (s[len] == '\0')
00136 break;
00137
00138 return len;
00139 }
00140 #endif
00141
00142 #if !defined(HAVE_STRNDUP) && !defined(__AST_DEBUG_MALLOC)
00143 char *strndup(const char *s, size_t n)
00144 {
00145 size_t len = strnlen(s, n);
00146 char *new = malloc(len + 1);
00147
00148 if (!new)
00149 return NULL;
00150
00151 new[len] = '\0';
00152 return memcpy(new, s, len);
00153 }
00154 #endif
00155
00156 #if !defined(HAVE_VASPRINTF) && !defined(__AST_DEBUG_MALLOC)
00157 int vasprintf(char **strp, const char *fmt, va_list ap)
00158 {
00159 int size;
00160 va_list ap2;
00161 char s;
00162
00163 *strp = NULL;
00164 va_copy(ap2, ap);
00165 size = vsnprintf(&s, 1, fmt, ap2);
00166 va_end(ap2);
00167 *strp = malloc(size + 1);
00168 if (!*strp)
00169 return -1;
00170 vsnprintf(*strp, size + 1, fmt, ap);
00171
00172 return size;
00173 }
00174 #endif
00175
00176 #ifndef HAVE_TIMERSUB
00177 void timersub(struct timeval *tvend, struct timeval *tvstart, struct timeval *tvdiff)
00178 {
00179 tvdiff->tv_sec = tvend->tv_sec - tvstart->tv_sec;
00180 tvdiff->tv_usec = tvend->tv_usec - tvstart->tv_usec;
00181 if (tvdiff->tv_usec < 0) {
00182 tvdiff->tv_sec --;
00183 tvdiff->tv_usec += 1000000;
00184 }
00185
00186 }
00187 #endif
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208 #if !defined(HAVE_ASPRINTF) && !defined(__AST_DEBUG_MALLOC)
00209 int asprintf(char **str, const char *fmt, ...)
00210 {
00211 va_list ap;
00212 int ret;
00213
00214 *str = NULL;
00215 va_start(ap, fmt);
00216 ret = vasprintf(str, fmt, ap);
00217 va_end(ap);
00218
00219 return ret;
00220 }
00221 #endif
00222
00223 #ifndef HAVE_STRTOQ
00224 #ifndef LONG_MIN
00225 #define LONG_MIN (-9223372036854775807L-1L)
00226
00227 #endif
00228 #ifndef LONG_MAX
00229 #define LONG_MAX 9223372036854775807L
00230
00231 #endif
00232
00233
00234
00235
00236
00237
00238
00239 uint64_t strtoq(const char *nptr, char **endptr, int base)
00240 {
00241 const char *s;
00242 uint64_t acc;
00243 unsigned char c;
00244 uint64_t qbase, cutoff;
00245 int neg, any, cutlim;
00246
00247
00248
00249
00250
00251
00252 s = nptr;
00253 do {
00254 c = *s++;
00255 } while (isspace(c));
00256 if (c == '-') {
00257 neg = 1;
00258 c = *s++;
00259 } else {
00260 neg = 0;
00261 if (c == '+')
00262 c = *s++;
00263 }
00264 if ((base == 0 || base == 16) &&
00265 c == '\0' && (*s == 'x' || *s == 'X')) {
00266 c = s[1];
00267 s += 2;
00268 base = 16;
00269 }
00270 if (base == 0)
00271 base = c == '\0' ? 8 : 10;
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291 qbase = (unsigned)base;
00292 cutoff = neg ? (uint64_t)-(LONG_MIN + LONG_MAX) + LONG_MAX : LONG_MAX;
00293 cutlim = cutoff % qbase;
00294 cutoff /= qbase;
00295 for (acc = 0, any = 0;; c = *s++) {
00296 if (!isascii(c))
00297 break;
00298 if (isdigit(c))
00299 c -= '\0';
00300 else if (isalpha(c))
00301 c -= isupper(c) ? 'A' - 10 : 'a' - 10;
00302 else
00303 break;
00304 if (c >= base)
00305 break;
00306 if (any < 0 || acc > cutoff || (acc == cutoff && c > cutlim))
00307 any = -1;
00308 else {
00309 any = 1;
00310 acc *= qbase;
00311 acc += c;
00312 }
00313 }
00314 if (any < 0) {
00315 acc = neg ? LONG_MIN : LONG_MAX;
00316 } else if (neg)
00317 acc = -acc;
00318 if (endptr != 0)
00319 *((const char **)endptr) = any ? s - 1 : nptr;
00320 return acc;
00321 }
00322 #endif
00323
00324 #ifndef HAVE_GETLOADAVG
00325 #ifdef linux
00326
00327 int getloadavg(double *list, int nelem)
00328 {
00329 FILE *LOADAVG;
00330 double avg[3] = { 0.0, 0.0, 0.0 };
00331 int i, res = -1;
00332
00333 if ((LOADAVG = fopen("/proc/loadavg", "r"))) {
00334 fscanf(LOADAVG, "%lf %lf %lf", &avg[0], &avg[1], &avg[2]);
00335 res = 0;
00336 fclose(LOADAVG);
00337 }
00338
00339 for (i = 0; (i < nelem) && (i < 3); i++) {
00340 list[i] = avg[i];
00341 }
00342
00343 return res;
00344 }
00345 #else
00346
00347
00348 int getloadavg(double *list, int nelem)
00349 {
00350 int i;
00351
00352 for (i = 0; i < nelem; i++) {
00353 list[i] = 0.1;
00354 }
00355 return -1;
00356 }
00357 #endif
00358 #endif
00359
00360 #ifndef HAVE_NTOHLL
00361 uint64_t ntohll(uint64_t net64)
00362 {
00363 #if BYTE_ORDER == BIG_ENDIAN
00364 return net64;
00365 #elif BYTE_ORDER == LITTLE_ENDIAN
00366 union {
00367 unsigned char c[8];
00368 uint64_t u;
00369 } number;
00370 number.u = net64;
00371 return
00372 (((uint64_t) number.c[0]) << 56) |
00373 (((uint64_t) number.c[1]) << 48) |
00374 (((uint64_t) number.c[2]) << 40) |
00375 (((uint64_t) number.c[3]) << 32) |
00376 (((uint64_t) number.c[4]) << 24) |
00377 (((uint64_t) number.c[5]) << 16) |
00378 (((uint64_t) number.c[6]) << 8) |
00379 (((uint64_t) number.c[7]) << 0);
00380 #else
00381 #error "Unknown byte order"
00382 #endif
00383 }
00384 #endif
00385
00386 #ifndef HAVE_HTONLL
00387 uint64_t htonll(uint64_t host64)
00388 {
00389 #if BYTE_ORDER == BIG_ENDIAN
00390 return host64;
00391 #elif BYTE_ORDER == LITTLE_ENDIAN
00392 union {
00393 unsigned char c[8];
00394 uint64_t u;
00395 } number;
00396 number.u = host64;
00397 return
00398 (((uint64_t) number.c[0]) << 56) |
00399 (((uint64_t) number.c[1]) << 48) |
00400 (((uint64_t) number.c[2]) << 40) |
00401 (((uint64_t) number.c[3]) << 32) |
00402 (((uint64_t) number.c[4]) << 24) |
00403 (((uint64_t) number.c[5]) << 16) |
00404 (((uint64_t) number.c[6]) << 8) |
00405 (((uint64_t) number.c[7]) << 0);
00406 #else
00407 #error "Unknown byte order"
00408 #endif
00409 }
00410 #endif
00411
00412 #ifndef HAVE_FFSLL
00413 int ffsll(long long n)
00414 {
00415 int i;
00416 for (i = 0; i < 64; i++) {
00417 if ((1LL << i) & n) {
00418 return i + 1;
00419 }
00420 }
00421 return 0;
00422 }
00423 #endif
00424
00425 #ifndef HAVE_CLOSEFROM
00426 void closefrom(int n)
00427 {
00428 long x;
00429 struct rlimit rl;
00430 DIR *dir;
00431 char path[16], *result;
00432 struct dirent *entry;
00433
00434 snprintf(path, sizeof(path), "/proc/%d/fd", (int) getpid());
00435 if ((dir = opendir(path))) {
00436 while ((entry = readdir(dir))) {
00437
00438 if (entry->d_name[0] == '.') {
00439 continue;
00440 }
00441 if ((x = strtol(entry->d_name, &result, 10)) && x >= n) {
00442 #ifdef STRICT_COMPAT
00443 close(x);
00444 #else
00445
00446
00447
00448
00449 long flags = fcntl(x, F_GETFD);
00450 if (flags == -1 && errno == EBADF) {
00451 continue;
00452 }
00453 fcntl(x, F_SETFD, flags | FD_CLOEXEC);
00454 #endif
00455 }
00456 }
00457 closedir(dir);
00458 } else {
00459 getrlimit(RLIMIT_NOFILE, &rl);
00460 if (rl.rlim_cur > 65535) {
00461
00462
00463
00464
00465
00466
00467
00468 rl.rlim_cur = 65535;
00469 }
00470 for (x = n; x < rl.rlim_cur; x++) {
00471 #ifdef STRICT_COMPAT
00472 close(x);
00473 #else
00474 long flags = fcntl(x, F_GETFD);
00475 if (flags == -1 && errno == EBADF) {
00476 continue;
00477 }
00478 fcntl(x, F_SETFD, flags | FD_CLOEXEC);
00479 #endif
00480 }
00481 }
00482 }
00483 #endif
00484
00485 #ifndef HAVE_MKDTEMP
00486
00487
00488
00489
00490
00491
00492
00493
00494
00495
00496
00497
00498
00499
00500
00501
00502
00503
00504 #define MKTEMP_NAME 0
00505 #define MKTEMP_FILE 1
00506 #define MKTEMP_DIR 2
00507
00508 #define TEMPCHARS "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_."
00509 #define NUM_CHARS (sizeof(TEMPCHARS) - 1)
00510
00511 static int mktemp_internal(char *path, int slen, int mode)
00512 {
00513 char *start, *cp, *ep;
00514 const char *tempchars = TEMPCHARS;
00515 unsigned int r, tries;
00516 struct stat sb;
00517 size_t len;
00518 int fd;
00519
00520 len = strlen(path);
00521 if (len == 0 || slen >= len) {
00522 errno = EINVAL;
00523 return(-1);
00524 }
00525 ep = path + len - slen;
00526
00527 tries = 1;
00528 for (start = ep; start > path && start[-1] == 'X'; start--) {
00529 if (tries < INT_MAX / NUM_CHARS) {
00530 tries *= NUM_CHARS;
00531 }
00532 }
00533 tries *= 2;
00534
00535 do {
00536 for (cp = start; cp != ep; cp++) {
00537 r = ast_random() % NUM_CHARS;
00538 *cp = tempchars[r];
00539 }
00540
00541 switch (mode) {
00542 case MKTEMP_NAME:
00543 if (lstat(path, &sb) != 0) {
00544 return (errno == ENOENT ? 0 : -1);
00545 }
00546 break;
00547 case MKTEMP_FILE:
00548 fd = open(path, O_CREAT | O_EXCL | O_RDWR, S_IRUSR | S_IWUSR);
00549 if (fd != -1 || errno != EEXIST) {
00550 return (fd);
00551 }
00552 break;
00553 case MKTEMP_DIR:
00554 if (mkdir(path, S_IRUSR | S_IWUSR | S_IXUSR) == 0) {
00555 return (0);
00556 }
00557 if (errno != EEXIST) {
00558 return (-1);
00559 }
00560 break;
00561 }
00562 } while (--tries);
00563
00564 errno = EEXIST;
00565 return(-1);
00566 }
00567
00568 char *mkdtemp(char *path)
00569 {
00570 return mktemp_internal(path, 0, MKTEMP_DIR) ? NULL : path;
00571 }
00572 #endif
00573
00574 #ifndef HAVE_ROUNDF
00575 #ifndef HAVE_ROUND
00576 float roundf(float x) {
00577 if (x < 0.0) {
00578 return (float)(int)((x) - 0.5);
00579 } else {
00580 return (float)(int)((x) + 0.5);
00581 }
00582 }
00583 #endif
00584 #endif