Thu Apr 28 2011 17:14:03

Asterisk developer's documentation


main/strcompat.c File Reference

Compatibility functions for strsep and strtoq missing on Solaris. More...

#include "asterisk.h"
#include <ctype.h>
Include dependency graph for main/strcompat.c:

Go to the source code of this file.

Defines

#define LONG_MAX   9223372036854775807L
#define LONG_MIN   (-9223372036854775807L-1L)

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 *haystack, const char *needle)
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 *s, size_t n)
size_t strnlen (const char *s, size_t n)
char * strsep (char **str, const char *delims)
uint64_t strtoq (const char *nptr, char **endptr, int base)
 Convert a string to a quad integer.
void timersub (struct timeval *tvend, struct timeval *tvstart, struct timeval *tvdiff)
int unsetenv (const char *name)
static char * upper (const char *orig, char *buf, int bufsize)
int vasprintf (char **strp, const char *fmt, va_list ap)

Detailed Description

Compatibility functions for strsep and strtoq missing on Solaris.

Definition in file main/strcompat.c.


Define Documentation

#define LONG_MAX   9223372036854775807L

Definition at line 219 of file main/strcompat.c.

Referenced by strtoq().

#define LONG_MIN   (-9223372036854775807L-1L)

Definition at line 215 of file main/strcompat.c.

Referenced by strtoq().


Function Documentation

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

Definition at line 199 of file main/strcompat.c.

References vasprintf.

{
   va_list ap;
   int ret;

   *str = NULL;
   va_start(ap, fmt);
   ret = vasprintf(str, fmt, ap);
   va_end(ap);

   return ret;
}
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 338 of file main/strcompat.c.

{
   int i;

   for (i = 0; i < nelem; i++) {
      list[i] = 0.1;
   }
   return -1;
}
int setenv ( const char *  name,
const char *  value,
int  overwrite 
)

Definition at line 54 of file main/strcompat.c.

References buf.

{
   unsigned char *buf;
   int buflen;

   buflen = strlen(name) + strlen(value) + 2;
   buf = alloca(buflen);

   if (!overwrite && getenv(name))
      return 0;

   snprintf(buf, buflen, "%s=%s", name, value);

   return putenv(buf);
}
char* strcasestr ( const char *  haystack,
const char *  needle 
)

Definition at line 93 of file main/strcompat.c.

References upper().

{
   char *u1, *u2;
   int u1len = strlen(haystack) + 1, u2len = strlen(needle) + 1;

   u1 = alloca(u1len);
   u2 = alloca(u2len);
   if (u1 && u2) {
      char *offset;
      if (u2len > u1len) {
         /* Needle bigger than haystack */
         return NULL;
      }
      offset = strstr(upper(haystack, u1, u1len), upper(needle, u2, u2len));
      if (offset) {
         /* Return the offset into the original string */
         return ((char *)((unsigned long)haystack + (unsigned long)(offset - u1)));
      } else {
         return NULL;
      }
   } else {
      return NULL;
   }
}
size_t strlcat ( char *  dst,
const char *  src,
size_t  siz 
)

Definition at line 388 of file main/strcompat.c.

References s.

{
   register char *d = dst;
   register const char *s = src;
   register size_t n = siz;
   size_t dlen;

   /* Find the end of dst and adjust bytes left but don't go past end */
   while (n-- != 0 && *d != '\0')
      d++;
   dlen = d - dst;
   n = siz - dlen;

   if (n == 0)
      return dlen + strlen(s);

   while (*s != '\0') {
      if (n != 1) {
         *d++ = *s;
         n--;
      }
      s++;
   }
   *d = '\0';

   return dlen + (s - src);   /* count does not include NUL */
}
size_t strlcpy ( char *  dst,
const char *  src,
size_t  siz 
)

Definition at line 452 of file main/strcompat.c.

References s.

{
   register char *d = dst;
   register const char *s = src;
   register size_t n = siz;

   /* Copy as many bytes as will fit */
   if (n != 0 && --n != 0) {
      do {
         if ((*d++ = *s++) == 0)
            break;
      } while (--n != 0);
   }

   /* Not enough room in dst, add NUL and traverse rest of src */
   if (n == 0) {
      if (siz != 0)
         *d = '\0';     /* NUL-terminate dst */
      while (*s++)
         ;
   }

   return s - src - 1;  /* count does not include NUL */
}
char* strndup ( const char *  s,
size_t  n 
)

Definition at line 133 of file main/strcompat.c.

References len(), malloc, and strnlen().

{
   size_t len = strnlen(s, n);
   char *new = malloc(len + 1);

   if (!new)
      return NULL;

   new[len] = '\0';
   return memcpy(new, s, len);
}
size_t strnlen ( const char *  s,
size_t  n 
)

Definition at line 120 of file main/strcompat.c.

References len().

{
   size_t len;

   for (len = 0; len < n; len++)
      if (s[len] == '\0')
         break;

   return len;
}
char* strsep ( char **  str,
const char *  delims 
)

Definition at line 27 of file main/strcompat.c.

References str.

{
   char *token;

   if (!*str) {
      /* No more tokens */
      return NULL;
   }

   token = *str;
   while (**str != '\0') {
      if (strchr(delims, **str)) {
         **str = '\0';
         (*str)++;
         return token;
      }
      (*str)++;
   }

   /* There is no other token */
   *str = NULL;

   return token;
}
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 229 of file main/strcompat.c.

References LONG_MAX, LONG_MIN, and s.

{
    const char *s;
    uint64_t acc;
    unsigned char c;
    uint64_t qbase, cutoff;
    int neg, any, cutlim;

    /*
     * Skip white space and pick up leading +/- sign if any.
     * If base is 0, allow 0x for hex and 0 for octal, else
     * assume decimal; if base is already 16, allow 0x.
     */
    s = nptr;
    do {
            c = *s++;
    } while (isspace(c));
    if (c == '-') {
            neg = 1;
            c = *s++;
    } else {
            neg = 0;
            if (c == '+')
                    c = *s++;
    }
    if ((base == 0 || base == 16) &&
        c == '\0' && (*s == 'x' || *s == 'X')) {
            c = s[1];
            s += 2;
            base = 16;
    }
    if (base == 0)
            base = c == '\0' ? 8 : 10;

    /*
     * Compute the cutoff value between legal numbers and illegal
     * numbers.  That is the largest legal value, divided by the
     * base.  An input number that is greater than this value, if
     * followed by a legal input character, is too big.  One that
     * is equal to this value may be valid or not; the limit
     * between valid and invalid numbers is then based on the last
     * digit.  For instance, if the range for quads is
     * [-9223372036854775808..9223372036854775807] and the input base
     * is 10, cutoff will be set to 922337203685477580 and cutlim to
     * either 7 (neg==0) or 8 (neg==1), meaning that if we have
     * accumulated a value > 922337203685477580, or equal but the
     * next digit is > 7 (or 8), the number is too big, and we will
     * return a range error.
     *
     * Set any if any `digits' consumed; make it negative to indicate
     * overflow.
     */
    qbase = (unsigned)base;
    cutoff = neg ? (uint64_t)-(LONG_MIN + LONG_MAX) + LONG_MAX : LONG_MAX;
    cutlim = cutoff % qbase;
    cutoff /= qbase;
    for (acc = 0, any = 0;; c = *s++) {
            if (!isascii(c))
                    break;
            if (isdigit(c))
                    c -= '\0';
            else if (isalpha(c))
                    c -= isupper(c) ? 'A' - 10 : 'a' - 10;
            else
                    break;
            if (c >= base)
                    break;
            if (any < 0 || acc > cutoff || (acc == cutoff && c > cutlim))
                    any = -1;
            else {
                    any = 1;
                    acc *= qbase;
                    acc += c;
            }
    }
    if (any < 0) {
            acc = neg ? LONG_MIN : LONG_MAX;
    } else if (neg)
            acc = -acc;
    if (endptr != 0)
            *((const char **)endptr) = any ? s - 1 : nptr;
    return acc;
}
void timersub ( struct timeval *  tvend,
struct timeval *  tvstart,
struct timeval *  tvdiff 
)

Definition at line 167 of file main/strcompat.c.

{
   tvdiff->tv_sec = tvend->tv_sec - tvstart->tv_sec;
   tvdiff->tv_usec = tvend->tv_usec - tvstart->tv_usec;
   if (tvdiff->tv_usec < 0) {
      tvdiff->tv_sec --;
      tvdiff->tv_usec += 1000000;
   }

}
int unsetenv ( const char *  name)

Definition at line 72 of file main/strcompat.c.

References setenv().

{
   return setenv(name, "", 0);
}
static char* upper ( const char *  orig,
char *  buf,
int  bufsize 
) [static]

Definition at line 79 of file main/strcompat.c.

References buf.

Referenced by strcasestr().

{
   int i = 0;

   while (i < (bufsize - 1) && orig[i]) {
      buf[i] = toupper(orig[i]);
      i++;
   }

   buf[i] = '\0';

   return buf;
}
int vasprintf ( char **  strp,
const char *  fmt,
va_list  ap 
)

Definition at line 147 of file main/strcompat.c.

References malloc, and s.

{
   int size;
   va_list ap2;
   char s;

   *strp = NULL;
   va_copy(ap2, ap);
   size = vsnprintf(&s, 1, fmt, ap2);
   va_end(ap2);
   *strp = malloc(size + 1);
   if (!*strp)
      return -1;
   vsnprintf(*strp, size + 1, fmt, ap);

   return size;
}