Fri Jul 24 00:41:05 2009

Asterisk developer's documentation


main/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 /*
00167  * Based on Code from bsd-asprintf from OpenSSH
00168  * Copyright (c) 2004 Darren Tucker.
00169  *
00170  * Based originally on asprintf.c from OpenBSD:
00171  * Copyright (c) 1997 Todd C. Miller <Todd.Miller@courtesan.com>
00172  *
00173  * Permission to use, copy, modify, and distribute this software for any
00174  * purpose with or without fee is hereby granted, provided that the above
00175  * copyright notice and this permission notice appear in all copies.
00176  *
00177  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
00178  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
00179  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
00180  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
00181  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
00182  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
00183  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
00184  */
00185 #if !defined(HAVE_ASPRINTF) && !defined(__AST_DEBUG_MALLOC) 
00186 int asprintf(char **str, const char *fmt, ...)
00187 {
00188    va_list ap;
00189    int ret;
00190 
00191    *str = NULL;
00192    va_start(ap, fmt);
00193    ret = vasprintf(str, fmt, ap);
00194    va_end(ap);
00195 
00196    return ret;
00197 }
00198 #endif /* !defined(HAVE_ASPRINTF) && !defined(__AST_DEBUG_MALLOC) */
00199 
00200 #ifndef HAVE_STRTOQ
00201 #ifndef LONG_MIN
00202 #define LONG_MIN        (-9223372036854775807L-1L)
00203                                     /* min value of a "long int" */
00204 #endif
00205 #ifndef LONG_MAX
00206 #define LONG_MAX        9223372036854775807L
00207                                     /* max value of a "long int" */
00208 #endif
00209 
00210 /*! \brief
00211  * Convert a string to a quad integer.
00212  *
00213  * \note Ignores `locale' stuff.  Assumes that the upper and lower case
00214  * alphabets and digits are each contiguous.
00215  */
00216 uint64_t strtoq(const char *nptr, char **endptr, int base)
00217 {
00218     const char *s;
00219     uint64_t acc;
00220     unsigned char c;
00221     uint64_t qbase, cutoff;
00222     int neg, any, cutlim;
00223 
00224     /*
00225      * Skip white space and pick up leading +/- sign if any.
00226      * If base is 0, allow 0x for hex and 0 for octal, else
00227      * assume decimal; if base is already 16, allow 0x.
00228      */
00229     s = nptr;
00230     do {
00231             c = *s++;
00232     } while (isspace(c));
00233     if (c == '-') {
00234             neg = 1;
00235             c = *s++;
00236     } else {
00237             neg = 0;
00238             if (c == '+')
00239                     c = *s++;
00240     }
00241     if ((base == 0 || base == 16) &&
00242         c == '\0' && (*s == 'x' || *s == 'X')) {
00243             c = s[1];
00244             s += 2;
00245             base = 16;
00246     }
00247     if (base == 0)
00248             base = c == '\0' ? 8 : 10;
00249 
00250     /*
00251      * Compute the cutoff value between legal numbers and illegal
00252      * numbers.  That is the largest legal value, divided by the
00253      * base.  An input number that is greater than this value, if
00254      * followed by a legal input character, is too big.  One that
00255      * is equal to this value may be valid or not; the limit
00256      * between valid and invalid numbers is then based on the last
00257      * digit.  For instance, if the range for quads is
00258      * [-9223372036854775808..9223372036854775807] and the input base
00259      * is 10, cutoff will be set to 922337203685477580 and cutlim to
00260      * either 7 (neg==0) or 8 (neg==1), meaning that if we have
00261      * accumulated a value > 922337203685477580, or equal but the
00262      * next digit is > 7 (or 8), the number is too big, and we will
00263      * return a range error.
00264      *
00265      * Set any if any `digits' consumed; make it negative to indicate
00266      * overflow.
00267      */
00268     qbase = (unsigned)base;
00269     cutoff = neg ? (uint64_t)-(LONG_MIN + LONG_MAX) + LONG_MAX : LONG_MAX;
00270     cutlim = cutoff % qbase;
00271     cutoff /= qbase;
00272     for (acc = 0, any = 0;; c = *s++) {
00273             if (!isascii(c))
00274                     break;
00275             if (isdigit(c))
00276                     c -= '\0';
00277             else if (isalpha(c))
00278                     c -= isupper(c) ? 'A' - 10 : 'a' - 10;
00279             else
00280                     break;
00281             if (c >= base)
00282                     break;
00283             if (any < 0 || acc > cutoff || (acc == cutoff && c > cutlim))
00284                     any = -1;
00285             else {
00286                     any = 1;
00287                     acc *= qbase;
00288                     acc += c;
00289             }
00290     }
00291     if (any < 0) {
00292             acc = neg ? LONG_MIN : LONG_MAX;
00293     } else if (neg)
00294             acc = -acc;
00295     if (endptr != 0)
00296             *((const char **)endptr) = any ? s - 1 : nptr;
00297     return acc;
00298 }
00299 #endif /* !HAVE_STRTOQ */
00300 
00301 #ifndef HAVE_GETLOADAVG
00302 #ifdef linux
00303 /*! \brief Alternative method of getting load avg on Linux only */
00304 int getloadavg(double *list, int nelem)
00305 {
00306    FILE *LOADAVG;
00307    double avg[3] = { 0.0, 0.0, 0.0 };
00308    int i, res = -1;
00309 
00310    if ((LOADAVG = fopen("/proc/loadavg", "r"))) {
00311       fscanf(LOADAVG, "%lf %lf %lf", &avg[0], &avg[1], &avg[2]);
00312       res = 0;
00313       fclose(LOADAVG);
00314    }
00315 
00316    for (i = 0; (i < nelem) && (i < 3); i++) {
00317       list[i] = avg[i];
00318    }
00319 
00320    return res;
00321 }
00322 #else /* !linux */
00323 /*! \brief Return something that won't cancel the call, but still return -1, in case
00324  * we correct the implementation to check return value */
00325 int getloadavg(double *list, int nelem)
00326 {
00327    int i;
00328 
00329    for (i = 0; i < nelem; i++) {
00330       list[i] = 0.1;
00331    }
00332    return -1;
00333 }
00334 #endif /* linux */
00335 #endif /* !HAVE_GETLOADAVG */
00336 
00337 
00338 /*
00339  * For strlcat()
00340  *
00341  * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
00342  * All rights reserved.
00343  *
00344  * Redistribution and use in source and binary forms, with or without
00345  * modification, are permitted provided that the following conditions
00346  * are met:
00347  * 1. Redistributions of source code must retain the above copyright
00348  *    notice, this list of conditions and the following disclaimer.
00349  * 2. Redistributions in binary form must reproduce the above copyright
00350  *    notice, this list of conditions and the following disclaimer in the
00351  *    documentation and/or other materials provided with the distribution.
00352  * 3. The name of the author may not be used to endorse or promote products
00353  *    derived from this software without specific prior written permission.
00354  *
00355  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
00356  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
00357  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
00358  * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00359  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00360  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
00361  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
00362  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
00363  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
00364  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00365  */
00366 
00367 /*
00368  * Appends src to string dst of size siz (unlike strncat, siz is the
00369  * full size of dst, not space left).  At most siz-1 characters
00370  * will be copied.  Always NUL terminates (unless siz <= strlen(dst)).
00371  * Returns strlen(src) + MIN(siz, strlen(initial dst)).
00372  * If retval >= siz, truncation occurred.
00373  */
00374 #ifndef HAVE_STRLCAT
00375 size_t strlcat(char *dst, const char *src, size_t siz)
00376 {
00377    register char *d = dst;
00378    register const char *s = src;
00379    register size_t n = siz;
00380    size_t dlen;
00381 
00382    /* Find the end of dst and adjust bytes left but don't go past end */
00383    while (n-- != 0 && *d != '\0')
00384       d++;
00385    dlen = d - dst;
00386    n = siz - dlen;
00387 
00388    if (n == 0)
00389       return dlen + strlen(s);
00390 
00391    while (*s != '\0') {
00392       if (n != 1) {
00393          *d++ = *s;
00394          n--;
00395       }
00396       s++;
00397    }
00398    *d = '\0';
00399 
00400    return dlen + (s - src);   /* count does not include NUL */
00401 }
00402 #endif /* HAVE_STRLCAT */
00403 
00404 /*
00405  * For strlcpy()
00406  *
00407  * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
00408  * All rights reserved.
00409  *
00410  * Redistribution and use in source and binary forms, with or without
00411  * modification, are permitted provided that the following conditions
00412  * are met:
00413  * 1. Redistributions of source code must retain the above copyright
00414  *    notice, this list of conditions and the following disclaimer.
00415  * 2. Redistributions in binary form must reproduce the above copyright
00416  *    notice, this list of conditions and the following disclaimer in the
00417  *    documentation and/or other materials provided with the distribution.
00418  * 3. The name of the author may not be used to endorse or promote products
00419  *    derived from this software without specific prior written permission.
00420  *
00421  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
00422  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
00423  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
00424  * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00425  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00426  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
00427  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
00428  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
00429  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
00430  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00431  */
00432 
00433 /*
00434  * Copy src to string dst of size siz.  At most siz-1 characters
00435  * will be copied.  Always NUL terminates (unless siz == 0).
00436  * Returns strlen(src); if retval >= siz, truncation occurred.
00437  */
00438 #ifndef HAVE_STRLCPY
00439 size_t strlcpy(char *dst, const char *src, size_t siz)
00440 {
00441    register char *d = dst;
00442    register const char *s = src;
00443    register size_t n = siz;
00444 
00445    /* Copy as many bytes as will fit */
00446    if (n != 0 && --n != 0) {
00447       do {
00448          if ((*d++ = *s++) == 0)
00449             break;
00450       } while (--n != 0);
00451    }
00452 
00453    /* Not enough room in dst, add NUL and traverse rest of src */
00454    if (n == 0) {
00455       if (siz != 0)
00456          *d = '\0';     /* NUL-terminate dst */
00457       while (*s++)
00458          ;
00459    }
00460 
00461    return s - src - 1;  /* count does not include NUL */
00462 }
00463 #endif /* HAVE_STRLCPY */

Generated on Fri Jul 24 00:41:05 2009 for Asterisk - the Open Source PBX by  doxygen 1.4.7