Thu Apr 28 2011 17:16:02

Asterisk developer's documentation


compat.h File Reference

General Definitions for Asterisk top level program Included by asterisk.h to handle platform-specific issues especially those related to header files. More...

#include "asterisk/compiler.h"
#include <inttypes.h>
#include <limits.h>
#include <unistd.h>
#include <stddef.h>
#include <stdint.h>
#include <sys/types.h>
#include <stdarg.h>
#include <stdlib.h>
#include <alloca.h>
#include <stdio.h>
#include <string.h>
#include <sys/poll.h>
#include <errno.h>
#include <glob.h>
#include "asterisk/compiler.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.

Defines

#define __STDC_VERSION__   0
#define MY_GLOB_FLAGS   (GLOB_NOMAGIC|GLOB_BRACE)

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.
void timersub (struct timeval *tvend, struct timeval *tvstart, struct timeval *tvdiff)
int unsetenv (const char *name)
int vasprintf (char **strp, const char *fmt, va_list ap)

Detailed Description

General Definitions for Asterisk top level program Included by asterisk.h to handle platform-specific issues especially those related to header files.

Definition in file compat.h.


Define Documentation

#define __STDC_VERSION__   0

Definition at line 25 of file compat.h.

#define MY_GLOB_FLAGS   (GLOB_NOMAGIC|GLOB_BRACE)

Definition at line 197 of file compat.h.

Referenced by config_text_file_load().


Function Documentation

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

Definition at line 199 of file agi/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 agi/strcompat.c.

Referenced by ast_readconfig(), cli_prompt(), increase_call_count(), and sysinfo_helper().

{
   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 agi/strcompat.c.

References buf.

Referenced by env_write(), launch_script(), load_odbc_config(), read_environment(), and unsetenv().

{
   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 *  ,
const char *   
)

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

References upper().

Referenced by action_originate(), anti_injection(), common_exec(), find_sdp(), find_table_cb(), get_rdnis(), get_refer_info(), gettag(), handle_request_invite(), handle_response_register(), handle_show_applications(), modlist_modentry(), parse_moved_contact(), parse_register_contact(), playback_exec(), process_dahdi(), realtime_multi_odbc(), realtime_odbc(), register_verify(), reqprep(), respprep(), search_directory_sub(), sip_sipredirect(), sip_uri_headers_cmp(), and word_match().

{
   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 agi/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 agi/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 *  ,
size_t   
)

Definition at line 133 of file agi/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 *  ,
size_t   
)

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

References len().

Referenced by strndup().

{
   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 agi/strcompat.c.

References str.

Referenced by __ast_play_and_record(), _ast_device_state(), _build_port_config(), _macro_exec(), _parse(), acf_curl_exec(), acf_vmcount_exec(), add_peer_mailboxes(), add_redirect(), adsi_load(), adsi_message(), append_history_va(), append_mailbox(), append_mailbox_mapping(), apply_options(), apply_outgoing(), ast_aji_get_client(), ast_app_getdata(), ast_build_timing(), ast_eivr_getvariable(), ast_eivr_setvariable(), ast_el_strtoarr(), ast_extension_state2(), ast_filehelper(), ast_format_str_reduce(), ast_get_group(), ast_netsock_bind(), ast_parse_allow_disallow(), ast_parse_arg(), ast_playtones_start(), ast_read_image(), ast_remotecontrol(), ast_utils_which(), astman_get_variables(), attempt_reconnect(), auth_exec(), authenticate_verify(), build_channels(), build_gateway(), build_peer(), builtin_atxfer(), callerid_read(), check_auth(), check_blacklist(), check_user_full(), check_via_response(), cleanup_stale_contexts(), collect_function_digits(), common_exec(), complete_dialplan_add_ignorepat(), complete_dialplan_add_include(), complete_dialplan_remove_ignorepat(), complete_dialplan_remove_include(), complete_meetmecmd(), conf_exec(), conf_run(), config_curl(), config_line(), config_parse_variables(), connect_link(), console_dial(), cut_internal(), dahdi_request(), decrypt_frame(), del_exec(), deltree_exec(), dial_exec_full(), dial_trunk(), do_say(), do_timelimit(), exec_exec(), exts_compare(), extstate_read(), feature_interpret(), fileexists_core(), find_gtalk(), forward_message(), function_fieldqty(), function_ilink(), function_remote(), function_sippeer(), get_destination(), get_range(), get_rdnis(), get_timerange(), gettag(), gosub_exec(), gtalk_alloc(), gtalk_request(), handle_cli_dialplan_add_extension(), handle_common_options(), handle_debug_dialplan(), handle_request_invite(), handle_request_notify(), handle_show_dialplan(), handle_statechange(), handle_t38_options(), handle_uri(), has_voicemail(), hint_read(), httpd_helper_thread(), iax2_register(), iftime(), inboxcount2(), is_prefix(), ivr_dispatch(), jingle_request(), leave_voicemail(), load_column_config(), load_config(), make_components(), metermaidstate(), misdn_request(), misdn_set_opt_exec(), mkintf(), notify_message(), notify_new_message(), orig_app(), orig_exten(), originate_exec(), page_exec(), parkandannounce_exec(), parse_cookies(), parse_dial_string(), parse_empty_options(), parse_register_contact(), parse_session_expires(), parse_uri(), parse_via(), pbx_builtin_background(), pbx_builtin_execiftime(), pbx_builtin_gotoif(), pbx_builtin_gotoiftime(), pbx_builtin_importvar(), pbx_builtin_saynumber(), pbx_builtin_setvar(), pbx_find_extension(), pbx_load_config(), pbx_load_users(), pbx_parseable_goto(), peer_set_srcaddr(), pickup_exec(), pickupchan_exec(), playback_exec(), process_dahdi(), process_sdp_o(), process_text_line(), queue_mwi_event(), queue_set_param(), read_config_maps(), readfile_exec(), realtime_curl(), realtime_multi_curl(), realtime_multi_odbc(), realtime_multi_pgsql(), realtime_odbc(), realtime_pgsql(), reg_source_db(), register_exten(), register_peer_exten(), register_verify(), reload_agents(), reload_config(), reload_queue_members(), reply_digest(), rpt_exec(), rpt_tele_thread(), search_directory_sub(), send_tone_telemetry(), sendmail(), set(), set_config_flags(), set_insecure_flags(), sip_do_debug_ip(), sip_sipredirect(), sip_uri_cmp(), sip_uri_headers_cmp(), sip_uri_params_cmp(), sla_add_trunk_to_station(), sla_check_device(), sla_queue_event_conf(), sla_ring_station(), sla_state(), sla_station_exec(), sort_internal(), spawn_mp3(), spawn_ras(), speech_background(), ss_thread(), stat_read(), store_tone_zone_ring_cadence(), timezone_add(), transmit_fake_auth_response(), transmit_state_notify(), tryexec_exec(), unistim_send_mwi_to_peer(), unregister_exten(), update_registry(), vmauthenticate(), write_htmldump(), and xml_translate().

{
   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 agi/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 agi/strcompat.c.

Referenced by ast_rtcp_write_rr(), ast_rtcp_write_sr(), and ast_select().

{
   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 agi/strcompat.c.

References setenv().

Referenced by env_write().

{
   return setenv(name, "", 0);
}
int vasprintf ( char **  strp,
const char *  fmt,
va_list  ap 
)

Definition at line 147 of file agi/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;
}