include/asterisk/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 <stdarg.h>
#include <stdio.h>
#include <sys/poll.h>
#include <errno.h>
#include <glob.h>

Include dependency graph for include/asterisk/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 INFINITY   (1.0/0.0)
#define LLONG_MAX   9223372036854775807LL
#define MY_GLOB_FLAGS   GLOB_NOCHECK
#define NAN   (0.0/0.0)
#define strlcat   __use__ast_str__functions_not__strlcat__
#define strlcpy   __use__ast_copy_string__not__strlcpy__

Functions

int asprintf (char **str, const char *fmt,...)
void closefrom (int lowfd)
int ffsll (long long n)
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.
uint64_t htonll (uint64_t host64)
char * mkdtemp (char *template_s)
uint64_t ntohll (uint64_t net64)
float roundf (float x)
int setenv (const char *name, const char *value, int overwrite)
char * strcasestr (const char *, const char *)
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 include/asterisk/compat.h.


Define Documentation

#define __STDC_VERSION__   0

Definition at line 25 of file include/asterisk/compat.h.

#define INFINITY   (1.0/0.0)

Definition at line 228 of file include/asterisk/compat.h.

Referenced by cli_endpoint_print_body().

#define LLONG_MAX   9223372036854775807LL

Definition at line 77 of file include/asterisk/compat.h.

Referenced by AST_TEST_DEFINE(), file_read(), and file_write().

#define MY_GLOB_FLAGS   GLOB_NOCHECK

Definition at line 214 of file include/asterisk/compat.h.

Referenced by config_text_file_load().

#define NAN   (0.0/0.0)

Definition at line 232 of file include/asterisk/compat.h.

Referenced by cli_contact_print_body().

#define strlcat   __use__ast_str__functions_not__strlcat__

Definition at line 144 of file include/asterisk/compat.h.

Referenced by el_source(), and history_def_add().

#define strlcpy   __use__ast_copy_string__not__strlcpy__

Definition at line 145 of file include/asterisk/compat.h.

Referenced by el_source(), and history_def_add().


Function Documentation

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

Definition at line 209 of file strcompat.c.

References NULL, and vasprintf.

00210 {
00211    va_list ap;
00212    int ret;
00213 
00214    *str = NULL;
00215    va_start(ap, fmt);
00216    ret = vasprintf(str, fmt, ap);
00217    va_end(ap);
00218 
00219    return ret;
00220 }

void closefrom ( int  lowfd  ) 

Examples:
/tmp/asterisk-trunk/trunk/main/app.c.

Definition at line 426 of file strcompat.c.

References errno, and result.

Referenced by ast_close_fds_above_n().

00427 {
00428    long x;
00429    struct rlimit rl;
00430    DIR *dir;
00431    char path[16], *result;
00432    struct dirent *entry;
00433 
00434    snprintf(path, sizeof(path), "/proc/%d/fd", (int) getpid());
00435    if ((dir = opendir(path))) {
00436       while ((entry = readdir(dir))) {
00437          /* Skip . and .. */
00438          if (entry->d_name[0] == '.') {
00439             continue;
00440          }
00441          if ((x = strtol(entry->d_name, &result, 10)) && x >= n) {
00442 #ifdef STRICT_COMPAT
00443             close(x);
00444 #else
00445             /* This isn't strictly compatible, but it's actually faster
00446              * for our purposes to set the CLOEXEC flag than to close
00447              * file descriptors.
00448              */
00449             long flags = fcntl(x, F_GETFD);
00450             if (flags == -1 && errno == EBADF) {
00451                continue;
00452             }
00453             fcntl(x, F_SETFD, flags | FD_CLOEXEC);
00454 #endif
00455          }
00456       }
00457       closedir(dir);
00458    } else {
00459       getrlimit(RLIMIT_NOFILE, &rl);
00460       if (rl.rlim_cur > 65535) {
00461          /* A more reasonable value.  Consider that the primary source of
00462           * file descriptors in Asterisk are UDP sockets, of which we are
00463           * limited to 65,535 per address.  We additionally limit that down
00464           * to about 10,000 sockets per protocol.  While the kernel will
00465           * allow us to set the fileno limit higher (up to 4.2 billion),
00466           * there really is no practical reason for it to be that high.
00467           */
00468          rl.rlim_cur = 65535;
00469       }
00470       for (x = n; x < rl.rlim_cur; x++) {
00471 #ifdef STRICT_COMPAT
00472          close(x);
00473 #else
00474          long flags = fcntl(x, F_GETFD);
00475          if (flags == -1 && errno == EBADF) {
00476             continue;
00477          }
00478          fcntl(x, F_SETFD, flags | FD_CLOEXEC);
00479 #endif
00480       }
00481    }
00482 }

int ffsll ( long long  n  ) 

Definition at line 413 of file strcompat.c.

00414 {
00415    int i;
00416    for (i = 0; i < 64; i++) {
00417       if ((1LL << i) & n) {
00418          return i + 1;
00419       }
00420    }
00421    return 0;
00422 }

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 348 of file strcompat.c.

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

00349 {
00350    int i;
00351 
00352    for (i = 0; i < nelem; i++) {
00353       list[i] = 0.1;
00354    }
00355    return -1;
00356 }

uint64_t htonll ( uint64_t  host64  ) 

Definition at line 387 of file strcompat.c.

References c.

Referenced by iax_ie_append_versioned_uint64().

00388 {
00389 #if BYTE_ORDER == BIG_ENDIAN
00390    return host64;
00391 #elif BYTE_ORDER == LITTLE_ENDIAN
00392    union {
00393       unsigned char c[8];
00394       uint64_t u;
00395    } number;
00396    number.u = host64;
00397    return
00398       (((uint64_t) number.c[0]) << 56) |
00399       (((uint64_t) number.c[1]) << 48) |
00400       (((uint64_t) number.c[2]) << 40) |
00401       (((uint64_t) number.c[3]) << 32) |
00402       (((uint64_t) number.c[4]) << 24) |
00403       (((uint64_t) number.c[5]) << 16) |
00404       (((uint64_t) number.c[6]) <<  8) |
00405       (((uint64_t) number.c[7]) <<  0);
00406 #else
00407    #error "Unknown byte order"
00408 #endif
00409 }

char* mkdtemp ( char *  template_s  ) 

Definition at line 568 of file strcompat.c.

References MKTEMP_DIR, mktemp_internal(), and NULL.

Referenced by AST_TEST_DEFINE().

00569 {
00570    return mktemp_internal(path, 0, MKTEMP_DIR) ? NULL : path;
00571 }

uint64_t ntohll ( uint64_t  net64  ) 

Definition at line 361 of file strcompat.c.

References c.

Referenced by dump_versioned_codec(), and iax_parse_ies().

00362 {
00363 #if BYTE_ORDER == BIG_ENDIAN
00364    return net64;
00365 #elif BYTE_ORDER == LITTLE_ENDIAN
00366    union {
00367       unsigned char c[8];
00368       uint64_t u;
00369    } number;
00370    number.u = net64;
00371    return
00372       (((uint64_t) number.c[0]) << 56) |
00373       (((uint64_t) number.c[1]) << 48) |
00374       (((uint64_t) number.c[2]) << 40) |
00375       (((uint64_t) number.c[3]) << 32) |
00376       (((uint64_t) number.c[4]) << 24) |
00377       (((uint64_t) number.c[5]) << 16) |
00378       (((uint64_t) number.c[6]) <<  8) |
00379       (((uint64_t) number.c[7]) <<  0);
00380 #else
00381    #error "Unknown byte order"
00382 #endif
00383 }

float roundf ( float  x  ) 

Definition at line 576 of file strcompat.c.

Referenced by ast_readaudio_callback(), dahdi_timer_set_rate(), and pthread_timer_set_rate().

00576                       {
00577    if (x < 0.0) {
00578       return (float)(int)((x) - 0.5);
00579    } else {
00580       return (float)(int)((x) + 0.5);
00581    }
00582 }

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

Definition at line 68 of file strcompat.c.

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

00069 {
00070    unsigned char *buf;
00071    int buflen;
00072 
00073    if (!overwrite && getenv(name))
00074       return 0;
00075 
00076    buflen = strlen(name) + strlen(value) + 2;
00077    buf = ast_alloca(buflen);
00078 
00079    snprintf(buf, buflen, "%s=%s", name, value);
00080 
00081    return putenv(buf);
00082 }

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

Definition at line 107 of file strcompat.c.

References ast_alloca, NULL, and upper().

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

00108 {
00109    char *u1, *u2;
00110    char *offset;
00111    int u1len = strlen(haystack) + 1, u2len = strlen(needle) + 1;
00112 
00113    if (u2len > u1len) {
00114       /* Needle bigger than haystack */
00115       return NULL;
00116    }
00117    u1 = ast_alloca(u1len);
00118    u2 = ast_alloca(u2len);
00119    offset = strstr(upper(haystack, u1, u1len), upper(needle, u2, u2len));
00120    if (offset) {
00121       /* Return the offset into the original string */
00122       return ((char *)((unsigned long)haystack + (unsigned long)(offset - u1)));
00123    } else {
00124       return NULL;
00125    }
00126 }

char* strndup ( const char *  ,
size_t   
)

Definition at line 143 of file strcompat.c.

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

00144 {
00145    size_t len = strnlen(s, n);
00146    char *new = malloc(len + 1);
00147 
00148    if (!new)
00149       return NULL;
00150 
00151    new[len] = '\0';
00152    return memcpy(new, s, len);
00153 }

size_t strnlen ( const char *  ,
size_t   
)

Definition at line 130 of file strcompat.c.

References len().

Referenced by strndup().

00131 {
00132    size_t len;
00133 
00134    for (len = 0; len < n; len++)
00135       if (s[len] == '\0')
00136          break;
00137 
00138    return len;
00139 }

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

Examples:
/tmp/asterisk-trunk/trunk/main/app.c.

Definition at line 41 of file strcompat.c.

Referenced by __analog_ss_thread(), __ast_play_and_record(), __ast_sorcery_apply_config(), _ast_device_state(), _build_port_config(), _macro_exec(), _parse(), acf_curl_helper(), action_messagesend(), action_playback(), action_playback_and_continue(), actual_load_config(), add_hintdevice(), add_menu_entry(), add_peer_mailboxes(), add_redirect(), adsi_load(), adsi_message(), ami_sip_qualify(), aMYSQL_clear(), aMYSQL_disconnect(), aMYSQL_fetch(), analog_ss_thread(), announce_to_dial(), append_history_va(), append_mailbox(), append_mailbox_mapping(), apply_options(), apply_outgoing(), apps_handler(), ast_app_getdata(), ast_append_acl(), ast_append_ha(), ast_ari_invoke(), ast_bridge_timelimit(), ast_build_timing(), ast_eivr_getvariable(), ast_eivr_setvariable(), ast_el_strtoarr(), ast_extension_state3(), ast_format_cap_update_by_allow_disallow(), ast_format_str_reduce(), ast_get_chan_applicationmap(), ast_get_group(), ast_get_namedgroups(), ast_http_get_auth(), ast_http_get_post_vars(), ast_http_header_parse(), ast_msg_send(), ast_parse_arg(), ast_parse_digest(), ast_playtones_start(), ast_presence_state_helper(), ast_read_image(), ast_remotecontrol(), ast_sdp_crypto_process(), ast_sip_auth_vector_init(), ast_sip_for_each_aor(), ast_sip_location_retrieve_contact_and_aor_from_list(), ast_utils_which(), ast_websocket_uri_cb(), asterisk_publication_mailboxstate(), auth_exec(), authenticate_api_key(), authenticate_verify(), build_calendar(), build_channels(), build_peer(), build_user(), check_blacklist(), check_nonce(), check_user_full(), cleanup_stale_contexts(), cli_qualify_contacts(), complete_dialplan_add_ignorepat(), complete_dialplan_add_include(), complete_dialplan_remove_ignorepat(), complete_dialplan_remove_include(), conf_exec(), conf_run(), config_curl(), config_parse_variables(), console_dial(), create_mwi_subscriptions_for_endpoint(), create_queue_member(), cut_internal(), data_filter_alloc(), decrypt_frame(), del_exec(), deltree_exec(), dial_exec_full(), dial_trunk(), do_say(), dtmf_info_incoming_request(), events_handler(), ewscal_write_event(), exec_exec(), expand_gosub_args(), exts_compare(), extstate_read(), filehelper(), find_aor(), find_body_generator_accept(), forward_message(), function_fieldnum_helper(), function_fieldqty_helper(), function_ooh323_write(), function_sippeer(), get_range(), get_rdnis(), get_timerange(), gettag(), gosub_exec(), h263_parse_sdp_fmtp(), h264_parse_sdp_fmtp(), handle_cli_dialplan_add_extension(), handle_common_options(), handle_debug_dialplan(), handle_request_invite(), handle_request_notify(), handle_show_dialplan(), handle_t38_options(), handle_uri(), has_voicemail(), hint_read(), http_request_headers_get(), iax2_register(), ident_handler(), iftime(), inboxcount2(), ip_identify_match_handler(), is_prefix(), ivr_dispatch(), jack_handler(), json_array_from_list(), leave_voicemail(), list_item_handler(), load_column_config(), load_values_config(), make_components(), make_email_file(), man_do_variable_value(), manager_dialplan_extension_add(), manager_dialplan_extension_remove(), mark_parsed_methods(), metermaidstate(), misdn_set_opt_exec(), msg_send_exec(), mwi_has_voicemail(), mwi_inboxcount(), mwi_on_aor(), mwi_to_event(), mwi_validate_for_aor(), next_node_name(), notify_endpoint(), notify_new_message(), on_endpoint(), orig_app(), orig_exten(), origin_allowed(), originate_exec(), page_exec(), parse_cookies(), parse_data(), parse_dial_string(), parse_empty_options(), parse_register_contact(), parse_session_expires(), parse_tone_uri(), parse_uri_full(), 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(), permanent_uri_handler(), persistent_endpoint_contact_observer(), pickup_exec(), pickupchan_exec(), pjsip_acf_dial_contacts_read(), play_files_helper(), playback_exec(), process_dahdi(), process_description_file(), process_sdp_o(), process_text_line(), qualify_and_schedule_all_cb(), queue_set_param(), read_config_maps(), realtime_curl(), realtime_multi_curl(), realtime_multi_mysql(), realtime_multi_odbc(), realtime_multi_pgsql(), realtime_mysql(), realtime_odbc(), realtime_pgsql(), register_exten(), register_peer_exten(), register_verify(), registrar_on_rx_request(), reload_config(), reload_queue_members(), reply_digest(), safe_scan_int(), search_directory_sub(), send_dial_tone(), send_unsolicited_mwi_notify(), sendfax_exec(), sendmail(), separate_mailbox(), set(), set_insecure_flags(), sip_digest_parser(), sip_get_cc_information(), sip_parse_nat_option(), sip_sipredirect(), sip_uri_cmp(), sip_uri_headers_cmp(), sip_uri_params_cmp(), skinny_call(), sla_add_trunk_to_station(), sla_check_device(), sla_queue_event_conf(), sla_ring_station(), sla_state(), sla_station_exec(), sorcery_config_open(), sort_internal(), spawn_mp3(), spawn_ras(), speech_background(), stasis_app_bridge_create(), stat_read(), state_notify_build_xml(), store_tone_zone_ring_cadence(), test_cel_peer_strings_match(), timezone_add(), transmit_fake_auth_response(), transport_tls_cipher_handler(), tryexec_exec(), unregister_exten(), user_event_hook_cb(), userevent_exec(), vmauthenticate(), write_htmldump(), xml_translate(), xmpp_client_authenticate_sasl(), xmpp_pubsub_handle_event(), and xmpp_send_cb().

00042 {
00043    char *token;
00044 
00045    if (!*str) {
00046       /* No more tokens */
00047       return NULL;
00048    }
00049 
00050    token = *str;
00051    while (**str != '\0') {
00052       if (strchr(delims, **str)) {
00053          **str = '\0';
00054          (*str)++;
00055          return token;
00056       }
00057       (*str)++;
00058    }
00059 
00060    /* There is no other token */
00061    *str = NULL;
00062 
00063    return token;
00064 }

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 239 of file strcompat.c.

References c, LONG_MAX, and LONG_MIN.

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

void timersub ( struct timeval *  tvend,
struct timeval *  tvstart,
struct timeval *  tvdiff 
)

Definition at line 177 of file strcompat.c.

Referenced by ast_rtcp_write_report(), and ast_select().

00178 {
00179    tvdiff->tv_sec = tvend->tv_sec - tvstart->tv_sec;
00180    tvdiff->tv_usec = tvend->tv_usec - tvstart->tv_usec;
00181    if (tvdiff->tv_usec < 0) {
00182       tvdiff->tv_sec --;
00183       tvdiff->tv_usec += 1000000;
00184    }
00185 
00186 }

int unsetenv ( const char *  name  ) 

Definition at line 86 of file strcompat.c.

Referenced by env_write(), and rxqcheck().

00087 {
00088    return setenv(name, "", 0);
00089 }

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

Definition at line 157 of file strcompat.c.

References malloc, and NULL.

00158 {
00159    int size;
00160    va_list ap2;
00161    char s;
00162 
00163    *strp = NULL;
00164    va_copy(ap2, ap);
00165    size = vsnprintf(&s, 1, fmt, ap2);
00166    va_end(ap2);
00167    *strp = malloc(size + 1);
00168    if (!*strp)
00169       return -1;
00170    vsnprintf(*strp, size + 1, fmt, ap);
00171 
00172    return size;
00173 }


Generated on Thu Apr 16 06:30:18 2015 for Asterisk - The Open Source Telephony Project by  doxygen 1.5.6