Sat Aug 6 00:39:32 2011

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

Generated on Sat Aug 6 00:39:32 2011 for Asterisk - the Open Source PBX by  doxygen 1.4.7