Wed Oct 28 11:53:17 2009

Asterisk developer's documentation


strcompat.c File Reference

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

#include "asterisk.h"
#include <ctype.h>

Include dependency graph for 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.
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 strcompat.c.


Define Documentation

#define LONG_MAX   9223372036854775807L

Definition at line 206 of file strcompat.c.

Referenced by process_sdp(), and strtoq().

#define LONG_MIN   (-9223372036854775807L-1L)

Definition at line 202 of file strcompat.c.

Referenced by process_sdp(), and strtoq().


Function Documentation

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

Definition at line 186 of file strcompat.c.

References vasprintf.

00187 {
00188    va_list ap;
00189    int ret;
00190 
00191    *str = NULL;
00192    va_start(ap, fmt);
00193    ret = vasprintf(str, fmt, ap);
00194    va_end(ap);
00195 
00196    return ret;
00197 }

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

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

00326 {
00327    int i;
00328 
00329    for (i = 0; i < nelem; i++) {
00330       list[i] = 0.1;
00331    }
00332    return -1;
00333 }

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

Definition at line 54 of file strcompat.c.

References buf.

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

00055 {
00056    unsigned char *buf;
00057    int buflen;
00058 
00059    buflen = strlen(name) + strlen(value) + 2;
00060    buf = alloca(buflen);
00061 
00062    if (!overwrite && getenv(name))
00063       return 0;
00064 
00065    snprintf(buf, buflen, "%s=%s", name, value);
00066 
00067    return putenv(buf);
00068 }

char* strcasestr ( const char *  haystack,
const char *  needle 
)

Definition at line 93 of file 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(), reqprep(), respprep(), search_directory(), sip_do_debug(), sip_prune_realtime(), sip_sipredirect(), sip_uri_headers_cmp(), and word_match().

00094 {
00095    char *u1, *u2;
00096    int u1len = strlen(haystack) + 1, u2len = strlen(needle) + 1;
00097 
00098    u1 = alloca(u1len);
00099    u2 = alloca(u2len);
00100    if (u1 && u2) {
00101       char *offset;
00102       if (u2len > u1len) {
00103          /* Needle bigger than haystack */
00104          return NULL;
00105       }
00106       offset = strstr(upper(haystack, u1, u1len), upper(needle, u2, u2len));
00107       if (offset) {
00108          /* Return the offset into the original string */
00109          return ((char *)((unsigned long)haystack + (unsigned long)(offset - u1)));
00110       } else {
00111          return NULL;
00112       }
00113    } else {
00114       return NULL;
00115    }
00116 }

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

Definition at line 375 of file strcompat.c.

References s.

00376 {
00377    register char *d = dst;
00378    register const char *s = src;
00379    register size_t n = siz;
00380    size_t dlen;
00381 
00382    /* Find the end of dst and adjust bytes left but don't go past end */
00383    while (n-- != 0 && *d != '\0')
00384       d++;
00385    dlen = d - dst;
00386    n = siz - dlen;
00387 
00388    if (n == 0)
00389       return dlen + strlen(s);
00390 
00391    while (*s != '\0') {
00392       if (n != 1) {
00393          *d++ = *s;
00394          n--;
00395       }
00396       s++;
00397    }
00398    *d = '\0';
00399 
00400    return dlen + (s - src);   /* count does not include NUL */
00401 }

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

Definition at line 439 of file strcompat.c.

References s.

00440 {
00441    register char *d = dst;
00442    register const char *s = src;
00443    register size_t n = siz;
00444 
00445    /* Copy as many bytes as will fit */
00446    if (n != 0 && --n != 0) {
00447       do {
00448          if ((*d++ = *s++) == 0)
00449             break;
00450       } while (--n != 0);
00451    }
00452 
00453    /* Not enough room in dst, add NUL and traverse rest of src */
00454    if (n == 0) {
00455       if (siz != 0)
00456          *d = '\0';     /* NUL-terminate dst */
00457       while (*s++)
00458          ;
00459    }
00460 
00461    return s - src - 1;  /* count does not include NUL */
00462 }

char* strndup ( const char *  s,
size_t  n 
)

Definition at line 133 of file strcompat.c.

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

00134 {
00135    size_t len = strnlen(s, n);
00136    char *new = malloc(len + 1);
00137 
00138    if (!new)
00139       return NULL;
00140 
00141    new[len] = '\0';
00142    return memcpy(new, s, len);
00143 }

size_t strnlen ( const char *  s,
size_t  n 
)

Definition at line 120 of file strcompat.c.

References len().

Referenced by strndup().

00121 {
00122    size_t len;
00123 
00124    for (len = 0; len < n; len++)
00125       if (s[len] == '\0')
00126          break;
00127 
00128    return len;
00129 }

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

Definition at line 27 of file strcompat.c.

Referenced by __ast_play_and_record(), _ast_device_state(), _build_port_config(), _macro_exec(), _parse(), 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_get_group(), ast_netsock_bind(), ast_parse_allow_disallow(), ast_parse_arg(), ast_playtones_start(), ast_read_image(), ast_remotecontrol(), astman_get_variables(), attempt_reconnect(), auth_exec(), authenticate_verify(), build_channels(), build_device(), 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(), 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_helper(), fileexists_core(), find_gtalk(), forward_message(), function_fieldqty(), function_ilink(), function_remote(), function_sippeer(), get_destination(), get_rdnis(), gettag(), gosub_exec(), gtalk_alloc(), gtalk_request(), handle_cli_dialplan_add_extension(), handle_common_options(), handle_debug_dialplan(), handle_request_invite(), handle_show_dialplan(), handle_statechange(), handle_t38_options(), handle_uri(), has_voicemail(), hint_read(), httpd_helper_thread(), iax2_register(), iftime(), inboxcount2(), ind_load_module(), is_prefix(), ivr_dispatch(), jingle_request(), leave_voicemail(), load_column_config(), load_config(), load_values_config(), make_components(), metermaidstate(), misdn_call(), misdn_request(), misdn_set_opt_exec(), mkintf(), notify_message(), notify_new_message(), orig_app(), orig_exten(), page_exec(), parkandannounce_exec(), parse_cookies(), parse_dial_string(), parse_register_contact(), parse_session_expires(), parse_uri(), 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(), 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(), send_tone_telemetry(), set(), set_config_flags(), set_insecure_flags(), sip_do_debug_ip(), sip_sipredirect(), sip_uri_cmp(), sip_uri_headers_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(), 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().

00028 {
00029    char *token;
00030 
00031    if (!*str) {
00032       /* No more tokens */
00033       return NULL;
00034    }
00035 
00036    token = *str;
00037    while (**str != '\0') {
00038       if (strchr(delims, **str)) {
00039          **str = '\0';
00040          (*str)++;
00041          return token;
00042       }
00043       (*str)++;
00044    }
00045 
00046    /* There is no other token */
00047    *str = NULL;
00048 
00049    return token;
00050 }

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

References LONG_MAX, LONG_MIN, and s.

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

int unsetenv ( const char *  name  ) 

Definition at line 72 of file strcompat.c.

References setenv().

Referenced by env_write().

00073 {
00074    return setenv(name, "", 0);
00075 }

static char* upper ( const char *  orig,
char *  buf,
int  bufsize 
) [static]

Definition at line 79 of file strcompat.c.

Referenced by strcasestr().

00080 {
00081    int i = 0;
00082 
00083    while (i < (bufsize - 1) && orig[i]) {
00084       buf[i] = toupper(orig[i]);
00085       i++;
00086    }
00087 
00088    buf[i] = '\0';
00089 
00090    return buf;
00091 }

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

Definition at line 147 of file strcompat.c.

References malloc, and s.

00148 {
00149    int size;
00150    va_list ap2;
00151    char s;
00152 
00153    *strp = NULL;
00154    va_copy(ap2, ap);
00155    size = vsnprintf(&s, 1, fmt, ap2);
00156    va_end(ap2);
00157    *strp = malloc(size + 1);
00158    if (!*strp)
00159       return -1;
00160    vsnprintf(*strp, size + 1, fmt, ap);
00161 
00162    return size;
00163 }


Generated on Wed Oct 28 11:53:17 2009 for Asterisk - the Open Source PBX by  doxygen 1.5.6