Wed Aug 15 01:25:03 2007

Asterisk developer's documentation


compat.h File Reference

General Definitions for Asterisk top level program. More...

#include "asterisk/autoconfig.h"
#include <inttypes.h>
#include <sys/types.h>
#include <stdarg.h>

Include dependency graph for compat.h:

This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

int asprintf (char **str, const char *fmt,...)
int getloadavg (double *list, int nelem)
 Return something that won't cancel the call, but still return -1, in case we correct the implementation to check return value.
int setenv (const char *name, const char *value, int overwrite)
char * strcasestr (const char *, const char *)
size_t strlcat (char *dst, const char *src, size_t siz)
size_t strlcpy (char *dst, const char *src, size_t siz)
char * strndup (const char *, size_t)
size_t strnlen (const char *, size_t)
char * strsep (char **str, const char *delims)
uint64_t strtoq (const char *nptr, char **endptr, int base)
 Convert a string to a quad integer.
int unsetenv (const char *name)
int vasprintf (char **strp, const char *fmt, va_list ap)


Detailed Description

General Definitions for Asterisk top level program.

Definition in file compat.h.


Function Documentation

int asprintf ( char **  str,
const char *  fmt,
  ... 
)

Definition at line 195 of file strcompat.c.

References vasprintf.

00196 {
00197         va_list ap;
00198         int ret;
00199 
00200         *str = NULL;
00201         va_start(ap, fmt);
00202         ret = vasprintf(str, fmt, ap);
00203         va_end(ap);
00204 
00205         return ret;
00206 }

int getloadavg ( double *  list,
int  nelem 
)

Return something that won't cancel the call, but still return -1, in case we correct the implementation to check return value.

Definition at line 334 of file strcompat.c.

00335 {
00336    int i;
00337 
00338    for (i = 0; i < nelem; i++) {
00339       list[i] = 0.1;
00340    }
00341    return -1;
00342 }

int setenv ( const char *  name,
const char *  value,
int  overwrite 
)

Definition at line 63 of file strcompat.c.

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 }

char* strcasestr ( const char *  ,
const char *   
)

Definition at line 102 of file strcompat.c.

References offset, and upper().

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 }

size_t strlcat ( char *  dst,
const char *  src,
size_t  siz 
)

Definition at line 384 of file strcompat.c.

References s.

00385 {
00386    register char *d = dst;
00387    register const char *s = src;
00388    register size_t n = siz;
00389    size_t dlen;
00390 
00391    /* Find the end of dst and adjust bytes left but don't go past end */
00392    while (n-- != 0 && *d != '\0')
00393       d++;
00394    dlen = d - dst;
00395    n = siz - dlen;
00396 
00397    if (n == 0)
00398       return dlen + strlen(s);
00399 
00400    while (*s != '\0') {
00401       if (n != 1) {
00402          *d++ = *s;
00403          n--;
00404       }
00405       s++;
00406    }
00407    *d = '\0';
00408 
00409    return dlen + (s - src);   /* count does not include NUL */
00410 }

size_t strlcpy ( char *  dst,
const char *  src,
size_t  siz 
)

Definition at line 448 of file strcompat.c.

References s.

00449 {
00450    register char *d = dst;
00451    register const char *s = src;
00452    register size_t n = siz;
00453 
00454    /* Copy as many bytes as will fit */
00455    if (n != 0 && --n != 0) {
00456       do {
00457          if ((*d++ = *s++) == 0)
00458             break;
00459       } while (--n != 0);
00460    }
00461 
00462    /* Not enough room in dst, add NUL and traverse rest of src */
00463    if (n == 0) {
00464       if (siz != 0)
00465          *d = '\0';     /* NUL-terminate dst */
00466       while (*s++)
00467          ;
00468    }
00469 
00470    return s - src - 1;  /* count does not include NUL */
00471 }

char* strndup ( const char *  ,
size_t   
)

Definition at line 142 of file strcompat.c.

References len, malloc, and strnlen().

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 }

size_t strnlen ( const char *  ,
size_t   
)

Definition at line 129 of file strcompat.c.

References len.

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 }

char* strsep ( char **  str,
const char *  delims 
)

Definition at line 36 of file strcompat.c.

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 }

uint64_t strtoq ( const char *  nptr,
char **  endptr,
int  base 
)

Convert a string to a quad integer.

Note:
Ignores `locale' stuff. Assumes that the upper and lower case alphabets and digits are each contiguous.

Definition at line 225 of file strcompat.c.

References LONG_MAX, LONG_MIN, and s.

00226 {
00227     const char *s;
00228     uint64_t acc;
00229     unsigned char c;
00230     uint64_t qbase, cutoff;
00231     int neg, any, cutlim;
00232 
00233     /*
00234      * Skip white space and pick up leading +/- sign if any.
00235      * If base is 0, allow 0x for hex and 0 for octal, else
00236      * assume decimal; if base is already 16, allow 0x.
00237      */
00238     s = nptr;
00239     do {
00240             c = *s++;
00241     } while (isspace(c));
00242     if (c == '-') {
00243             neg = 1;
00244             c = *s++;
00245     } else {
00246             neg = 0;
00247             if (c == '+')
00248                     c = *s++;
00249     }
00250     if ((base == 0 || base == 16) &&
00251         c == '\0' && (*s == 'x' || *s == 'X')) {
00252             c = s[1];
00253             s += 2;
00254             base = 16;
00255     }
00256     if (base == 0)
00257             base = c == '\0' ? 8 : 10;
00258 
00259     /*
00260      * Compute the cutoff value between legal numbers and illegal
00261      * numbers.  That is the largest legal value, divided by the
00262      * base.  An input number that is greater than this value, if
00263      * followed by a legal input character, is too big.  One that
00264      * is equal to this value may be valid or not; the limit
00265      * between valid and invalid numbers is then based on the last
00266      * digit.  For instance, if the range for quads is
00267      * [-9223372036854775808..9223372036854775807] and the input base
00268      * is 10, cutoff will be set to 922337203685477580 and cutlim to
00269      * either 7 (neg==0) or 8 (neg==1), meaning that if we have
00270      * accumulated a value > 922337203685477580, or equal but the
00271      * next digit is > 7 (or 8), the number is too big, and we will
00272      * return a range error.
00273      *
00274      * Set any if any `digits' consumed; make it negative to indicate
00275      * overflow.
00276      */
00277     qbase = (unsigned)base;
00278     cutoff = neg ? (uint64_t)-(LONG_MIN + LONG_MAX) + LONG_MAX : LONG_MAX;
00279     cutlim = cutoff % qbase;
00280     cutoff /= qbase;
00281     for (acc = 0, any = 0;; c = *s++) {
00282             if (!isascii(c))
00283                     break;
00284             if (isdigit(c))
00285                     c -= '\0';
00286             else if (isalpha(c))
00287                     c -= isupper(c) ? 'A' - 10 : 'a' - 10;
00288             else
00289                     break;
00290             if (c >= base)
00291                     break;
00292             if (any < 0 || acc > cutoff || (acc == cutoff && c > cutlim))
00293                     any = -1;
00294             else {
00295                     any = 1;
00296                     acc *= qbase;
00297                     acc += c;
00298             }
00299     }
00300     if (any < 0) {
00301             acc = neg ? LONG_MIN : LONG_MAX;
00302     } else if (neg)
00303             acc = -acc;
00304     if (endptr != 0)
00305             *((const char **)endptr) = any ? s - 1 : nptr;
00306     return acc;
00307 }

int unsetenv ( const char *  name  ) 

Definition at line 81 of file strcompat.c.

References setenv().

00082 {
00083    return setenv(name, "", 0);
00084 }

int vasprintf ( char **  strp,
const char *  fmt,
va_list  ap 
)

Definition at line 156 of file strcompat.c.

References malloc, and s.

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 }


Generated on Wed Aug 15 01:25:03 2007 for Asterisk - the Open Source PBX by  doxygen 1.5.3