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