Thu Apr 28 2011 17:13:35

Asterisk developer's documentation


agi/strcompat.c

Go to the documentation of this file.
00001 /*
00002  * Asterisk -- An open source telephony toolkit.
00003  *
00004  * Copyright (C) 1999 - 2006, Digium, Inc.
00005  *
00006  * See http://www.asterisk.org for more information about
00007  * the Asterisk project. Please do not directly contact
00008  * any of the maintainers of this project for assistance;
00009  * the project provides a web site, mailing lists and IRC
00010  * channels for your use.
00011  *
00012  * This program is free software, distributed under the terms of
00013  * the GNU General Public License Version 2. See the LICENSE file
00014  * at the top of the source tree.
00015  */
00016 
00017 /*! \file
00018  *
00019  * \brief Compatibility functions for strsep and strtoq missing on Solaris 
00020  */
00021 
00022 #include "asterisk.h"
00023 
00024 #include <ctype.h>
00025 
00026 #ifndef HAVE_STRSEP
00027 char *strsep(char **str, const char *delims)
00028 {
00029    char *token;
00030 
00031    if (!*str) {
00032       /* No more tokens */
00033       return NULL;
00034    }
00035 
00036    token = *str;
00037    while (**str != '\0') {
00038       if (strchr(delims, **str)) {
00039          **str = '\0';
00040          (*str)++;
00041          return token;
00042       }
00043       (*str)++;
00044    }
00045 
00046    /* There is no other token */
00047    *str = NULL;
00048 
00049    return token;
00050 }
00051 #endif
00052 
00053 #ifndef HAVE_SETENV
00054 int setenv(const char *name, const char *value, int overwrite)
00055 {
00056    unsigned char *buf;
00057    int buflen;
00058 
00059    buflen = strlen(name) + strlen(value) + 2;
00060    buf = alloca(buflen);
00061 
00062    if (!overwrite && getenv(name))
00063       return 0;
00064 
00065    snprintf(buf, buflen, "%s=%s", name, value);
00066 
00067    return putenv(buf);
00068 }
00069 #endif
00070 
00071 #ifndef HAVE_UNSETENV
00072 int unsetenv(const char *name)
00073 {
00074    return setenv(name, "", 0);
00075 }
00076 #endif
00077 
00078 #ifndef HAVE_STRCASESTR
00079 static char *upper(const char *orig, char *buf, int bufsize)
00080 {
00081    int i = 0;
00082 
00083    while (i < (bufsize - 1) && orig[i]) {
00084       buf[i] = toupper(orig[i]);
00085       i++;
00086    }
00087 
00088    buf[i] = '\0';
00089 
00090    return buf;
00091 }
00092 
00093 char *strcasestr(const char *haystack, const char *needle)
00094 {
00095    char *u1, *u2;
00096    int u1len = strlen(haystack) + 1, u2len = strlen(needle) + 1;
00097 
00098    u1 = alloca(u1len);
00099    u2 = alloca(u2len);
00100    if (u1 && u2) {
00101       char *offset;
00102       if (u2len > u1len) {
00103          /* Needle bigger than haystack */
00104          return NULL;
00105       }
00106       offset = strstr(upper(haystack, u1, u1len), upper(needle, u2, u2len));
00107       if (offset) {
00108          /* Return the offset into the original string */
00109          return ((char *)((unsigned long)haystack + (unsigned long)(offset - u1)));
00110       } else {
00111          return NULL;
00112       }
00113    } else {
00114       return NULL;
00115    }
00116 }
00117 #endif /* !HAVE_STRCASESTR */
00118 
00119 #ifndef HAVE_STRNLEN
00120 size_t strnlen(const char *s, size_t n)
00121 {
00122    size_t len;
00123 
00124    for (len = 0; len < n; len++)
00125       if (s[len] == '\0')
00126          break;
00127 
00128    return len;
00129 }
00130 #endif /* !HAVE_STRNLEN */
00131 
00132 #if !defined(HAVE_STRNDUP) && !defined(__AST_DEBUG_MALLOC)
00133 char *strndup(const char *s, size_t n)
00134 {
00135    size_t len = strnlen(s, n);
00136    char *new = malloc(len + 1);
00137 
00138    if (!new)
00139       return NULL;
00140 
00141    new[len] = '\0';
00142    return memcpy(new, s, len);
00143 }
00144 #endif /* !defined(HAVE_STRNDUP) && !defined(__AST_DEBUG_MALLOC) */
00145 
00146 #if !defined(HAVE_VASPRINTF) && !defined(__AST_DEBUG_MALLOC)
00147 int vasprintf(char **strp, const char *fmt, va_list ap)
00148 {
00149    int size;
00150    va_list ap2;
00151    char s;
00152 
00153    *strp = NULL;
00154    va_copy(ap2, ap);
00155    size = vsnprintf(&s, 1, fmt, ap2);
00156    va_end(ap2);
00157    *strp = malloc(size + 1);
00158    if (!*strp)
00159       return -1;
00160    vsnprintf(*strp, size + 1, fmt, ap);
00161 
00162    return size;
00163 }
00164 #endif /* !defined(HAVE_VASPRINTF) && !defined(__AST_DEBUG_MALLOC) */
00165 
00166 #ifndef HAVE_TIMERSUB
00167 void timersub(struct timeval *tvend, struct timeval *tvstart, struct timeval *tvdiff)
00168 {
00169    tvdiff->tv_sec = tvend->tv_sec - tvstart->tv_sec;
00170    tvdiff->tv_usec = tvend->tv_usec - tvstart->tv_usec;
00171    if (tvdiff->tv_usec < 0) {
00172       tvdiff->tv_sec --;
00173       tvdiff->tv_usec += 1000000;
00174    }
00175 
00176 }
00177 #endif
00178 
00179 /*
00180  * Based on Code from bsd-asprintf from OpenSSH
00181  * Copyright (c) 2004 Darren Tucker.
00182  *
00183  * Based originally on asprintf.c from OpenBSD:
00184  * Copyright (c) 1997 Todd C. Miller <Todd.Miller@courtesan.com>
00185  *
00186  * Permission to use, copy, modify, and distribute this software for any
00187  * purpose with or without fee is hereby granted, provided that the above
00188  * copyright notice and this permission notice appear in all copies.
00189  *
00190  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
00191  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
00192  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
00193  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
00194  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
00195  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
00196  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
00197  */
00198 #if !defined(HAVE_ASPRINTF) && !defined(__AST_DEBUG_MALLOC) 
00199 int asprintf(char **str, const char *fmt, ...)
00200 {
00201    va_list ap;
00202    int ret;
00203 
00204    *str = NULL;
00205    va_start(ap, fmt);
00206    ret = vasprintf(str, fmt, ap);
00207    va_end(ap);
00208 
00209    return ret;
00210 }
00211 #endif /* !defined(HAVE_ASPRINTF) && !defined(__AST_DEBUG_MALLOC) */
00212 
00213 #ifndef HAVE_STRTOQ
00214 #ifndef LONG_MIN
00215 #define LONG_MIN        (-9223372036854775807L-1L)
00216                                     /* min value of a "long int" */
00217 #endif
00218 #ifndef LONG_MAX
00219 #define LONG_MAX        9223372036854775807L
00220                                     /* max value of a "long int" */
00221 #endif
00222 
00223 /*! \brief
00224  * Convert a string to a quad integer.
00225  *
00226  * \note Ignores `locale' stuff.  Assumes that the upper and lower case
00227  * alphabets and digits are each contiguous.
00228  */
00229 uint64_t strtoq(const char *nptr, char **endptr, int base)
00230 {
00231     const char *s;
00232     uint64_t acc;
00233     unsigned char c;
00234     uint64_t qbase, cutoff;
00235     int neg, any, cutlim;
00236 
00237     /*
00238      * Skip white space and pick up leading +/- sign if any.
00239      * If base is 0, allow 0x for hex and 0 for octal, else
00240      * assume decimal; if base is already 16, allow 0x.
00241      */
00242     s = nptr;
00243     do {
00244             c = *s++;
00245     } while (isspace(c));
00246     if (c == '-') {
00247             neg = 1;
00248             c = *s++;
00249     } else {
00250             neg = 0;
00251             if (c == '+')
00252                     c = *s++;
00253     }
00254     if ((base == 0 || base == 16) &&
00255         c == '\0' && (*s == 'x' || *s == 'X')) {
00256             c = s[1];
00257             s += 2;
00258             base = 16;
00259     }
00260     if (base == 0)
00261             base = c == '\0' ? 8 : 10;
00262 
00263     /*
00264      * Compute the cutoff value between legal numbers and illegal
00265      * numbers.  That is the largest legal value, divided by the
00266      * base.  An input number that is greater than this value, if
00267      * followed by a legal input character, is too big.  One that
00268      * is equal to this value may be valid or not; the limit
00269      * between valid and invalid numbers is then based on the last
00270      * digit.  For instance, if the range for quads is
00271      * [-9223372036854775808..9223372036854775807] and the input base
00272      * is 10, cutoff will be set to 922337203685477580 and cutlim to
00273      * either 7 (neg==0) or 8 (neg==1), meaning that if we have
00274      * accumulated a value > 922337203685477580, or equal but the
00275      * next digit is > 7 (or 8), the number is too big, and we will
00276      * return a range error.
00277      *
00278      * Set any if any `digits' consumed; make it negative to indicate
00279      * overflow.
00280      */
00281     qbase = (unsigned)base;
00282     cutoff = neg ? (uint64_t)-(LONG_MIN + LONG_MAX) + LONG_MAX : LONG_MAX;
00283     cutlim = cutoff % qbase;
00284     cutoff /= qbase;
00285     for (acc = 0, any = 0;; c = *s++) {
00286             if (!isascii(c))
00287                     break;
00288             if (isdigit(c))
00289                     c -= '\0';
00290             else if (isalpha(c))
00291                     c -= isupper(c) ? 'A' - 10 : 'a' - 10;
00292             else
00293                     break;
00294             if (c >= base)
00295                     break;
00296             if (any < 0 || acc > cutoff || (acc == cutoff && c > cutlim))
00297                     any = -1;
00298             else {
00299                     any = 1;
00300                     acc *= qbase;
00301                     acc += c;
00302             }
00303     }
00304     if (any < 0) {
00305             acc = neg ? LONG_MIN : LONG_MAX;
00306     } else if (neg)
00307             acc = -acc;
00308     if (endptr != 0)
00309             *((const char **)endptr) = any ? s - 1 : nptr;
00310     return acc;
00311 }
00312 #endif /* !HAVE_STRTOQ */
00313 
00314 #ifndef HAVE_GETLOADAVG
00315 #ifdef linux
00316 /*! \brief Alternative method of getting load avg on Linux only */
00317 int getloadavg(double *list, int nelem)
00318 {
00319    FILE *LOADAVG;
00320    double avg[3] = { 0.0, 0.0, 0.0 };
00321    int i, res = -1;
00322 
00323    if ((LOADAVG = fopen("/proc/loadavg", "r"))) {
00324       fscanf(LOADAVG, "%lf %lf %lf", &avg[0], &avg[1], &avg[2]);
00325       res = 0;
00326       fclose(LOADAVG);
00327    }
00328 
00329    for (i = 0; (i < nelem) && (i < 3); i++) {
00330       list[i] = avg[i];
00331    }
00332 
00333    return res;
00334 }
00335 #else /* !linux */
00336 /*! \brief Return something that won't cancel the call, but still return -1, in case
00337  * we correct the implementation to check return value */
00338 int getloadavg(double *list, int nelem)
00339 {
00340    int i;
00341 
00342    for (i = 0; i < nelem; i++) {
00343       list[i] = 0.1;
00344    }
00345    return -1;
00346 }
00347 #endif /* linux */
00348 #endif /* !HAVE_GETLOADAVG */
00349 
00350 
00351 /*
00352  * For strlcat()
00353  *
00354  * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
00355  * All rights reserved.
00356  *
00357  * Redistribution and use in source and binary forms, with or without
00358  * modification, are permitted provided that the following conditions
00359  * are met:
00360  * 1. Redistributions of source code must retain the above copyright
00361  *    notice, this list of conditions and the following disclaimer.
00362  * 2. Redistributions in binary form must reproduce the above copyright
00363  *    notice, this list of conditions and the following disclaimer in the
00364  *    documentation and/or other materials provided with the distribution.
00365  * 3. The name of the author may not be used to endorse or promote products
00366  *    derived from this software without specific prior written permission.
00367  *
00368  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
00369  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
00370  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
00371  * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00372  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00373  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
00374  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
00375  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
00376  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
00377  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00378  */
00379 
00380 /*
00381  * Appends src to string dst of size siz (unlike strncat, siz is the
00382  * full size of dst, not space left).  At most siz-1 characters
00383  * will be copied.  Always NUL terminates (unless siz <= strlen(dst)).
00384  * Returns strlen(src) + MIN(siz, strlen(initial dst)).
00385  * If retval >= siz, truncation occurred.
00386  */
00387 #ifndef HAVE_STRLCAT
00388 size_t strlcat(char *dst, const char *src, size_t siz)
00389 {
00390    register char *d = dst;
00391    register const char *s = src;
00392    register size_t n = siz;
00393    size_t dlen;
00394 
00395    /* Find the end of dst and adjust bytes left but don't go past end */
00396    while (n-- != 0 && *d != '\0')
00397       d++;
00398    dlen = d - dst;
00399    n = siz - dlen;
00400 
00401    if (n == 0)
00402       return dlen + strlen(s);
00403 
00404    while (*s != '\0') {
00405       if (n != 1) {
00406          *d++ = *s;
00407          n--;
00408       }
00409       s++;
00410    }
00411    *d = '\0';
00412 
00413    return dlen + (s - src);   /* count does not include NUL */
00414 }
00415 #endif /* HAVE_STRLCAT */
00416 
00417 /*
00418  * For strlcpy()
00419  *
00420  * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
00421  * All rights reserved.
00422  *
00423  * Redistribution and use in source and binary forms, with or without
00424  * modification, are permitted provided that the following conditions
00425  * are met:
00426  * 1. Redistributions of source code must retain the above copyright
00427  *    notice, this list of conditions and the following disclaimer.
00428  * 2. Redistributions in binary form must reproduce the above copyright
00429  *    notice, this list of conditions and the following disclaimer in the
00430  *    documentation and/or other materials provided with the distribution.
00431  * 3. The name of the author may not be used to endorse or promote products
00432  *    derived from this software without specific prior written permission.
00433  *
00434  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
00435  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
00436  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
00437  * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00438  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00439  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
00440  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
00441  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
00442  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
00443  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00444  */
00445 
00446 /*
00447  * Copy src to string dst of size siz.  At most siz-1 characters
00448  * will be copied.  Always NUL terminates (unless siz == 0).
00449  * Returns strlen(src); if retval >= siz, truncation occurred.
00450  */
00451 #ifndef HAVE_STRLCPY
00452 size_t strlcpy(char *dst, const char *src, size_t siz)
00453 {
00454    register char *d = dst;
00455    register const char *s = src;
00456    register size_t n = siz;
00457 
00458    /* Copy as many bytes as will fit */
00459    if (n != 0 && --n != 0) {
00460       do {
00461          if ((*d++ = *s++) == 0)
00462             break;
00463       } while (--n != 0);
00464    }
00465 
00466    /* Not enough room in dst, add NUL and traverse rest of src */
00467    if (n == 0) {
00468       if (siz != 0)
00469          *d = '\0';     /* NUL-terminate dst */
00470       while (*s++)
00471          ;
00472    }
00473 
00474    return s - src - 1;  /* count does not include NUL */
00475 }
00476 #endif /* HAVE_STRLCPY */